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

dg / texy / 22262497275

21 Feb 2026 07:01PM UTC coverage: 93.057% (+0.7%) from 92.367%
22262497275

push

github

dg
added CLAUDE.md

2426 of 2607 relevant lines covered (93.06%)

0.93 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 declare(strict_types=1);
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
namespace Texy\Modules;
9

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

17

18
/**
19
 * Processes inline text formatting (bold, italic, code, etc.).
20
 */
21
final class PhraseModule extends Texy\Module
22
{
23
        /** @var array<string, string> */
24
        public array $tags = [
25
                'phrase/strong' => 'strong', // or 'b'
26
                'phrase/em' => 'em', // or 'i'
27
                'phrase/em-alt' => 'em',
28
                'phrase/em-alt2' => 'em',
29
                'phrase/ins' => 'ins',
30
                'phrase/del' => 'del',
31
                'phrase/sup' => 'sup',
32
                'phrase/sup-alt' => 'sup',
33
                'phrase/sub' => 'sub',
34
                'phrase/sub-alt' => 'sub',
35
                'phrase/span' => 'a',
36
                'phrase/span-alt' => 'a',
37
                'phrase/acronym' => 'abbr',
38
                'phrase/acronym-alt' => 'abbr',
39
                'phrase/code' => 'code',
40
                'phrase/quote' => 'q',
41
                'phrase/quicklink' => 'a',
42
        ];
43

44
        public bool $linksAllowed = true;
45

46

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

216

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

223

224
        /**
225
         * Callback for: **.... .(title)[class]{style}**:LINK.
226
         * @param  string[]  $matches
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
         * @param  string[]  $matches
269
         */
270
        public function patternSupSub(LineParser $parser, array $matches, string $phrase): Texy\HtmlElement|string|null
1✔
271
        {
272
                [, $mContent] = $matches;
1✔
273
                $mod = new Modifier;
1✔
274
                $link = null;
1✔
275
                $mContent = str_replace('-', "\u{2212}", $mContent); // &minus;
1✔
276
                return $this->texy->invokeAroundHandlers('phrase', $parser, [$phrase, $mContent, $mod, $link]);
1✔
277
        }
278

279

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

287

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

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

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

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

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

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

326
                return $el;
1✔
327
        }
328
}
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