1<?php
2
3/**
4 * DokuWiki Plugin dw2pdf (Syntax Component)
5 *
6 * For marking changes in page orientation.
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Sam Wilson <sam@samwilson.id.au>
10 */
11
12use dokuwiki\Extension\SyntaxPlugin;
13use dokuwiki\File\PageResolver;
14
15/**
16 * Syntax for page specific directions for mpdf library
17 */
18class syntax_plugin_dw2pdf_exportlink extends SyntaxPlugin
19{
20    /**
21     * Syntax Type
22     *
23     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
24     *
25     * @return string
26     */
27    public function getType()
28    {
29        return 'substition';
30    }
31
32    /**
33     * Sort for applying this mode
34     *
35     * @return int
36     */
37    public function getSort()
38    {
39        return 41;
40    }
41
42    /**
43     * @param string $mode
44     */
45    public function connectTo($mode)
46    {
47        $this->Lexer->addSpecialPattern('~~PDFNS>(?:.*?)\|(?:.*?)~~', $mode, 'plugin_dw2pdf_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        $ns = substr($match, 8, strpos($match, '|') - 8);
63        $id = $ns . ':start';
64        $resolver = new PageResolver($ID);
65        $page = $resolver->resolveId($id);
66        $ns = getNS($page);
67        $title = substr($match, strpos($match, '|') + 1, -2);
68        $link = '?do=export_pdfns&book_ns=' . $ns . '&book_title=' . $title;
69
70        // check if there is an ampersand in the title
71        $amp = strpos($title, '&');
72        if ($amp !== false) {
73            $title = substr($title, 0, $amp);
74        }
75
76        return [
77            'link' => $link,
78            'title' => sprintf($this->getLang('export_ns'), $ns, $title),
79            $state,
80            $pos
81        ];
82    }
83
84    /**
85     * Handles the actual output creation.
86     *
87     * @param string $format output format being rendered
88     * @param Doku_Renderer $renderer the current renderer object
89     * @param array $data data created by handler()
90     * @return  boolean                 rendered correctly? (however, returned value is not used at the moment)
91     */
92    public function render($format, Doku_Renderer $renderer, $data)
93    {
94        if ($format == 'xhtml' && !is_a($renderer, 'renderer_plugin_dw2pdf')) {
95            $renderer->internallink($data['link'], $data['title']);
96            return true;
97        }
98        return false;
99    }
100}
101