1<?php
2/**
3 * Tag Plugin: Display a link to the listing of all pages with a certain tag.
4 *
5 * Usage: {{tagpage>mytag[&dynamic][|title]}}
6 *
7 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author   Matthias Schulte <dokuwiki@lupo49.de>
9 */
10
11/** Tagpage syntax, allows to link to a given tag */
12class syntax_plugin_tag_tagpage extends DokuWiki_Syntax_Plugin {
13
14    /**
15     * @return string Syntax type
16     */
17    function getType() {
18        return 'substition';
19    }
20
21    /**
22     * @return int Sort order
23     */
24    function getSort() {
25        return 305;
26    }
27
28    /**
29     * @return string Paragraph type
30     */
31    function getPType() {
32        return 'normal';
33    }
34
35    /**
36     * @param string $mode Parser mode
37     */
38    function connectTo($mode) {
39        $this->Lexer->addSpecialPattern('\{\{tagpage>.*?\}\}', $mode, 'plugin_tag_tagpage');
40    }
41
42    /**
43     * Handle matches of the count syntax
44     *
45     * @param string          $match The match of the syntax
46     * @param int             $state The state of the handler
47     * @param int             $pos The position in the document
48     * @param Doku_Handler    $handler The handler
49     * @return array Data for the renderer
50     */
51    function handle($match, $state, $pos, Doku_Handler $handler) {
52        $params = [];
53        $match = trim(substr($match, 10, -2)); // get given tag
54        $match = array_pad(explode('|', $match, 2), 2, ''); // split to tags, link name and options
55        $params['title'] = $match[1];
56        [$tag, $flag] = array_pad(explode('&', $match[0], 2), 2, '');
57        $params['dynamic'] = ($flag == 'dynamic');
58        $params['tag'] = trim($tag);
59
60        return $params;
61    }
62
63    /**
64     * Render xhtml output
65     *
66     * @param string         $format      Renderer mode (supported modes: xhtml)
67     * @param Doku_Renderer  $renderer  The renderer
68     * @param array          $data      The data from the handler function
69     * @return bool If rendering was successful.
70     */
71    function render($format, Doku_Renderer $renderer, $data) {
72        if($data['tag'] === '') return false;
73
74        if($format == "xhtml") {
75            if($data['dynamic']) {
76                // deactivate (renderer) cache as long as there is no proper cache handling
77                // implemented for the count syntax
78                $renderer->nocache();
79            }
80
81            /** @var helper_plugin_tag $helper */
82            if(!$helper = $this->loadHelper('tag')) {
83                return false;
84            }
85
86            $renderer->doc .= $helper->tagLink($data['tag'], $data['title'], $data['dynamic']);
87            return true;
88        }
89        return false;
90    }
91}
92