• 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

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

15

16
/**
17
 * Long words wrap module.
18
 */
19
final class LongWordsModule extends Texy\Module
20
{
21
        private const
22
                Dont = 0, // don't hyphenate
23
                Here = 1, // hyphenate here
24
                After = 2; // hyphenate after
25

26
        private const SafeLimit = 1000;
27

28
        public int $wordLimit = 20;
29

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

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

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

54
        private array $before_h = ['c', 'C', 's', 'S'];
55

56
        private array $doubleVowels = ['a', 'A', 'o', 'O'];
57

58

59
        public function __construct(Texy\Texy $texy)
1✔
60
        {
61
                $this->texy = $texy;
1✔
62

63
                $this->consonants = array_flip($this->consonants);
1✔
64
                $this->vowels = array_flip($this->vowels);
1✔
65
                $this->before_r = array_flip($this->before_r);
1✔
66
                $this->before_l = array_flip($this->before_l);
1✔
67
                $this->before_h = array_flip($this->before_h);
1✔
68
                $this->doubleVowels = array_flip($this->doubleVowels);
1✔
69

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

73

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

83

84
        /**
85
         * Callback for long words.
86
         */
87
        private function parse(array $matches): string
1✔
88
        {
89
                [$mWord] = $matches;
1✔
90
                // [0] => lllloooonnnnggggwwwoorrdddd
91

92
                if (iconv_strlen($mWord, 'UTF-8') > self::SafeLimit) {
1✔
93
                        return $mWord;
1✔
94
                }
95

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

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

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

113
                        $s[] = $char;
1✔
114
                        $trans[] = $key;
1✔
115
                }
116

117
                $s[] = '';
1✔
118
                $len = count($s) - 2;
1✔
119

120
                $positions = [];
1✔
121
                $a = 0;
1✔
122
                $last = 1;
1✔
123

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

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

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

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

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

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

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

160
                $syllables[] = implode('', $chars);
1✔
161
                return implode("\u{AD}", $syllables);
1✔
162
        }
163

164

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

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

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

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

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

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

190
                                return self::After;
1✔
191
                        }
192

193
                        return self::Dont;
1✔
194

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

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

202
                return self::Dont; // Do not hyphenate
1✔
203
        }
204
}
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