Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 886a23c

Browse files
committed
regexp: more readable notation
1 parent a0e43a4 commit 886a23c

23 files changed

+592
-120
lines changed

examples/syntax highlighting/demo-fshl-alt.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,20 @@ function codeBlockHandler(Texy\BlockParser $parser, array $matches, string $name
8787
// add new syntax: <?php ... ? >
8888
$texy->registerBlockPattern(
8989
'codeBlockHandler',
90-
'~^<\?php\n.+?\n\?>$~ms', // block patterns must be multiline and line-anchored
90+
'~^
91+
<\?php \n .+? \n \?>
92+
$~ms', // block patterns must be multiline and line-anchored
9193
'phpBlockSyntax',
9294
);
9395

9496
// add new syntax: <script ...> ... </script>
9597
$texy->registerBlockPattern(
9698
'codeBlockHandler',
97-
'~^<script(?: type=.?text/javascript.?)?>\n.+?\n</script>$~ms', // block patterns must be multiline and line-anchored
99+
'~^
100+
<script (?: type=.?text/javascript.?)? > \n
101+
.+? \n
102+
</script>
103+
$~ms', // block patterns must be multiline and line-anchored
98104
'scriptBlockSyntax',
99105
);
100106

examples/user syntax/demo.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,35 @@
2727
// add new syntax: *bold*
2828
$texy->registerLinePattern(
2929
'userInlineHandler', // callback function or method
30-
'~(?<!\*)\*(?!\ |\*)(.+)' . Texy\Patterns::MODIFIER . '?(?<!\ |\*)\*(?!\*)()~U', // regular expression
30+
'~
31+
(?<! \* ) \* (?! [ *] )
32+
(.+)
33+
' . Texy\Patterns::MODIFIER . '?
34+
(?<! [ *] ) \* (?! \* )
35+
()~U', // regular expression
3136
'myInlineSyntax1', // any syntax name
3237
);
3338

3439
// add new syntax: _italic_
3540
$texy->registerLinePattern(
3641
'userInlineHandler',
37-
'~(?<!_)_(?!\ |_)(.+)' . Texy\Patterns::MODIFIER . '?(?<!\ |_)_(?!_)()~U',
42+
'~
43+
(?<! _ ) _ (?! [ _] )
44+
(.+)
45+
' . Texy\Patterns::MODIFIER . '?
46+
(?<! [ _] ) _ (?! _ )
47+
()~U',
3848
'myInlineSyntax2',
3949
);
4050

4151

4252
// add new syntax: .h1 ...
4353
$texy->registerBlockPattern(
4454
'userBlockHandler',
45-
'~^\.([a-z0-9]+)\n(.+)$~m', // block patterns must be multiline and line-anchored
55+
'~^
56+
\. ([a-z0-9]+) \n
57+
(.+)
58+
$~m', // block patterns must be multiline and line-anchored
4659
'myBlockSyntax1',
4760
);
4861

src/Texy/BlockParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Texy;
1111

12+
use JetBrains\PhpStorm\Language;
1213
use function is_string, max, strlen, substr, trim, usort;
1314

1415

@@ -41,7 +42,11 @@ public function isIndented(): bool
4142

4243
// match current line against RE.
4344
// if succesfull, increments current position and returns true
44-
public function next(string $pattern, &$matches): bool
45+
public function next(
46+
#[Language('PhpRegExpXTCommentMode')]
47+
string $pattern,
48+
&$matches,
49+
): bool
4550
{
4651
if ($this->offset > strlen($this->text)) {
4752
return false;

src/Texy/Modifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function setProperties(?string $s): void
6464
$ch = $s[$p];
6565

6666
if ($ch === '(') { // title
67-
$m = Regexp::match($s, '~(?:\\\\\)|[^)\n])++\)~', offset: $p);
67+
$m = Regexp::match($s, '~(?: \\\\\) | [^)\n] )++\)~', offset: $p);
6868
$this->title = Helpers::unescapeHtml(str_replace('\)', ')', trim(substr($m[0], 1, -1))));
6969
$p += strlen($m[0]);
7070

src/Texy/Modules/BlockModule.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,20 @@ public function __construct(Texy\Texy $texy)
4040

4141
$texy->registerBlockPattern(
4242
$this->pattern(...),
43-
'~^/--++\ *+(.*)' . Texy\Patterns::MODIFIER_H . '?$((?:\n(?0)|\n.*+)*)(?:\n\\\--.*$|\z)~mUi',
43+
'~^
44+
/--++ \ *+ # opening tag /--
45+
(.*) # content type (1)
46+
' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
47+
$
48+
((?: # content (3)
49+
\n (?0) | # recursive nested blocks
50+
\n.*+ # or any content
51+
)*)
52+
(?:
53+
\n \\\--.* $ | # closing tag
54+
\z # or end of input
55+
)
56+
~mUi',
4457
'blocks',
4558
);
4659
}
@@ -54,8 +67,16 @@ private function beforeBlockParse(Texy\BlockParser $parser, string &$text): void
5467
// autoclose exclusive blocks
5568
$text = Texy\Regexp::replace(
5669
$text,
57-
'~^(/--++\ *+(?!div|texysource).*)$((?:\n.*+)*?)(?:\n\\\--.*$|(?=(\n/--.*$)))~mi',
58-
"\$1\$2\n\\--",
70+
'~^
71+
( /--++ \ *+ (?! div|texysource ) .* ) # opening tag except div/texysource (1)
72+
$
73+
((?: \n.*+ )*?) # content (2)
74+
(?:
75+
\n \\\--.* $ | # closing tag
76+
(?= (\n /--.* $)) # or next block starts (3)
77+
)
78+
~mi',
79+
"\$1\$2\n\\--", // add closing tag
5980
);
6081
}
6182

src/Texy/Modules/BlockQuoteModule.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ public function __construct(Texy\Texy $texy)
2424

2525
$texy->registerBlockPattern(
2626
$this->pattern(...),
27-
'~^(?:' . Texy\Patterns::MODIFIER_H . '\n)?\>([\ \t]++|:)(\S.*+)$~mU', // original
27+
'~^
28+
(?: ' . Texy\Patterns::MODIFIER_H . '\n)? # modifier (1)
29+
> # blockquote char
30+
( [ \t]++ | : ) # space/tab or colon (2)
31+
( \S.*+ ) # content (3)
32+
$~mU',
2833
'blockquote',
2934
);
3035
}
@@ -60,7 +65,7 @@ public function pattern(Texy\BlockParser $parser, array $matches): Texy\HtmlElem
6065
}
6166
$content .= $mContent . "\n";
6267

63-
if (!$parser->next("~^>(?:|([\\ \\t]{1,$spaces}|:)(.*))()$~mA", $matches)) {
68+
if (!$parser->next("~^>(?: | ([ \\t]{1,$spaces} | :) (.*))()$~mA", $matches)) {
6469
break;
6570
}
6671

src/Texy/Modules/EmoticonModule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ private function beforeParse(): void
6666

6767
$this->texy->registerLinePattern(
6868
$this->pattern(...),
69-
'~(?<=^|[\x00-\x20])(' . implode('|', $pattern) . ')~',
69+
'~
70+
(?<= ^ | [\x00-\x20] )
71+
(' . implode('|', $pattern) . ')
72+
~',
7073
'emoticon',
7174
'~' . implode('|', $pattern) . '~',
7275
);

src/Texy/Modules/FigureModule.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,24 @@ private function beforeParse(): void
4949
{
5050
$this->texy->registerBlockPattern(
5151
$this->pattern(...),
52-
'~^(?>\[\*\ *+([^\n' . Patterns::MARK . ']{1,1000})' . Patterns::MODIFIER . '?\ *+(\*|(?<!<)>|<)\])' // [* urls .(title)[class]{style} >]
53-
. '(?::(' . Patterns::LINK_URL . '|:))??'
54-
. '(?:\ ++\*\*\*\ ++(.{0,2000}))' . ($this->requireCaption ? '' : '?')
55-
. Patterns::MODIFIER_H . '?()$~mU',
52+
'~^
53+
(?>
54+
\[\*\ *+ # opening bracket with asterisk
55+
([^\n' . Patterns::MARK . ']{1,1000}) # URLs (1)
56+
' . Patterns::MODIFIER . '? # modifier (2)
57+
\ *+
58+
( \* | (?<! < ) > | < ) # alignment (3)
59+
]
60+
)
61+
(?:
62+
:(' . Patterns::LINK_URL . ' | : ) # link or colon (4)
63+
)??
64+
(?:
65+
\ ++ \*\*\* \ ++ # separator
66+
(.{0,2000}) # caption (5)
67+
)' . ($this->requireCaption ? '' : '?') . '
68+
' . Patterns::MODIFIER_H . '? # modifier (6)
69+
()$~mU',
5670
'figure',
5771
);
5872
}

src/Texy/Modules/HeadingModule.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,22 @@ public function __construct(Texy\Texy $texy)
6565

6666
$texy->registerBlockPattern(
6767
$this->patternUnderline(...),
68-
'~^(\S.{0,1000})' . Texy\Patterns::MODIFIER_H . '?\n'
69-
. '(\#{3,}+|\*{3,}+|={3,}+|-{3,}+)$~mU',
68+
'~^
69+
( \S .{0,1000} ) # heading text (1)
70+
' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
71+
\n
72+
( \#{3,}+ | \*{3,}+ | ={3,}+ | -{3,}+ ) # underline characters (3)
73+
$~mU',
7074
'heading/underlined',
7175
);
7276

7377
$texy->registerBlockPattern(
7478
$this->patternSurround(...),
75-
'~^(\#{2,}+|={2,}+)(.+)' . Texy\Patterns::MODIFIER_H . '?()$~mU',
79+
'~^
80+
( \#{2,}+ | ={2,}+ ) # opening characters (1)
81+
(.+) # heading text (2)
82+
' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
83+
()$~mU',
7684
'heading/surrounded',
7785
);
7886
}

src/Texy/Modules/HorizLineModule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public function __construct(Texy\Texy $texy)
3232

3333
$texy->registerBlockPattern(
3434
$this->pattern(...),
35-
'~^(\*{3,}+|-{3,}+)[\ \t]*' . Texy\Patterns::MODIFIER . '?()$~mU',
35+
'~^
36+
( \*{3,}+ | -{3,}+ ) # three or more * or - (1)
37+
[ \t]* # optional spaces
38+
' . Texy\Patterns::MODIFIER . '? # modifier (2)
39+
()$~mU',
3640
'horizline',
3741
);
3842
}

0 commit comments

Comments
 (0)