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

dg / texy / 22262381750

25 Jan 2026 11:44PM UTC coverage: 92.367% (-0.7%) from 93.057%
22262381750

push

github

dg
cs

9 of 11 new or added lines in 8 files covered. (81.82%)

161 existing lines in 23 files now uncovered.

2384 of 2581 relevant lines covered (92.37%)

0.92 hits per line

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

94.05
/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
 * Heading module.
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 array<int, array{el: Texy\HtmlElement, level: int, type: 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
                                if (!empty($item['el']->attrs['style']['toc']) && is_array($item['el']->attrs['style'])) {
1✔
132
                                        $title = $item['el']->attrs['style']['toc'];
1✔
133
                                        unset($item['el']->attrs['style']['toc']);
1✔
134
                                } else {
135
                                        $title = trim($item['el']->toText($this->texy));
1✔
136
                                }
137

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

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

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

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

164

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

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

184

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

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

204

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

220
                $el->parseLine($this->texy, trim($content));
1✔
221

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

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