xref: /dokuwiki/inc/Parsing/ParserMode/GfmBacktickDouble.php (revision 8ed75a23932353c18b43f67323808e9a662f532a)
1*8ed75a23SAndreas Gohr<?php
2*8ed75a23SAndreas Gohr
3*8ed75a23SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4*8ed75a23SAndreas Gohr
5*8ed75a23SAndreas Gohr/**
6*8ed75a23SAndreas Gohr * GFM inline code span bounded by double backticks: ``text``.
7*8ed75a23SAndreas Gohr *
8*8ed75a23SAndreas Gohr * The whole point of the double form is to let users embed literal
9*8ed75a23SAndreas Gohr * backticks in inline code. The input ``foo`bar`` renders as
10*8ed75a23SAndreas Gohr * <code>foo`bar</code> because a lone backtick in the body cannot
11*8ed75a23SAndreas Gohr * form a valid two-backtick closer. Combined with the edge-space
12*8ed75a23SAndreas Gohr * strip rule, you can embed backticks right at the boundaries: the
13*8ed75a23SAndreas Gohr * input `` `foo` `` renders as <code>`foo`</code>.
14*8ed75a23SAndreas Gohr *
15*8ed75a23SAndreas Gohr * Extends GfmBacktickSingle to inherit handle() and normalizeBody;
16*8ed75a23SAndreas Gohr * only the delimiter length and the body character class differ.
17*8ed75a23SAndreas Gohr * Sort and category match the parent so the two modes share one
18*8ed75a23SAndreas Gohr * precedence slot — the (?<!`)...(?!`) guards on both mean the n=1
19*8ed75a23SAndreas Gohr * and n=2 patterns never steal each other's input regardless of
20*8ed75a23SAndreas Gohr * registration order.
21*8ed75a23SAndreas Gohr *
22*8ed75a23SAndreas Gohr * @see GfmBacktickSingle
23*8ed75a23SAndreas Gohr */
24*8ed75a23SAndreas Gohrclass GfmBacktickDouble extends GfmBacktickSingle
25*8ed75a23SAndreas Gohr{
26*8ed75a23SAndreas Gohr    /** @inheritdoc */
27*8ed75a23SAndreas Gohr    protected function getModeName(): string
28*8ed75a23SAndreas Gohr    {
29*8ed75a23SAndreas Gohr        return 'gfm_backtick_double';
30*8ed75a23SAndreas Gohr    }
31*8ed75a23SAndreas Gohr
32*8ed75a23SAndreas Gohr    /**
33*8ed75a23SAndreas Gohr     * Entry pattern. Same shape as the parent but with doubled
34*8ed75a23SAndreas Gohr     * delimiters. The body character class admits either a non-backtick
35*8ed75a23SAndreas Gohr     * or a lone backtick (one that isn't followed by another, so not
36*8ed75a23SAndreas Gohr     * part of a run-of-two) — such a stray backtick cannot form a valid
37*8ed75a23SAndreas Gohr     * n=2 closer.
38*8ed75a23SAndreas Gohr     */
39*8ed75a23SAndreas Gohr    protected function getEntryPattern(): string
40*8ed75a23SAndreas Gohr    {
41*8ed75a23SAndreas Gohr        return '(?<!`)``(?!`)(?='
42*8ed75a23SAndreas Gohr            . '(?:' . self::NOT_AT_PARA_BREAK . '(?:[^`]|`(?!`)))+'
43*8ed75a23SAndreas Gohr            . '(?<!`)``(?!`)'
44*8ed75a23SAndreas Gohr            . ')';
45*8ed75a23SAndreas Gohr    }
46*8ed75a23SAndreas Gohr
47*8ed75a23SAndreas Gohr    /** @inheritdoc */
48*8ed75a23SAndreas Gohr    protected function getExitPattern(): string
49*8ed75a23SAndreas Gohr    {
50*8ed75a23SAndreas Gohr        return '(?<!`)``(?!`)';
51*8ed75a23SAndreas Gohr    }
52*8ed75a23SAndreas Gohr}
53