1<?php
2/**
3 * DokuWiki Plugin Bookcreator (Syntax Component)
4 *
5 * Copy from dw2pdf plugin
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Sam Wilson <sam@samwilson.id.au>
9 */
10
11use dokuwiki\File\PageResolver;
12
13/**
14 * Syntax for page specific directions for mpdf library
15 */
16class syntax_plugin_bookcreator_exportlink extends DokuWiki_Syntax_Plugin
17{
18
19    /**
20     * Syntax Type
21     *
22     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
23     *
24     * @return string
25     */
26    public function getType()
27    {
28        return 'substition';
29    }
30
31    /**
32     * Sort for applying this mode
33     *
34     * @return int
35     */
36    public function getSort()
37    {
38        return 41;
39    }
40
41    /**
42     * @param string $mode
43     */
44    public function connectTo($mode)
45    {
46        $this->Lexer->addSpecialPattern('~~HTMLNS>(?:.*?)\|(?:.*?)~~', $mode, 'plugin_bookcreator_exportlink');
47        $this->Lexer->addSpecialPattern('~~TEXTNS>(?:.*?)\|(?:.*?)~~', $mode, 'plugin_bookcreator_exportlink');
48    }
49
50    /**
51     * Handler to prepare matched data for the rendering process
52     *
53     * @param string $match The text matched by the patterns
54     * @param int $state The lexer state for the match
55     * @param int $pos The character position of the matched text
56     * @param Doku_Handler $handler The Doku_Handler object
57     * @return  array Return an array with all data you want to use in render, false don't add an instruction
58     */
59    public function handle($match, $state, $pos, Doku_Handler $handler)
60    {
61        global $ID;
62        $match = substr($match, 2, -2); //remove ~~
63        [$type, $match] = array_pad(explode('>', $match, 2),2, '');
64
65        $type = strtolower(substr($type, 0, -2)); //remove NS
66        [$ns, $title] = array_pad(explode('|', $match, 2), 2, '');
67        $id = $ns . ':start';
68        $resolver = new PageResolver($ID);
69        $page = $resolver->resolveId($id);
70        $ns = getNS($page);
71        $link = '?do=export_' . strtolower($type) . 'ns&book_ns=' . $ns . '&book_title=' . $title;
72
73        // check if there is an ampersand in the title
74        $amp = strpos($title, '&');
75        if ($amp !== false) {
76            $title = substr($title, 0, $amp);
77        }
78
79        return [
80            'link' => $link,
81            'title' => sprintf($this->getLang("export_{$type}ns"), $ns, $title),
82            $state,
83            $pos
84        ];
85    }
86
87    /**
88     * Handles the actual output creation.
89     *
90     * @param string $format output format being rendered
91     * @param Doku_Renderer $renderer the current renderer object
92     * @param array $data data created by handler()
93     * @return  boolean                 rendered correctly? (however, returned value is not used at the moment)
94     */
95    public function render($format, Doku_Renderer $renderer, $data)
96    {
97        if ($format == 'xhtml' && !is_a($renderer, 'renderer_plugin_dw2pdf')) {
98            $renderer->internallink($data['link'], $data['title']);
99            return true;
100        }
101        return false;
102    }
103
104}
105