xref: /webtrees/app/CommonMark/XrefParser.php (revision 1062a1429914c995339f502856821457aa975a5a)
10a016d04SGreg Roach<?php
20a016d04SGreg Roach/**
30a016d04SGreg Roach * webtrees: online genealogy
4*1062a142SGreg Roach * Copyright (C) 2018 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 */
160a016d04SGreg Roachnamespace Fisharebest\Webtrees\CommonMark;
170a016d04SGreg Roach
180a016d04SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
190a016d04SGreg Roachuse Fisharebest\Webtrees\Tree;
200a016d04SGreg Roachuse League\CommonMark\Inline\Element\Link;
210a016d04SGreg Roachuse League\CommonMark\Inline\Parser\AbstractInlineParser;
220a016d04SGreg Roachuse League\CommonMark\InlineParserContext;
230a016d04SGreg Roach
240a016d04SGreg Roach/**
250a016d04SGreg Roach * Convert XREFs within markdown text to links
260a016d04SGreg Roach */
270a016d04SGreg Roachclass XrefParser extends AbstractInlineParser {
280a016d04SGreg Roach	/** @var Tree - match XREFs in this tree */
290a016d04SGreg Roach	private $tree;
300a016d04SGreg Roach
310a016d04SGreg Roach	/**
320a016d04SGreg Roach	 * MarkdownXrefParser constructor.
330a016d04SGreg Roach	 *
340a016d04SGreg Roach	 * @param Tree $tree
350a016d04SGreg Roach	 */
360a016d04SGreg Roach	public function __construct(Tree $tree) {
370a016d04SGreg Roach		$this->tree = $tree;
380a016d04SGreg Roach	}
390a016d04SGreg Roach
400a016d04SGreg Roach	/**
410a016d04SGreg Roach	 * We are only interested in text that begins with '@'.
420a016d04SGreg Roach	 *
430a016d04SGreg Roach	 * @return array
440a016d04SGreg Roach	 */
450a016d04SGreg Roach	public function getCharacters() {
460a016d04SGreg Roach		return ['@'];
470a016d04SGreg Roach	}
480a016d04SGreg Roach
490a016d04SGreg Roach	/**
500a016d04SGreg Roach	 * @param InlineParserContext $context
510a016d04SGreg Roach	 *
520a016d04SGreg Roach	 * @return bool
530a016d04SGreg Roach	 */
540a016d04SGreg Roach	public function parse(InlineParserContext $context) {
550a016d04SGreg Roach		// The cursor should be positioned on the opening '@'.
560a016d04SGreg Roach		$cursor = $context->getcursor();
570a016d04SGreg Roach
580a016d04SGreg Roach		// If this isn't the start of an XREF, we'll need to rewind.
590a016d04SGreg Roach		$previous_state = $cursor->saveState();
600a016d04SGreg Roach
610a016d04SGreg Roach		$handle = $cursor->match('/@' . WT_REGEX_XREF . '@/');
620a016d04SGreg Roach		if (empty($handle)) {
630a016d04SGreg Roach			// Not an XREF?
640a016d04SGreg Roach			$cursor->restoreState($previous_state);
650a016d04SGreg Roach
660a016d04SGreg Roach			return false;
670a016d04SGreg Roach		}
680a016d04SGreg Roach
690a016d04SGreg Roach		$xref = trim($handle, '@');
700a016d04SGreg Roach
710a016d04SGreg Roach		$record = GedcomRecord::getInstance($xref, $this->tree);
720a016d04SGreg Roach
730a016d04SGreg Roach		if ($record === null) {
740a016d04SGreg Roach			// Linked record does not exist?
750a016d04SGreg Roach			$cursor->restoreState($previous_state);
760a016d04SGreg Roach
770a016d04SGreg Roach			return false;
780a016d04SGreg Roach		}
790a016d04SGreg Roach
80b1f1e4efSGreg Roach		$url   = $record->url();
810a016d04SGreg Roach		$label = $handle;
820a016d04SGreg Roach		$title = strip_tags($record->getFullName());
830a016d04SGreg Roach		$context->getContainer()->appendChild(new Link($url, $label, $title));
840a016d04SGreg Roach
850a016d04SGreg Roach		return true;
860a016d04SGreg Roach	}
870a016d04SGreg Roach}
88