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

dg / texy / 21501721037

30 Jan 2026 02:00AM UTC coverage: 91.159% (-1.3%) from 92.426%
21501721037

push

github

dg
wip

2681 of 2941 relevant lines covered (91.16%)

0.91 hits per line

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

93.33
/src/Texy/Modules/BlockModule.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\Helpers;
14
use Texy\Nodes\BlockNode;
15
use Texy\Nodes\CodeBlockNode;
16
use Texy\Nodes\CommentNode;
17
use Texy\Nodes\SectionNode;
18
use Texy\ParseContext;
19
use Texy\Position;
20
use Texy\Syntax;
21

22

23
/**
24
 * Processes special blocks (/-- code, html, text, div, etc.).
25
 */
26
final class BlockModule extends Texy\Module
27
{
28
        public function __construct(
1✔
29
                private Texy\Texy $texy,
30
        ) {
31
                $texy->allowed[Syntax::BlockDefault] = true;
1✔
32
                $texy->allowed[Syntax::BlockPre] = true;
1✔
33
                $texy->allowed[Syntax::BlockCode] = true;
1✔
34
                $texy->allowed[Syntax::BlockHtml] = true;
1✔
35
                $texy->allowed[Syntax::BlockText] = true;
1✔
36
                $texy->allowed[Syntax::BlockTexySource] = true;
1✔
37
                $texy->allowed[Syntax::BlockComment] = true;
1✔
38
                $texy->allowed[Syntax::BlockDiv] = true;
1✔
39
        }
1✔
40

41

42
        public function beforeParse(string &$text): void
1✔
43
        {
44
                $this->texy->registerBlockPattern(
1✔
45
                        $this->parse(...),
1✔
46
                        '~^
47
                                /--++ \ *+                    # opening tag /--
48
                                (.*)                          # content type (1)
49
                                ' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
1✔
50
                                $
51
                                ((?:                         # content (3)
52
                                        \n (?0) |                # recursive nested blocks
53
                                        \n.*+                    # or any content
54
                                )*)
55
                                (?:
56
                                        \n \\\--.* $ |           # closing tag
57
                                        \z                       # or end of input
58
                                )
59
                        ~mUi',
60
                        Syntax::Blocks,
1✔
61
                );
62
        }
1✔
63

64

65
        /**
66
         * Parses blocks /--foo
67
         * @param  array<?string>  $matches
68
         * @param  array<?int>  $offsets
69
         */
70
        public function parse(ParseContext $context, array $matches, array $offsets): ?BlockNode
1✔
71
        {
72
                [, $mParam, $mMod, $mContent] = $matches;
1✔
73

74
                $mod = Texy\Modifier::parse($mMod);
1✔
75
                $parts = Texy\Regexp::split($mParam, '~\s+~', limit: 2);
1✔
76
                $blocktype = empty($parts[0]) ? Syntax::BlockDefault : 'block/' . $parts[0];
1✔
77
                $param = empty($parts[1]) ? null : $parts[1];
1✔
78

79
                $content = Helpers::outdent($mContent);
1✔
80
                $position = new Position($offsets[0], strlen($matches[0]));
1✔
81

82
                if ($blocktype === Syntax::BlockCode) {
1✔
83
                        return new CodeBlockNode('code', $content, $param, $mod, $position);
1✔
84

85
                } elseif ($blocktype === Syntax::BlockDefault || $blocktype === Syntax::BlockPre) {
1✔
86
                        return new CodeBlockNode($blocktype === Syntax::BlockPre ? 'pre' : 'default', $content, $param, $mod, $position);
1✔
87

88
                } elseif ($blocktype === Syntax::BlockComment) {
1✔
89
                        return new CommentNode($content, $position);
1✔
90

91
                } elseif ($blocktype === Syntax::BlockHtml) {
1✔
92
                        // html/text blocks don't use outdent - preserve original indentation
93
                        return new CodeBlockNode('html', trim($mContent, "\n"), null, $mod, $position);
1✔
94

95
                } elseif ($blocktype === Syntax::BlockText) {
1✔
96
                        // html/text blocks don't use outdent - preserve original indentation
97
                        return new CodeBlockNode('text', trim($mContent, "\n"), null, $mod, $position);
1✔
98

99
                } elseif ($blocktype === Syntax::BlockDiv) {
1✔
100
                        $content = Helpers::outdent($mContent, firstLine: true);
1✔
101
                        if ($content === '') {
1✔
102
                                return null;
×
103
                        }
104
                        return new SectionNode($context->parseBlock($content), 'div', $mod, $position);
1✔
105

106
                } elseif ($blocktype === Syntax::BlockTexySource) {
1✔
107
                        // Store raw texy content, will be parsed and displayed as HTML source in handler
108
                        $content = Helpers::outdent($mContent);
1✔
109
                        if ($content === '') {
1✔
110
                                return null;
×
111
                        }
112
                        return new CodeBlockNode('texysource', $content, null, $mod, $position);
1✔
113
                }
114

115
                return null;
×
116
        }
117
}
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