xref: /dokuwiki/inc/Parsing/ParserMode/AbstractFormatting.php (revision 1f44347694a1b2d0182a0aaa0ca79ba9930f1d2a)
1*1f443476SAndreas Gohr<?php
2*1f443476SAndreas Gohr
3*1f443476SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4*1f443476SAndreas Gohr
5*1f443476SAndreas Gohruse dokuwiki\Parsing\ModeRegistry;
6*1f443476SAndreas Gohr
7*1f443476SAndreas Gohr/**
8*1f443476SAndreas Gohr * Base class for inline formatting modes (bold, italic, underline, etc.)
9*1f443476SAndreas Gohr *
10*1f443476SAndreas Gohr * Each concrete subclass defines its entry/exit patterns, mode name, and sort order.
11*1f443476SAndreas Gohr */
12*1f443476SAndreas Gohrabstract class AbstractFormatting extends AbstractMode
13*1f443476SAndreas Gohr{
14*1f443476SAndreas Gohr    /**
15*1f443476SAndreas Gohr     * @return string The regex pattern that starts this formatting
16*1f443476SAndreas Gohr     */
17*1f443476SAndreas Gohr    abstract protected function getEntryPattern(): string;
18*1f443476SAndreas Gohr
19*1f443476SAndreas Gohr    /**
20*1f443476SAndreas Gohr     * @return string The regex pattern that ends this formatting
21*1f443476SAndreas Gohr     */
22*1f443476SAndreas Gohr    abstract protected function getExitPattern(): string;
23*1f443476SAndreas Gohr
24*1f443476SAndreas Gohr    /**
25*1f443476SAndreas Gohr     * @return string The mode name used for lexer registration
26*1f443476SAndreas Gohr     */
27*1f443476SAndreas Gohr    abstract protected function getModeName(): string;
28*1f443476SAndreas Gohr
29*1f443476SAndreas Gohr    /**
30*1f443476SAndreas Gohr     * Constructor. Sets up allowed modes for this formatting type.
31*1f443476SAndreas Gohr     *
32*1f443476SAndreas Gohr     * Formatting modes accept other formatting, substitutions, and disabled modes,
33*1f443476SAndreas Gohr     * but exclude themselves to prevent self-nesting (e.g. bold inside bold).
34*1f443476SAndreas Gohr     */
35*1f443476SAndreas Gohr    public function __construct()
36*1f443476SAndreas Gohr    {
37*1f443476SAndreas Gohr        $self = $this->getModeName();
38*1f443476SAndreas Gohr        $this->allowedModes = array_filter(
39*1f443476SAndreas Gohr            ModeRegistry::getInstance()->getModesForCategories([
40*1f443476SAndreas Gohr                ModeRegistry::CATEGORY_FORMATTING,
41*1f443476SAndreas Gohr                ModeRegistry::CATEGORY_SUBSTITION,
42*1f443476SAndreas Gohr                ModeRegistry::CATEGORY_DISABLED,
43*1f443476SAndreas Gohr            ]),
44*1f443476SAndreas Gohr            static fn($mode) => $mode !== $self
45*1f443476SAndreas Gohr        );
46*1f443476SAndreas Gohr    }
47*1f443476SAndreas Gohr
48*1f443476SAndreas Gohr    /** @inheritdoc */
49*1f443476SAndreas Gohr    public function connectTo($mode)
50*1f443476SAndreas Gohr    {
51*1f443476SAndreas Gohr        // Can't nest formatting in itself
52*1f443476SAndreas Gohr        if ($mode === $this->getModeName()) {
53*1f443476SAndreas Gohr            return;
54*1f443476SAndreas Gohr        }
55*1f443476SAndreas Gohr
56*1f443476SAndreas Gohr        $this->Lexer->addEntryPattern(
57*1f443476SAndreas Gohr            $this->getEntryPattern(),
58*1f443476SAndreas Gohr            $mode,
59*1f443476SAndreas Gohr            $this->getModeName()
60*1f443476SAndreas Gohr        );
61*1f443476SAndreas Gohr    }
62*1f443476SAndreas Gohr
63*1f443476SAndreas Gohr    /** @inheritdoc */
64*1f443476SAndreas Gohr    public function postConnect()
65*1f443476SAndreas Gohr    {
66*1f443476SAndreas Gohr        $this->Lexer->addExitPattern(
67*1f443476SAndreas Gohr            $this->getExitPattern(),
68*1f443476SAndreas Gohr            $this->getModeName()
69*1f443476SAndreas Gohr        );
70*1f443476SAndreas Gohr    }
71*1f443476SAndreas Gohr}
72