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

dg / texy / 20980727294

14 Jan 2026 03:00AM UTC coverage: 92.376%. Remained the same
20980727294

push

github

dg
added CLAUDE.md

2387 of 2584 relevant lines covered (92.38%)

0.92 hits per line

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

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

19

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

39

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

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

49

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

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

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

76

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

92
                $el = new HtmlElement;
1✔
93

94
                $bullet = $min = null;
1✔
95
                foreach ($this->bullets as $type => $desc) {
1✔
96
                        if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
1✔
97
                                $bullet = $desc[3] ?? $desc[0];
1✔
98
                                $min = isset($desc[3]) ? 2 : 1;
1✔
99
                                $el->setName($desc[1] ? 'ol' : 'ul');
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

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

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

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

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

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

131
                return $el;
1✔
132
        }
133

134

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

152
                $texy = $this->texy;
1✔
153

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

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

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

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

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

180
                                $elItem = new HtmlElement('dt');
1✔
181
                                $modItem = new Modifier($mMod);
1✔
182
                                $modItem->decorate($texy, $elItem);
1✔
183

184
                                $elItem->parseLine($texy, $mContent);
1✔
185
                                $el->add($elItem);
1✔
186
                                continue;
1✔
187
                        }
188

189
                        break;
1✔
190
                }
191

192
                // event listener
193
                $texy->invokeHandlers('afterDefinitionList', [$parser, $el, $mod]);
1✔
194

195
                return $el;
1✔
196
        }
197

198

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

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

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

218
                $elItem = new HtmlElement($tag);
1✔
219
                $mod = new Modifier($mMod);
1✔
220
                $mod->decorate($this->texy, $elItem);
1✔
221

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

231
                        if ($spaces === '') {
1✔
232
                                $spaces = strlen($mSpaces);
1✔
233
                        }
234

235
                        $content .= "\n" . $mBlank . $mContent;
1✔
236
                }
237

238
                // parse content
239
                $elItem->parseBlock($this->texy, $content, true);
1✔
240

241
                if (isset($elItem[0]) && $elItem[0] instanceof HtmlElement) {
1✔
242
                        $elItem[0]->setName(null);
1✔
243
                }
244

245
                return $elItem;
1✔
246
        }
247
}
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