• 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

94.12
/src/Texy/Modules/HeadingModule.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\Modifier;
14
use function array_flip, array_values, asort, count, is_array, max, min, reset, rtrim, strlen, trim;
15

16

17
/**
18
 * Processes heading syntax (underlined and surrounded) and builds table of contents.
19
 */
20
final class HeadingModule extends Texy\Module
21
{
22
        public const
23
                DYNAMIC = 1, // auto-leveling
24
                FIXED = 2; // fixed-leveling
25

26
        /** textual content of first heading */
27
        public ?string $title = null;
28

29
        /** @var list<array{el: Texy\HtmlElement, level: int, type: string, title?: string}>  generated Table of Contents */
30
        public array $TOC = [];
31

32
        public bool $generateID = false;
33

34
        /** prefix for autogenerated ID */
35
        public string $idPrefix = 'toc-';
36

37
        /** level of top heading, 1..6 */
38
        public int $top = 1;
39

40
        /** surrounded headings: more #### means higher heading */
41
        public bool $moreMeansHigher = true;
42

43
        /** balancing mode */
44
        public int $balancing = self::DYNAMIC;
45

46
        /** @var array<string, int>  when $balancing = HeadingModule::FIXED */
47
        public array $levels = [
48
                '#' => 0, // # --> $levels['#'] + $top = 0 + 1 = 1 --> <h1> ... </h1>
49
                '*' => 1,
50
                '=' => 2,
51
                '-' => 3,
52
        ];
53

54
        /** @var array<string, true>  used ID's */
55
        private array $usedID = [];
56

57

58
        public function __construct(Texy\Texy $texy)
1✔
59
        {
60
                $this->texy = $texy;
1✔
61

62
                $texy->addHandler('heading', $this->solve(...));
1✔
63
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
64
                $texy->addHandler('afterParse', $this->afterParse(...));
1✔
65

66
                $texy->registerBlockPattern(
1✔
67
                        $this->patternUnderline(...),
1✔
68
                        '#^(\S.{0,1000})' . Texy\Patterns::MODIFIER_H . '?\n'
69
                        . '(\#{3,}+|\*{3,}+|={3,}+|-{3,}+)$#mU',
1✔
70
                        'heading/underlined',
1✔
71
                );
72

73
                $texy->registerBlockPattern(
1✔
74
                        $this->patternSurround(...),
1✔
75
                        '#^(\#{2,}+|={2,}+)(.+)' . Texy\Patterns::MODIFIER_H . '?()$#mU',
1✔
76
                        'heading/surrounded',
1✔
77
                );
78
        }
1✔
79

80

81
        private function beforeParse(): void
82
        {
83
                $this->title = null;
1✔
84
                $this->usedID = [];
1✔
85
                $this->TOC = [];
1✔
86
        }
1✔
87

88

89
        private function afterParse(Texy\Texy $texy, Texy\HtmlElement $DOM, bool $isSingleLine): void
1✔
90
        {
91
                if ($isSingleLine) {
1✔
92
                        return;
×
93
                }
94

95
                if ($this->balancing === self::DYNAMIC) {
1✔
96
                        $top = $this->top;
1✔
97
                        $map = [];
1✔
98
                        $min = 100;
1✔
99
                        foreach ($this->TOC as $item) {
1✔
100
                                $level = $item['level'];
1✔
101
                                if ($item['type'] === 'surrounded') {
1✔
102
                                        $min = min($level, $min);
1✔
103
                                        $top = $this->top - $min;
1✔
104

105
                                } elseif ($item['type'] === 'underlined') {
1✔
106
                                        $map[$level] = $level;
1✔
107
                                }
108
                        }
109

110
                        asort($map);
1✔
111
                        $map = array_flip(array_values($map));
1✔
112
                }
113

114
                foreach ($this->TOC as $key => $item) {
1✔
115
                        if ($this->balancing === self::DYNAMIC) {
1✔
116
                                if ($item['type'] === 'surrounded') {
1✔
117
                                        $level = $item['level'] + $top;
1✔
118

119
                                } elseif ($item['type'] === 'underlined') {
1✔
120
                                        $level = $map[$item['level']] + $this->top;
1✔
121

122
                                } else {
123
                                        $level = $item['level'];
1✔
124
                                }
125

126
                                $item['el']->setName('h' . min(6, max(1, $level)));
1✔
127
                                $this->TOC[$key]['level'] = $level;
1✔
128
                        }
129

130
                        if ($this->generateID) {
1✔
131
                                $style = $item['el']->attrs['style'] ?? null;
1✔
132
                                if (is_array($style) && !empty($style['toc'])) {
1✔
133
                                        $title = (string) $style['toc'];
1✔
134
                                        unset($item['el']->attrs['style']['toc']);
1✔
135
                                } else {
136
                                        $title = trim($item['el']->toText($this->texy));
1✔
137
                                }
138

139
                                $this->TOC[$key]['title'] = $title;
1✔
140
                                if (empty($item['el']->attrs['id'])) {
1✔
141
                                        $id = $this->idPrefix . Texy\Helpers::webalize($title);
1✔
142
                                        $counter = '';
1✔
143
                                        if (isset($this->usedID[$id . $counter])) {
1✔
144
                                                $counter = 2;
×
145
                                                while (isset($this->usedID[$id . '-' . $counter])) {
×
146
                                                        $counter++;
×
147
                                                }
148

149
                                                $id .= '-' . $counter;
×
150
                                        }
151

152
                                        $this->usedID[$id] = true;
1✔
153
                                        $item['el']->attrs['id'] = $id;
1✔
154
                                }
155
                        }
156
                }
157

158
                // document title
159
                if ($this->title === null && count($this->TOC)) {
1✔
160
                        $item = reset($this->TOC);
1✔
161
                        $this->title = $item['title'] ?? trim($item['el']->toText($this->texy));
1✔
162
                }
163
        }
1✔
164

165

166
        /**
167
         * Callback for underlined heading.
168
         *
169
         * Heading .(title)[class]{style}>
170
         * -------------------------------
171
         * @param  string[]  $matches
172
         */
173
        public function patternUnderline(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
174
        {
175
                [, $mContent, $mMod, $mLine] = $matches;
1✔
176
                // $matches:
177
                // [1] => ...
178
                // [2] => .(title)[class]{style}<>
179
                // [3] => ...
180

181
                $mod = new Modifier($mMod);
1✔
182
                $level = $this->levels[$mLine[0]];
1✔
183
                return $this->texy->invokeAroundHandlers('heading', $parser, [$level, $mContent, $mod, false]);
1✔
184
        }
185

186

187
        /**
188
         * Callback for surrounded heading.
189
         *
190
         * ### Heading .(title)[class]{style}>
191
         * @param  string[]  $matches
192
         */
193
        public function patternSurround(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
194
        {
195
                [, $mLine, $mContent, $mMod] = $matches;
1✔
196
                // [1] => ###
197
                // [2] => ...
198
                // [3] => .(title)[class]{style}<>
199

200
                $mod = new Modifier($mMod);
1✔
201
                $level = min(7, max(2, strlen($mLine)));
1✔
202
                $level = $this->moreMeansHigher ? 7 - $level : $level - 2;
1✔
203
                $mContent = rtrim($mContent, $mLine[0] . ' ');
1✔
204
                return $this->texy->invokeAroundHandlers('heading', $parser, [$level, $mContent, $mod, true]);
1✔
205
        }
206

207

208
        /**
209
         * Finish invocation.
210
         */
211
        private function solve(
1✔
212
                Texy\HandlerInvocation $invocation,
213
                int $level,
214
                string $content,
215
                Modifier $mod,
216
                bool $isSurrounded,
217
        ): Texy\HtmlElement
218
        {
219
                // as fixed balancing, for block/texysource & correct decorating
220
                $el = new Texy\HtmlElement('h' . min(6, max(1, $level + $this->top)));
1✔
221
                $mod->decorate($this->texy, $el);
1✔
222

223
                $el->parseLine($this->texy, trim($content));
1✔
224

225
                $this->TOC[] = [
1✔
226
                        'el' => $el,
1✔
227
                        'level' => $level,
1✔
228
                        'type' => $isSurrounded ? 'surrounded' : 'underlined',
1✔
229
                ];
230

231
                return $el;
1✔
232
        }
233
}
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