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

dg / texy / 16790351274

06 Aug 2025 10:42PM UTC coverage: 92.746% (+0.005%) from 92.741%
16790351274

push

github

dg
HtmlElement: removed toHtml() & toText()

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

136 existing lines in 23 files now uncovered.

2391 of 2578 relevant lines covered (92.75%)

0.93 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.info)
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->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✔
UNCOV
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
                                if (!empty($item['el']->attrs['style']['toc']) && is_array($item['el']->attrs['style'])) {
1✔
140
                                        $title = $item['el']->attrs['style']['toc'];
1✔
141
                                        unset($item['el']->attrs['style']['toc']);
1✔
142
                                } else {
143
                                        $title = trim($this->texy->maskedStringToText($this->texy->elemToMaskedString($item['el'])));
1✔
144
                                }
145

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

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

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

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

172

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

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

192

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

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

212

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

225
                $el->inject($this->texy->parseLine(trim($content)));
1✔
226

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

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