-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathParser.php
4681 lines (3934 loc) · 138 KB
/
Parser.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
/**
* Textile - A Humane Web Text Generator.
*
* @link https://github.com/textile/php-textile
*/
/*
* Textile - A Humane Web Text Generator
*
* Copyright (c) 2003-2004, Dean Allen <dean@textism.com>
* All rights reserved.
*
* Thanks to Carlo Zottmann <carlo@g-blog.net> for refactoring
* Textile's procedural code into a class framework
*
* Additions and fixes Copyright (c) 2006 Alex Shiels https://twitter.com/tellyworth
* Additions and fixes Copyright (c) 2010 Stef Dawson http://stefdawson.com/
* Additions and fixes Copyright (c) 2010-17 Netcarver https://github.com/netcarver
* Additions and fixes Copyright (c) 2011 Jeff Soo http://ipsedixit.net/
* Additions and fixes Copyright (c) 2012 Robert Wetzlmayr http://wetzlmayr.com/
* Additions and fixes Copyright (c) 2012-18 Jukka Svahn http://rahforum.biz/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name Textile nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
Textile usage examples.
Block modifier syntax:
Header: h(1-6).
Paragraphs beginning with 'hn. ' (where n is 1-6) are wrapped in header tags.
Example: h1. Header... -> <h1>Header...</h1>
Paragraph: p. (also applied by default)
Example: p. Text -> <p>Text</p>
Blockquote: bq.
Example: bq. Block quotation... -> <blockquote>Block quotation...</blockquote>
Blockquote with citation: bq.:http://citation.url
Example: bq.:http://textism.com/ Text...
-> <blockquote cite="http://textism.com">Text...</blockquote>
Footnote: fn(1-100).
Example: fn1. Footnote... -> <p id="fn1">Footnote...</p>
Numeric list: #, ##
Consecutive paragraphs beginning with # are wrapped in ordered list tags.
Example: <ol><li>ordered list</li></ol>
Bulleted list: *, **
Consecutive paragraphs beginning with * are wrapped in unordered list tags.
Example: <ul><li>unordered list</li></ul>
Definition list:
Terms ;, ;;
Definitions :, ::
Consecutive paragraphs beginning with ; or : are wrapped in definition list tags.
Example: <dl><dt>term</dt><dd>definition</dd></dl>
Redcloth-style Definition list:
- Term1 := Definition1
- Term2 := Extended
definition =:
Phrase modifier syntax:
_emphasis_ -> <em>emphasis</em>
__italic__ -> <i>italic</i>
*strong* -> <strong>strong</strong>
**bold** -> <b>bold</b>
??citation?? -> <cite>citation</cite>
-deleted text- -> <del>deleted</del>
+inserted text+ -> <ins>inserted</ins>
^superscript^ -> <sup>superscript</sup>
~subscript~ -> <sub>subscript</sub>
@code@ -> <code>computer code</code>
%(bob)span% -> <span class="bob">span</span>
==notextile== -> leave text alone (do not format)
"linktext":url -> <a href="url">linktext</a>
"linktext(title)":url -> <a href="url" title="title">linktext</a>
"$":url -> <a href="url">url</a>
"$(title)":url -> <a href="url" title="title">url</a>
!imageurl! -> <img src="imageurl" />
!imageurl(alt text)! -> <img src="imageurl" alt="alt text" />
!imageurl!:linkurl -> <a href="linkurl"><img src="imageurl" /></a>
ABC(Always Be Closing) -> <acronym title="Always Be Closing">ABC</acronym>
Linked Notes:
Allows the generation of an automated list of notes with links.
Linked notes are composed of three parts, a set of named _definitions_, a set of
_references_ to those definitions and one or more _placeholders_ indicating where
the consolidated list of notes is to be placed in your document.
Definitions:
Each note definition must occur in its own paragraph and should look like this...
note#mynotelabel. Your definition text here.
You are free to use whatever label you wish after the # as long as it is made up
of letters, numbers, colon(:) or dash(-).
References:
Each note reference is marked in your text like this[#mynotelabel] and
it will be replaced with a superscript reference that links into the list of
note definitions.
List placeholder(s):
The note list can go anywhere in your document. You have to indicate where
like this:
notelist.
notelist can take attributes (class#id) like this: notelist(class#id).
By default, the note list will show each definition in the order that they
are referenced in the text by the _references_. It will show each definition with
a full list of backlinks to each reference. If you do not want this, you can choose
to override the backlinks like this...
notelist(class#id)!. Produces a list with no backlinks.
notelist(class#id)^. Produces a list with only the first backlink.
Should you wish to have a specific definition display backlinks differently to this
then you can override the backlink method by appending a link override to the
_definition_ you wish to customise.
note#label. Uses the citelist's setting for backlinks.
note#label!. Causes that definition to have no backlinks.
note#label^. Causes that definition to have one backlink (to the first ref.)
note#label*. Causes that definition to have all backlinks.
Any unreferenced notes will be left out of the list unless you explicitly state
you want them by adding a '+'. Like this...
notelist(class#id)!+. Giving a list of all notes without any backlinks.
You can mix and match the list backlink control and unreferenced links controls
but the backlink control (if any) must go first. Like so: notelist^+. , not
like this: notelist+^.
Example...
Scientists say[#lavader] the moon is small.
note#other. An unreferenced note.
note#lavader(myliclass). "Proof":http://example.com of a small moon.
notelist(myclass#myid)+.
Would output (the actual IDs used would be randomised)...
<p>Scientists say<sup><a href="#note1" id="noteref1">1</sup> the moon is small.</p>
<ol class="myclass" id="myid">
<li class="myliclass"><a href="#noteref1"><sup>a</sup></a>
<span id="note1"> </span><a href="http://example.com">Proof</a> of a small moon.</li>
<li>An unreferenced note.</li>
</ol>
The 'a b c' backlink characters can be altered too.
For example if you wanted the notes to have numeric backlinks starting from 1:
notelist:1.
Table syntax:
Simple tables:
|a|simple|table|row|
|And|Another|table|row|
|With an||empty|cell|
|=. My table caption goes here
|_. A|_. table|_. header|_.row|
|A|simple|table|row|
Note: Table captions *must* be the first line of the table else treated as a center-aligned cell.
Tables with attributes:
table{border:1px solid black}. My table summary here
{background:#ddd;color:red}. |{}| | | |
To specify thead / tfoot / tbody groups, add one of these on its own line
above the row(s) you wish to wrap (you may specify attributes before the dot):
|^. # thead
|-. # tbody
|~. # tfoot
Column groups:
|:\3. 100|
Becomes:
<colgroup span="3" width="100"></colgroup>
You can omit either or both of the \N or width values. You may also
add cells after the colgroup definition to specify col elements with
span, width, or standard Textile attributes:
|:. 50|(firstcol). |\2. 250||300|
Becomes:
<colgroup width="50">
<col class="firstcol" />
<col span="2" width="250" />
<col />
<col width="300" />
</colgroup>
(Note that, per the HTML specification, you should not add span
to the colgroup if specifying col elements.)
Applying Attributes:
Most anywhere Textile code is used, attributes such as arbitrary css style,
css classes, and ids can be applied. The syntax is fairly consistent.
The following characters quickly alter the alignment of block elements:
< -> left align ex. p<. left-aligned para
> -> right align h3>. right-aligned header 3
= -> centred h4=. centred header 4
<> -> justified p<>. justified paragraph
These will change vertical alignment in table cells:
^ -> top ex. |^. top-aligned table cell|
- -> middle |-. middle aligned|
~ -> bottom |~. bottom aligned cell|
Plain (parentheses) inserted between block syntax and the closing dot-space
indicate classes and ids:
p(hector). paragraph -> <p class="hector">paragraph</p>
p(#fluid). paragraph -> <p id="fluid">paragraph</p>
(classes and ids can be combined)
p(hector#fluid). paragraph -> <p class="hector" id="fluid">paragraph</p>
Curly {brackets} insert arbitrary css style
p{line-height:18px}. paragraph -> <p style="line-height:18px">paragraph</p>
h3{color:red}. header 3 -> <h3 style="color:red">header 3</h3>
Square [brackets] insert language attributes
p[no]. paragraph -> <p lang="no">paragraph</p>
%[fr]phrase% -> <span lang="fr">phrase</span>
Usually Textile block element syntax requires a dot and space before the block
begins, but since lists don't, they can be styled just using braces
#{color:blue} one -> <ol style="color:blue">
# big <li>one</li>
# list <li>big</li>
<li>list</li>
</ol>
Using the span tag to style a phrase
It goes like this, %{color:red}the fourth the fifth%
-> It goes like this, <span style="color:red">the fourth the fifth</span>
Ordered list start and continuation:
You can control the start attribute of an ordered list like so;
#5 Item 5
# Item 6
You can resume numbering list items after some intervening anonymous block like so...
#_ Item 7
# Item 8
*/
namespace Netcarver\Textile;
/**
* Textile parser.
*
* The Parser class takes Textile input and
* converts it to well formatted HTML. This is
* the library's main class, hosting the parsing
* functionality and exposing a simple
* public interface for you to use.
*
* The most basic use case would involve initialising
* a instance of the class and calling the Parser::parse()
* method:
*
* bc. $parser = new \Netcarver\Textile\Parser();
* echo $parser->parse('h1. Hello World!');
*
* Generates:
*
* bc. <h1>Hello World!</h1>
*
* @see Parser::__construct()
* @see Parser::parse()
*/
class Parser
{
/**
* Version number.
*
* @var string
*/
protected $ver = '3.6.1';
/**
* Regular expression snippets.
*
* @var array
*/
protected $regex_snippets;
/**
* Pattern for horizontal align.
*
* @var string
*/
protected $hlgn = "(?:\<(?!>)|<>|>|<|(?<!<)\>|\<\>|\=|[()]+(?! ))";
/**
* Pattern for vertical align.
*
* @var string
*/
protected $vlgn = "[\-^~]";
/**
* Pattern for HTML classes and IDs.
*
* Does not allow classes/ids/languages/styles to span across
* newlines if used in a dotall regular expression.
*
* @var string
*/
protected $clas = "(?:\([^)\n]+\))";
/**
* Pattern for language attribute.
*
* @var string
*/
protected $lnge = "(?:\[[^]\n]+\])";
/**
* Pattern for style attribute.
*
* @var string
*/
protected $styl = "(?:\{[^}\n]+\})";
/**
* Regular expression pattern for column spans in tables.
*
* @var string
*/
protected $cspn = "(?:\\\\[0-9]+)";
/**
* Regular expression for row spans in tables.
*
* @var string
*/
protected $rspn = "(?:\/[0-9]+)";
/**
* Regular expression for horizontal or vertical alignment.
*
* @var string
*/
protected $a;
/**
* Regular expression for column or row spans in tables.
*
* @var string
*/
protected $s;
/**
* Pattern that matches a class, style, language and horizontal alignment attributes.
*
* @var string
*/
protected $c;
/**
* Pattern that matches class, style and language attributes.
*
* Allows all 16 possible permutations of class, style and language attributes.
* No attribute, c, cl, cs, cls, csl, l, lc, ls, lcs, lsc, s, sc, sl, scl or slc.
*
* @var string
*/
protected $cls;
/**
* Whitelisted block tags.
*
* @var array
*/
protected $blocktag_whitelist = array();
/**
* Whether block tags are enabled.
*
* @var bool
* @since 3.6.0
*/
protected $blockTagsEnabled = true;
/**
* Whether lines are wrapped.
*
* @var bool
* @since 3.6.0
*/
protected $lineWrapEnabled = true;
/**
* Pattern for punctation.
*
* @var string
*/
protected $pnct = '[\!"#\$%&\'()\*\+,\-\./:;<=>\?@\[\\\]\^_`{\|}\~]';
/**
* Pattern for URL.
*
* @var string
*/
protected $urlch;
/**
* Matched marker symbols.
*
* @var string
*/
protected $syms = '¤§µ¶†‡•∗∴◊♠♣♥♦';
/**
* HTML rel attribute used for links.
*
* @var string
*/
protected $rel = '';
/**
* Array of footnotes.
*
* @var array
*/
protected $fn;
/**
* Shelved content.
*
* Stores fragments of the source text that have been parsed
* and require no more processing.
*
* @var array
*/
protected $shelf = array();
/**
* Restricted mode.
*
* @var bool
*/
protected $restricted = false;
/**
* Disallow images.
*
* @var bool
*/
protected $noimage = false;
/**
* Lite mode.
*
* @var bool
*/
protected $lite = false;
/**
* Accepted link protocols.
*
* @var array
*/
protected $url_schemes = array();
/**
* Restricted link protocols.
*
* @var array
*/
protected $restricted_url_schemes = array(
'http',
'https',
'ftp',
'mailto',
);
/**
* Unrestricted link protocols.
*
* @var array
*/
protected $unrestricted_url_schemes = array(
'http',
'https',
'ftp',
'mailto',
'file',
'tel',
'callto',
'sftp',
);
/**
* Span tags.
*
* @var array
*/
protected $span_tags = array(
'*' => 'strong',
'**' => 'b',
'??' => 'cite',
'_' => 'em',
'__' => 'i',
'-' => 'del',
'%' => 'span',
'+' => 'ins',
'~' => 'sub',
'^' => 'sup',
);
/**
* Patterns for finding glyphs.
*
* An array of regex patterns used to find text features
* such as apostrophes, fractions and em-dashes. Each
* entry in this array must have a corresponding entry in
* the $glyph_replace array.
*
* @var null|array
* @see Parser::$glyph_replace
*/
protected $glyph_search = null;
/**
* Glyph replacements.
*
* An array of replacements used to insert typographic glyphs
* into the text. Each entry must have a corresponding entry in
* the $glyph_search array and may refer to values captured in
* the corresponding search regex.
*
* @var null|array
* @see Parser::$glyph_search
*/
protected $glyph_replace = null;
/**
* Indicates whether glyph substitution is required.
*
* Dirty flag, set by Parser::setSymbol(), indicating the parser needs to
* rebuild the glyph substitutions before the next parse.
*
* @var bool
* @see Parser::setSymbol()
*/
protected $rebuild_glyphs = true;
/**
* Relative image path.
*
* @var string
*/
protected $relativeImagePrefix = '';
/**
* Maximum nesting level for inline elements.
*
* @var int
*/
protected $max_span_depth = 5;
/**
* Server document root.
*
* @var string
*/
protected $doc_root;
/**
* Target document type.
*
* @var string
*/
protected $doctype;
/**
* An array of supported doctypes.
*
* @var array
* @since 3.6.0
*/
protected $doctypes = array(
'xhtml',
'html5',
);
/**
* Substitution symbols.
*
* Basic symbols used in textile glyph replacements. To override these, call
* setSymbol method before calling Parser::parse().
*
* @var array
* @see Parser::setSymbol()
* @see Parser::parse()
*/
protected $symbols = array(
'quote_single_open' => '‘',
'quote_single_close' => '’',
'quote_double_open' => '“',
'quote_double_close' => '”',
'apostrophe' => '’',
'prime' => '′',
'prime_double' => '″',
'ellipsis' => '…',
'emdash' => '—',
'endash' => '–',
'dimension' => '×',
'trademark' => '™',
'registered' => '®',
'copyright' => '©',
'half' => '½',
'quarter' => '¼',
'threequarters' => '¾',
'degrees' => '°',
'plusminus' => '±',
'fn_ref_pattern' => '<sup{atts}>{marker}</sup>',
'fn_foot_pattern' => '<sup{atts}>{marker}</sup>',
'nl_ref_pattern' => '<sup{atts}>{marker}</sup>',
);
/**
* Dimensionless images flag.
*
* @var bool
*/
protected $dimensionless_images = false;
/**
* Directory separator.
*
* @var string
*/
protected $ds = '/';
/**
* Whether mbstring extension is installed.
*
* @var bool
*/
protected $mb;
/**
* Multi-byte conversion map.
*
* @var array
*/
protected $cmap = array(0x0080, 0xffff, 0, 0xffff);
/**
* Stores note index.
*
* @var int
*/
protected $note_index = 1;
/**
* Stores unreferenced notes.
*
* @var array
*/
protected $unreferencedNotes = array();
/**
* Stores note lists.
*
* @var array
*/
protected $notelist_cache = array();
/**
* Stores notes.
*
* @var array
*/
protected $notes = array();
/**
* Stores URL references.
*
* @var array
*/
protected $urlrefs = array();
/**
* Stores span depth.
*
* @var int
*/
protected $span_depth = 0;
/**
* Unique ID used for reference tokens.
*
* @var string
*/
protected $uid;
/**
* Token reference index.
*
* @var int
*/
protected $refIndex = 1;
/**
* Stores references values.
*
* @var array
*/
protected $refCache = array();
/**
* Matched open and closed quotes.
*
* @var array
*/
protected $quotes = array(
'"' => '"',
"'" => "'",
'(' => ')',
'{' => '}',
'[' => ']',
'«' => '»',
'»' => '«',
'‹' => '›',
'›' => '‹',
'„' => '“',
'‚' => '‘',
'‘' => '’',
'”' => '“',
);
/**
* Regular expression that matches starting quotes.
*
* @var string
*/
protected $quote_starts;
/**
* Ordered list starts.
*
* @var array
*/
protected $olstarts = array();
/**
* Link prefix.
*
* @var string
*/
protected $linkPrefix;
/**
* Link index.
*
* @var int
*/
protected $linkIndex = 1;
/**
* Constructor.
*
* The constructor allows setting options that affect the
* class instance as a whole, such as the output doctype.
* To instruct the parser to return HTML5 markup instead of
* XHTML, set $doctype argument to 'html5'.
*
* bc. $parser = new \Netcarver\Textile\Parser('html5');
* echo $parser->parse('HTML(HyperText Markup Language)");
*
* @param string $doctype The output document type, either 'xhtml' or 'html5'
* @throws \InvalidArgumentException
* @see Parser::parse()
* @api
*/
public function __construct($doctype = 'xhtml')
{
$this->setDocumentType($doctype)->setRestricted(false);
$uid = uniqid(rand());
$this->uid = 'textileRef:'.$uid.':';
$this->linkPrefix = $uid.'-';
$this->a = "(?:$this->hlgn|$this->vlgn)*";
$this->s = "(?:$this->cspn|$this->rspn)*";
$this->c = "(?:$this->clas|$this->styl|$this->lnge|$this->hlgn)*";
$this->cls = '(?:'.
"$this->clas(?:".
"$this->lnge(?:$this->styl)?|$this->styl(?:$this->lnge)?".
')?|'.
"$this->lnge(?:".
"$this->clas(?:$this->styl)?|$this->styl(?:$this->clas)?".
')?|'.
"$this->styl(?:".
"$this->clas(?:$this->lnge)?|$this->lnge(?:$this->clas)?".
')?'.
')?';
if ($this->isUnicodePcreSupported()) {
$this->regex_snippets = array(
'acr' => '\p{Lu}\p{Nd}',
'abr' => '\p{Lu}',
'nab' => '\p{Ll}',
'wrd' => '(?:\p{L}|\p{M}|\p{N}|\p{Pc})',
'mod' => 'u', // Make sure to mark the unicode patterns as such, Some servers seem to need this.
'cur' => '\p{Sc}',
'digit' => '\p{N}',
'space' => '(?:\p{Zs}|\h|\v)',
'char' => '(?:[^\p{Zs}\h\v])',
);
} else {
$this->regex_snippets = array(
'acr' => 'A-Z0-9',
'abr' => 'A-Z',
'nab' => 'a-z',
'wrd' => '\w',
'mod' => '',
'cur' => '',
'digit' => '\d',
'space' => '(?:\s|\h|\v)',
'char' => '\S',
);
}
$this->urlch = '['.$this->regex_snippets['wrd'].'"$\-_.+!*\'(),";\/?:@=&%#{}|\\^~\[\]`]';
$this->quote_starts = implode('|', array_map('preg_quote', array_keys($this->quotes)));
if (defined('DIRECTORY_SEPARATOR')) {
$this->ds = DIRECTORY_SEPARATOR;
}
if (php_sapi_name() === 'cli') {
$this->setDocumentRootDirectory(getcwd());
} elseif (!empty($_SERVER['DOCUMENT_ROOT'])) {
$this->setDocumentRootDirectory($_SERVER['DOCUMENT_ROOT']);
} elseif (!empty($_SERVER['PATH_TRANSLATED'])) {
$this->setDocumentRootDirectory($_SERVER['PATH_TRANSLATED']);
}
}
/**
* Sets the output document type.
*
* bc. $parser = new \Netcarver\Textile\Parser();
* echo $parser
* ->setDocumentType('html5')
* ->parse('HTML(HyperText Markup Language)");
*
* @param string $doctype Either 'xhtml' or 'html5'
* @return Parser
* @since 3.6.0
* @see Parser::getDocumentType()
* @api
*/
public function setDocumentType($doctype)
{
if (in_array($doctype, $this->doctypes, true)) {
if ($this->getDocumentType() !== $doctype) {
$this->doctype = $doctype;
$this->rebuild_glyphs = true;
}
return $this;
}
throw new \InvalidArgumentException('Invalid doctype given.');