16f595250SGreg Roach<?php 26f595250SGreg Roach 36f595250SGreg Roach/** 46f595250SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 66f595250SGreg Roach * This program is free software: you can redistribute it and/or modify 76f595250SGreg Roach * it under the terms of the GNU General Public License as published by 86f595250SGreg Roach * the Free Software Foundation, either version 3 of the License, or 96f595250SGreg Roach * (at your option) any later version. 106f595250SGreg Roach * This program is distributed in the hope that it will be useful, 116f595250SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 126f595250SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 136f595250SGreg Roach * GNU General Public License for more details. 146f595250SGreg Roach * You should have received a copy of the GNU General Public License 156f595250SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 166f595250SGreg Roach */ 176f595250SGreg Roach 186f595250SGreg Roachdeclare(strict_types=1); 196f595250SGreg Roach 206f595250SGreg Roachnamespace Fisharebest\Webtrees\CommonMark; 216f595250SGreg Roach 226f595250SGreg Roachuse League\CommonMark\Extension\Table\Table; 236f595250SGreg Roachuse League\CommonMark\Extension\Table\TableCell; 246f595250SGreg Roachuse League\CommonMark\Extension\Table\TableRow; 256f595250SGreg Roachuse League\CommonMark\Extension\Table\TableSection; 266f595250SGreg Roachuse League\CommonMark\Node\Block\AbstractBlock; 276f595250SGreg Roachuse League\CommonMark\Node\Inline\Text; 286f595250SGreg Roachuse League\CommonMark\Parser\Block\AbstractBlockContinueParser; 296f595250SGreg Roachuse League\CommonMark\Parser\Block\BlockContinue; 306f595250SGreg Roachuse League\CommonMark\Parser\Block\BlockContinueParserInterface; 316f595250SGreg Roachuse League\CommonMark\Parser\Cursor; 326f595250SGreg Roach 336f595250SGreg Roachuse function array_map; 346f595250SGreg Roachuse function explode; 356f595250SGreg Roachuse function str_starts_with; 366f595250SGreg Roachuse function strlen; 376f595250SGreg Roachuse function substr; 386f595250SGreg Roach 396f595250SGreg Roach/** 406f595250SGreg Roach * Convert webtrees 1.x census-assistant markup into tables. 416f595250SGreg Roach * Note that webtrees 2.0 generates markdown tables directly. 426f595250SGreg Roach */ 436f595250SGreg Roachclass CensusTableContinueParser extends AbstractBlockContinueParser 446f595250SGreg Roach{ 456f595250SGreg Roach private Table $table; 466f595250SGreg Roach 476f595250SGreg Roach private TableSection $thead; 486f595250SGreg Roach 496f595250SGreg Roach private TableSection $tbody; 506f595250SGreg Roach 516f595250SGreg Roach /** 526f595250SGreg Roach * Constructor 536f595250SGreg Roach */ 546f595250SGreg Roach public function __construct() 556f595250SGreg Roach { 566f595250SGreg Roach $this->table = new Table(); 576f595250SGreg Roach $this->thead = new TableSection(TableSection::TYPE_HEAD); 586f595250SGreg Roach $this->tbody = new TableSection(TableSection::TYPE_BODY); 596f595250SGreg Roach $this->table->appendChild($this->thead); 606f595250SGreg Roach $this->table->appendChild($this->tbody); 616f595250SGreg Roach } 626f595250SGreg Roach 636f595250SGreg Roach /** 646f595250SGreg Roach * @param Cursor $cursor 656f595250SGreg Roach * @param BlockContinueParserInterface $activeBlockParser 666f595250SGreg Roach * 676f595250SGreg Roach * @return BlockContinue|null 686f595250SGreg Roach */ 69*1ff45046SGreg Roach public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): BlockContinue|null 706f595250SGreg Roach { 716f595250SGreg Roach $line = $cursor->getLine(); 726f595250SGreg Roach 736f595250SGreg Roach if ($line === CensusTableExtension::CA_SUFFIX) { 746f595250SGreg Roach return BlockContinue::finished(); 756f595250SGreg Roach } 766f595250SGreg Roach 776f595250SGreg Roach // Blank line before the suffix is an error. 786f595250SGreg Roach if ($line === '') { 7912e45925SGreg Roach return BlockContinue::none(); 806f595250SGreg Roach } 816f595250SGreg Roach 826f595250SGreg Roach $cells = explode('|', $line); 836f595250SGreg Roach 846f595250SGreg Roach $callback = static function (string $text): string { 856f595250SGreg Roach if (str_starts_with($text, CensusTableExtension::TH_PREFIX)) { 866f595250SGreg Roach return substr($text, strlen(CensusTableExtension::TH_PREFIX)); 876f595250SGreg Roach } 886f595250SGreg Roach 896f595250SGreg Roach return $text; 906f595250SGreg Roach }; 916f595250SGreg Roach 926f595250SGreg Roach $tr = new TableRow(); 936f595250SGreg Roach 946f595250SGreg Roach if (empty($this->thead->children())) { 956f595250SGreg Roach $cells = array_map($callback, $cells); 966f595250SGreg Roach 976f595250SGreg Roach foreach ($cells as $cell) { 986f595250SGreg Roach $table_cell = new TableCell(TableCell::TYPE_HEADER); 996f595250SGreg Roach $table_cell->appendChild(new Text($cell)); 1006f595250SGreg Roach $tr->appendChild($table_cell); 1016f595250SGreg Roach } 1026f595250SGreg Roach 1036f595250SGreg Roach $this->thead->appendChild($tr); 1046f595250SGreg Roach } else { 1056f595250SGreg Roach foreach ($cells as $cell) { 1066f595250SGreg Roach $table_cell = new TableCell(TableCell::TYPE_DATA); 1076f595250SGreg Roach $table_cell->appendChild(new Text($cell)); 1086f595250SGreg Roach $tr->appendChild($table_cell); 1096f595250SGreg Roach } 1106f595250SGreg Roach 1116f595250SGreg Roach $this->tbody->appendChild($tr); 1126f595250SGreg Roach } 1136f595250SGreg Roach 1146f595250SGreg Roach return BlockContinue::at($cursor); 1156f595250SGreg Roach } 1166f595250SGreg Roach 1176f595250SGreg Roach /** 1186f595250SGreg Roach * @return Table 1196f595250SGreg Roach */ 1206f595250SGreg Roach public function getBlock(): AbstractBlock 1216f595250SGreg Roach { 1226f595250SGreg Roach return $this->table; 1236f595250SGreg Roach } 1246f595250SGreg Roach} 125