• 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

96.91
/src/Texy/Modules/ListModule.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\BlockParser;
12
use Texy\HtmlElement;
13
use Texy\Modifier;
14
use Texy\Patterns;
15
use function implode, ord, preg_match, strlen;
16

17

18
/**
19
 * Processes ordered, unordered, and definition lists with nesting.
20
 */
21
final class ListModule extends Texy\Module
22
{
23
        /** @var array<string, array{string, int, string, 3?: string}> [regex, ordered?, list-style-type, next-regex?] */
24
        public array $bullets = [
25
                // first-rexexp ordered? list-style-type next-regexp
26
                '*' => ['\*[\ \t]', 0, ''],
27
                '-' => ['[\x{2013}-](?![>-])', 0, ''],
28
                '+' => ['\+[\ \t]', 0, ''],
29
                '1.' => ['1\.[\ \t]', /* not \d !*/ 1, '', '\d{1,3}\.[\ \t]'],
30
                '1)' => ['\d{1,3}\)[\ \t]', 1, ''],
31
                'I.' => ['I\.[\ \t]', 1, 'upper-roman', '[IVX]{1,4}\.[\ \t]'],
32
                'I)' => ['[IVX]+\)[\ \t]', 1, 'upper-roman'], // before A) !
33
                'a)' => ['[a-z]\)[\ \t]', 1, 'lower-alpha'],
34
                'A)' => ['[A-Z]\)[\ \t]', 1, 'upper-alpha'],
35
        ];
36

37

38
        public function __construct(Texy\Texy $texy)
1✔
39
        {
40
                $this->texy = $texy;
1✔
41

42
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
43
                $texy->allowed['list'] = true;
1✔
44
                $texy->allowed['list/definition'] = true;
1✔
45
        }
1✔
46

47

48
        private function beforeParse(): void
49
        {
50
                $RE = $REul = [];
1✔
51
                foreach ($this->bullets as $desc) {
1✔
52
                        $RE[] = $desc[0];
1✔
53
                        if (!$desc[1]) {
1✔
54
                                $REul[] = $desc[0];
1✔
55
                        }
56
                }
57

58
                $this->texy->registerBlockPattern(
1✔
59
                        $this->patternList(...),
1✔
60
                        '#^(?:' . Patterns::MODIFIER_H . '\n)?' // .{color: red}
61
                        . '(' . implode('|', $RE) . ')[\ \t]*+\S.*$#mUu', // item (unmatched)
1✔
62
                        'list',
1✔
63
                );
64

65
                $this->texy->registerBlockPattern(
1✔
66
                        $this->patternDefList(...),
1✔
67
                        '#^(?:' . Patterns::MODIFIER_H . '\n)?' // .{color:red}
68
                        . '(\S.{0,2000})\:[\ \t]*' . Patterns::MODIFIER_H . '?\n' // Term:
69
                        . '([\ \t]++)(' . implode('|', $REul) . ')[\ \t]*+\S.*$#mUu', // - description
1✔
70
                        'list/definition',
1✔
71
                );
72
        }
1✔
73

74

75
        /**
76
         * Callback for:.
77
         *
78
         * 1) .... .(title)[class]{style}>
79
         * 2) ....
80
         *   + ...
81
         *   + ...
82
         * 3) ....
83
         * @param  string[]  $matches
84
         */
85
        public function patternList(BlockParser $parser, array $matches): ?HtmlElement
1✔
86
        {
87
                [, $mMod, $mBullet] = $matches;
1✔
88
                // [1] => .(title)[class]{style}<>
89
                // [2] => bullet * + - 1) a) A) IV)
90

91
                $el = new HtmlElement;
1✔
92

93
                $bullet = $min = null;
1✔
94
                foreach ($this->bullets as $type => $desc) {
1✔
95
                        if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
1✔
96
                                $bullet = $desc[3] ?? $desc[0];
1✔
97
                                $min = isset($desc[3]) ? 2 : 1;
1✔
98
                                $el->setName($desc[1] ? 'ol' : 'ul');
1✔
99
                                $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
1✔
100
                                $el->attrs['style']['list-style-type'] = $desc[2];
1✔
101
                                if ($desc[1]) { // ol
1✔
102
                                        if ($type[0] === '1' && (int) $mBullet > 1) {
1✔
103
                                                $el->attrs['start'] = (int) $mBullet;
×
104
                                        } elseif ($type[0] === 'a' && $mBullet[0] > 'a') {
1✔
105
                                                $el->attrs['start'] = ord($mBullet[0]) - 96;
×
106
                                        } elseif ($type[0] === 'A' && $mBullet[0] > 'A') {
1✔
107
                                                $el->attrs['start'] = ord($mBullet[0]) - 64;
×
108
                                        }
109
                                }
110

111
                                break;
1✔
112
                        }
113
                }
114
                assert($bullet !== null);
115

116
                $mod = new Modifier($mMod);
1✔
117
                $mod->decorate($this->texy, $el);
1✔
118

119
                $parser->moveBackward(1);
1✔
120

121
                while ($elItem = $this->patternItem($parser, $bullet, false, 'li')) {
1✔
122
                        $el->add($elItem);
1✔
123
                }
124

125
                if ($el->count() < $min) {
1✔
126
                        return null;
1✔
127
                }
128

129
                // event listener
130
                $this->texy->invokeHandlers('afterList', [$parser, $el, $mod]);
1✔
131

132
                return $el;
1✔
133
        }
134

135

136
        /**
137
         * Callback for:.
138
         *
139
         * Term: .(title)[class]{style}>
140
         * - description 1
141
         * - description 2
142
         * - description 3
143
         * @param  string[]  $matches
144
         */
145
        public function patternDefList(BlockParser $parser, array $matches): HtmlElement
1✔
146
        {
147
                [, $mMod, , , , $mBullet] = $matches;
1✔
148
                // [1] => .(title)[class]{style}<>
149
                // [2] => ...
150
                // [3] => .(title)[class]{style}<>
151
                // [4] => space
152
                // [5] => - * +
153

154
                $texy = $this->texy;
1✔
155

156
                $bullet = null;
1✔
157
                foreach ($this->bullets as $desc) {
1✔
158
                        if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
1✔
159
                                $bullet = $desc[3] ?? $desc[0];
1✔
160
                                break;
1✔
161
                        }
162
                }
163
                assert($bullet !== null);
164

165
                $el = new HtmlElement('dl');
1✔
166
                $mod = new Modifier($mMod);
1✔
167
                $mod->decorate($texy, $el);
1✔
168
                $parser->moveBackward(2);
1✔
169

170
                $patternTerm = '#^\n?(\S.*)\:[\ \t]*' . Patterns::MODIFIER_H . '?()$#mUA';
1✔
171

172
                while (true) {
1✔
173
                        if ($elItem = $this->patternItem($parser, $bullet, true, 'dd')) {
1✔
174
                                $el->add($elItem);
1✔
175
                                continue;
1✔
176
                        }
177

178
                        if ($parser->next($patternTerm, $matches)) {
1✔
179
                                [, $mContent, $mMod] = $matches;
1✔
180
                                // [1] => ...
181
                                // [2] => .(title)[class]{style}<>
182

183
                                $elItem = new HtmlElement('dt');
1✔
184
                                $modItem = new Modifier($mMod);
1✔
185
                                $modItem->decorate($texy, $elItem);
1✔
186

187
                                $elItem->parseLine($texy, $mContent);
1✔
188
                                $el->add($elItem);
1✔
189
                                continue;
1✔
190
                        }
191

192
                        break;
1✔
193
                }
194

195
                // event listener
196
                $texy->invokeHandlers('afterDefinitionList', [$parser, $el, $mod]);
1✔
197

198
                return $el;
1✔
199
        }
200

201

202
        /**
203
         * Callback for single list item.
204
         */
205
        private function patternItem(BlockParser $parser, string $bullet, bool $indented, string $tag): ?HtmlElement
1✔
206
        {
207
                $spacesBase = $indented ? ('[\ \t]{1,}') : '';
1✔
208
                $patternItem = "#^\n?($spacesBase){$bullet}[ \\t]*(\\S.*)?" . Patterns::MODIFIER_H . '?()$#mAUu';
1✔
209

210
                // first line with bullet
211
                $matches = null;
1✔
212
                if (!$parser->next($patternItem, $matches)) {
1✔
213
                        return null;
1✔
214
                }
215

216
                [, $mIndent, $mContent, $mMod] = $matches;
1✔
217
                // [1] => indent
218
                // [2] => ...
219
                // [3] => .(title)[class]{style}<>
220

221
                $elItem = new HtmlElement($tag);
1✔
222
                $mod = new Modifier($mMod);
1✔
223
                $mod->decorate($this->texy, $elItem);
1✔
224

225
                // next lines
226
                $spaces = '';
1✔
227
                $content = ' ' . $mContent; // trick
1✔
228
                while ($parser->next('#^(\n*)' . $mIndent . '([\ \t]{1,' . $spaces . '})(.*)()$#Am', $matches)) {
1✔
229
                        [, $mBlank, $mSpaces, $mContent] = $matches;
1✔
230
                        // [1] => blank line?
231
                        // [2] => spaces
232
                        // [3] => ...
233

234
                        if ($spaces === '') {
1✔
235
                                $spaces = strlen($mSpaces);
1✔
236
                        }
237

238
                        $content .= "\n" . $mBlank . $mContent;
1✔
239
                }
240

241
                // parse content
242
                $elItem->parseBlock($this->texy, $content, true);
1✔
243

244
                if (isset($elItem[0]) && $elItem[0] instanceof HtmlElement) {
1✔
245
                        $elItem[0]->setName(null);
1✔
246
                }
247

248
                return $elItem;
1✔
249
        }
250
}
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