xref: /webtrees/app/CommonMark/XrefParser.php (revision 1f3fb95c905e623518962b025974a0e6c19d068b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\CommonMark;
17
18use Fisharebest\Webtrees\GedcomRecord;
19use Fisharebest\Webtrees\Tree;
20use League\CommonMark\Inline\Element\Link;
21use League\CommonMark\Inline\Parser\AbstractInlineParser;
22use League\CommonMark\InlineParserContext;
23
24/**
25 * Convert XREFs within markdown text to links
26 */
27class XrefParser extends AbstractInlineParser {
28	/** @var Tree - match XREFs in this tree */
29	private $tree;
30
31	/**
32	 * MarkdownXrefParser constructor.
33	 *
34	 * @param Tree $tree
35	 */
36	public function __construct(Tree $tree) {
37		$this->tree = $tree;
38	}
39
40	/**
41	 * We are only interested in text that begins with '@'.
42	 *
43	 * @return array
44	 */
45	public function getCharacters() {
46		return ['@'];
47	}
48
49	/**
50	 * @param InlineParserContext $context
51	 *
52	 * @return bool
53	 */
54	public function parse(InlineParserContext $context) {
55		// The cursor should be positioned on the opening '@'.
56		$cursor = $context->getcursor();
57
58		// If this isn't the start of an XREF, we'll need to rewind.
59		$previous_state = $cursor->saveState();
60
61		$handle = $cursor->match('/@' . WT_REGEX_XREF . '@/');
62		if (empty($handle)) {
63			// Not an XREF?
64			$cursor->restoreState($previous_state);
65
66			return false;
67		}
68
69		$xref = trim($handle, '@');
70
71		$record = GedcomRecord::getInstance($xref, $this->tree);
72
73		if ($record === null) {
74			// Linked record does not exist?
75			$cursor->restoreState($previous_state);
76
77			return false;
78		}
79
80		$url   = $record->getRawUrl();
81		$label = $handle;
82		$title = strip_tags($record->getFullName());
83		$context->getContainer()->appendChild(new Link($url, $label, $title));
84
85		return true;
86	}
87}
88