1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin dw2pdf (Syntax Component)
7 *
8 * For marking changes in page orientation.
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Sam Wilson <sam@samwilson.id.au>
12 */
13class syntax_plugin_dw2pdf_pagesetting extends SyntaxPlugin
14{
15    /**
16     * Syntax Type
17     *
18     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
19     *
20     * @return string
21     */
22    public function getType()
23    {
24        return 'substition';
25    }
26
27    /**
28     * Sort for applying this mode
29     *
30     * @return int
31     */
32    public function getSort()
33    {
34        return 40;
35    }
36
37    /**
38     * Paragraph Type
39     *
40     * @see Doku_Handler_Block
41     *
42     * @return string
43     */
44    public function getPType()
45    {
46        return 'block';
47    }
48
49    /**
50     * @param string $mode
51     */
52    public function connectTo($mode)
53    {
54        $this->Lexer->addSpecialPattern('~~PDF:(?:LANDSCAPE|PORTRAIT)~~', $mode, 'plugin_dw2pdf_pagesetting');
55    }
56
57    /**
58     * Handler to prepare matched data for the rendering process
59     *
60     * @param   string       $match   The text matched by the patterns
61     * @param   int          $state   The lexer state for the match
62     * @param   int          $pos     The character position of the matched text
63     * @param   Doku_Handler $handler The Doku_Handler object
64     * @return  bool|array Return an array with all data you want to use in render, false don't add an instruction
65     */
66    public function handle($match, $state, $pos, Doku_Handler $handler)
67    {
68        return [$match, $state, $pos];
69    }
70
71    /**
72     * Handles the actual output creation.
73     *
74     * @param string        $mode     output format being rendered
75     * @param Doku_Renderer $renderer the current renderer object
76     * @param array         $data     data created by handler()
77     * @return  boolean                 rendered correctly? (however, returned value is not used at the moment)
78     */
79    public function render($mode, Doku_Renderer $renderer, $data)
80    {
81        if ($mode == 'xhtml') {
82            $orientation = strtolower(substr($data[0], 6, -2));
83            $renderer->doc .= "<div class='dw2pdf-$orientation'></div>" . DOKU_LF;
84            return true;
85        }
86        return false;
87    }
88}
89