99
1010use Texy ;
1111use Texy \HandlerInvocation ;
12- use Texy \InlineParser ;
1312use Texy \Link ;
1413use Texy \Patterns ;
1514use Texy \Regexp ;
@@ -30,34 +29,16 @@ final class LinkModule extends Texy\Module
3029 /** @var array<string, Link> link references */
3130 private array $ references = [];
3231
33- /** @var array<string, true> */
34- private static array $ livelock ;
35-
3632
3733 public function __construct (
3834 private Texy \Texy $ texy ,
3935 ) {
4036 $ texy ->allowed ['link/definition ' ] = true ;
41- $ texy ->addHandler ('newReference ' , $ this ->solveNewReference (...));
42- $ texy ->addHandler ('linkReference ' , $ this ->solve (...));
4337 }
4438
4539
4640 public function beforeParse (string &$ text ): void
4741 {
48- // [reference]
49- $ this ->texy ->registerLinePattern (
50- $ this ->parseReference (...),
51- '~(
52- \[
53- [^\[\]*\n ' . Patterns::MARK . ']++ # reference
54- ]
55- )~U ' ,
56- 'link/reference ' ,
57- );
58-
59- self ::$ livelock = [];
60-
6142 // [la trine]: http://www.latrine.cz/ text odkazu .(title)[class]{style}
6243 if (!empty ($ this ->texy ->allowed ['link/definition ' ])) {
6344 $ text = Texy \Regexp::replace (
@@ -100,47 +81,6 @@ private function parseDefinition(array $matches): string
10081 }
10182
10283
103- /**
104- * Parses [ref]
105- * @param array<?string> $matches
106- */
107- public function parseReference (InlineParser $ parser , array $ matches ): Texy \HtmlElement |string |null
108- {
109- /** @var array{string, string} $matches */
110- [, $ mRef ] = $ matches ;
111- // [1] => [ref]
112-
113- $ texy = $ this ->texy ;
114- $ name = substr ($ mRef , 1 , -1 );
115- $ link = $ this ->getReference ($ name );
116-
117- if (!$ link ) {
118- return $ texy ->invokeAroundHandlers ('newReference ' , $ parser , [$ name ]);
119- }
120-
121- $ link ->type = $ link ::BRACKET ;
122-
123- if ($ link ->label != '' ) { // null or ''
124- // prevent circular references
125- assert ($ link ->name !== null );
126- if (isset (self ::$ livelock [$ link ->name ])) {
127- $ content = $ link ->label ;
128- } else {
129- self ::$ livelock [$ link ->name ] = true ;
130- $ lineParser = $ texy ->createInlineParser ();
131- $ el = new Texy \HtmlElement (null , $ lineParser ->parse ($ link ->label ));
132- $ content = $ el ->toString ($ texy );
133- unset(self ::$ livelock [$ link ->name ]);
134- }
135- } else {
136- $ content = $ texy ->autolinkModule ->textualUrl ($ link );
137- $ content = $ texy ->protect ($ content , $ texy ::CONTENT_TEXTUAL );
138- }
139-
140- return $ texy ->invokeAroundHandlers ('linkReference ' , $ parser , [$ link , $ content ]);
141- }
142-
143-
14484 /**
14585 * Adds a user-defined link definition (persists across process() calls).
14686 */
@@ -223,7 +163,7 @@ public function factoryLink(string $dest, ?string $mMod, ?string $label): Link
223163
224164
225165 /**
226- * Finish invocation - generates <a> element.
166+ * Generates <a> element from Link .
227167 */
228168 public function solve (
229169 ?HandlerInvocation $ invocation ,
@@ -267,15 +207,6 @@ public function solve(
267207 }
268208
269209
270- /**
271- * Handler for undefined references.
272- */
273- private function solveNewReference (HandlerInvocation $ invocation , string $ name ): void
274- {
275- // no change
276- }
277-
278-
279210 /**
280211 * Checks and corrects URL in Link.
281212 */
0 commit comments