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

dg / texy / 21344532034

26 Jan 2026 02:43AM UTC coverage: 91.98% (-0.4%) from 92.376%
21344532034

push

github

dg
added CLAUDE.md

2397 of 2606 relevant lines covered (91.98%)

0.92 hits per line

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

90.0
/src/Texy/Modules/BlockModule.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\Helpers;
14
use Texy\HtmlElement;
15
use function assert, htmlspecialchars, preg_split, str_replace, trim;
16
use const ENT_NOQUOTES;
17

18

19
/**
20
 * Processes special blocks (/-- code, html, text, div, etc.).
21
 */
22
final class BlockModule extends Texy\Module
23
{
24
        public function __construct(Texy\Texy $texy)
1✔
25
        {
26
                $this->texy = $texy;
1✔
27

28
                //$texy->allowed['blocks'] = true;
29
                $texy->allowed['block/default'] = true;
1✔
30
                $texy->allowed['block/pre'] = true;
1✔
31
                $texy->allowed['block/code'] = true;
1✔
32
                $texy->allowed['block/html'] = true;
1✔
33
                $texy->allowed['block/text'] = true;
1✔
34
                $texy->allowed['block/texysource'] = true;
1✔
35
                $texy->allowed['block/comment'] = true;
1✔
36
                $texy->allowed['block/div'] = true;
1✔
37

38
                $texy->addHandler('block', $this->solve(...));
1✔
39
                $texy->addHandler('beforeBlockParse', $this->beforeBlockParse(...));
1✔
40

41
                $texy->registerBlockPattern(
1✔
42
                        $this->pattern(...),
1✔
43
                        '#^/--++\ *+(.*)' . Texy\Patterns::MODIFIER_H . '?$((?:\n(?0)|\n.*+)*)(?:\n\\\--.*$|\z)#mUi',
1✔
44
                        'blocks',
1✔
45
                );
46
        }
1✔
47

48

49
        /**
50
         * Single block pre-processing.
51
         */
52
        private function beforeBlockParse(Texy\BlockParser $parser, string &$text): void
1✔
53
        {
54
                // autoclose exclusive blocks
55
                $text = Texy\Regexp::replace(
1✔
56
                        $text,
1✔
57
                        '#^(/--++\ *+(?!div|texysource).*)$((?:\n.*+)*?)(?:\n\\\--.*$|(?=(\n/--.*$)))#mi',
1✔
58
                        "\$1\$2\n\\--",
1✔
59
                );
60
        }
1✔
61

62

63
        /**
64
         * Callback for:.
65
         * /-----code html .(title)[class]{style}
66
         * ....
67
         * ....
68
         * \----
69
         * @param  string[]  $matches
70
         */
71
        public function pattern(Texy\BlockParser $parser, array $matches): HtmlElement|string|null
1✔
72
        {
73
                [, $mParam, $mMod, $mContent] = $matches;
1✔
74
                // [1] => code | text | ...
75
                // [2] => ... additional parameters
76
                // [3] => .(title)[class]{style}<>
77
                // [4] => ... content
78

79
                $mod = new Texy\Modifier($mMod);
1✔
80
                $parts = preg_split('#\s+#u', $mParam, 2);
1✔
81
                $blocktype = empty($parts[0]) ? 'block/default' : 'block/' . $parts[0];
1✔
82
                $param = empty($parts[1]) ? null : $parts[1];
1✔
83

84
                return $this->texy->invokeAroundHandlers('block', $parser, [$blocktype, $mContent, $param, $mod]);
1✔
85
        }
86

87

88
        /**
89
         * Finish invocation.
90
         */
91
        private function solve(
1✔
92
                Texy\HandlerInvocation $invocation,
93
                string $blocktype,
94
                string $s,
95
                ?string $param,
96
                Texy\Modifier $mod,
97
        ): HtmlElement|string|null
98
        {
99
                $texy = $this->texy;
1✔
100
                $parser = $invocation->getParser();
1✔
101
                assert($parser instanceof Texy\BlockParser);
102

103
                if ($blocktype === 'block/texy') {
1✔
104
                        return $this->blockTexy($s, $texy, $parser);
×
105
                } elseif (empty($texy->allowed[$blocktype])) {
1✔
106
                        return null;
1✔
107
                } elseif ($blocktype === 'block/texysource') {
1✔
108
                        return $this->blockTexySource($s, $texy, $mod, $param);
1✔
109
                } elseif ($blocktype === 'block/code') {
1✔
110
                        return $this->blockCode($s, $texy, $mod, $param);
1✔
111
                } elseif ($blocktype === 'block/default') {
1✔
112
                        return $this->blockDefault($s, $texy, $mod, $param);
1✔
113
                } elseif ($blocktype === 'block/pre') {
1✔
114
                        return $this->blockPre($s, $texy, $mod);
1✔
115
                } elseif ($blocktype === 'block/html') {
1✔
116
                        return $this->blockHtml($s, $texy);
1✔
117
                } elseif ($blocktype === 'block/text') {
1✔
118
                        return $this->blockText($s, $texy);
1✔
119
                } elseif ($blocktype === 'block/comment') {
1✔
120
                        return $this->blockComment();
1✔
121
                } elseif ($blocktype === 'block/div') {
1✔
122
                        return $this->blockDiv($s, $texy, $mod, $parser);
1✔
123
                }
124

125
                return null;
×
126
        }
127

128

129
        private function blockTexy(string $s, Texy\Texy $texy, Texy\BlockParser $parser): HtmlElement
130
        {
131
                $el = new HtmlElement;
×
132
                $el->parseBlock($texy, $s, $parser->isIndented());
×
133
                return $el;
×
134
        }
135

136

137
        private function blockTexySource(string $s, Texy\Texy $texy, Texy\Modifier $mod, ?string $param): string|HtmlElement
1✔
138
        {
139
                $s = Helpers::outdent($s);
1✔
140
                if ($s === '') {
1✔
141
                        return "\n";
×
142
                }
143

144
                $el = new HtmlElement;
1✔
145
                if ($param === 'line') {
1✔
146
                        $el->parseLine($texy, $s);
×
147
                } else {
148
                        $el->parseBlock($texy, $s);
1✔
149
                }
150

151
                $s = $el->toHtml($texy);
1✔
152
                return $this->blockCode($s, $texy, $mod, 'html');
1✔
153
        }
154

155

156
        private function blockCode(string $s, Texy\Texy $texy, Texy\Modifier $mod, ?string $param): string|HtmlElement
1✔
157
        {
158
                $s = Helpers::outdent($s);
1✔
159
                if ($s === '') {
1✔
160
                        return "\n";
×
161
                }
162

163
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
164
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
165
                $el = new HtmlElement('pre');
1✔
166
                $mod->decorate($texy, $el);
1✔
167
                if ($param !== null) {
1✔
168
                        $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
169
                        $el->attrs['class'][] = $param;
1✔
170
                }
171
                $el->create('code', $s);
1✔
172
                return $el;
1✔
173
        }
174

175

176
        private function blockDefault(string $s, Texy\Texy $texy, Texy\Modifier $mod, ?string $param): string|HtmlElement
1✔
177
        {
178
                $s = Helpers::outdent($s);
1✔
179
                if ($s === '') {
1✔
180
                        return "\n";
×
181
                }
182

183
                $el = new HtmlElement('pre');
1✔
184
                $mod->decorate($texy, $el);
1✔
185
                if ($param !== null) {
1✔
186
                        $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
×
187
                        $el->attrs['class'][] = $param;
×
188
                }
189
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
190
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
191
                $el->setText($s);
1✔
192
                return $el;
1✔
193
        }
194

195

196
        private function blockPre(string $s, Texy\Texy $texy, Texy\Modifier $mod): string|HtmlElement
1✔
197
        {
198
                $s = Helpers::outdent($s);
1✔
199
                if ($s === '') {
1✔
200
                        return "\n";
×
201
                }
202

203
                $el = new HtmlElement('pre');
1✔
204
                $mod->decorate($texy, $el);
1✔
205
                $lineParser = new Texy\LineParser($texy, $el);
1✔
206
                // special mode - parse only html tags
207
                $tmp = $lineParser->patterns;
1✔
208
                $lineParser->patterns = [];
1✔
209
                if (isset($tmp['html/tag'])) {
1✔
210
                        $lineParser->patterns['html/tag'] = $tmp['html/tag'];
1✔
211
                }
212

213
                if (isset($tmp['html/comment'])) {
1✔
214
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
215
                }
216

217
                unset($tmp);
1✔
218

219
                $lineParser->parse($s);
1✔
220
                $s = $el->getText();
1✔
221
                assert($s !== null);
222
                $s = Helpers::unescapeHtml($s);
1✔
223
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
224
                $s = $texy->unprotect($s);
1✔
225
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
226
                $el->setText($s);
1✔
227
                return $el;
1✔
228
        }
229

230

231
        private function blockHtml(string $s, Texy\Texy $texy): string
1✔
232
        {
233
                $s = trim($s, "\n");
1✔
234
                if ($s === '') {
1✔
235
                        return "\n";
×
236
                }
237

238
                $el = new HtmlElement;
1✔
239
                $lineParser = new Texy\LineParser($texy, $el);
1✔
240
                // special mode - parse only html tags
241
                $tmp = $lineParser->patterns;
1✔
242
                $lineParser->patterns = [];
1✔
243
                if (isset($tmp['html/tag'])) {
1✔
244
                        $lineParser->patterns['html/tag'] = $tmp['html/tag'];
1✔
245
                }
246

247
                if (isset($tmp['html/comment'])) {
1✔
248
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
249
                }
250

251
                unset($tmp);
1✔
252

253
                $lineParser->parse($s);
1✔
254
                $s = $el->getText();
1✔
255
                assert($s !== null);
256
                $s = Helpers::unescapeHtml($s);
1✔
257
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
258
                $s = $texy->unprotect($s);
1✔
259
                return $texy->protect($s, $texy::CONTENT_BLOCK) . "\n";
1✔
260
        }
261

262

263
        private function blockText(string $s, Texy\Texy $texy): string
1✔
264
        {
265
                $s = trim($s, "\n");
1✔
266
                if ($s === '') {
1✔
267
                        return "\n";
×
268
                }
269

270
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
271
                $s = str_replace("\n", (new HtmlElement('br'))->startTag(), $s); // nl2br
1✔
272
                return $texy->protect($s, $texy::CONTENT_BLOCK) . "\n";
1✔
273
        }
274

275

276
        private function blockComment(): string
277
        {
278
                return "\n";
1✔
279
        }
280

281

282
        private function blockDiv(
1✔
283
                string $s,
284
                Texy\Texy $texy,
285
                Texy\Modifier $mod,
286
                Texy\BlockParser $parser,
287
        ): string|HtmlElement
288
        {
289
                $s = Helpers::outdent($s, firstLine: true);
1✔
290
                if ($s === '') {
1✔
291
                        return "\n";
×
292
                }
293

294
                $el = new HtmlElement('div');
1✔
295
                $mod->decorate($texy, $el);
1✔
296
                $el->parseBlock($texy, $s, $parser->isIndented()); // TODO: INDENT or NORMAL ?
1✔
297
                return $el;
1✔
298
        }
299
}
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