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

dg / texy / 22262589061

21 Feb 2026 07:04PM UTC coverage: 92.991% (+1.8%) from 91.178%
22262589061

push

github

dg
LinkModule: deprecated label and modifiers in link definitions

3 of 3 new or added lines in 1 file covered. (100.0%)

126 existing lines in 22 files now uncovered.

2083 of 2240 relevant lines covered (92.99%)

0.93 hits per line

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

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

14

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

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

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

30
        public bool $generateID = false;
31

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

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

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

41
        /** balancing mode */
42
        public int $balancing = self::DYNAMIC;
43

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

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

55

56
        public function __construct(
1✔
57
                private Texy\Texy $texy,
58
        ) {
59
                $texy->addHandler('heading', $this->solve(...));
1✔
60
                $texy->addHandler('afterParse', $this->afterParse(...));
1✔
61
        }
1✔
62

63

64
        public function beforeParse(string &$text): void
1✔
65
        {
66
                $this->texy->registerBlockPattern(
1✔
67
                        $this->parseUnderline(...),
1✔
68
                        '~^
69
                                ( \S .{0,1000} )                 # heading text (1)
70
                                ' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
1✔
71
                                \n
72
                                ( \#{3,}+ | \*{3,}+ | ={3,}+ | -{3,}+ )  # underline characters (3)
73
                        $~mU',
74
                        'heading/underlined',
1✔
75
                );
76

77
                $this->texy->registerBlockPattern(
1✔
78
                        $this->parseSurround(...),
1✔
79
                        '~^
80
                                ( \#{2,}+ | ={2,}+ )             # opening characters (1)
81
                                (.+)                             # heading text (2)
82
                                ' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
1✔
83
                        $~mU',
84
                        'heading/surrounded',
1✔
85
                );
86

87
                $this->title = null;
1✔
88
                $this->usedID = [];
1✔
89
                $this->TOC = [];
1✔
90
        }
1✔
91

92

93
        public function afterParse(Texy\HtmlElement $DOM, bool $isSingleLine): void
1✔
94
        {
95
                if ($isSingleLine) {
1✔
UNCOV
96
                        return;
×
97
                }
98

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

109
                                } elseif ($item['type'] === 'underlined') {
1✔
110
                                        $map[$level] = $level;
1✔
111
                                }
112
                        }
113

114
                        asort($map);
1✔
115
                        $map = array_flip(array_values($map));
1✔
116
                }
117

118
                foreach ($this->TOC as $key => $item) {
1✔
119
                        if ($this->balancing === self::DYNAMIC) {
1✔
120
                                if ($item['type'] === 'surrounded') {
1✔
121
                                        $level = $item['level'] + $top;
1✔
122

123
                                } elseif ($item['type'] === 'underlined') {
1✔
124
                                        $level = $map[$item['level']] + $this->top;
1✔
125

126
                                } else {
127
                                        $level = $item['level'];
1✔
128
                                }
129

130
                                $item['el']->setName('h' . min(6, max(1, $level)));
1✔
131
                                $this->TOC[$key]['level'] = $level;
1✔
132
                        }
133

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

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

153
                                                $id .= '-' . $counter;
1✔
154
                                        }
155

156
                                        $this->usedID[$id] = true;
1✔
157
                                        $item['el']->attrs['id'] = $id;
1✔
158
                                }
159
                        }
160
                }
161

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

169

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

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

187

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

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

206

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

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

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

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