1<?php
2/**
3 * DokuWiki Syntax Plugin Related.
4 *
5 */
6
7use ComboStrap\ExceptionCompile;
8use ComboStrap\LogUtility;
9use ComboStrap\LinkMarkup;
10use ComboStrap\MarkupPath;
11use ComboStrap\PluginUtility;
12use ComboStrap\Site;
13use ComboStrap\SiteConfig;
14use ComboStrap\Tag\RelatedTag;
15use ComboStrap\TagAttributes;
16
17
18require_once(DOKU_INC . 'inc/parserutils.php');
19
20/**
21 * All DokuWiki plugins to extend the parser/rendering mechanism
22 * need to inherit from this class
23 *
24 * The name of the class must follow a pattern (don't change it)
25 *
26 * The index and the metadata key for backlinks is  called 'relation_references'
27 * It's the key value that you need to pass in the {@link lookupKey} of the {@link \dokuwiki\Search\Indexer}
28 *
29 * Type of conf[index]/index:
30 *   * page.idx (id of the page is the element number)
31 *   * title
32 *   * relation_references_w.idx - _w for words
33 *   * relation_references_w.idx - _i for lines (index by lines)
34 *
35 * The index is a associative map by key
36 *
37 *
38 */
39class syntax_plugin_combo_related extends DokuWiki_Syntax_Plugin
40{
41
42
43    /**
44     * Syntax Type.
45     *
46     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
47     * @see DokuWiki_Syntax_Plugin::getType()
48     */
49    function getType()
50    {
51        return 'substition';
52    }
53
54    /**
55     * @see DokuWiki_Syntax_Plugin::getPType()
56     */
57    function getPType()
58    {
59        return 'block';
60    }
61
62    /**
63     * @see Doku_Parser_Mode::getSort()
64     */
65    function getSort()
66    {
67        return 100;
68    }
69
70    /**
71     * Create a pattern that will called this plugin
72     *
73     * @param string $mode
74     * @see Doku_Parser_Mode::connectTo()
75     */
76    function connectTo($mode)
77    {
78
79
80        // To replace backlinks, you may add it in the configuration
81        $extraPattern = $this->getConf(RelatedTag::EXTRA_PATTERN_CONF);
82        if ($extraPattern != "") {
83            $this->Lexer->addSpecialPattern($extraPattern, $mode, 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
84        }
85
86    }
87
88    /**
89     *
90     * The handle function goal is to parse the matched syntax through the pattern function
91     * and to return the result for use in the renderer
92     * This result is always cached until the page is modified.
93     * @param string $match
94     * @param int $state
95     * @param int $pos
96     * @param Doku_Handler $handler
97     * @return array|bool
98     * @see DokuWiki_Syntax_Plugin::handle()
99     *
100     */
101    function handle($match, $state, $pos, Doku_Handler $handler)
102    {
103
104        // As there is only one call to connect to in order to a add a pattern,
105        // there is only one state entering the function
106        // but I leave it for better understanding of the process flow
107        if ($state == DOKU_LEXER_SPECIAL) {
108            $qualifiedMach = trim($match);
109            $attributes = [];
110            if ($qualifiedMach[0] === "<") {
111                // not an extra pattern
112                $tagAttributes = TagAttributes::createFromTagMatch($match);
113                $attributes = $tagAttributes->toCallStackArray();
114            }
115            return array(
116                PluginUtility::STATE => $state,
117                PluginUtility::ATTRIBUTES => $attributes
118            );
119        }
120
121        // Cache the values
122        return array($state);
123    }
124
125    /**
126     * Render the output
127     * @param string $format
128     * @param Doku_Renderer $renderer
129     * @param array $data - what the function handle() return'ed
130     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
131     * @see DokuWiki_Syntax_Plugin::render()
132     *
133     *
134     */
135    function render($format, Doku_Renderer $renderer, $data): bool
136    {
137
138
139        if ($format == 'xhtml') {
140
141            $tagAttributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES])
142                ->setLogicalTag(RelatedTag::TAG);
143            $renderer->doc .= RelatedTag::render($tagAttributes);
144            return true;
145        }
146        return false;
147    }
148
149    public static function getTag(): string
150    {
151        return RelatedTag::TAG;
152    }
153
154
155}
156