10a016d04SGreg Roach<?php 20a016d04SGreg Roach/** 30a016d04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 50a016d04SGreg Roach * This program is free software: you can redistribute it and/or modify 60a016d04SGreg Roach * it under the terms of the GNU General Public License as published by 70a016d04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 80a016d04SGreg Roach * (at your option) any later version. 90a016d04SGreg Roach * This program is distributed in the hope that it will be useful, 100a016d04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 110a016d04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 120a016d04SGreg Roach * GNU General Public License for more details. 130a016d04SGreg Roach * You should have received a copy of the GNU General Public License 140a016d04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 150a016d04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 180a016d04SGreg Roachnamespace Fisharebest\Webtrees\CommonMark; 190a016d04SGreg Roach 208d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 210a016d04SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 220a016d04SGreg Roachuse Fisharebest\Webtrees\Tree; 230a016d04SGreg Roachuse League\CommonMark\Inline\Element\Link; 240a016d04SGreg Roachuse League\CommonMark\Inline\Parser\AbstractInlineParser; 250a016d04SGreg Roachuse League\CommonMark\InlineParserContext; 260a016d04SGreg Roach 270a016d04SGreg Roach/** 280a016d04SGreg Roach * Convert XREFs within markdown text to links 290a016d04SGreg Roach */ 30c1010edaSGreg Roachclass XrefParser extends AbstractInlineParser 31c1010edaSGreg Roach{ 320a016d04SGreg Roach /** @var Tree - match XREFs in this tree */ 330a016d04SGreg Roach private $tree; 340a016d04SGreg Roach 350a016d04SGreg Roach /** 360a016d04SGreg Roach * MarkdownXrefParser constructor. 370a016d04SGreg Roach * 380a016d04SGreg Roach * @param Tree $tree 390a016d04SGreg Roach */ 40c1010edaSGreg Roach public function __construct(Tree $tree) 41c1010edaSGreg Roach { 420a016d04SGreg Roach $this->tree = $tree; 430a016d04SGreg Roach } 440a016d04SGreg Roach 450a016d04SGreg Roach /** 460a016d04SGreg Roach * We are only interested in text that begins with '@'. 470a016d04SGreg Roach * 48cfe766afSGreg Roach * @return string[] 490a016d04SGreg Roach */ 508f53f488SRico Sonntag public function getCharacters(): array 51c1010edaSGreg Roach { 520a016d04SGreg Roach return ['@']; 530a016d04SGreg Roach } 540a016d04SGreg Roach 550a016d04SGreg Roach /** 560a016d04SGreg Roach * @param InlineParserContext $context 570a016d04SGreg Roach * 580a016d04SGreg Roach * @return bool 590a016d04SGreg Roach */ 608f53f488SRico Sonntag public function parse(InlineParserContext $context): bool 61c1010edaSGreg Roach { 620a016d04SGreg Roach // The cursor should be positioned on the opening '@'. 6312c79f74SRico Sonntag $cursor = $context->getCursor(); 640a016d04SGreg Roach 650a016d04SGreg Roach // If this isn't the start of an XREF, we'll need to rewind. 660a016d04SGreg Roach $previous_state = $cursor->saveState(); 670a016d04SGreg Roach 688d0ebef0SGreg Roach $handle = $cursor->match('/@' . Gedcom::REGEX_XREF . '@/'); 690a016d04SGreg Roach if (empty($handle)) { 700a016d04SGreg Roach // Not an XREF? 710a016d04SGreg Roach $cursor->restoreState($previous_state); 720a016d04SGreg Roach 730a016d04SGreg Roach return false; 740a016d04SGreg Roach } 750a016d04SGreg Roach 760a016d04SGreg Roach $xref = trim($handle, '@'); 770a016d04SGreg Roach 780a016d04SGreg Roach $record = GedcomRecord::getInstance($xref, $this->tree); 790a016d04SGreg Roach 800a016d04SGreg Roach if ($record === null) { 810a016d04SGreg Roach // Linked record does not exist? 820a016d04SGreg Roach $cursor->restoreState($previous_state); 830a016d04SGreg Roach 840a016d04SGreg Roach return false; 850a016d04SGreg Roach } 860a016d04SGreg Roach 87b1f1e4efSGreg Roach $url = $record->url(); 880a016d04SGreg Roach $label = $handle; 89*39ca88baSGreg Roach $title = strip_tags($record->fullName()); 900a016d04SGreg Roach $context->getContainer()->appendChild(new Link($url, $label, $title)); 910a016d04SGreg Roach 920a016d04SGreg Roach return true; 930a016d04SGreg Roach } 940a016d04SGreg Roach} 95