-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWikiParser.php
1033 lines (907 loc) · 26.5 KB
/
WikiParser.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class WikiParser implements IteratorAggregate
{
static public function execute($class, $content, $cache_data=array())
{
$class .= 'WikiParser';
$parser = new $class($content, $cache_data);
return $parser->render();
}
/**
* Provides a data store for temporary information used in parsing
*/
protected $cache = array();
/**
* Internal character counter for PHP_LexerGenerator
*
* @var integer
*/
protected $counter = 0;
/**
* Provides a data store for user-provided information
*/
protected $data = array();
/**
* The document that is being parsed
*
* This is used by PHP_LexerGenerator
*
* @var string
*/
protected $input = NULL;
/**
* Internal line counter for PHP_LexerGenerator
*
* @var integer
*/
protected $line = 1;
/**
* The stack is an array tags in the tree that are the parent tags of the
* tip.
*
* The tip is the last tag in the stack, which is what most manipulation
* will be happening with.
*
* @var array
*/
protected $stack = array();
/**
* The internal state tracker used by PHP_LexerGenerator
*
* @var mixed
*/
protected $state = 1;
/**
* Contains the definitions of each tag, with the name as the array
* key and the value being an associative array of information. Each
* tag should contain:
*
* - A 'type' key containing one of the following values:
* - 'root': reserved for the root element of the tree
* - 'block': a block-level tag, which closes all inline tags when ending
* - 'inline': must be contained withing a block-level tag, does not close other inline tags
* - 'self': a self-closing tag that is not added to the stack
*
* Both 'root' and 'self' tags must have the following values:
* - 'tag': The tag to print out for out, may contain any number of sprintf() placeholders
*
* Both 'block' and 'inline' tags must have the following values:
* - 'open': The opening tag for output, may contain any number of sprintf() placeholders
* - 'close': The closing tag for output, may contain any number of sprintf() placeholders
*
* Any tag may contain 'defaults' which defines an array of default values
* to use with sprintf().
*
* When dealing with 'block' and 'inline' tags, the 'open' and 'close'
* string are combined together for sprintf(), so if there are two placeholders
* in 'open' and one in 'close', then there should be three values saved in
* the tree.
*
* @var array
*/
static public $tags = array(
'root' => array(
'type' => 'root',
'tag' => ''
),
'plugin' => array(
'type' => 'special',
'tag' => '%s%s'
),
'raw' => array(
'type' => 'block',
'open' => '%s',
'close' => ''
),
'div' => array(
'type' => 'block',
'open' => "<div>",
'close' => "</div>\n"
),
'span' => array(
'type' => 'inline',
'open' => '<span>',
'close' => '</span>'
),
'string' => array(
'type' => 'self',
'tag' => '%s'
),
'p' => array(
'type' => 'block',
'open' => "\n<p>\n",
'close' => "\n</p>\n"
),
'heading' => array(
'type' => 'block',
'open' => "\n<h%s>",
'close' => "</h%s>\n"
),
'block' => array(
'type' => 'block',
'open' => '<pre><code>',
'close' => "</code></pre>\n"
),
'bold' => array(
'type' => 'inline',
'open' => '<strong>',
'close' => '</strong>'
),
'italic' => array(
'type' => 'inline',
'open' => '<em>',
'close' => '</em>'
),
'mono' => array(
'type' => 'inline',
'open' => '<code>',
'close' => '</code>'
),
'nowiki' => array(
'type' => 'inline',
'open' => '<code>',
'close' => '</code>'
),
'sub' => array(
'type' => 'inline',
'open' => '<sub>',
'close' => '</sub>'
),
'sup' => array(
'type' => 'inline',
'open' => '<sup>',
'close' => '</sup>'
),
'underline' => array(
'type' => 'inline',
'open' => '<span class="underline">',
'close' => '</span>'
),
'br' => array(
'type' => 'self',
'tag' => "<br />\n"
),
'link' => array(
'type' => 'inline',
'open' => '<a>',
'close' => '</a>'
),
'img' => array(
'type' => 'self',
'tag' => '<img />'
),
'hr' => array(
'type' => 'self',
'tag' => "<hr />\n"
),
'ul' => array(
'type' => 'block',
'open' => "\n<ul>\n",
'close' => "</ul>\n"
),
'ol' => array(
'type' => 'block',
'open' => "\n<ol>\n",
'close' => "</ol>\n"
),
'li' => array(
'type' => 'block',
'open' => '<li>',
'close' => "</li>\n"
),
'table' => array(
'type' => 'block',
'open' => "<table><tbody>\n",
'close' => "</tbody></table>\n"
),
'tr' => array(
'type' => 'block',
'open' => "<tr>\n",
'close' => "</tr>\n"
),
'th' => array(
'type' => 'block',
'open' => '<th>',
'close' => "</th>\n"
),
'td' => array(
'type' => 'block',
'open' => '<td>',
'close' => "</td>\n"
),
'dl' => array(
'type' => 'block',
'open' => '<dl>',
'close' => '</dl>'
),
'dt' => array(
'type' => 'block',
'open' => '<dt>',
'close' => '</dt>'
),
'dd' => array(
'type' => 'block',
'open' => '<dd>',
'close' => '</dd>'
),
'blockquote' => array(
'type' => 'block',
'open' => '<blockquote>',
'close' => '</blockquote>'
)
);
/**
* The tip is the tag object in the tree that is represents the part of the
* document that is currently being parsed. This will move with each tag
* that is parsed.
*
* The stack contains the parent tag objects to allow for moving back up
* the tree. The tip is always the last tag in the stack.
*
* @var stdClass
*/
public $tip = NULL;
/**
* The token number most recently parsed by PHP_LexerGenerator
*
* @var integer
*/
public $token = NULL;
/**
* Contains a tree-structure that represents how the content will be
* output as HTML. The structure is a root object with the members:
*
* - ->name: the special value 'root'
* - ->children: a numerically-indexed array of parsed children tags
*
* Each element in the children array will contain a stdClass object with
* the members:
*
* - ->name: This is the name of the tag - required
* - ->children: If the tag is not a self-closing tag, it will have an array of chilren
* - ->data: If the tag has any sprintf() placeholders, there will be an array to hold the values
* - ->attr: An associative array of attributes for the tag
*
* @var stdClass
*/
protected $tree;
/**
* The value of the element most recently matched by PHP_LexerGenerator
*
* @var string
*/
public $value = NULL;
/**
* Takes the document to be parsed and initializes the parser
*
* @param string $input The document to parse
* @param array $data Extra user-supplied data for the parser
* @return WikiParser
*/
public function __construct($input, $data=array())
{
$this->data = $data;
$this->input = $input;
// Make sure we have newline since we can't ever match the end of the
// input using regex
if (substr($this->input, -1) != "\n") {
$this->input .= "\n";
}
// Dynamically determine the number of placeholders for each tag so
// that we don't need to figure it out each time
foreach (self::$tags as $tag => $info) {
self::$tags[$tag]['placeholders'] = 0;
foreach ($info as $element => $value) {
if (!in_array($element, array('tag', 'open', 'close'))) {
continue;
}
$num = substr_count($value, '%s');
if ($element == 'open') {
self::$tags[$tag]['open_placeholders'] = $num;
}
if ($element == 'close') {
self::$tags[$tag]['close_placeholders'] = $num;
}
if ($element == 'tag') {
self::$tags[$tag]['tag_placeholders'] = $num;
}
self::$tags[$tag]['placeholders'] += $num;
}
}
}
/**
* Camelizes underscore_notation to UpperCamelCase
*
* @param string $string The string to camelize
* @return string The camelized string
*/
public function camelize($string)
{
$string = ucfirst($string);
return preg_replace_callback('#_([a-z0-9])#i', array('self', 'camelizeCallback'), $string);
}
/**
* A callback used by ::camelize() to handle converting underscore to camelCase
*
* @param array $match The regular expression match
* @return string The value to replace the string with
*/
private function camelizeCallback($match)
{
return strtoupper($match[1]);
}
/**
* Gets all of the text under the tag specified
*
* @param stdClass $parent_tag The tag to get all text under
* @return string The captured text
*/
public function captureText($parent_tag)
{
$iter = new ParserIterator($parent_tag, 'inclusive', array('string'));
$text = '';
foreach ($iter as $num => $tag) {
$text .= $tag->data[0];
if (end($iter->parent()->children) === $tag && self::$tags[$iter->parent()->name]['type'] == 'block') {
$text .= ' ';
}
}
return html_entity_decode($text, ENT_COMPAT, 'UTF-8');
}
/**
* Changes a tag from one name to another, handling switches from inline to block-level
*
* @param array $parents
* @param stdClass $tag
* @param string $new_name
* @param NULL|array $attr
* @param NULL|array $data
*/
public function changeTag($parents, $tag, $new_name, $attr=NULL, $data=NULL)
{
$old_name = $tag->name;
$old_type = self::$tags[$tag->name]['type'];
$new_type = self::$tags[$new_name]['type'];
$tag->name = $new_name;
if (in_array($new_type, array('block', 'special')) && !in_array($old_type, array('block', 'special'))) {
// Create a sibling for the tag we are changing to contain all content
// that existing after the tag we are changing
$new_sibling = new stdClass;
$new_sibling->name = 'p';
$new_sibling->attr = array();
$new_sibling->data = array();
$new_sibling->children = array();
while ($block_tag = array_pop($parents)) {
$parent_type = self::$tags[$block_tag->name]['type'];
if ($parent_type == 'block' || $parent_type == 'root' || $parent_type == 'special') {
break;
}
}
// If the children are not in a p tag, they should be now since
// there will be a new block-level tag and a p tag after that
if ($block_tag->name != 'p') {
$parents[] = $block_tag;
$block_tag = $this->wrapChildren($block_tag, 'p');
}
// Loop through all tags after the tag we are changing and add them
// to the new sibling and remove them from the block tag
$found_tag = FALSE;
$last_depth = 1000000;
$iter = new ParserIterator($block_tag, 'all');
foreach ($iter as $block_child) {
if ($block_child === $tag) {
$this->removeTag($iter->parent(), $block_child);
$found_tag = TRUE;
continue;
}
if (!$found_tag) { continue; }
if ($iter->depth() <= $last_depth) {
$new_sibling->children[] = $block_child;
$this->removeTag($iter->parent(), $block_child);
$last_depth = $iter->depth();
}
}
$this->injectTag(end($parents), $block_tag, $tag);
$this->injectTag(end($parents), $tag, $new_sibling);
} elseif (in_array($old_type, array('block', 'special')) && !in_array($new_type, array('block', 'special'))) {
throw new Exception('A tag can not be changed from a block-level tag to an inline-level or self-closing tag');
}
if ($attr !== NULL) {
$tag->attr = $attr;
}
if ($data !== NULL) {
$tag->data = $data;
}
if ($new_type == 'self') {
unset($tag->children);
}
return $this;
}
/**
* Changes the tip of the parser for use when injecting tags into the middle
* of the tree
*
* @param array $parents The parent tags of the new tip
* @param stdClass $tag The tag to change the tip to
* @return array The old parents and tag for use when restoring the tip
*/
public function changeTip($parents, $tag)
{
$output = array($this->stack, $this->tip);
$this->stack = array_merge($parents, array($tag));
$this->tip = $tag;
return $output;
}
/**
* Closes a tag, if open
*
* @param string $name The name of the tag to close
* @return void
*/
public function closeTag($name)
{
$pos = $this->getOpenTagPos($name);
if ($pos === FALSE) {
return;
}
// When closing a tag, we want to implicitly close all tags
// that have been opened since. In the next section we will
// re-open inline tags, but if any block level tags are
// present, we won't re-open anything.
$removed_tags = array_values(array_slice($this->stack, $pos));
$trimmed_tags = array_values(array_slice($removed_tags, 1));
$this->stack = array_slice($this->stack, 0, $pos);
$this->tip = $this->stack[count($this->stack)-1];
// This code allows markup to not be perfectly balanced, but
// to created a balanaced tree by closing and then reopening
// inline tags
// First we check to make sure we only have inline tags, since
// re-opening inline tags shouldn't happen if a block-level has
// been closed
$all_inline = TRUE;
foreach ($trimmed_tags as $trimmed_tag) {
if (self::$tags[$trimmed_tag->name]['type'] == 'block') {
$all_inline = FALSE;
}
}
// This takes every inline tag and reopens it, preserving data
// so that think like links are identical
if ($all_inline) {
foreach ($trimmed_tags as $trimmed_tag) {
$this->openTag($trimmed_tag->name);
if (isset($trimmed_tag->data)) {
$this->tip->data = $trimmed_tag->data;
}
}
}
// Loop through all removed tags and see if we are closing a tag that
// is not self-closing and it has no children so we remove it since it
// is empty and useless
for ($j=count($removed_tags)-1; $j>=0; $j--) {
$removed_tag = $removed_tags[$j];
$type = self::$tags[$removed_tag->name]['type'];
$not_self = $type == 'inline' || $type == 'block';
$num_children = count($removed_tag->children);
if ($not_self && (!$num_children || ($num_children == 1 && $removed_tag->children[0]->name == 'string' && trim($removed_tag->children[0]->data[0]) == ''))) {
if ($j > 0) {
$parent = $removed_tags[$j-1];
} else {
$parent = $this->tip;
}
$parent->children = array_slice($parent->children, 0, -1);
}
}
return $this;
}
/**
* A shortcut for encoding special HTML character in UTF-8
*
* @param string $value The value to encode
* @return string The encoding value
*/
public function encode($value)
{
return htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
}
/**
* Generates an HTML id attribute for the tag specified if none exists
*
* @param stdClass $tag The tag to generate the id for
* @return void
*/
public function generateId($tag)
{
if (empty($tag->attr['id'])) {
$text = $this->captureText($tag);
$text = $this->makeFriendly($text);
if ($text && preg_match('#^([0-9]+)(?=[^0-9])#', $text, $match)) {
$new = array();
for ($i=0; $i<strlen($match[0]); $i++) {
$new[] = strtr(
$match[0][$i],
array(
'0' => 'oh', '1' => 'one', '2' => 'two',
'3' => 'three', '4' => 'four', '5' => 'five',
'6' => 'six', '7' => 'seven', '8' => 'eight',
'9' => 'nine'
)
);
}
$text = join('-', $new) . substr($text, strlen($match[0]));
}
$tag->attr['id'] = $text;
}
return $this;
}
/**
* Returns the parser data
*
* @return array The user-supplied parser data
*/
public function getData()
{
return $this->data;
}
/**
* Return an Iterator object for the parser
*
* @return ParserIterator The iterator for the parse tree
*/
public function getIterator()
{
return new ParserIterator($this);
}
/**
* Finds the position in the stack of a tag being opened
*
* @param string $name The name of the tag to find the open position of
* @return integer|FALSE The position of the tag in the stack, or FALSE if not found
*/
protected function getOpenTagPos($name)
{
for ($i=count($this->stack)-1; $i >= 0; $i--) {
if ($this->stack[$i]->name == $name) {
return $i;
}
}
return FALSE;
}
/**
* Returns the parse tree object
*
* @return stdClass The root element of the parse tree
*/
public function getTree()
{
return $this->tree;
}
/**
* Adds a string (non-tag text) to the tree, appending it to a previous
* string if possible
*
* @param string $string The string to add to the tree
* @param boolean $encode If the string should be encoded
* @return void
*/
public function handleString($string, $encode=TRUE)
{
if (!strlen($string)) {
return $this;
}
if ($encode) {
$string = $this->encode($string);
}
// If possible, the string is appended to the last tag if that is also
// a string. This is necessary to prevent lots of excess nodes in the
// tree, due to the way that the parsing is being done.
if ($this->tip->children) {
if (end($this->tip->children)->name == 'string') {
end($this->tip->children)->data[0] .= $string;
return $this;
}
}
$this->openTag('string', NULL, array($string));
return $this;
}
/**
* Handles opening, closing or adding a tag to the tree
*
* @param string $name The name of the tag to add
* @return void
*/
protected function handleTag($name)
{
$pos = $this->getOpenTagPos($name);
if ($pos) {
$this->closeTag($name);
} else {
$this->openTag($name);
}
return $this;
}
/**
* Injects a new tag as a child of the parent after the tag specified
*
* @param stdClass $parent The parent tag
* @param stdClass $after_tag The tag to inject the new tag after
* @param stdClass $new_tag The new tag to inject
*/
public function injectTag($parent, $after_tag, $new_tag)
{
$key = array_search($after_tag, $parent->children, TRUE);
$parent->children = array_merge(
array_slice($parent->children, 0, $key+1),
array($new_tag),
array_slice($parent->children, $key+1)
);
return $this;
}
protected function insertAttributes($output, $tag)
{
if (!empty($tag->attr)) {
$attributes = '';
foreach ($tag->attr as $attribute => $value) {
$attributes .= ' ' . $attribute . '="' . $value . '"';
}
// This prevents issues with sprintf() incorrectly interpreting a literal % as part of a placeholder
$attributes = str_replace('%', '%%', $attributes);
if (preg_match('#[ /]*>.*$#Ds', $output, $match)) {
$insert_pos = strlen($output) - strlen($match[0]);
$output = substr($output, 0, $insert_pos) . $attributes . substr($output, $insert_pos);
}
}
return $output;
}
/**
* Changes a string into a URL-friendly string
*
* @param string $string The string to convert
* @param integer $max_length The maximum length of the friendly URL
* @param string $delimiter The delimiter to use between words, defaults to `_`
* @return string The URL-friendly version of the string
*/
private function makeFriendly($string, $max_length=NULL, $delimiter=NULL)
{
// This allows omitting the max length, but including a delimiter
if ($max_length && !is_numeric($max_length)) {
$delimiter = $max_length;
$max_length = NULL;
}
$string = trim($string);
$string = str_replace("'", '', $string);
if (!strlen($delimiter)) {
$delimiter = '';
}
$delimiter_replacement = strtr($delimiter, array('\\' => '\\\\', '$' => '\\$'));
$delimiter_regex = preg_quote($delimiter, '#');
$string = preg_replace('#[^a-zA-Z0-9\-_]+#', $delimiter_replacement, $string);
//$string = preg_replace('#' . $delimiter_regex . '{2,}#', $delimiter_replacement, $string);
//$string = preg_replace('#_-_#', '-', $string);
//$string = preg_replace('#(^' . $delimiter_regex . '+|' . $delimiter_regex . '+$)#D', '', $string);
$length = strlen($string);
if ($max_length && $length > $max_length) {
$last_pos = strrpos($string, $delimiter, ($length - $max_length - 1) * -1);
if ($last_pos < ceil($max_length / 2)) {
$last_pos = $max_length;
}
$string = substr($string, 0, $last_pos);
}
return $string;
}
/**
* Opens an HTML tag
*
* @param string $name The name of the tag to open
* @param array $attr The attributes for the new tag
* @param array $data Data for self-closing tags
* @return stdClass The tag that was just opened, even if it is self-closing
*/
public function openTag($name, $attr=array(), $data=array())
{
// Ensures that we always have a block-level tag before adding inline or self-closing tags
if (!in_array(self::$tags[$name]['type'], array('block', 'special'))) {
$found = FALSE;
foreach ($this->stack as $stack_tag) {
if (self::$tags[$stack_tag->name]['type'] == 'block') { $found = TRUE; break; }
}
if (!$found) {
$this->openTag('p');
}
// If we are opening a block level tag and we just opened a paragraph, remove it
} elseif (in_array(self::$tags[$name]['type'], array('block', 'special')) && $this->tip && $this->tip->name == 'p') {
if (!$this->tip->children) {
$this->stack = array_slice($this->stack, 0, -1);
$this->tip = end($this->stack);
$this->tip->children = array_slice($this->tip->children, 0, -1);
} elseif (self::$tags[$name]['type'] == 'block') {
$this->closeTag('p');
}
}
$self_tag = in_array(self::$tags[$name]['type'], array('self', 'special'));
$new_tag = new stdClass();
$new_tag->name = $name;
if (self::$tags[$name]['placeholders']) {
for ($i=0; $i < self::$tags[$name]['placeholders']; $i++) {
if (isset($data[$i])) {
$new_tag->data[$i] = $data[$i];
} elseif (isset(self::$tags[$name]['defaults'][$i])) {
$new_tag->data[$i] = self::$tags[$name]['defaults'][$i];
} else {
$new_tag->data[$i] = '';
}
}
}
if ($name != 'string') {
$new_tag->attr = $attr;
}
if (!$self_tag) {
$new_tag->children = array();
}
$this->tip->children[] = $new_tag;
if ($self_tag) {
return $new_tag;
}
$this->tip = end($this->tip->children);
$this->stack[] = $this->tip;
return $this;
}
/**
* Parses the wiki document into a tree representing the HTML
*
* @return void
*/
public function parse()
{
$root = new stdClass();
$root->name = 'root';
$root->children = array();
$this->tree = $root;
$this->tip = $this->tree;
$this->stack[0] = $this->tree;
while ($this->yylex() !== FALSE);
if (method_exists($this, 'finishParsing')) {
$this->finishParsing();
}
while (end($this->stack) && end($this->stack)->name != 'root') {
$this->closeTag(end($this->stack)->name);
}
return $this->tree;
}
/**
* Removes a tag from its parent
*
* @param stdClass $parent The parent tag
* @param stdClass $tag The tag to remove
* @return void
*/
public function removeTag($parent, $tag)
{
$key = array_search($tag, $parent->children, TRUE);
if ($key === FALSE) { return; }
unset($parent->children[$key]);
$parent->children = array_values($parent->children);
return $this;
}
/**
* Renders the parse tree into HTML
*
* @return string The rendered HTML
*/
public function render()
{
if (!$this->tree) {
$this->parse();
}
foreach (array('One', 'Two') as $phase) {
do {
$changed = FALSE;
$iter = new ParserIterator($this, 'inclusive', 'plugin');
foreach ($iter as $num => $tag) {
$name = $tag->data[0];
$class = 'Plugin' . $this->camelize($name);
$parameters = $tag->data[1];
if (class_exists($class, FALSE)) {
$old_tip = $this->tip;
$old_stack = $this->stack;
$this->tip = $tag;
$this->stack = array_merge($iter->parents(), array($tag));
$object = new $class($this, $iter->parents(), $tag, $parameters);
$method_name = 'executePhase' . $phase;
if (method_exists($object, $method_name)) {
$changed = $object->$method_name();
}
$this->tip = $old_tip;
$this->stack = $old_stack;
// If anything has changed, we need to restart the parsing
if ($changed) {
break;
}
} else {
$tag->name = 'span';
$tag->attr = array('class' => 'parse_warning');
$tag->data = array();
$string = new stdClass();
$string->name = 'string';
$string->attr = array();
$string->data = 'Unknown plugin ' . $name;
$tag->children = array($string);
}
}
} while ($changed);
}
$output = '';
$stack = array();
$tip = NULL;
$iter = new ParserIterator($this);
foreach ($iter as $num => $tag) {
while ($iter->depth() <= count($stack)) {
$close_tag = array_pop($stack);
$value = self::$tags[$close_tag->name]['close'];
if (self::$tags[$close_tag->name]['close_placeholders']) {
$value = vsprintf($value, array_slice($close_tag->data, self::$tags[$close_tag->name]['open_placeholders']));
}
$output .= $value;
}
if (self::$tags[$tag->name]['type'] == 'self') {
$value = self::$tags[$tag->name]['tag'];
$value = $this->insertAttributes($value, $tag);
if (!empty($tag->data)) {
$value = vsprintf($value, $tag->data);
}
$output .= $value;
} else {
$value = self::$tags[$tag->name]['open'];
$value = $this->insertAttributes($value, $tag);
if (self::$tags[$tag->name]['open_placeholders']) {
$value = vsprintf($value, array_slice($tag->data, 0, self::$tags[$tag->name]['open_placeholders']));
}
$output .= $value;
$stack[] = $tag;
}
}
while ($stack) {
$close_tag = array_pop($stack);
$value = WikiParser::$tags[$close_tag->name]['close'];
if (WikiParser::$tags[$close_tag->name]['close_placeholders']) {
$value = vsprintf($value, array_slice($close_tag->data, WikiParser::$tags[$close_tag->name]['open_placeholders']));
}
$output .= $value;
}
return $output;
}
/**
* Takes a tag and unwraps all children into its location in the tree
*
* @param stdClass $parent The parent of the tag
* @param stdClass $tag The tag to unwrap the children of
* @return WikiParser The parser, for the purpose of chaining
*/