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