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

dg / texy / 42887648368

10 Nov 2026 01:51PM UTC coverage: 96.622% (-0.1%) from 96.735%
42887648368

push

github

dg
Config::$locales: each locale is a list of four quotes (BC break)

Opening and closing double quotes, then the single ones, instead of the pairs
singleQuotes and doubleQuotes. The compatibility layer converts the old table.

cabae8f0-9dec-45ab-a919-45e6dadc3ad6

8 of 8 new or added lines in 3 files covered. (100.0%)

73 existing lines in 18 files now uncovered.

3719 of 3849 relevant lines covered (96.62%)

0.97 hits per line

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

95.0
/src/Modules/BlockModule.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Texy! (https://texy.deegee.dev)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Texy\Modules;
9

10
use Texy;
11
use Texy\{Helpers, Range, Syntax};
12
use Texy\Nodes\{
13
        BlockNode, CodeBlockNode, CommentNode, ContentNode, DivNode, HtmlBlockNode, PreformattedNode, TextBlockNode, TexySourceNode,
14
};
15
use Texy\Parsing\{OffsetMap, ParseContext};
16
use function strlen;
17

18

19
/**
20
 * Processes special blocks (/-- code, html, text, div, etc.).
21
 */
22
final class BlockModule extends Texy\Module
23
{
24
        public function __construct(
1 ✔
25
                private Texy\TexyConfig $config,
26
        ) {
27
        }
1 ✔
28

29

30
        public function register(Texy\Parsing\Registry $registry): void
31
        {
32
                foreach ([
33
                        Syntax::BlockDefault,
1 ✔
34
                        Syntax::BlockPre,
35
                        Syntax::BlockCode,
36
                        Syntax::BlockHtml,
37
                        Syntax::BlockText,
38
                        Syntax::BlockTexySource,
39
                        Syntax::BlockComment,
40
                        Syntax::BlockDiv,
41
                ] as $name) {
42
                        $registry->declare($name);
1 ✔
43
                }
44

45
                foreach ($this->config->blockTypes as $type => $_) {
1 ✔
46
                        $registry->declare('block/' . $type);
1 ✔
47
                }
48

49
                $registry->addBlock(
1 ✔
50
                        Syntax::Blocks,
1 ✔
51
                        '~^
52
                                /--++ [ \t]*+                 # opening tag /--
53
                                (.*)                          # content type (1)
54
                                ' . Texy\Patterns::ModifierHAlign . '? # modifier (2)
1 ✔
55
                                $
56
                                ((?:                         # content (3)
57
                                        \n (?0) |                # recursive nested blocks
58
                                        \n.*+                    # or any content
59
                                )*)
60
                                (?:
61
                                        \n \\\--.* $ |           # closing tag
62
                                        \z                       # or end of input
63
                                )
64
                        ~mUix',
65
                        $this->parse(...),
1 ✔
66
                );
67

68
                // ```language
69
                $registry->addBlock(
1 ✔
70
                        Syntax::CodeFenced,
1 ✔
71
                        '~^
1 ✔
72
                                (`{3,}) [ \t]*+                     # opening fence (1)
73
                                ([^\n`]*)                           # info string (2)
74
                                $
75
                                ((?: \n (?! \1 `*+ [ \t]*+ $ ) .*+ )*) # content (3): lines that are not a valid closing fence
76
                                (?:
77
                                        \n \1 `*+ [ \t]*+ $ |           # closing fence (same length or longer)
78
                                        \z                              # or end of input
79
                                )
80
                        ~mx',
81
                        $this->parseFenced(...),
1 ✔
82
                );
83
        }
1 ✔
84

85

86
        /**
87
         * Parses ```language fenced code blocks; content is verbatim.
88
         * @param  array{string, string, string, string}  $matches
89
         * @param  array{int, int, int, int}  $offsets
90
         */
91
        public function parseFenced(ParseContext $context, array $matches, array $offsets): CodeBlockNode
92
        {
93
                [, , $mInfo, $mContent] = $matches;
1 ✔
94

95
                // a Texy modifier may trail the info string: ```php .{file: Foo.php}
96
                $mod = null;
1 ✔
97
                if ($m = Texy\Regexp::match($mInfo, '~' . Texy\Patterns::Modifier . '$~x', captureOffset: true)) {
1 ✔
UNCOV
98
                        $mod = Texy\Modifier::parse($m[1][0], $offsets[2] + $m[1][1]);
×
UNCOV
99
                        $mInfo = substr($mInfo, 0, $m[0][1]);
×
100
                }
101

102
                // first word of the info string is the language (GFM)
103
                $language = Texy\Regexp::split(trim($mInfo), '~\s+~', limit: 2)[0];
1 ✔
104

105
                $code = substr($mContent, 1); // drop the separator "\n"; intentional blank lines stay
1 ✔
106
                return new CodeBlockNode(
1 ✔
107
                        $code,
108
                        $language === '' ? null : $language,
1 ✔
109
                        $mod,
110
                        new Range($offsets[0], strlen($matches[0])),
1 ✔
111
                        OffsetMap::fromLines(OffsetMap::linesOf($code, $offsets[3] + 1)),
1 ✔
112
                );
113
        }
114

115

116
        /**
117
         * Parses blocks /--foo
118
         * @param  array{string, string, ?string, string}  $matches
119
         * @param  array{int, int, ?int, int}  $offsets
120
         */
121
        public function parse(ParseContext $context, array $matches, array $offsets): ?BlockNode
122
        {
123
                [, $mParam, $mMod, $mContent] = $matches;
1 ✔
124

125
                $mod = Texy\Modifier::parse($mMod, $offsets[2] ?? null);
1 ✔
126
                $parts = Texy\Regexp::split($mParam, '~\s+~', limit: 2);
1 ✔
127
                // the opening pattern matches case-insensitively, so /--PRE must mean the same
128
                // as /--pre; only the type is lowercased, a language name keeps its spelling
129
                $blocktype = empty($parts[0]) ? Syntax::BlockDefault : 'block/' . Helpers::toLower($parts[0]);
1 ✔
130
                $param = empty($parts[1]) ? null : $parts[1];
1 ✔
131

132
                [$content, $map] = OffsetMap::outdentLines(OffsetMap::linesOf($mContent, $offsets[3]));
1 ✔
133
                $range = new Range($offsets[0], strlen($matches[0]));
1 ✔
134

135
                // a disallowed type stays as source text; /--php and friends are code blocks
136
                $allowedAs = isset($context->allowedSyntaxes[$blocktype]) ? $blocktype : Syntax::BlockCode;
1 ✔
137
                if (empty($context->allowedSyntaxes[$allowedAs])) {
1 ✔
138
                        return null;
1 ✔
139
                }
140

141
                if ($parser = $this->config->blockTypes[Helpers::toLower($parts[0])] ?? null) {
1 ✔
142
                        $node = $parser($context, $content, $param, $mod);
1 ✔
143
                        if ($node !== null) {
1 ✔
144
                                foreach ($node->getChildren() as $child) {
1 ✔
145
                                        $map->applyTo($child); // the parser read the outdented content
1 ✔
146
                                }
147
                                $node->range ??= $range;
1 ✔
148
                        }
149
                        return $node;
1 ✔
150
                }
151

152
                if ($blocktype === Syntax::BlockCode) {
1 ✔
153
                        return new CodeBlockNode($content, $param, $mod, $range, $map);
1 ✔
154

155
                } elseif ($blocktype === Syntax::BlockPre) {
1 ✔
156
                        return new PreformattedNode($content, $this->parseVerbatim($context, $content, $map), $mod, $range, $map, Syntax::BlockPre);
1 ✔
157

158
                } elseif ($blocktype === Syntax::BlockDefault) {
1 ✔
159
                        $text = $this->parseVerbatim($context, $content, $map, []);
1 ✔
160
                        return new PreformattedNode($content, $text, $mod, $range, $map, Syntax::BlockDefault);
1 ✔
161

162
                } elseif ($blocktype === Syntax::BlockComment) {
1 ✔
163
                        return new CommentNode($content, $range);
1 ✔
164

165
                } elseif ($blocktype === Syntax::BlockHtml || $blocktype === Syntax::BlockText) {
1 ✔
166
                        // html/text blocks don't use outdent - preserve original indentation
167
                        $content = trim($mContent, "\n");
1 ✔
168
                        $map = OffsetMap::fromLines(OffsetMap::linesOf($content, $offsets[3] + strspn($mContent, "\n")));
1 ✔
169
                        return $blocktype === Syntax::BlockHtml
1 ✔
170
                                ? new HtmlBlockNode($content, $this->parseVerbatim($context, $content, $map), $mod, $range, $map)
1 ✔
171
                                : new TextBlockNode($content, $mod, $range, $map);
1 ✔
172

173
                } elseif ($blocktype === Syntax::BlockDiv) {
1 ✔
174
                        [$content, $map] = OffsetMap::outdentLines(OffsetMap::linesOf($mContent, $offsets[3]), firstLine: true);
1 ✔
175
                        if ($content === '') {
1 ✔
UNCOV
176
                                return null;
×
177
                        }
178
                        $parsed = $context->parseBlock($content);
1 ✔
179
                        $map->applyTo($parsed);
1 ✔
180
                        return new DivNode($parsed, $mod, $range);
1 ✔
181

182
                } elseif ($blocktype === Syntax::BlockTexySource) {
1 ✔
183
                        // parsed as Texy, its HTML is displayed as source code
184
                        if ($content === '') {
1 ✔
UNCOV
185
                                return null;
×
186
                        }
187
                        $parsed = $context->parseBlock($content);
1 ✔
188
                        $map->applyTo($parsed);
1 ✔
189
                        return new TexySourceNode($content, $parsed, $mod, $range, $map);
1 ✔
190
                }
191

192
                // /--php and friends: a code block in that language. Anything that does not look
193
                // like a language name stays refused and degrades to visible text.
194
                return Texy\Regexp::match($parts[0], '~^[a-z][a-z0-9_+#-]*$~i')
1 ✔
195
                        ? new CodeBlockNode($content, $parts[0], $mod, $range, $map)
1 ✔
196
                        : null;
1 ✔
197
        }
198

199

200
        /**
201
         * Parses the markup of a verbatim block, which knows the given syntaxes only: HTML tags,
202
         * comments and entities in html and pre blocks, none in a bare /--.
203
         * @param  list<string>  $syntaxes
204
         */
205
        private function parseVerbatim(
206
                ParseContext $context,
207
                string $text,
208
                OffsetMap $map,
209
                array $syntaxes = [Syntax::HtmlTag, Syntax::HtmlComment, Syntax::HtmlEntity],
210
        ): ContentNode
211
        {
212
                $parsed = $context->getInlineParser()
1 ✔
213
                        ->withPatterns($syntaxes)
1 ✔
214
                        ->parse($context, $text);
1 ✔
215
                $map->applyTo($parsed);
1 ✔
216
                return $parsed;
1 ✔
217
        }
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc