xref: /template/strap/syntax/link.php (revision ef295d81c8f9cce3d7e7673ca8909fdd45b2e219)
1<?php
2
3
4require_once(__DIR__ . "/../class/Analytics.php");
5require_once(__DIR__ . "/../class/PluginUtility.php");
6require_once(__DIR__ . "/../class/LinkUtility.php");
7require_once(__DIR__ . "/../class/HtmlUtility.php");
8
9use ComboStrap\Analytics;
10use ComboStrap\HtmlUtility;
11use ComboStrap\LinkUtility;
12use ComboStrap\LogUtility;
13use ComboStrap\NavBarUtility;
14use ComboStrap\Page;
15use ComboStrap\PluginUtility;
16use ComboStrap\LowQualityPage;
17use ComboStrap\Tag;
18
19if (!defined('DOKU_INC')) die();
20
21/**
22 *
23 * A link pattern to take over the link of Dokuwiki
24 * and transform it as a bootstrap link
25 *
26 * The handle of the move of link is to be found in the
27 * admin action {@link action_plugin_combo_linkmove}
28 *
29 */
30class syntax_plugin_combo_link extends DokuWiki_Syntax_Plugin
31{
32    const TAG = 'link';
33    const COMPONENT = 'combo_link';
34
35
36    /**
37     * Syntax Type.
38     *
39     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
40     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
41     */
42    function getType()
43    {
44        return 'substition';
45    }
46
47    /**
48     * How Dokuwiki will add P element
49     *
50     *  * 'normal' - The plugin can be used inside paragraphs
51     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
52     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
53     *
54     * @see DokuWiki_Syntax_Plugin::getPType()
55     */
56    function getPType()
57    {
58        return 'normal';
59    }
60
61    /**
62     * @return array
63     * Allow which kind of plugin inside
64     *
65     * No one of array('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
66     * because we manage self the content and we call self the parser
67     */
68    function getAllowedTypes()
69    {
70        return array('substition', 'formatting', 'disabled');
71    }
72
73    /**
74     * @see Doku_Parser_Mode::getSort()
75     * The mode with the lowest sort number will win out
76     */
77    function getSort()
78    {
79        return 100;
80    }
81
82
83    function connectTo($mode)
84    {
85
86        $this->Lexer->addSpecialPattern(LinkUtility::LINK_PATTERN, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
87
88    }
89
90
91    /**
92     * The handler for an internal link
93     * based on `internallink` in {@link Doku_Handler}
94     * The handler call the good renderer in {@link Doku_Renderer_xhtml} with
95     * the parameters (ie for instance internallink)
96     * @param string $match
97     * @param int $state
98     * @param int $pos
99     * @param Doku_Handler $handler
100     * @return array|bool
101     */
102    function handle($match, $state, $pos, Doku_Handler $handler)
103    {
104
105        /**
106         * Because we use the specialPattern, there is only one state ie DOKU_LEXER_SPECIAL
107         */
108        $attributes = LinkUtility::getAttributes($match);
109        $tag = new Tag(self::TAG, $attributes, $state, $handler->calls);
110        $parent = $tag->getParent();
111        $parentName = "";
112        if ($parent != null) {
113            $parentName = $parent->getName();
114        }
115        return array(
116            PluginUtility::ATTRIBUTES => $attributes,
117            PluginUtility::PARENT_TAG => $parentName
118        );
119
120
121    }
122
123    /**
124     * Render the output
125     * @param string $format
126     * @param Doku_Renderer $renderer
127     * @param array $data - what the function handle() return'ed
128     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
129     * @see DokuWiki_Syntax_Plugin::render()
130     *
131     *
132     */
133    function render($format, Doku_Renderer $renderer, $data)
134    {
135        // The data
136        switch ($format) {
137            case 'xhtml':
138
139                /** @var Doku_Renderer_xhtml $renderer */
140
141                /**
142                 * Cache problem may occurs while releasing
143                 */
144                if (isset($data[PluginUtility::ATTRIBUTES])) {
145                    $attributes = $data[PluginUtility::ATTRIBUTES];
146                } else {
147                    $attributes = $data;
148                }
149                $ref = $attributes[LinkUtility::ATTRIBUTE_REF];
150                $name = $attributes[LinkUtility::ATTRIBUTE_NAME];
151                $type = $attributes[LinkUtility::ATTRIBUTE_TYPE];
152                $link = new LinkUtility($ref);
153                if ($name != null) {
154                    $link->setName($name);
155                }
156                $link->setType($type);
157
158                /**
159                 * Render the link
160                 */
161                $htmlLink = $link->render($renderer);
162
163                /**
164                 * Extra styling for internal link
165                 */
166                $parentTag = $data[PluginUtility::PARENT_TAG];
167                switch ($parentTag) {
168                    case syntax_plugin_combo_button::TAG:
169                        if ($link->getType() == LinkUtility::TYPE_INTERNAL) {
170                            if ($link->getInternalPage()->existInFs()) {
171                                $htmlLink = LinkUtility::deleteDokuWikiClass($htmlLink);
172                            }
173                        }
174                        $htmlLink = LinkUtility::inheritColorFromParent($htmlLink);
175                        break;
176                    case syntax_plugin_combo_cite::TAG:
177                    case syntax_plugin_combo_listitem::TAG:
178                    case syntax_plugin_combo_preformatted::TAG:
179                        if ($link->getType() == LinkUtility::TYPE_INTERNAL) {
180                            if ($link->getInternalPage()->existInFs()) {
181                                $htmlLink = LinkUtility::deleteDokuWikiClass($htmlLink);
182                            }
183                        }
184                        break;
185                    case syntax_plugin_combo_dropdown::TAG:
186                        if ($link->getType() == LinkUtility::TYPE_INTERNAL) {
187                            if ($link->getInternalPage()->existInFs()) {
188                                $htmlLink = LinkUtility::deleteDokuWikiClass($htmlLink);
189                            }
190                        }
191                        $htmlLink = HtmlUtility::addAttributeValue($htmlLink, "class", "dropdown-item");
192                        break;
193                    case syntax_plugin_combo_navbarcollapse::COMPONENT:
194                        $htmlLink = '<div class="navbar-nav">' . NavBarUtility::switchDokuwiki2BootstrapClass($htmlLink) . '</div>';
195                        break;
196                    case syntax_plugin_combo_navbargroup::COMPONENT:
197                        $htmlLink = '<li class="nav-item">' . NavBarUtility::switchDokuwiki2BootstrapClass($htmlLink) . '</li>';
198                        break;
199
200                }
201
202
203                /**
204                 * Add it to the rendering
205                 */
206                $renderer->doc .= $htmlLink;
207
208                return true;
209                break;
210
211            case
212            'metadata':
213
214                /**
215                 * Keep track of the backlinks ie meta['relation']['references']
216                 * @var Doku_Renderer_metadata $renderer
217                 */
218                if (isset($data[PluginUtility::ATTRIBUTES])) {
219                    $attributes = $data[PluginUtility::ATTRIBUTES];
220                } else {
221                    $attributes = $data;
222                }
223                $ref = $attributes[LinkUtility::ATTRIBUTE_REF];
224
225                $link = new LinkUtility($ref);
226                $name = $attributes[LinkUtility::ATTRIBUTE_NAME];
227                if ($name != null) {
228                    $link->setName($name);
229                }
230                $link->handleMetadata($renderer);
231
232                return true;
233                break;
234
235            case Analytics::RENDERER_FORMAT:
236                /**
237                 *
238                 * @var renderer_plugin_combo_analytics $renderer
239                 */
240                $attributes = $data[PluginUtility::ATTRIBUTES];
241                $ref = $attributes[LinkUtility::ATTRIBUTE_REF];
242                $link = new LinkUtility($ref);
243                $link->processLinkStats($renderer->stats);
244                break;
245
246        }
247        // unsupported $mode
248        return false;
249    }
250
251
252}
253
254