|
1 <?php |
|
2 |
|
3 /** |
|
4 * |
|
5 * Parses for heading text. |
|
6 * |
|
7 * @category Text |
|
8 * |
|
9 * @package Text_Wiki |
|
10 * |
|
11 * @author Paul M. Jones <pmjones@php.net> |
|
12 * |
|
13 * @author Moritz Venn <moritz.venn@freaque.net> |
|
14 * |
|
15 * @license LGPL |
|
16 * |
|
17 * @version $Id: Heading.php,v 1.3 2006/02/03 19:47:02 toggg Exp $ |
|
18 * |
|
19 */ |
|
20 |
|
21 /** |
|
22 * |
|
23 * Parses for heading text. |
|
24 * |
|
25 * This class implements a Text_Wiki_Parse to find source text marked to |
|
26 * be a heading element, as defined by text on a line by itself prefixed |
|
27 * with a number of plus signs (+). The heading text itself is left in |
|
28 * the source, but is prefixed and suffixed with delimited tokens marking |
|
29 * the start and end of the heading. |
|
30 * |
|
31 * @category Text |
|
32 * |
|
33 * @package Text_Wiki |
|
34 * |
|
35 * @author Moritz Venn <moritz.venn@freaque.net> |
|
36 * |
|
37 */ |
|
38 |
|
39 class Text_Wiki_Parse_Heading extends Text_Wiki_Parse { |
|
40 |
|
41 |
|
42 /** |
|
43 * |
|
44 * The regular expression used to parse the source text and find |
|
45 * matches conforming to this rule. Used by the parse() method. |
|
46 * |
|
47 * @access public |
|
48 * |
|
49 * @var string |
|
50 * |
|
51 * @see parse() |
|
52 * |
|
53 */ |
|
54 |
|
55 var $regex = '/^(={2,6})(.*?)\1(?:\s|$)/m'; |
|
56 |
|
57 var $conf = array( |
|
58 'id_prefix' => 'toc' |
|
59 ); |
|
60 |
|
61 /** |
|
62 * |
|
63 * Generates a replacement for the matched text. Token options are: |
|
64 * |
|
65 * 'type' => ['start'|'end'] The starting or ending point of the |
|
66 * heading text. The text itself is left in the source. |
|
67 * |
|
68 * @access public |
|
69 * |
|
70 * @param array &$matches The array of matches from parse(). |
|
71 * |
|
72 * @return string A pair of delimited tokens to be used as a |
|
73 * placeholder in the source text surrounding the heading text. |
|
74 * |
|
75 */ |
|
76 |
|
77 function process(&$matches) |
|
78 { |
|
79 // keep a running count for header IDs. we use this later |
|
80 // when constructing TOC entries, etc. |
|
81 static $id; |
|
82 if (! isset($id)) { |
|
83 $id = 0; |
|
84 } |
|
85 |
|
86 $prefix = htmlspecialchars($this->getConf('id_prefix')); |
|
87 |
|
88 $start = $this->wiki->addToken( |
|
89 $this->rule, |
|
90 array( |
|
91 'type' => 'start', |
|
92 'level' => strlen($matches[1]), |
|
93 'text' => trim($matches[2]), |
|
94 'id' => $prefix . $id ++ |
|
95 ) |
|
96 ); |
|
97 |
|
98 $end = $this->wiki->addToken( |
|
99 $this->rule, |
|
100 array( |
|
101 'type' => 'end', |
|
102 'level' => strlen($matches[1]) |
|
103 ) |
|
104 ); |
|
105 |
|
106 return $start . trim($matches[2]) . $end . "\n"; |
|
107 } |
|
108 } |
|
109 ?> |