1515use Lkrms \Console \Support \ConsoleTagAttributes as TagAttributes ;
1616use Lkrms \Console \Support \ConsoleTagFormats as TagFormats ;
1717use Lkrms \Exception \UnexpectedValueException ;
18- use Lkrms \Support \Catalog \RegularExpression as Regex ;
1918use Lkrms \Utility \Convert ;
2019use Lkrms \Utility \Pcre ;
2120use Lkrms \Utility \Str ;
@@ -66,8 +65,8 @@ final class ConsoleFormatter
6665 * Splits the subject into formattable paragraphs, fenced code blocks and
6766 * code spans
6867 */
69- private const PARSER_REGEX = <<<'REGEX'
70- (?msx)
68+ private const MARKUP = <<<'REGEX'
69+ /
7170 (?(DEFINE)
7271 (?<endofline> \h*+ \n )
7372 (?<endofblock> ^ \k<indent> \k<fence> \h*+ $ )
@@ -85,7 +84,7 @@ final class ConsoleFormatter
8584 (?<breaks> (?&endofline)+ ) |
8685 # Everything except unescaped backticks until the start of the next
8786 # paragraph
88- (?<text> (?> (?: [^\\`\n]+ | \\ [-\\!"\#$%&'()*+,./:;<=>?@[\]^_`{|}~\n] | \\ | \n (?! (?&endofline) ) )+ (?&endofline)* ) ) |
87+ (?<text> (?> (?: [^\\`\n]+ | \\ [-\\!"\#$%&'()*+,.\ /:;<=>?@[\]^_`{|}~\n] | \\ | \n (?! (?&endofline) ) )+ (?&endofline)* ) ) |
8988 # CommonMark-compliant fenced code blocks
9089 (?> (?(indent)
9190 (?> (?<fence> ```+ ) (?<infostring> [^\n]* ) \n )
@@ -100,39 +99,39 @@ final class ConsoleFormatter
10099 # Unmatched backticks
101100 (?<extra> `+ ) |
102101 \z
103- )
102+ ) /mxs
104103 REGEX;
105104
106105 /**
107106 * Matches inline formatting tags used outside fenced code blocks and code
108107 * spans
109108 */
110- private const TAG_REGEX = <<<'REGEX'
111- (?xm)
109+ private const TAG = <<<'REGEX'
110+ /
112111 (?(DEFINE)
113- (?<esc> \\ [-\\!"\#$%&'()*+,./:;<=>?@[\]^_`{|}~] | \\ )
112+ (?<esc> \\ [-\\!"\#$%&'()*+,.\ /:;<=>?@[\]^_`{|}~] | \\ )
114113 )
115114 (?<! \\ ) (?: \\\\ )* \K (?|
116115 \b (?<tag> _ {1,3}+ ) (?! \s ) (?> (?<text> (?: [^_\\]+ | (?&esc) | (?! (?<! \s ) \k<tag> \b ) _ + )* ) ) (?<! \s ) \k<tag> \b |
117116 (?<tag> \* {1,3}+ ) (?! \s ) (?> (?<text> (?: [^*\\]+ | (?&esc) | (?! (?<! \s ) \k<tag> ) \* + )* ) ) (?<! \s ) \k<tag> |
118117 (?<tag> < ) (?! \s ) (?> (?<text> (?: [^>\\]+ | (?&esc) | (?! (?<! \s ) > ) > + )* ) ) (?<! \s ) > |
119118 (?<tag> ~~ ) (?! \s ) (?> (?<text> (?: [^~\\]+ | (?&esc) | (?! (?<! \s ) ~~ ) ~ + )* ) ) (?<! \s ) ~~ |
120119 ^ (?<tag> \#\# ) \h+ (?> (?<text> (?: [^\#\s\\]+ | (?&esc) | \#+ (?! \h* $ ) | \h++ (?! (?: \#+ \h* )? $ ) )* ) ) (?: \h+ \#+ | \h* ) $
121- )
120+ ) /mx
122121 REGEX;
123122
124123 /**
125- * A CommonMark-compliant backslash escape, or an escaped line break with an
126- * optional leading space
124+ * Matches a CommonMark-compliant backslash escape, or an escaped line break
125+ * with an optional leading space
127126 */
128- private const UNESCAPE_REGEX = <<<'REGEX'
129- (?x)
127+ private const ESCAPE = <<<'REGEX'
128+ /
130129 (?|
131- \\ ( [-\\ !"\#$%&'()*+,./:;<=>?@[\]^_`{|}~] ) |
130+ \\ ( [-\\ !"\#$%&'()*+,.\ /:;<=>?@[\]^_`{|}~] ) |
132131 # Lookbehind assertions are unnecessary because the first branch
133132 # matches escaped spaces and backslashes
134133 \ ? \\ ( \n )
135- )
134+ ) /x
136135 REGEX;
137136
138137 private static ConsoleFormatter $ DefaultFormatter ;
@@ -316,7 +315,7 @@ public function formatTags(
316315 // Normalise line endings and split the string into formattable text,
317316 // fenced code blocks and code spans
318317 if (!Pcre::matchAll (
319- Regex:: delimit ( self ::PARSER_REGEX ) . ' u ' ,
318+ self ::MARKUP ,
320319 Str::setEol ($ string ),
321320 $ matches ,
322321 \PREG_SET_ORDER | \PREG_UNMATCHED_AS_NULL
@@ -352,7 +351,7 @@ public function formatTags(
352351
353352 $ adjust = 0 ;
354353 $ text = Pcre::replaceCallback (
355- Regex:: delimit ( self ::TAG_REGEX ) . ' u ' ,
354+ self ::TAG ,
356355 function (array $ match ) use (
357356 &$ replace ,
358357 $ textFormats ,
@@ -462,7 +461,7 @@ function (array $match) use (
462461 $ adjust = 0 ;
463462 $ placeholders = 0 ;
464463 $ string = Pcre::replaceCallback (
465- Regex:: delimit ( self ::UNESCAPE_REGEX ) . ' u ' ,
464+ self ::ESCAPE ,
466465 function (array $ match ) use (
467466 $ unescape ,
468467 $ wrapAfterApply ,
@@ -566,10 +565,6 @@ function (array $match) use (
566565 $ string = substr_replace ($ string , $ replacement , $ offset , $ length );
567566 }
568567
569- if (\PHP_EOL !== "\n" ) {
570- $ string = str_replace ("\n" , \PHP_EOL , $ string );
571- }
572-
573568 return $ string . $ append ;
574569 }
575570
@@ -639,7 +634,7 @@ public static function escapeTags(string $string, bool $newlines = false): strin
639634 public static function unescapeTags (string $ string ): string
640635 {
641636 return Pcre::replace (
642- Regex:: delimit ( self ::UNESCAPE_REGEX ) . ' u ' ,
637+ self ::ESCAPE ,
643638 '$1 ' ,
644639 $ string ,
645640 );
@@ -688,7 +683,7 @@ private function applyTags(
688683 $ tag = $ matchHasOffset ? $ match ['tag ' ][0 ] : $ match ['tag ' ];
689684
690685 $ text = Pcre::replaceCallback (
691- Regex:: delimit ( self ::TAG_REGEX ) . ' u ' ,
686+ self ::TAG ,
692687 fn (array $ match ): string =>
693688 $ this ->applyTags ($ match , false , $ unescape , $ formats , $ depth + 1 ),
694689 $ text ,
@@ -698,7 +693,7 @@ private function applyTags(
698693
699694 if ($ unescape ) {
700695 $ text = Pcre::replace (
701- Regex:: delimit ( self ::UNESCAPE_REGEX ) . ' u ' ,
696+ self ::ESCAPE ,
702697 '$1 ' ,
703698 $ text ,
704699 );
0 commit comments