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

dg / texy / 42887648368

10 Nov 2026 01:51PM UTC coverage: 96.622% (-0.1%) from 96.735%
42887648368

push

github

dg
Config::$locales: each locale is a list of four quotes (BC break)

Opening and closing double quotes, then the single ones, instead of the pairs
singleQuotes and doubleQuotes. The compatibility layer converts the old table.

cabae8f0-9dec-45ab-a919-45e6dadc3ad6

8 of 8 new or added lines in 3 files covered. (100.0%)

73 existing lines in 18 files now uncovered.

3719 of 3849 relevant lines covered (96.62%)

0.97 hits per line

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

95.9
/src/Modules/ListModule.php
1
<?php declare(strict_types=1);
2

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

8
namespace Texy\Modules;
9

10
use Texy;
11
use Texy\{Modifier, Patterns, Range, Regexp, Syntax};
12
use Texy\Nodes\{DefinitionListNode, ListItemNode, ListNode, ListType};
13
use Texy\Parsing\ParseContext;
14
use function count, strlen;
15

16

17
/**
18
 * Processes ordered, unordered, and definition lists with nesting.
19
 */
20
final class ListModule extends Texy\Module
21
{
22
        public function __construct(
1 ✔
23
                private Texy\TexyConfig $config,
24
        ) {
25
        }
1 ✔
26

27

28
        public function register(Texy\Parsing\Registry $registry): void
29
        {
30
                $re = $reUl = [];
1 ✔
31
                foreach ($this->config->listBullets as $desc) {
1 ✔
32
                        $re[] = $desc[0];
1 ✔
33
                        if (!$desc[1]->isOrdered()) {
1 ✔
34
                                $reUl[] = $desc[0];
1 ✔
35
                        }
36
                }
37

38
                $registry->addBlock(
1 ✔
39
                        Syntax::List,
1 ✔
40
                        '~^
41
                                (?:' . Patterns::ModifierHAlign . '\n)? # modifier (1)
42
                                (' . implode('|', $re) . ')         # list marker (2)
1 ✔
43
                                [ \t]*+
44
                                \S .*                               # content
45
                        $~mUx',
46
                        $this->parseList(...),
1 ✔
47
                );
48

49
                $registry->addBlock(
1 ✔
50
                        Syntax::DefinitionList,
1 ✔
51
                        '~^
52
                                (?:' . Patterns::ModifierHAlign . '\n)?   # modifier (1)
53
                                ( \S .{0,2000} )                      # definition term (2)
54
                                : [ \t]*                              # colon separator
55
                                ' . Patterns::ModifierHAlign . '?         # modifier (3)
56
                                \n
57
                                ([ \t]++)                             # indentation (4)
58
                                (' . implode('|', $reUl) . ')         # description marker (5)
1 ✔
59
                                [ \t]*+
60
                                \S .*                                 # content
61
                        $~mUx',
62
                        $this->parseDefList(...),
1 ✔
63
                );
64
        }
1 ✔
65

66

67
        /**
68
         * Parses list.
69
         * @param  array{string, ?string, string}  $matches
70
         * @param  array{int, ?int, int}  $offsets
71
         */
72
        public function parseList(ParseContext $context, array $matches, array $offsets): ?ListNode
73
        {
74
                [, $mMod, $mBullet] = $matches;
1 ✔
75

76
                $bullet = null;
1 ✔
77
                $min = 1;
1 ✔
78
                $start = null;
1 ✔
79
                $listType = ListType::Unordered;
1 ✔
80

81
                foreach ($this->config->listBullets as $key => $desc) {
1 ✔
82
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~Ax')) {
1 ✔
83
                                $bullet = $desc[2] ?? $desc[0];
1 ✔
84
                                $min = isset($desc[2]) ? 2 : 1;
1 ✔
85
                                $listType = $desc[1];
1 ✔
86
                                if ($listType->isOrdered()) {
1 ✔
87
                                        if ($key[0] === '1' && (int) $mBullet > 1) {
1 ✔
88
                                                $start = (int) $mBullet;
1 ✔
89
                                        } elseif ($key[0] === 'a' && $mBullet[0] > 'a') {
1 ✔
UNCOV
90
                                                $start = ord($mBullet[0]) - 96;
×
91
                                        } elseif ($key[0] === 'A' && $mBullet[0] > 'A') {
1 ✔
UNCOV
92
                                                $start = ord($mBullet[0]) - 64;
×
93
                                        }
94
                                }
95
                                break;
1 ✔
96
                        }
97
                }
98

99
                if ($bullet === null) {
1 ✔
UNCOV
100
                        return null;
×
101
                }
102

103
                $context->getBlockParser()->moveBackward();
1 ✔
104

105
                $items = [];
1 ✔
106
                while ($item = $this->parseItem($context, $bullet, false)) {
1 ✔
107
                        $items[] = $item;
1 ✔
108
                }
109

110
                if (count($items) < $min) {
1 ✔
111
                        return null;
1 ✔
112
                }
113

114
                return new ListNode(
1 ✔
115
                        $items,
116
                        $listType,
117
                        $start,
118
                        Modifier::parse($mMod, $offsets[1]),
1 ✔
119
                        $this->createRange($offsets[0], $items),
1 ✔
120
                );
121
        }
122

123

124
        /**
125
         * Parses definition list.
126
         * @param  array{string, ?string, string, ?string, string, string}  $matches
127
         * @param  array{int, ?int, int, ?int, int, int}  $offsets
128
         */
129
        public function parseDefList(ParseContext $context, array $matches, array $offsets): ?DefinitionListNode
130
        {
131
                [, $mMod, , , , $mBullet] = $matches;
1 ✔
132

133
                $bullet = null;
1 ✔
134

135
                foreach ($this->config->listBullets as $desc) {
1 ✔
136
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~Ax')) {
1 ✔
137
                                $bullet = $desc[2] ?? $desc[0];
1 ✔
138
                                break;
1 ✔
139
                        }
140
                }
141

142
                if ($bullet === null) {
1 ✔
UNCOV
143
                        return null;
×
144
                }
145

146
                $context->getBlockParser()->moveBackward(2);
1 ✔
147

148
                $items = [];
1 ✔
149
                $patternTerm = '~^
1 ✔
150
                        \n?
151
                        ( \S .* )                       # term content
152
                        : [ \t]*                        # colon separator
153
                        ' . Patterns::ModifierHAlign . '?
154
                $~mUAx';
155

156
                while (true) {
1 ✔
157
                        if ($item = $this->parseItem($context, $bullet, true)) {
1 ✔
158
                                $items[] = $item;
1 ✔
159
                                continue;
1 ✔
160
                        }
161

162
                        $termMatches = null;
1 ✔
163
                        $termOffsets = null;
1 ✔
164
                        if ($context->getBlockParser()->next($patternTerm, $termMatches, $termOffsets)) {
1 ✔
165
                                /** @var array{string, string, ?string} $termMatches */
166
                                [, $mContent, $mTermMod] = $termMatches;
1 ✔
167
                                $termMod = Modifier::parse($mTermMod, $termOffsets[2] ?? null);
1 ✔
168
                                // groups 0 and 1 always participate in a successful match, but next() cannot type that
169
                                $termOffset = $termOffsets[0] ?? throw new \LogicException('Match without group 0.');
1 ✔
170
                                $contentOffset = $termOffsets[1] ?? throw new \LogicException('Match without group 1.');
1 ✔
171
                                $termContent = $context->parseInline($mContent, $contentOffset);
1 ✔
172
                                $items[] = new ListItemNode($termContent, true, $termMod, new Range($termOffset, strlen($termMatches[0])));
1 ✔
173
                                continue;
1 ✔
174
                        }
175

176
                        break;
1 ✔
177
                }
178

179
                if (!$items) {
1 ✔
UNCOV
180
                        return null;
×
181
                }
182

183
                return new DefinitionListNode(
1 ✔
184
                        $items,
185
                        Modifier::parse($mMod, $offsets[1]),
1 ✔
186
                        $this->createRange($offsets[0], $items),
1 ✔
187
                );
188
        }
189

190

191
        /**
192
         * Parses single list item.
193
         */
194
        private function parseItem(ParseContext $context, string $bullet, bool $indented): ?ListItemNode
195
        {
196
                $spacesBase = $indented ? ('[\ \t]{1,}') : '';
1 ✔
197
                $patternItem = "~^
1 ✔
198
                        \\n?
199
                        ($spacesBase)                            # base indentation (1)
1 ✔
200
                        ({$bullet})                              # bullet character (2)
1 ✔
201
                        [ \\t]*
202
                        ( \\S .* )?                              # content (3)
203
                        " . Patterns::ModifierHAlign . '?           # modifier (4)
1 ✔
204
                $~mAUx';
205

206
                $matches = null;
1 ✔
207
                $offsets = null;
1 ✔
208
                if (!$context->getBlockParser()->next($patternItem, $matches, $offsets)) {
1 ✔
209
                        return null;
1 ✔
210
                }
211

212
                /** @var array{string, string, string, ?string, ?string} $matches */
213
                [, $mIndent, $mBullet, $mContent, $mMod] = $matches;
1 ✔
214
                // groups 0 and 2 always participate in a successful match, but next() cannot type that
215
                $itemOffset = $offsets[0] ?? throw new \LogicException('Match without group 0.');
1 ✔
216
                $bulletRange = new Range($offsets[2] ?? throw new \LogicException('Match without group 2.'), strlen(rtrim($mBullet)));
1 ✔
217
                $modOffset = $offsets[4] ?? null;
1 ✔
218
                $itemEnd = $itemOffset + strlen($matches[0]);
1 ✔
219

220
                // Assemble content and map local line starts to absolute source offsets
221
                $map = [];
1 ✔
222
                $content = ' ' . ($mContent ?? '');
1 ✔
223
                if ($mContent !== null) {
1 ✔
224
                        // content matched, so group 3 participated and carries an offset
225
                        $map[1] = $offsets[3] ?? throw new \LogicException('Content without group 3.'); // 1 = the leading space
1 ✔
226
                }
227

228
                // a bullet opening the first line is text, - * text is no nested list; the leading space keeps it so
229
                $isBullet = Regexp::match(
1 ✔
230
                        $mContent ?? '',
1 ✔
231
                        '~(?:' . implode('|', array_column($this->config->listBullets, 0)) . ')~Ax',
1 ✔
232
                ) !== null;
1 ✔
233

234
                // next lines
235
                $spaces = '';
1 ✔
236
                while ($context->getBlockParser()->next('~^
1 ✔
237
                        (\n*)
238
                        ' . Regexp::quote($mIndent) . '
1 ✔
239
                        ([ \t]{1,' . $spaces . '})
1 ✔
240
                        (.*)                                     # content (3)
241
                $~Amx', $matches, $offsets)) {
242
                        /** @var array{string, string, string, string} $matches */
243
                        [, $mBlank, $mSpaces, $mContent] = $matches;
1 ✔
244

245
                        if ($spaces === '') {
1 ✔
246
                                $spaces = strlen($mSpaces);
1 ✔
247
                        }
248

249
                        if ($mContent !== '' && $offsets[3] !== null) {
1 ✔
250
                                $map[strlen($content) + 1 + strlen($mBlank)] = $offsets[3]; // 1 = "\n"
1 ✔
251
                        }
252

253
                        $content .= "\n" . $mBlank . $mContent;
1 ✔
254
                        $itemEnd = ($offsets[0] ?? throw new \LogicException('Match without group 0.')) + strlen($matches[0]);
1 ✔
255
                }
256

257
                $text = $isBullet ? rtrim($content) : trim($content);
1 ✔
258
                $parsed = $context->parseBlock($text);
1 ✔
259
                if ($map) {
1 ✔
260
                        new Texy\Parsing\OffsetMap($map, strlen(rtrim($content)) - strlen($text))->applyTo($parsed);
1 ✔
261
                }
262

263
                return new ListItemNode(
1 ✔
264
                        $parsed,
1 ✔
265
                        false,
1 ✔
266
                        Modifier::parse($mMod, $modOffset),
1 ✔
267
                        new Range($itemOffset, $itemEnd - $itemOffset),
1 ✔
268
                        rtrim($mBullet),
1 ✔
269
                        $bulletRange,
270
                );
271
        }
272

273

274
        /**
275
         * Range of a whole list, from its start to the end of its last item.
276
         * @param  non-empty-list<ListItemNode>  $items
277
         */
278
        private function createRange(int $offset, array $items): Range
279
        {
280
                $last = $items[count($items) - 1]->range ?? throw new \LogicException('List item without range.');
1 ✔
281
                return new Range($offset, $last->offset + $last->length - $offset);
1 ✔
282
        }
283
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc