Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit c10e8b2

Browse files
committed
Init
0 parents  commit c10e8b2

File tree

167 files changed

+15501
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+15501
-0
lines changed

Text/Wiki.php

Lines changed: 1329 additions & 0 deletions
Large diffs are not rendered by default.

Text/Wiki/Parse.php

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
3+
/**
4+
* Baseline rule class for extension into a "real" parser component.
5+
*
6+
* PHP versions 4 and 5
7+
*
8+
* @category Text
9+
* @package Text_Wiki
10+
* @author Paul M. Jones <[email protected]>
11+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
12+
* @version $Id$
13+
* @link http://pear.php.net/package/Text_Wiki
14+
*/
15+
16+
/**
17+
* Baseline rule class for extension into a "real" parser component.
18+
*
19+
* Text_Wiki_Rule classes do not stand on their own; they are called by a
20+
* Text_Wiki object, typcially in the transform() method. Each rule class
21+
* performs three main activities: parse, process, and render.
22+
*
23+
* The parse() method takes a regex and applies it to the whole block of
24+
* source text at one time. Each match is sent as $matches to the
25+
* process() method.
26+
*
27+
* The process() method acts on the matched text from the source, and
28+
* then processes the source text is some way. This may mean the
29+
* creation of a delimited token using addToken(). In every case, the
30+
* process() method returns the text that should replace the matched text
31+
* from parse().
32+
*
33+
* @category Text
34+
* @package Text_Wiki
35+
* @author Paul M. Jones <[email protected]>
36+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
37+
* @version Release: @package_version@
38+
* @link http://pear.php.net/package/Text_Wiki
39+
*/
40+
class Text_Wiki_Parse {
41+
42+
/**
43+
*
44+
* Configuration options for this parser rule.
45+
*
46+
* @access public
47+
*
48+
* @var string
49+
*
50+
*/
51+
52+
public $conf = array();
53+
54+
/**
55+
*
56+
* Regular expression to find matching text for this rule.
57+
*
58+
* @access public
59+
*
60+
* @var string
61+
*
62+
* @see parse()
63+
*
64+
*/
65+
66+
public $regex = null;
67+
68+
/**
69+
*
70+
* The name of this rule for new token array elements.
71+
*
72+
* @access public
73+
*
74+
* @var string
75+
*
76+
*/
77+
78+
public $rule = null;
79+
80+
/**
81+
*
82+
* A reference to the calling Text_Wiki object.
83+
*
84+
* This is needed so that each rule has access to the same source
85+
* text, token set, URLs, interwiki maps, page names, etc.
86+
*
87+
* @access public
88+
*
89+
* @var object
90+
*/
91+
92+
public $wiki = null;
93+
94+
/**
95+
*
96+
* Constructor for this parser rule.
97+
*
98+
* @access public
99+
*
100+
* @param object &$obj The calling "parent" Text_Wiki object.
101+
*
102+
*/
103+
104+
function Text_Wiki_Parse(&$obj)
105+
{
106+
// this allows us access to the shared source text, token
107+
// array, etc.
108+
$this->wiki =& $obj;
109+
110+
// set the name of this rule; generally used when adding
111+
// to the tokens array. strip off the Text_Wiki_Parse_ portion.
112+
// text_wiki_parse_
113+
// 0123456789012345
114+
$tmp = substr(get_class($this), 16);
115+
$this->rule = ucwords(strtolower($tmp));
116+
117+
// override config options for the rule if specified
118+
if (isset($this->wiki->parseConf[$this->rule]) &&
119+
is_array($this->wiki->parseConf[$this->rule])) {
120+
121+
$this->conf = array_merge(
122+
$this->conf,
123+
$this->wiki->parseConf[$this->rule]
124+
);
125+
126+
}
127+
}
128+
129+
/**
130+
*
131+
* Abstrct method to parse source text for matches.
132+
*
133+
* Applies the rule's regular expression to the source text, passes
134+
* every match to the process() method, and replaces the matched text
135+
* with the results of the processing.
136+
*
137+
* @access public
138+
*
139+
* @see Text_Wiki_Parse::process()
140+
*
141+
*/
142+
143+
function parse()
144+
{
145+
if($this->regex){
146+
$this->wiki->source = preg_replace_callback(
147+
$this->regex,
148+
array(&$this, 'process'),
149+
$this->wiki->source
150+
);
151+
}
152+
}
153+
154+
/**
155+
*
156+
* Abstract method to generate replacements for matched text.
157+
*
158+
* @access public
159+
*
160+
* @param array $matches An array of matches from the parse() method
161+
* as generated by preg_replace_callback. $matches[0] is the full
162+
* matched string, $matches[1] is the first matched pattern,
163+
* $matches[2] is the second matched pattern, and so on.
164+
*
165+
* @return string The processed text replacement; defaults to the
166+
* full matched string (i.e., no changes to the text).
167+
*
168+
* @see Text_Wiki_Parse::parse()
169+
*
170+
*/
171+
172+
function process(&$matches)
173+
{
174+
return $matches[0];
175+
}
176+
177+
/**
178+
*
179+
* Simple method to safely get configuration key values.
180+
*
181+
* @access public
182+
*
183+
* @param string $key The configuration key.
184+
*
185+
* @param mixed $default If the key does not exist, return this value
186+
* instead.
187+
*
188+
* @return mixed The configuration key value (if it exists) or the
189+
* default value (if not).
190+
*
191+
*/
192+
193+
function getConf($key, $default = null)
194+
{
195+
if (isset($this->conf[$key])) {
196+
return $this->conf[$key];
197+
} else {
198+
return $default;
199+
}
200+
}
201+
202+
/**
203+
*
204+
* Extract 'attribute="value"' portions of wiki markup.
205+
*
206+
* This kind of markup is typically used only in macros, but is useful
207+
* anywhere.
208+
*
209+
* The syntax is pretty strict; there can be no spaces between the
210+
* option name, the equals, and the first double-quote; the value
211+
* must be surrounded by double-quotes. You can escape characters in
212+
* the value with a backslash, and the backslash will be stripped for
213+
* you.
214+
*
215+
* @access public
216+
*
217+
* @param string $text The "attributes" portion of markup.
218+
*
219+
* @return array An associative array of key-value pairs where the
220+
* key is the option name and the value is the option value.
221+
*
222+
*/
223+
224+
function getAttrs($text)
225+
{
226+
$tmp = explode('="', trim($text));
227+
228+
// basic setup
229+
$k = count($tmp) - 1;
230+
$attrs = array();
231+
$key = null;
232+
233+
// loop through the sections
234+
foreach ($tmp as $i => $val) {
235+
236+
// first element is always the first key
237+
if ($i == 0) {
238+
$key = trim($val);
239+
continue;
240+
}
241+
242+
// find the last double-quote in the value.
243+
// the part to the left is the value for the last key,
244+
// the part to the right is the next key name
245+
$pos = strrpos($val, '"');
246+
$attrs[$key] = stripslashes(substr($val, 0, $pos));
247+
$key = trim(substr($val, $pos+1));
248+
249+
}
250+
251+
return $attrs;
252+
253+
}
254+
}

Text/Wiki/Parse/Default/Anchor.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Parses for anchor targets.
6+
*
7+
* @category Text
8+
*
9+
* @package Text_Wiki
10+
*
11+
* @author Manuel Holtgrewe <purestorm at ggnore dot net>
12+
*
13+
* @author Paul M. Jones <[email protected]>
14+
*
15+
* @license LGPL
16+
*
17+
* @version $Id$
18+
*
19+
*/
20+
21+
/**
22+
*
23+
* This class implements a Text_Wiki_Parse to add an anchor target name
24+
* in the wiki page.
25+
*
26+
* @author Manuel Holtgrewe <purestorm at ggnore dot net>
27+
*
28+
* @author Paul M. Jones <pmjones at ciaweb dot net>
29+
*
30+
* @category Text
31+
*
32+
* @package Text_Wiki
33+
*
34+
*/
35+
36+
class Text_Wiki_Parse_Anchor extends Text_Wiki_Parse {
37+
38+
/**
39+
*
40+
* The regular expression used to find source text matching this
41+
* rule. Looks like a macro: [[# anchor_name]]
42+
*
43+
* @access public
44+
*
45+
* @var string
46+
*
47+
*/
48+
public $regex = '/
49+
(\[\[#\s) # Two brackets, then hash
50+
([-_A-Za-z0-9.%]+?) # Contents of anchor
51+
(\]\]) # Closing brackets
52+
/ix';
53+
54+
/**
55+
*
56+
* Generates a token entry for the matched text. Token options are:
57+
*
58+
* 'text' => The full matched text, not including the <code></code> tags.
59+
*
60+
* @access public
61+
*
62+
* @param array &$matches The array of matches from parse().
63+
*
64+
* @return A delimited token number to be used as a placeholder in
65+
* the source text.
66+
*
67+
*/
68+
function process(&$matches) {
69+
70+
$name = $matches[2];
71+
72+
$start = $this->wiki->addToken($this->rule,
73+
array(
74+
'name' => $name));
75+
76+
// done, place the script output directly in the source
77+
return $start;
78+
}
79+
}

Text/Wiki/Parse/Default/Bibcite.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Parses for bib citations.
6+
*
7+
* @category Text
8+
*
9+
* @package Text_Wiki
10+
*
11+
* @author Michal Frackowiak
12+
*
13+
* @license LGPL
14+
*
15+
* @version $Id$
16+
*
17+
*/
18+
19+
/**
20+
*
21+
* Parses for bibliography citations, e.g. ((bibcite somelabel)).
22+
*
23+
* This class implements a Text_Wiki_Parse to find math invocation code,
24+
* i.e. [[footnote]] text [[/footnote]]
25+
*
26+
* @category Text
27+
*
28+
* @package Text_Wiki
29+
*
30+
* @author Michal Frackowiak
31+
*
32+
*/
33+
class Text_Wiki_Parse_Bibcite extends Text_Wiki_Parse {
34+
35+
/**
36+
*
37+
* The regular expression used to find source text matching this
38+
* rule.
39+
*
40+
* @access public
41+
*
42+
* @var string
43+
*
44+
*/
45+
46+
public $regex = '/
47+
\(\( # Opening parens
48+
bibcite\s # Module name and whitespace
49+
([a-z0-9]+) # Alphanumeric citation
50+
\)\) # Closing parens
51+
/ix';
52+
53+
function process(&$matches) {
54+
$label = $matches[1];
55+
$options = array(
56+
'label' => $label);
57+
return $this->wiki->addToken($this->rule, $options);
58+
}
59+
}

0 commit comments

Comments
 (0)