-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.php
69 lines (55 loc) · 1.97 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* This function generates an ID from the given text based on the given options
* and regex replacement-rules.
*/
function buildID($options, $enum, $text) {
$id = $text;
if ($options['id_prepend_enum']) $id = $enum . $options['enum_seperator'] . $id;
foreach($options['id_rules'] as $pattern => $replacement) {
if (is_callable($replacement)) {
$id = preg_replace_callback($pattern, $replacement, $id);
} else {
$id = preg_replace($pattern, $replacement, $id);
}
}
if ($options['id_prefix']) $id = $options['id_prefix'] . $id;
return $id;
}
/**
* This function enumerates all headings in the parsed document and inserts new
* markup for each heading including an anchor-link to the generated ID.
*/
function insertAnchors($options)
{
$h_counter = array_fill($options['heading_min'], $options['heading_max'], --$options['enum_start']);
$h_pattern = "/h[${options['heading_min']}..${options['heading_max']}]/i";
$h_query = $options['toplevel_only'] ? '> *:header' : '*:header';
foreach (pq($h_query) as $h) {
// Determine tagname and ensure it's within range
$tagname = pq($h)->get(0)->tagName;
if (!preg_match($h_pattern, $tagname)) continue;
$level = intval(ltrim($tagname, 'h'));
$count = ++$h_counter[$level];
// Determine current enumerations
$enum = "";
for($l = $options['heading_min']; $l < $level; ++$l) {
$l_count = $h_counter[$l];
$enum .= "${l_count}${options['enum_seperator']}";
}
$enum .= "${count}";
// Build ID and apply it to the element
$id = buildID($options, $enum, pq($h)->text());
pq($h)->attr('id', $id);
// Generate new markup and set it inside the heading-element
pq($h)->html(str::template($options['markup'], [
'heading' => pq($h)->html(),
'id' => $id,
'enum' => $enum
]));
// Reset higher-level counter
for($i = $level + 1; $i <= $options['heading_max']; ++$i) {
$h_counter[$i] = $options['enum_start'];
}
}
}