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

dg / texy / 13105427993

03 Feb 2025 02:50AM UTC coverage: 92.32% (-0.003%) from 92.323%
13105427993

push

github

dg
released 3.2.4

2368 of 2565 relevant lines covered (92.32%)

0.92 hits per line

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

94.0
/src/Texy/Modules/FigureModule.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 Texy\Patterns;
14

15

16
/**
17
 * The captioned figures.
18
 */
19
final class FigureModule extends Texy\Module
20
{
21
        public string $tagName = 'div';
22

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

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

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

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

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

38

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

46

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

59

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

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

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

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

93

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

107
                $hAlign = $image->modifier->hAlign;
1✔
108
                $image->modifier->hAlign = null;
1✔
109

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

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

120
                $mod->decorate($texy, $el);
1✔
121

122
                $el[0] = $elImg;
1✔
123

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

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

135
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
136
                                $el->attrs['style']['float'] = $hAlign;
1✔
137

138
                        } else {
139
                                $class .= '-' . $texy->alignClasses[$hAlign];
×
140
                        }
141
                }
142

143
                $el->attrs['class'][] = $class;
1✔
144

145
                return $el;
1✔
146
        }
147
}
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