| 1 |
<?php
|
| 2 |
// $Id: drutex_numbering.inc,v 1.2 2006/09/06 02:25:41 dfg Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Assists automatic numbering of tex, equation und equations environments.
|
| 7 |
*
|
| 8 |
* Note that a missing subhook_info() means that this submodule cannot be disabled (without hacking).
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of subhook_info().
|
| 13 |
*/
|
| 14 |
function drutex_numbering_info($format = -1) {
|
| 15 |
return (object) array(
|
| 16 |
'title' => t('Numbering / Referencing'),
|
| 17 |
'description' => t('Assists automatic numbering of tex, equation und equations environments.'),
|
| 18 |
'toggle' => false,
|
| 19 |
'weight' => 20
|
| 20 |
);
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of subhook_defaults().
|
| 25 |
*/
|
| 26 |
function drutex_numbering_defaults() {
|
| 27 |
$D['drutex_numbering_active'] = true;
|
| 28 |
return $D;
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of subhook_node2html().
|
| 33 |
*/
|
| 34 |
function drutex_numbering_node2html() {
|
| 35 |
$E = array();
|
| 36 |
|
| 37 |
$E[] = (object) array(
|
| 38 |
'pattern' => '@<(equation|equations)( [^>]*)>@se',
|
| 39 |
'replacement' => "'<$1 ' . _drutex_numbering_rewrite_attributes(_drutex_unescape('$2')) . '>'",
|
| 40 |
'weight' => -90 /* very early, but after drutex_verbatim.inc */
|
| 41 |
);
|
| 42 |
|
| 43 |
$E[] = (object) array(
|
| 44 |
'pattern' => '@\(\\\\ref\{(.+?)\}\)@se',
|
| 45 |
'replacement' => "drutex_reference_link($1, '(', ')')",
|
| 46 |
'weight' => 90 /* very late */
|
| 47 |
);
|
| 48 |
|
| 49 |
$E[] = (object) array(
|
| 50 |
'pattern' => '@\\\\ref\{(.+?)\}@se',
|
| 51 |
'replacement' => "drutex_reference_link($1)",
|
| 52 |
'weight' => 91 /* very late */
|
| 53 |
);
|
| 54 |
|
| 55 |
return $E;
|
| 56 |
}
|
| 57 |
|
| 58 |
function _drutex_numbering_rewrite_attributes($attribute_string) {
|
| 59 |
static $num = 1;
|
| 60 |
$attributes = drutex_parse_attributes($attribute_string);
|
| 61 |
|
| 62 |
if (isset($attributes['id'])) {
|
| 63 |
/* if id is set, but no name, make the name implicitly a number */
|
| 64 |
if (empty($attributes['name'])) {
|
| 65 |
$attributes['name'] = _drutex_numbers('inc counter');
|
| 66 |
}
|
| 67 |
|
| 68 |
/* save the id/name pair */
|
| 69 |
_drutex_references('set', $attributes['id'], $attributes['name']);
|
| 70 |
}
|
| 71 |
|
| 72 |
return drutex_merge_attributes($attributes);
|
| 73 |
}
|
| 74 |
|
| 75 |
function _drutex_numbers($action) {
|
| 76 |
/* for automatic, continuous numbering */
|
| 77 |
static $num = 0;
|
| 78 |
|
| 79 |
switch ($action) {
|
| 80 |
case 'inc counter':
|
| 81 |
return ++$num;
|
| 82 |
case 'get conuter':
|
| 83 |
return $num;
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
| 87 |
function _drutex_references($action, $id = '', $name = '') {
|
| 88 |
/* cache id's for \ref{id} */
|
| 89 |
static $id_cache = array();
|
| 90 |
|
| 91 |
switch ($action) {
|
| 92 |
case 'get name by id':
|
| 93 |
return $id_cache[$id];
|
| 94 |
|
| 95 |
case 'get array size':
|
| 96 |
return count($id_cache);
|
| 97 |
|
| 98 |
case 'set':
|
| 99 |
$id_cache[$id] = $name;
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
function drutex_reference_link($id, $prepend = '', $append = '') {
|
| 104 |
return "<a href=\"#$id\">{$prepend}". _drutex_references('get name by id', $id) ."{$append}</a>";
|
| 105 |
}
|