10a016d04SGreg Roach<?php 23976b470SGreg 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 */ 17*fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 200a016d04SGreg Roachnamespace Fisharebest\Webtrees\CommonMark; 210a016d04SGreg Roach 228d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 230a016d04SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 240a016d04SGreg Roachuse Fisharebest\Webtrees\Tree; 250a016d04SGreg Roachuse League\CommonMark\Inline\Element\Link; 264a1f1d43SGreg Roachuse League\CommonMark\Inline\Parser\InlineParserInterface; 270a016d04SGreg Roachuse League\CommonMark\InlineParserContext; 280a016d04SGreg Roach 290a016d04SGreg Roach/** 300a016d04SGreg Roach * Convert XREFs within markdown text to links 310a016d04SGreg Roach */ 324a1f1d43SGreg Roachclass XrefParser implements InlineParserInterface 33c1010edaSGreg Roach{ 340a016d04SGreg Roach /** @var Tree - match XREFs in this tree */ 350a016d04SGreg Roach private $tree; 360a016d04SGreg Roach 370a016d04SGreg Roach /** 380a016d04SGreg Roach * MarkdownXrefParser constructor. 390a016d04SGreg Roach * 400a016d04SGreg Roach * @param Tree $tree 410a016d04SGreg Roach */ 42c1010edaSGreg Roach public function __construct(Tree $tree) 43c1010edaSGreg Roach { 440a016d04SGreg Roach $this->tree = $tree; 450a016d04SGreg Roach } 460a016d04SGreg Roach 470a016d04SGreg Roach /** 480a016d04SGreg Roach * We are only interested in text that begins with '@'. 490a016d04SGreg Roach * 50cfe766afSGreg Roach * @return string[] 510a016d04SGreg Roach */ 528f53f488SRico Sonntag public function getCharacters(): array 53c1010edaSGreg Roach { 540a016d04SGreg Roach return ['@']; 550a016d04SGreg Roach } 560a016d04SGreg Roach 570a016d04SGreg Roach /** 580a016d04SGreg Roach * @param InlineParserContext $context 590a016d04SGreg Roach * 600a016d04SGreg Roach * @return bool 610a016d04SGreg Roach */ 628f53f488SRico Sonntag public function parse(InlineParserContext $context): bool 63c1010edaSGreg Roach { 640a016d04SGreg Roach // The cursor should be positioned on the opening '@'. 6512c79f74SRico Sonntag $cursor = $context->getCursor(); 660a016d04SGreg Roach 670a016d04SGreg Roach // If this isn't the start of an XREF, we'll need to rewind. 680a016d04SGreg Roach $previous_state = $cursor->saveState(); 690a016d04SGreg Roach 708d0ebef0SGreg Roach $handle = $cursor->match('/@' . Gedcom::REGEX_XREF . '@/'); 710a016d04SGreg Roach if (empty($handle)) { 720a016d04SGreg Roach // Not an XREF? 730a016d04SGreg Roach $cursor->restoreState($previous_state); 740a016d04SGreg Roach 750a016d04SGreg Roach return false; 760a016d04SGreg Roach } 770a016d04SGreg Roach 780a016d04SGreg Roach $xref = trim($handle, '@'); 790a016d04SGreg Roach 800a016d04SGreg Roach $record = GedcomRecord::getInstance($xref, $this->tree); 810a016d04SGreg Roach 820a016d04SGreg Roach if ($record === null) { 830a016d04SGreg Roach // Linked record does not exist? 840a016d04SGreg Roach $cursor->restoreState($previous_state); 850a016d04SGreg Roach 860a016d04SGreg Roach return false; 870a016d04SGreg Roach } 880a016d04SGreg Roach 89b1f1e4efSGreg Roach $url = $record->url(); 900a016d04SGreg Roach $label = $handle; 9139ca88baSGreg Roach $title = strip_tags($record->fullName()); 920a016d04SGreg Roach $context->getContainer()->appendChild(new Link($url, $label, $title)); 930a016d04SGreg Roach 940a016d04SGreg Roach return true; 950a016d04SGreg Roach } 960a016d04SGreg Roach} 97