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

dg / texy / 21345344909

26 Jan 2026 03:32AM UTC coverage: 92.382% (-0.4%) from 92.744%
21345344909

push

github

dg
HtmlElement: removed toHtml() & toText()

18 of 19 new or added lines in 5 files covered. (94.74%)

149 existing lines in 21 files now uncovered.

2401 of 2599 relevant lines covered (92.38%)

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->toElement(...));
1✔
63
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
64
                $texy->addHandler('afterParse', $this->afterParse(...));
1✔
65

66
                $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
                $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
        }
1✔
87

88

89
        private function beforeParse(): void
90
        {
91
                $this->title = null;
1✔
92
                $this->usedID = [];
1✔
93
                $this->TOC = [];
1✔
94
        }
1✔
95

96

97
        private function afterParse(Texy\Texy $texy, Texy\HtmlElement $DOM, bool $isSingleLine): void
1✔
98
        {
99
                if ($isSingleLine) {
1✔
100
                        return;
×
101
                }
102

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

113
                                } elseif ($item['type'] === 'underlined') {
1✔
114
                                        $map[$level] = $level;
1✔
115
                                }
116
                        }
117

118
                        asort($map);
1✔
119
                        $map = array_flip(array_values($map));
1✔
120
                }
121

122
                foreach ($this->TOC as $key => $item) {
1✔
123
                        if ($this->balancing === self::DYNAMIC) {
1✔
124
                                if ($item['type'] === 'surrounded') {
1✔
125
                                        $level = $item['level'] + $top;
1✔
126

127
                                } elseif ($item['type'] === 'underlined') {
1✔
128
                                        $level = $map[$item['level']] + $this->top;
1✔
129

130
                                } else {
131
                                        $level = $item['level'];
1✔
132
                                }
133

134
                                $item['el']->setName('h' . min(6, max(1, $level)));
1✔
135
                                $this->TOC[$key]['level'] = $level;
1✔
136
                        }
137

138
                        if ($this->generateID) {
1✔
139
                                $style = $item['el']->attrs['style'] ?? null;
1✔
140
                                if (is_array($style) && !empty($style['toc'])) {
1✔
141
                                        $title = (string) $style['toc'];
1✔
142
                                        unset($item['el']->attrs['style']['toc']);
1✔
143
                                } else {
144
                                        $title = trim($this->texy->maskedStringToText($this->texy->elemToMaskedString($item['el'])));
1✔
145
                                }
146

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

UNCOV
157
                                                $id .= '-' . $counter;
×
158
                                        }
159

160
                                        $this->usedID[$id] = true;
1✔
161
                                        $item['el']->attrs['id'] = $id;
1✔
162
                                }
163
                        }
164
                }
165

166
                // document title
167
                if ($this->title === null && count($this->TOC)) {
1✔
168
                        $item = reset($this->TOC);
1✔
169
                        $this->title = $item['title'] ?? trim($this->texy->maskedStringToText($this->texy->elemToMaskedString($item['el'])));
1✔
170
                }
171
        }
1✔
172

173

174
        /**
175
         * Callback for underlined heading.
176
         *
177
         * Heading .(title)[class]{style}>
178
         * -------------------------------
179
         * @param  string[]  $matches
180
         */
181
        public function parseUnderline(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
182
        {
183
                [, $mContent, $mMod, $mLine] = $matches;
1✔
184
                // $matches:
185
                // [1] => ...
186
                // [2] => .(title)[class]{style}<>
187
                // [3] => ...
188

189
                $mod = new Modifier($mMod);
1✔
190
                $level = $this->levels[$mLine[0]];
1✔
191
                return $this->texy->invokeAroundHandlers('heading', $parser, [$level, $mContent, $mod, false]);
1✔
192
        }
193

194

195
        /**
196
         * Callback for surrounded heading.
197
         *
198
         * ### Heading .(title)[class]{style}>
199
         * @param  string[]  $matches
200
         */
201
        public function parseSurround(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
202
        {
203
                [, $mLine, $mContent, $mMod] = $matches;
1✔
204
                // [1] => ###
205
                // [2] => ...
206
                // [3] => .(title)[class]{style}<>
207

208
                $mod = new Modifier($mMod);
1✔
209
                $level = min(7, max(2, strlen($mLine)));
1✔
210
                $level = $this->moreMeansHigher ? 7 - $level : $level - 2;
1✔
211
                $mContent = rtrim($mContent, $mLine[0] . ' ');
1✔
212
                return $this->texy->invokeAroundHandlers('heading', $parser, [$level, $mContent, $mod, true]);
1✔
213
        }
214

215

216
        public function toElement(
1✔
217
                Texy\HandlerInvocation $invocation,
218
                int $level,
219
                string $content,
220
                Modifier $mod,
221
                bool $isSurrounded,
222
        ): Texy\HtmlElement
223
        {
224
                // as fixed balancing, for block/texysource & correct decorating
225
                $el = new Texy\HtmlElement('h' . min(6, max(1, $level + $this->top)));
1✔
226
                $mod->decorate($this->texy, $el);
1✔
227

228
                $el->inject($this->texy->parseLine(trim($content)));
1✔
229

230
                $this->TOC[] = [
1✔
231
                        'el' => $el,
1✔
232
                        'level' => $level,
1✔
233
                        'type' => $isSurrounded ? 'surrounded' : 'underlined',
1✔
234
                ];
235

236
                return $el;
1✔
237
        }
238
}
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