xref: /webtrees/app/CommonMark/XrefParser.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
10a016d04SGreg Roach<?php
23976b470SGreg Roach
30a016d04SGreg Roach/**
40a016d04SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
160a016d04SGreg Roach */
17fcfa147eSGreg 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;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
250a016d04SGreg Roachuse Fisharebest\Webtrees\Tree;
266f595250SGreg Roachuse League\CommonMark\Parser\Inline\InlineParserInterface;
276f595250SGreg Roachuse League\CommonMark\Parser\Inline\InlineParserMatch;
286f595250SGreg Roachuse League\CommonMark\Parser\InlineParserContext;
29eec99f1aSGreg Roach
300a016d04SGreg Roach/**
310a016d04SGreg Roach * Convert XREFs within markdown text to links
320a016d04SGreg Roach */
334a1f1d43SGreg Roachclass XrefParser implements InlineParserInterface
34c1010edaSGreg Roach{
35c4943cffSGreg Roach    private Tree $tree;
360a016d04SGreg Roach
370a016d04SGreg Roach    /**
38c4943cffSGreg Roach     * @param Tree $tree Match XREFs in this tree
390a016d04SGreg Roach     */
40c1010edaSGreg Roach    public function __construct(Tree $tree)
41c1010edaSGreg Roach    {
420a016d04SGreg Roach        $this->tree = $tree;
430a016d04SGreg Roach    }
440a016d04SGreg Roach
450a016d04SGreg Roach    /**
460a016d04SGreg Roach     * We are only interested in text that begins with '@'.
470a016d04SGreg Roach     *
486f595250SGreg Roach     * @return InlineParserMatch
490a016d04SGreg Roach     */
506f595250SGreg Roach    public function getMatchDefinition(): InlineParserMatch
51c1010edaSGreg Roach    {
526f595250SGreg Roach        return InlineParserMatch::regex('@(' . Gedcom::REGEX_XREF . ')@');
530a016d04SGreg Roach    }
540a016d04SGreg Roach
550a016d04SGreg Roach    /**
5656d47542SGreg Roach     * @param InlineParserContext $inlineContext
570a016d04SGreg Roach     *
580a016d04SGreg Roach     * @return bool
590a016d04SGreg Roach     */
6056d47542SGreg Roach    public function parse(InlineParserContext $inlineContext): bool
61c1010edaSGreg Roach    {
6256d47542SGreg Roach        $cursor = $inlineContext->getCursor();
636f595250SGreg Roach        [$xref] = $inlineContext->getSubMatches();
646b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $this->tree);
650a016d04SGreg Roach
66eec99f1aSGreg Roach        if ($record instanceof GedcomRecord) {
676f595250SGreg Roach            $cursor->advanceBy($inlineContext->getFullMatchLength());
686f595250SGreg Roach
6956d47542SGreg Roach            $inlineContext->getContainer()->appendChild(new XrefNode($record));
70eec99f1aSGreg Roach
71eec99f1aSGreg Roach            return true;
72eec99f1aSGreg Roach        }
730a016d04SGreg Roach
740a016d04SGreg Roach        return false;
750a016d04SGreg Roach    }
760a016d04SGreg Roach}
77