xref: /dokuwiki/inc/Parsing/ParserMode/GfmEmphasisUnderscore.php (revision 864d6c6daac408fa4fc25801adcaf0f744288b30)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5/**
6 * GFM / CommonMark emphasis via single underscores: `_text_`.
7 *
8 * Only loaded when Markdown is the only or preferred syntax
9 *
10 * Emits emphasis_open / emphasis_close — the same instructions as DokuWiki's
11 * Emphasis (`//`) and GfmEmphasis (`*`), so all three render as <em>.
12 */
13class GfmEmphasisUnderscore extends AbstractFormatting
14{
15    /** @inheritdoc */
16    public function getSort()
17    {
18        return 80;
19    }
20
21    /** @inheritdoc */
22    protected function getModeName(): string
23    {
24        return 'gfm_emphasis_underscore';
25    }
26
27    /** @inheritdoc */
28    protected function getInstructionName(): string
29    {
30        return 'emphasis';
31    }
32
33    /** @inheritdoc */
34    protected function getEntryPattern(): string
35    {
36        // Entry requires a valid closing `_` (non-whitespace char before it AND
37        // NO_WORD_AFTER following). Otherwise emphasis would open with no way
38        // to ever close (or close at an invalid position).
39        return self::NO_WORD_BEFORE
40            . '_(?=[^\s_])'
41            . '(?=' . self::CONTENT_UNTIL_PARA . '[^\s]_' . self::NO_WORD_AFTER . ')';
42    }
43
44    /** @inheritdoc */
45    protected function getExitPattern(): string
46    {
47        return '(?<=[^\s])_' . self::NO_WORD_AFTER;
48    }
49}
50