• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / texy / 42887648368

10 Nov 2026 01:51PM UTC coverage: 96.622% (-0.1%) from 96.735%
42887648368

push

github

dg
Config::$locales: each locale is a list of four quotes (BC break)

Opening and closing double quotes, then the single ones, instead of the pairs
singleQuotes and doubleQuotes. The compatibility layer converts the old table.

cabae8f0-9dec-45ab-a919-45e6dadc3ad6

8 of 8 new or added lines in 3 files covered. (100.0%)

73 existing lines in 18 files now uncovered.

3719 of 3849 relevant lines covered (96.62%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.4
/src/Modules/LinkReferenceModule.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Texy! (https://texy.deegee.dev)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Texy\Modules;
9

10
use Texy;
11
use Texy\{Helpers, Nodes, Patterns, Range, Syntax};
12
use Texy\Nodes\{DocumentNode, LinkDefinitionNode};
13
use Texy\Parsing\ParseContext;
14
use function in_array, strlen;
15

16

17
/**
18
 * Processes link references and generates link elements.
19
 */
20
final class LinkReferenceModule extends Texy\Module
21
{
22
        /** @var array<string, LinkDefinitionNode> link definitions */
23
        private array $definitions = [];
24

25

26
        public function __construct(
1 ✔
27
                private Texy\TexyConfig $config,
28
                private ImageModule $imageModule,
29
        ) {
30
        }
1 ✔
31

32

33
        public function register(Texy\Parsing\Registry $registry): void
34
        {
35
                $registry->declare(Syntax::LinkReference, enabled: false);
1 ✔
36

37
                // [ref]: url label .(title)[class]{style}
38
                $registry->addBlock(
1 ✔
39
                        Syntax::LinkDefinition,
1 ✔
40
                        '~^
41
                                \[
42
                                ( [^\[\]#?*\n]{1,100} )           # reference (1)
43
                                ] : [ \t]++
44
                                ( \S{1,1000} )                    # URL (2)
45
                                ( [ \t] .{1,1000} )?              # optional label (3)
46
                                ' . Patterns::Modifier . '?       # modifier (4)
1 ✔
47
                                \s*
48
                        $~mUx',
49
                        $this->parseDefinition(...),
1 ✔
50
                );
51

52
                // [reference] - bare reference link (opt-in); the raw target is kept in
53
                // LinkNode::$ref for consumer resolution passes (wiki links etc.)
54
                $registry->addInline(
1 ✔
55
                        Syntax::LinkReference,
1 ✔
56
                        '~
1 ✔
57
                                (?<! [.\#\[] )        # exclude modifier .[class], PHP attribute \#[Attr] and nested [
58
                                \[
59
                                ( [^\[\]*|\n]++ )     # reference name (1); * would be an image, | a labeled form
60
                                ]
61
                        ~Ux',
62
                        $this->parseReference(...),
1 ✔
63
                );
64
        }
1 ✔
65

66

67
        /**
68
         * Parses bare [ref]
69
         * @param  array{string, string}  $matches
70
         * @param  array{int, int}  $offsets
71
         */
72
        public function parseReference(ParseContext $context, array $matches, array $offsets): Nodes\LinkNode
73
        {
74
                [, $mRef] = $matches;
1 ✔
75
                $refRange = new Range($offsets[1], strlen($mRef));
1 ✔
76
                return new Nodes\LinkNode(
1 ✔
77
                        content: new Nodes\ContentNode([new Nodes\TextNode(Helpers::decodeEntities($mRef), $refRange)]),
1 ✔
78
                        range: new Range($offsets[0], strlen($matches[0])),
1 ✔
79
                        ref: $mRef,
80
                        syntax: Syntax::LinkReference,
1 ✔
81
                        textRange: $refRange,
82
                        urlRange: $refRange,
83
                );
84
        }
85

86

87
        /**
88
         * Parses [ref]: url
89
         * @param  array{string, string, string, ?string, ?string}  $matches
90
         * @param  array{int, int, int, ?int, ?int}  $offsets
91
         */
92
        public function parseDefinition(ParseContext $context, array $matches, array $offsets): LinkDefinitionNode
93
        {
94
                [, $mRef, $mLink, $mLabel, $mMod] = $matches;
1 ✔
95
                if ($mMod || $mLabel) {
1 ✔
UNCOV
96
                        trigger_error('Modifiers and label in link definitions are deprecated.', E_USER_DEPRECATED);
×
97
                }
98
                return new LinkDefinitionNode(
1 ✔
99
                        $mRef,
100
                        Helpers::decodeEntities($mLink),
1 ✔
101
                        new Range($offsets[0], strlen($matches[0])),
1 ✔
102
                );
103
        }
104

105

106
        /**
107
         * Recognizes link targets written as a reference, [ref] or [*img*]: the name goes
108
         * to LinkNode::$ref and the URL is left to resolveReferences().
109
         */
110
        public function recognizeReferences(DocumentNode $doc): void
111
        {
112
                foreach ($doc->find(Nodes\LinkNode::class) as $node) {
1 ✔
113
                        $url = $node->url ?? '';
1 ✔
114
                        $len = strlen($url);
1 ✔
115
                        if ($len > 4 && $url[0] === '[' && $url[1] === '*' && $url[$len - 1] === ']'
1 ✔
116
                                && in_array($url[$len - 2], ['*', '<', '>'], true)
1 ✔
117
                        ) {
118
                                $node->ref = trim(substr($url, 2, -2));
1 ✔
119
                                $node->isImageLink = true;
1 ✔
120
                                $node->url = null;
1 ✔
121

122
                        } elseif ($len > 2 && $url[0] === '[' && $url[$len - 1] === ']') {
1 ✔
123
                                $node->ref = substr($url, 1, -1);
1 ✔
124
                                $node->url = null;
1 ✔
125
                        }
126
                }
127
        }
1 ✔
128

129

130
        /**
131
         * Resolve link references in the document. A Transform pass, runs after the one of ImageModule.
132
         */
133
        public function resolveReferences(DocumentNode $doc): void
134
        {
135
                // Start with the definitions of the application
136
                $this->definitions = $this->config->linkDefinitions;
1 ✔
137

138
                // Pass 1: Collect document definitions (overwrites user-defined)
139
                foreach ($doc->find(LinkDefinitionNode::class) as $node) {
1 ✔
140
                        $this->definitions[Helpers::toLower($node->reference)] = $node;
1 ✔
141
                }
142

143
                // Pass 2: Resolve references in LinkNode.url
144
                foreach ($doc->find(Nodes\LinkNode::class) as $node) {
1 ✔
145
                        $this->resolveLinkNodeUrl($node);
1 ✔
146
                }
147
        }
1 ✔
148

149

150
        /**
151
         * Resolves the URL of a link written as [ref] or [*img*]; a reference not found is used as the URL.
152
         * A %s in the URL stands for the link text, such as "strlen":[php] with php.net/%s.
153
         */
154
        private function resolveLinkNodeUrl(Nodes\LinkNode $node): void
155
        {
156
                if ($node->url === null && $node->ref !== null) {
1 ✔
157
                        if ($node->isImageLink) {
1 ✔
158
                                $node->url = $this->imageModule->getDefinition($node->ref)->url ?? $node->ref;
1 ✔
159
                                return;
1 ✔
160
                        }
161
                        $node->url = $this->resolveDefinition($node->ref)->url ?? $node->ref;
1 ✔
162
                }
163

164
                if ($node->url !== null && $node->syntax !== Syntax::LinkReference && str_contains($node->url, '%s')) {
1 ✔
165
                        $node->url = str_replace('%s', urlencode(trim($node->content->getPlainText())), $node->url);
1 ✔
166
                }
167

168
                $this->convertEmailUrl($node);
1 ✔
169
        }
1 ✔
170

171

172
        /**
173
         * Convert e-mail URLs to mailto: scheme. Requires a full e-mail address
174
         * (Patterns::Email) so that wiki-style targets containing @ are left alone.
175
         */
176
        private function convertEmailUrl(Nodes\LinkNode $node): void
177
        {
178
                if (
179
                        $node->url !== null
1 ✔
180
                        && !str_contains($node->url, '/')
1 ✔
181
                        && !preg_match('~^[a-z][a-z0-9+.-]*:~i', $node->url)
1 ✔
182
                        && Texy\Regexp::match($node->url, '~' . Patterns::Email . '(\?\S*)?$~A')
1 ✔
183
                ) { // optional ?subject=... query
184
                        $node->url = 'mailto:' . $node->url;
1 ✔
185
                }
186
        }
1 ✔
187

188

189
        /**
190
         * Find definition by identifier, supports #fragment and ?query.
191
         */
192
        private function resolveDefinition(string $identifier): ?LinkDefinitionNode
193
        {
194
                $key = Helpers::toLower($identifier);
1 ✔
195

196
                if (isset($this->definitions[$key])) {
1 ✔
197
                        return $this->definitions[$key];
1 ✔
198
                }
199

200
                // Support #fragment and ?query
201
                $hashPos = strpos($key, '#');
1 ✔
202
                $queryPos = strpos($key, '?');
1 ✔
203

204
                // Find the earliest delimiter
205
                $pos = null;
1 ✔
206
                if ($hashPos !== false && $queryPos !== false) {
1 ✔
UNCOV
207
                        $pos = min($hashPos, $queryPos);
×
208
                } elseif ($hashPos !== false) {
1 ✔
209
                        $pos = $hashPos;
1 ✔
210
                } elseif ($queryPos !== false) {
1 ✔
211
                        $pos = $queryPos;
1 ✔
212
                }
213

214
                if ($pos !== null) {
1 ✔
215
                        $baseKey = substr($key, 0, $pos);
1 ✔
216
                        if (isset($this->definitions[$baseKey])) {
1 ✔
217
                                $def = clone $this->definitions[$baseKey];
1 ✔
218
                                $def->url .= substr($identifier, $pos);
1 ✔
219
                                return $def;
1 ✔
220
                        }
221
                }
222

223
                return null;
1 ✔
224
        }
225
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc