• 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

96.39
/src/Texy/Modules/LongWordsModule.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 function array_flip, array_pop, array_splice, count, end, iconv_strlen, implode, ord;
12

13

14
/**
15
 * Breaks long words with soft hyphens for better line wrapping.
16
 */
17
final class LongWordsModule extends Texy\Module
18
{
19
        private const
20
                Dont = 0, // don't hyphenate
21
                Here = 1, // hyphenate here
22
                After = 2; // hyphenate after
23

24
        private const SafeLimit = 1000;
25

26
        public int $wordLimit = 20;
27

28
        /** @var array<string, int>|string[] */
29
        private array $consonants = [
30
                'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z',
31
                'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Z',
32
                "\u{10D}", "\u{10F}", "\u{148}", "\u{159}", "\u{161}", "\u{165}", "\u{17E}", // Czech UTF-8
33
                "\u{10C}", "\u{10E}", "\u{147}", "\u{158}", "\u{160}", "\u{164}", "\u{17D}",
34
        ];
35

36
        /** @var array<string, int>|string[] */
37
        private array $vowels = [
38
                'a', 'e', 'i', 'o', 'u', 'y',
39
                'A', 'E', 'I', 'O', 'U', 'Y',
40
                "\u{E1}", "\u{E9}", "\u{11B}", "\u{ED}", "\u{F3}", "\u{FA}", "\u{16F}", "\u{FD}", // Czech UTF-8
41
                "\u{C1}", "\u{C9}", "\u{11A}", "\u{CD}", "\u{D3}", "\u{DA}", "\u{16E}", "\u{DD}",
42
        ];
43

44
        /** @var array<string, int>|string[] */
45
        private array $before_r = [
46
                'b', 'B', 'c', 'C', 'd', 'D', 'f', 'F', 'g', 'G', 'k', 'K', 'p', 'P', 'r', 'R', 't', 'T', 'v', 'V',
47
                "\u{10D}", "\u{10C}", "\u{10F}", "\u{10E}", "\u{159}", "\u{158}", "\u{165}", "\u{164}", // Czech UTF-8
48
        ];
49

50
        /** @var array<string, int>|string[] */
51
        private array $before_l = [
52
                'b', 'B', 'c', 'C', 'd', 'D', 'f', 'F', 'g', 'G', 'k', 'K', 'l', 'L', 'p', 'P', 't', 'T', 'v', 'V',
53
                "\u{10D}", "\u{10C}", "\u{10F}", "\u{10E}", "\u{165}", "\u{164}", // Czech UTF-8
54
        ];
55

56
        /** @var array<string, int>|string[] */
57
        private array $before_h = ['c', 'C', 's', 'S'];
58

59
        /** @var array<string, int>|string[] */
60
        private array $doubleVowels = ['a', 'A', 'o', 'O'];
61

62

63
        public function __construct(Texy\Texy $texy)
1✔
64
        {
65
                $this->consonants = array_flip($this->consonants);
1✔
66
                $this->vowels = array_flip($this->vowels);
1✔
67
                $this->before_r = array_flip($this->before_r);
1✔
68
                $this->before_l = array_flip($this->before_l);
1✔
69
                $this->before_h = array_flip($this->before_h);
1✔
70
                $this->doubleVowels = array_flip($this->doubleVowels);
1✔
71

72
                $texy->registerPostLine($this->postLine(...), 'longwords');
1✔
73
        }
1✔
74

75

76
        public function postLine(string $text): string
1✔
77
        {
78
                return Texy\Regexp::replace(
1✔
79
                        $text,
1✔
80
                        '~[^ \n\t\x14\x15\x16\x{2013}\x{2014}\x{ad}-]{' . $this->wordLimit . ',}~',
1✔
81
                        $this->pattern(...),
1✔
82
                );
83
        }
84

85

86
        /**
87
         * Parses long words.
88
         * @param  string[]  $matches
89
         */
90
        private function pattern(array $matches): string
1✔
91
        {
92
                [$mWord] = $matches;
1✔
93
                // [0] => lllloooonnnnggggwwwoorrdddd
94

95
                if (iconv_strlen($mWord, 'UTF-8') > self::SafeLimit) {
1✔
96
                        return $mWord;
1✔
97
                }
98

99
                $chars = Texy\Regexp::matchAll(
1✔
100
                        $mWord,
1✔
101
                        '~[' . Texy\Patterns::MARK . ']+|.~',
1✔
102
                );
103

104
                $chars = array_column($chars, 0);
1✔
105
                if (count($chars) < $this->wordLimit) {
1✔
106
                        return $mWord;
1✔
107
                }
108

109
                $s = [''];
1✔
110
                $trans = [-1];
1✔
111
                foreach ($chars as $key => $char) {
1✔
112
                        if (ord($char[0]) < 32) {
1✔
113
                                continue;
1✔
114
                        }
115

116
                        $s[] = $char;
1✔
117
                        $trans[] = $key;
1✔
118
                }
119

120
                $s[] = '';
1✔
121
                $len = count($s) - 2;
1✔
122

123
                $positions = [];
1✔
124
                $a = 0;
1✔
125
                $last = 1;
1✔
126

127
                while (++$a < $len) {
1✔
128
                        if ($s[$a] === "\u{A0}") {
1✔
UNCOV
129
                                $a++;
×
UNCOV
130
                                continue;  // here and after never
×
131
                        }
132

133
                        $hyphen = $this->getHyphen($s[$a], $s[$a - 1], $s[$a + 1]);
1✔
134

135
                        if ($hyphen === self::Dont && ($a - $last > $this->wordLimit * 0.6)) {
1✔
136
                                $positions[] = $last = $a - 1; // Hyphenate here
1✔
137
                        }
138

139
                        if ($hyphen === self::Here) {
1✔
140
                                $positions[] = $last = $a - 1; // Hyphenate here
1✔
141
                        }
142

143
                        if ($hyphen === self::After) {
1✔
144
                                $positions[] = $last = $a;
1✔
145
                                $a++; // Hyphenate after
1✔
146
                        }
147
                }
148

149
                $a = end($positions);
1✔
150
                if (($a === $len - 1) && isset($this->consonants[$s[$len]])) {
1✔
151
                        array_pop($positions);
1✔
152
                }
153

154
                $syllables = [];
1✔
155
                $last = 0;
1✔
156
                foreach ($positions as $pos) {
1✔
157
                        if ($pos - $last > $this->wordLimit * 0.6) {
1✔
158
                                $syllables[] = implode('', array_splice($chars, 0, $trans[$pos] - $trans[$last]));
1✔
159
                                $last = $pos;
1✔
160
                        }
161
                }
162

163
                $syllables[] = implode('', $chars);
1✔
164
                return implode("\u{AD}", $syllables);
1✔
165
        }
166

167

168
        private function getHyphen(string $ch, string $prev, string $next): int
1✔
169
        {
170
                if ($ch === '.') {
1✔
171
                        return self::Here;
1✔
172

173
                } elseif (isset($this->consonants[$ch])) { // consonants
1✔
174
                        if (isset($this->vowels[$next])) {
1✔
175
                                return isset($this->vowels[$prev]) ? self::Here : self::Dont;
1✔
176

177
                        } elseif (($ch === 's') && ($prev === 'n') && isset($this->consonants[$next])) {
1✔
UNCOV
178
                                return self::After;
×
179

180
                        } elseif (isset($this->consonants[$next], $this->vowels[$prev])) {
1✔
181
                                if ($next === 'r') {
1✔
182
                                        return isset($this->before_r[$ch]) ? self::Here : self::After;
1✔
183

184
                                } elseif ($next === 'l') {
1✔
185
                                        return isset($this->before_l[$ch]) ? self::Here : self::After;
1✔
186

187
                                } elseif ($next === 'h') { // CH
1✔
188
                                        return isset($this->before_h[$ch])
1✔
189
                                                ? self::Dont
1✔
190
                                                : self::After;
1✔
191
                                }
192

193
                                return self::After;
1✔
194
                        }
195

196
                        return self::Dont;
1✔
197

198
                } elseif (($ch === 'u') && isset($this->doubleVowels[$prev])) {
1✔
199
                        return self::After;
1✔
200

201
                } elseif (isset($this->vowels[$ch], $this->vowels[$prev])) {
1✔
202
                        return self::Here;
1✔
203
                }
204

205
                return self::Dont; // Do not hyphenate
1✔
206
        }
207
}
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