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

dg / texy / 21344532034

26 Jan 2026 02:43AM UTC coverage: 91.98% (-0.4%) from 92.376%
21344532034

push

github

dg
added CLAUDE.md

2397 of 2606 relevant lines covered (91.98%)

0.92 hits per line

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

92.45
/src/Texy/Modules/FigureModule.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\Patterns;
14
use function trim;
15

16

17
/**
18
 * Processes images with captions.
19
 */
20
final class FigureModule extends Texy\Module
21
{
22
        public string $tagName = 'div';
23

24
        /** non-floated box CSS class */
25
        public ?string $class = 'figure';
26

27
        /** left-floated box CSS class */
28
        public ?string $leftClass = null;
29

30
        /** right-floated box CSS class */
31
        public ?string $rightClass = null;
32

33
        /** how calculate div's width */
34
        public int|false $widthDelta = 10;
35

36
        /** caption after *** is required */
37
        public bool $requireCaption = true;
38

39

40
        public function __construct(Texy\Texy $texy)
1✔
41
        {
42
                $this->texy = $texy;
1✔
43
                $texy->addHandler('figure', $this->solve(...));
1✔
44
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
45
        }
1✔
46

47

48
        private function beforeParse(): void
49
        {
50
                $this->texy->registerBlockPattern(
1✔
51
                        $this->pattern(...),
1✔
52
                        '#^(?>\[\*\ *+([^\n' . Patterns::MARK . ']{1,1000})' . Patterns::MODIFIER . '?\ *+(\*|(?<!<)>|<)\])' // [* urls .(title)[class]{style} >]
53
                        . '(?::(' . Patterns::LINK_URL . '|:))??'
54
                        . '(?:\ ++\*\*\*\ ++(.{0,2000}))' . ($this->requireCaption ? '' : '?')
1✔
55
                        . Patterns::MODIFIER_H . '?()$#mUu',
1✔
56
                        'figure',
1✔
57
                );
58
        }
1✔
59

60

61
        /**
62
         * Callback for [*image*]:link *** .... .(title)[class]{style}>.
63
         * @param  string[]  $matches
64
         */
65
        public function pattern(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
66
        {
67
                [, $mURLs, $mImgMod, $mAlign, $mLink, $mContent, $mMod] = $matches;
1✔
68
                // [1] => URLs
69
                // [2] => .(title)[class]{style}<>
70
                // [3] => * < >
71
                // [4] => url | [ref] | [*image*]
72
                // [5] => ...
73
                // [6] => .(title)[class]{style}<>
74

75
                $texy = $this->texy;
1✔
76
                $image = $texy->imageModule->factoryImage($mURLs, $mImgMod . $mAlign);
1✔
77
                $mod = new Texy\Modifier($mMod);
1✔
78
                $mContent = trim($mContent);
1✔
79

80
                if ($mLink) {
1✔
81
                        if ($mLink === ':') {
1✔
82
                                $link = new Texy\Link($image->linkedURL ?? $image->URL);
1✔
83
                                $link->raw = ':';
1✔
84
                                $link->type = $link::IMAGE;
1✔
85
                        } else {
86
                                $link = $texy->linkModule->factoryLink($mLink, null, null);
1✔
87
                        }
88
                } else {
89
                        $link = null;
1✔
90
                }
91

92
                return $texy->invokeAroundHandlers('figure', $parser, [$image, $link, $mContent, $mod]);
1✔
93
        }
94

95

96
        /**
97
         * Finish invocation.
98
         */
99
        private function solve(
1✔
100
                Texy\HandlerInvocation $invocation,
101
                Texy\Image $image,
102
                ?Texy\Link $link,
103
                string $content,
104
                Texy\Modifier $mod,
105
        ): ?Texy\HtmlElement
106
        {
107
                $texy = $this->texy;
1✔
108

109
                $hAlign = $image->modifier->hAlign;
1✔
110
                $image->modifier->hAlign = null;
1✔
111

112
                $elImg = $texy->imageModule->solve(null, $image, $link); // returns Texy\HtmlElement or null!
1✔
113
                if (!$elImg) {
1✔
114
                        return null;
×
115
                }
116

117
                $el = new Texy\HtmlElement($this->tagName);
1✔
118
                if (!empty($image->width) && $this->widthDelta !== false) {
1✔
119
                        $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
×
120
                        $el->attrs['style']['max-width'] = ($image->width + $this->widthDelta) . 'px';
×
121
                }
122

123
                $mod->decorate($texy, $el);
1✔
124

125
                $el->add($elImg);
1✔
126

127
                if ($content !== '') {
1✔
128
                        $el[1] = new Texy\HtmlElement($this->tagName === 'figure' ? 'figcaption' : 'p');
1✔
129
                        $el[1]->parseLine($texy, $content);
1✔
130
                }
131

132
                $class = $this->class;
1✔
133
                if ($hAlign) {
1✔
134
                        $var = $hAlign . 'Class'; // leftClass, rightClass
1✔
135
                        if (!empty($this->$var)) {
1✔
136
                                $class = $this->$var;
1✔
137

138
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
139
                                $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
1✔
140
                                $el->attrs['style']['float'] = $hAlign;
1✔
141

142
                        } else {
143
                                $class .= '-' . $texy->alignClasses[$hAlign];
×
144
                        }
145
                }
146

147
                $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
148
                $el->attrs['class'][] = $class;
1✔
149

150
                return $el;
1✔
151
        }
152
}
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