xref: /webtrees/app/CommonMark/XrefParser.php (revision cfe766af61e4d860f17afd91dc1b2e538caffa79)
10a016d04SGreg Roach<?php
20a016d04SGreg Roach/**
30a016d04SGreg Roach * webtrees: online genealogy
41062a142SGreg 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 */
27c1010edaSGreg Roachclass XrefParser extends AbstractInlineParser
28c1010edaSGreg Roach{
290a016d04SGreg Roach    /** @var Tree - match XREFs in this tree */
300a016d04SGreg Roach    private $tree;
310a016d04SGreg Roach
320a016d04SGreg Roach    /**
330a016d04SGreg Roach     * MarkdownXrefParser constructor.
340a016d04SGreg Roach     *
350a016d04SGreg Roach     * @param Tree $tree
360a016d04SGreg Roach     */
37c1010edaSGreg Roach    public function __construct(Tree $tree)
38c1010edaSGreg Roach    {
390a016d04SGreg Roach        $this->tree = $tree;
400a016d04SGreg Roach    }
410a016d04SGreg Roach
420a016d04SGreg Roach    /**
430a016d04SGreg Roach     * We are only interested in text that begins with '@'.
440a016d04SGreg Roach     *
45*cfe766afSGreg Roach     * @return string[]
460a016d04SGreg Roach     */
478f53f488SRico Sonntag    public function getCharacters(): array
48c1010edaSGreg Roach    {
490a016d04SGreg Roach        return ['@'];
500a016d04SGreg Roach    }
510a016d04SGreg Roach
520a016d04SGreg Roach    /**
530a016d04SGreg Roach     * @param InlineParserContext $context
540a016d04SGreg Roach     *
550a016d04SGreg Roach     * @return bool
560a016d04SGreg Roach     */
578f53f488SRico Sonntag    public function parse(InlineParserContext $context): bool
58c1010edaSGreg Roach    {
590a016d04SGreg Roach        // The cursor should be positioned on the opening '@'.
6012c79f74SRico Sonntag        $cursor = $context->getCursor();
610a016d04SGreg Roach
620a016d04SGreg Roach        // If this isn't the start of an XREF, we'll need to rewind.
630a016d04SGreg Roach        $previous_state = $cursor->saveState();
640a016d04SGreg Roach
650a016d04SGreg Roach        $handle = $cursor->match('/@' . WT_REGEX_XREF . '@/');
660a016d04SGreg Roach        if (empty($handle)) {
670a016d04SGreg Roach            // Not an XREF?
680a016d04SGreg Roach            $cursor->restoreState($previous_state);
690a016d04SGreg Roach
700a016d04SGreg Roach            return false;
710a016d04SGreg Roach        }
720a016d04SGreg Roach
730a016d04SGreg Roach        $xref = trim($handle, '@');
740a016d04SGreg Roach
750a016d04SGreg Roach        $record = GedcomRecord::getInstance($xref, $this->tree);
760a016d04SGreg Roach
770a016d04SGreg Roach        if ($record === null) {
780a016d04SGreg Roach            // Linked record does not exist?
790a016d04SGreg Roach            $cursor->restoreState($previous_state);
800a016d04SGreg Roach
810a016d04SGreg Roach            return false;
820a016d04SGreg Roach        }
830a016d04SGreg Roach
84b1f1e4efSGreg Roach        $url   = $record->url();
850a016d04SGreg Roach        $label = $handle;
860a016d04SGreg Roach        $title = strip_tags($record->getFullName());
870a016d04SGreg Roach        $context->getContainer()->appendChild(new Link($url, $label, $title));
880a016d04SGreg Roach
890a016d04SGreg Roach        return true;
900a016d04SGreg Roach    }
910a016d04SGreg Roach}
92