Issues (2564)

app/CommonMark/CensusTableContinueParser.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\CommonMark;
21
22
use League\CommonMark\Extension\Table\Table;
23
use League\CommonMark\Extension\Table\TableCell;
24
use League\CommonMark\Extension\Table\TableRow;
25
use League\CommonMark\Extension\Table\TableSection;
26
use League\CommonMark\Node\Block\AbstractBlock;
27
use League\CommonMark\Node\Inline\Text;
28
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
29
use League\CommonMark\Parser\Block\BlockContinue;
30
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
31
use League\CommonMark\Parser\Cursor;
32
33
use function array_map;
34
use function explode;
35
use function str_starts_with;
36
use function strlen;
37
use function substr;
38
39
/**
40
 * Convert webtrees 1.x census-assistant markup into tables.
41
 * Note that webtrees 2.0 generates markdown tables directly.
42
 */
43
class CensusTableContinueParser extends AbstractBlockContinueParser
44
{
45
    private Table $table;
46
47
    private TableSection $thead;
48
49
    private TableSection $tbody;
50
51
    /**
52
     * Constructor
53
     */
54
    public function __construct()
55
    {
56
        $this->table = new Table();
57
        $this->thead = new TableSection(TableSection::TYPE_HEAD);
58
        $this->tbody = new TableSection(TableSection::TYPE_BODY);
59
        $this->table->appendChild($this->thead);
60
        $this->table->appendChild($this->tbody);
61
    }
62
63
    /**
64
     * @param Cursor                       $cursor
65
     * @param BlockContinueParserInterface $activeBlockParser
66
     *
67
     * @return BlockContinue|null
68
     */
69
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): BlockContinue|null
70
    {
71
        $line = $cursor->getLine();
72
73
        if ($line === CensusTableExtension::CA_SUFFIX) {
0 ignored issues
show
The type Fisharebest\Webtrees\Com...rk\CensusTableExtension was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
            return BlockContinue::finished();
75
        }
76
77
        // Blank line before the suffix is an error.
78
        if ($line === '') {
79
            return BlockContinue::none();
80
        }
81
82
        $cells = explode('|', $line);
83
84
        $callback = static function (string $text): string {
85
            if (str_starts_with($text, CensusTableExtension::TH_PREFIX)) {
86
                return substr($text, strlen(CensusTableExtension::TH_PREFIX));
87
            }
88
89
            return $text;
90
        };
91
92
        $tr = new TableRow();
93
94
        if (empty($this->thead->children())) {
95
            $cells = array_map($callback, $cells);
96
97
            foreach ($cells as $cell) {
98
                $table_cell = new TableCell(TableCell::TYPE_HEADER);
99
                $table_cell->appendChild(new Text($cell));
100
                $tr->appendChild($table_cell);
101
            }
102
103
            $this->thead->appendChild($tr);
104
        } else {
105
            foreach ($cells as $cell) {
106
                $table_cell = new TableCell(TableCell::TYPE_DATA);
107
                $table_cell->appendChild(new Text($cell));
108
                $tr->appendChild($table_cell);
109
            }
110
111
            $this->tbody->appendChild($tr);
112
        }
113
114
        return BlockContinue::at($cursor);
115
    }
116
117
    /**
118
     * @return Table
119
     */
120
    public function getBlock(): AbstractBlock
121
    {
122
        return $this->table;
123
    }
124
}
125