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

dg / texy / 22262381750

25 Jan 2026 11:44PM UTC coverage: 92.367% (-0.7%) from 93.057%
22262381750

push

github

dg
cs

9 of 11 new or added lines in 8 files covered. (81.82%)

161 existing lines in 23 files now uncovered.

2384 of 2581 relevant lines covered (92.37%)

0.92 hits per line

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

98.58
/src/Texy/Modules/PhraseModule.php
1
<?php
2

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

8
declare(strict_types=1);
9

10
namespace Texy\Modules;
11

12
use Texy;
13
use Texy\LineParser;
14
use Texy\Modifier;
15
use Texy\Patterns;
16
use function htmlspecialchars, str_replace, trim;
17
use const ENT_NOQUOTES;
18

19

20
/**
21
 * Phrases module.
22
 */
23
final class PhraseModule extends Texy\Module
24
{
25
        public array $tags = [
26
                'phrase/strong' => 'strong', // or 'b'
27
                'phrase/em' => 'em', // or 'i'
28
                'phrase/em-alt' => 'em',
29
                'phrase/em-alt2' => 'em',
30
                'phrase/ins' => 'ins',
31
                'phrase/del' => 'del',
32
                'phrase/sup' => 'sup',
33
                'phrase/sup-alt' => 'sup',
34
                'phrase/sub' => 'sub',
35
                'phrase/sub-alt' => 'sub',
36
                'phrase/span' => 'a',
37
                'phrase/span-alt' => 'a',
38
                'phrase/acronym' => 'abbr',
39
                'phrase/acronym-alt' => 'abbr',
40
                'phrase/code' => 'code',
41
                'phrase/quote' => 'q',
42
                'phrase/quicklink' => 'a',
43
        ];
44

45
        public bool $linksAllowed = true;
46

47

48
        public function __construct(Texy\Texy $texy)
1✔
49
        {
50
                $this->texy = $texy;
1✔
51

52
                $texy->addHandler('phrase', $this->solve(...));
1✔
53

54
                /*
55
                // UNIVERSAL
56
                $texy->registerLinePattern(
57
                        array($this, 'patternPhrase'),
58
                        '#((?>([*+/^_"~`-])+?))(?!\s)(.*(?!\2).)'.Texy\Patterns::MODIFIER.'?(?<!\s)\1(?!\2)(?::('.Texy\Patterns::LINK_URL.'))??()#Uus',
59
                        'phrase/strong'
60
                );
61
                */
62

63
                // ***strong+emphasis***
64
                $texy->registerLinePattern(
1✔
65
                        $this->patternPhrase(...),
1✔
66
                        '#(?<![*\\\])\*\*\*(?![\s*])((?:[^ *]++|[ *])+)' . Patterns::MODIFIER . '?(?<![\s*\\\])\*\*\*(?!\*)(?::(' . Patterns::LINK_URL . '))??()#Uus',
1✔
67
                        'phrase/strong+em',
1✔
68
                );
69

70
                // **strong**
71
                $texy->registerLinePattern(
1✔
72
                        $this->patternPhrase(...),
1✔
73
                        '#(?<![*\\\])\*\*(?![\s*])((?:[^ *]++|[ *])+)' . Patterns::MODIFIER . '?(?<![\s*\\\])\*\*(?!\*)(?::(' . Patterns::LINK_URL . '))??()#Uus',
1✔
74
                        'phrase/strong',
1✔
75
                );
76

77
                // //emphasis//
78
                $texy->registerLinePattern(
1✔
79
                        $this->patternPhrase(...),
1✔
80
                        '#(?<![/:])\/\/(?![\s/])((?:[^ /]++|[ /])+)' . Patterns::MODIFIER . '?(?<![\s/:])\/\/(?!\/)(?::(' . Patterns::LINK_URL . '))??()#Uus',
1✔
81
                        'phrase/em',
1✔
82
                );
83

84
                // *emphasisAlt*
85
                $texy->registerLinePattern(
1✔
86
                        $this->patternPhrase(...),
1✔
87
                        '#(?<![*\\\])\*(?![\s*])((?:[^\s*]++|[*])+)' . Patterns::MODIFIER . '?(?<![\s*\\\])\*(?!\*)(?::(' . Patterns::LINK_URL . '))??()#Uus',
1✔
88
                        'phrase/em-alt',
1✔
89
                );
90

91
                // *emphasisAlt2*
92
                $texy->registerLinePattern(
1✔
93
                        $this->patternPhrase(...),
1✔
94
                        '#(?<![^\s.,;:<>()"\'' . Patterns::MARK . '-])\*(?![\s*])((?:[^ *]++|[ *])+)' . Patterns::MODIFIER . '?(?<![\s*\\\])\*(?![^\s.,;:<>()"?!\'-])(?::(' . Patterns::LINK_URL . '))??()#Uus',
1✔
95
                        'phrase/em-alt2',
1✔
96
                );
97

98
                // ++inserted++
99
                $texy->registerLinePattern(
1✔
100
                        $this->patternPhrase(...),
1✔
101
                        '#(?<!\+)\+\+(?![\s+])((?:[^\r\n +]++|[ +])+)' . Patterns::MODIFIER . '?(?<![\s+])\+\+(?!\+)()#Uu',
1✔
102
                        'phrase/ins',
1✔
103
                );
104

105
                // --deleted--
106
                $texy->registerLinePattern(
1✔
107
                        $this->patternPhrase(...),
1✔
108
                        '#(?<![<-])\-\-(?![\s>-])((?:[^\r\n -]++|[ -])+)' . Patterns::MODIFIER . '?(?<![\s<-])\-\-(?![>-])()#Uu',
1✔
109
                        'phrase/del',
1✔
110
                );
111

112
                // ^^superscript^^
113
                $texy->registerLinePattern(
1✔
114
                        $this->patternPhrase(...),
1✔
115
                        '#(?<!\^)\^\^(?![\s^])((?:[^\r\n ^]++|[ ^])+)' . Patterns::MODIFIER . '?(?<![\s^])\^\^(?!\^)()#Uu',
1✔
116
                        'phrase/sup',
1✔
117
                );
118

119
                // m^2 alternative superscript
120
                $texy->registerLinePattern(
1✔
121
                        $this->patternSupSub(...),
1✔
122
                        '#(?<=[a-z0-9])\^([n0-9+-]{1,4}?)(?![a-z0-9])#Uui',
1✔
123
                        'phrase/sup-alt',
1✔
124
                );
125

126
                // __subscript__
127
                $texy->registerLinePattern(
1✔
128
                        $this->patternPhrase(...),
1✔
129
                        '#(?<!\_)\_\_(?![\s_])((?:[^\r\n _]++|[ _])+)' . Patterns::MODIFIER . '?(?<![\s_])\_\_(?!\_)()#Uu',
1✔
130
                        'phrase/sub',
1✔
131
                );
132

133
                // m_2 alternative subscript
134
                $texy->registerLinePattern(
1✔
135
                        $this->patternSupSub(...),
1✔
136
                        '#(?<=[a-z])\_([n0-9]{1,3})(?![a-z0-9])#Uui',
1✔
137
                        'phrase/sub-alt',
1✔
138
                );
139

140
                // "span"
141
                $texy->registerLinePattern(
1✔
142
                        $this->patternPhrase(...),
1✔
143
                        '#(?<!\")\"(?!\s)((?:[^\r "]++|[ ])+)' . Patterns::MODIFIER . '?(?<!\s)\"(?!\")(?::(' . Patterns::LINK_URL . '))??()#Uu',
1✔
144
                        'phrase/span',
1✔
145
                );
146

147
                // ~alternative span~
148
                $texy->registerLinePattern(
1✔
149
                        $this->patternPhrase(...),
1✔
150
                        '#(?<!\~)\~(?!\s)((?:[^\r ~]++|[ ])+)' . Patterns::MODIFIER . '?(?<!\s)\~(?!\~)(?::(' . Patterns::LINK_URL . '))??()#Uu',
1✔
151
                        'phrase/span-alt',
1✔
152
                );
153

154
                // >>quote<<
155
                $texy->registerLinePattern(
1✔
156
                        $this->patternPhrase(...),
1✔
157
                        '#(?<!\>)\>\>(?![\s>])((?:[^\r\n <]++|[ <])+)' . Patterns::MODIFIER . '?(?<![\s<])\<\<(?!\<)(?::(' . Patterns::LINK_URL . '))??()#Uu',
1✔
158
                        'phrase/quote',
1✔
159
                );
160

161
                // acronym/abbr "et al."((and others))
162
                $texy->registerLinePattern(
1✔
163
                        $this->patternPhrase(...),
1✔
164
                        '#(?<!\")\"(?!\s)((?:[^\r\n "]++|[ ])+)' . Patterns::MODIFIER . '?(?<!\s)\"(?!\")\(\((.+)\)\)()#Uu',
1✔
165
                        'phrase/acronym',
1✔
166
                );
167

168
                // acronym/abbr NATO((North Atlantic Treaty Organisation))
169
                $texy->registerLinePattern(
1✔
170
                        $this->patternPhrase(...),
1✔
171
                        '#(?<![' . Patterns::CHAR . '])([' . Patterns::CHAR . ']{2,})()\(\(((?:[^\n )]++|[ )])+)\)\)#Uu',
1✔
172
                        'phrase/acronym-alt',
1✔
173
                );
174

175
                // ''notexy''
176
                $texy->registerLinePattern(
1✔
177
                        $this->patternNoTexy(...),
1✔
178
                        '#(?<!\')\'\'(?![\s\'])((?:[^' . Patterns::MARK . '\r\n\']++|[\'])+)(?<![\s\'])\'\'(?!\')()#Uu',
1✔
179
                        'phrase/notexy',
1✔
180
                );
181

182
                // `code`
183
                $texy->registerLinePattern(
1✔
184
                        $this->patternPhrase(...),
1✔
185
                        '#\`(\S(?:[^' . Patterns::MARK . '\r\n `]++|[ `])*)' . Patterns::MODIFIER . '?(?<!\s)\`(?::(' . Patterns::LINK_URL . '))??()#Uu',
1✔
186
                        'phrase/code',
1✔
187
                );
188

189
                // ....:LINK
190
                $texy->registerLinePattern(
1✔
191
                        $this->patternPhrase(...),
1✔
192
                        '#([' . Patterns::CHAR . '0-9@\#$%&.,_-]++)()(?=:\[)(?::(' . Patterns::LINK_URL . '))()#Uu',
1✔
193
                        'phrase/quicklink',
1✔
194
                );
195

196
                // [text |link]
197
                $texy->registerLinePattern(
1✔
198
                        $this->patternPhrase(...),
1✔
199
                        '#(?<!\[)\[(?![\s*])([^|\r\n\]]++)\|((?:[^' . Patterns::MARK . '|\r\n \]]++|[ ])+)' . Patterns::MODIFIER . '?(?<!\s)\](?!\])()#Uu',
1✔
200
                        'phrase/wikilink',
1✔
201
                );
202

203
                // [text](link)
204
                $texy->registerLinePattern(
1✔
205
                        $this->patternPhrase(...),
1✔
206
                        '#(?<![[.])\[(?![\s*])((?:[^|\r\n \]]++|[ ])+)' . Patterns::MODIFIER . '?(?<!\s)\]\(((?:[^' . Patterns::MARK . '\r )]++|[ ])+)\)()#Uu',
1✔
207
                        'phrase/markdown',
1✔
208
                );
209

210
                // \* escaped asterix
211
                $texy->registerLinePattern(
1✔
212
                        fn() => '*',
1✔
213
                        '#\\\\\*#',
1✔
214
                        'phrase/escaped-asterix',
1✔
215
                );
216

217

218
                $texy->allowed['phrase/ins'] = false;
1✔
219
                $texy->allowed['phrase/del'] = false;
1✔
220
                $texy->allowed['phrase/sup'] = false;
1✔
221
                $texy->allowed['phrase/sub'] = false;
1✔
222
        }
1✔
223

224

225
        /**
226
         * Callback for: **.... .(title)[class]{style}**:LINK.
227
         */
228
        public function patternPhrase(LineParser $parser, array $matches, string $phrase): Texy\HtmlElement|string|null
1✔
229
        {
230
                [, $mContent, $mMod, $mLink] = $matches;
1✔
231
                // [1] => **
232
                // [2] => ...
233
                // [3] => .(title)[class]{style}
234
                // [4] => LINK
235

236
                if ($phrase === 'phrase/wikilink') {
1✔
237
                        [$mLink, $mMod] = [$mMod, $mLink];
1✔
238
                        $mContent = trim($mContent);
1✔
239
                }
240

241
                $texy = $this->texy;
1✔
242
                $mod = new Modifier($mMod);
1✔
243
                $link = null;
1✔
244

245
                $parser->again = $phrase !== 'phrase/code' && $phrase !== 'phrase/quicklink';
1✔
246

247
                if ($phrase === 'phrase/span' || $phrase === 'phrase/span-alt') {
1✔
248
                        if ($mLink == null) {
1✔
249
                                if (!$mMod) {
1✔
250
                                        return null; // means "..."
1✔
251
                                }
252
                        } else {
253
                                $link = $texy->linkModule->factoryLink($mLink, $mMod, $mContent);
1✔
254
                        }
255
                } elseif ($phrase === 'phrase/acronym' || $phrase === 'phrase/acronym-alt') {
1✔
256
                        $mod->title = trim(Texy\Helpers::unescapeHtml($mLink));
1✔
257

258
                } elseif ($mLink != null) {
1✔
259
                        $link = $texy->linkModule->factoryLink($mLink, null, $mContent);
1✔
260
                }
261

262
                return $texy->invokeAroundHandlers('phrase', $parser, [$phrase, $mContent, $mod, $link]);
1✔
263
        }
264

265

266
        /**
267
         * Callback for: any^2 any_2.
268
         */
269
        public function patternSupSub(LineParser $parser, array $matches, string $phrase): Texy\HtmlElement|string|null
1✔
270
        {
271
                [, $mContent] = $matches;
1✔
272
                $mod = new Modifier;
1✔
273
                $link = null;
1✔
274
                $mContent = str_replace('-', "\u{2212}", $mContent); // &minus;
1✔
275
                return $this->texy->invokeAroundHandlers('phrase', $parser, [$phrase, $mContent, $mod, $link]);
1✔
276
        }
277

278

279
        public function patternNoTexy(LineParser $parser, array $matches): string
280
        {
UNCOV
281
                [, $mContent] = $matches;
×
UNCOV
282
                return $this->texy->protect(htmlspecialchars($mContent, ENT_NOQUOTES, 'UTF-8'), Texy\Texy::CONTENT_TEXTUAL);
×
283
        }
284

285

286
        /**
287
         * Finish invocation.
288
         */
289
        private function solve(
1✔
290
                Texy\HandlerInvocation $invocation,
291
                string $phrase,
292
                string $content,
293
                Modifier $mod,
294
                ?Texy\Link $link = null,
295
        ): Texy\HtmlElement|string|null
296
        {
297
                $texy = $this->texy;
1✔
298
                $tag = $this->tags[$phrase] ?? null;
1✔
299

300
                if ($tag === 'a') {
1✔
301
                        $tag = $link && $this->linksAllowed ? null : 'span';
1✔
302
                }
303

304
                if ($phrase === 'phrase/code') {
1✔
305
                        $content = $texy->protect(htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8'), $texy::CONTENT_TEXTUAL);
1✔
306
                }
307

308
                if ($phrase === 'phrase/strong+em') {
1✔
309
                        $el = new Texy\HtmlElement($this->tags['phrase/strong']);
1✔
310
                        $el->create($this->tags['phrase/em'], $content);
1✔
311
                        $mod->decorate($texy, $el);
1✔
312

313
                } elseif ($tag) {
1✔
314
                        $el = new Texy\HtmlElement($tag, $content);
1✔
315
                        $mod->decorate($texy, $el);
1✔
316
                } else {
317
                        $el = $content; // trick
1✔
318
                }
319

320
                if ($link && $this->linksAllowed) {
1✔
321
                        return $texy->linkModule->solve(null, $link, $el);
1✔
322
                }
323

324
                return $el;
1✔
325
        }
326
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc