xref: /plugin/dw2pdf/syntax/exportlink.php (revision 743904e5456510aa7abe51f2f2f60e76859f6d54)
1<?php
2/**
3 * DokuWiki Plugin dw2pdf (Syntax Component)
4 *
5 * For marking changes in page orientation.
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Sam Wilson <sam@samwilson.id.au>
9 */
10/* Must be run within Dokuwiki */
11if(!defined('DOKU_INC')) die();
12
13/**
14 * Syntax for page specific directions for mpdf library
15 */
16class syntax_plugin_dw2pdf_exportlink extends DokuWiki_Syntax_Plugin {
17
18    /**
19     * Syntax Type
20     *
21     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
22     *
23     * @return string
24     */
25    public function getType() {
26        return 'substition';
27    }
28
29    /**
30     * Sort for applying this mode
31     *
32     * @return int
33     */
34    public function getSort() {
35        return 41;
36    }
37
38    /**
39     * @param string $mode
40     */
41    public function connectTo($mode) {
42        $this->Lexer->addSpecialPattern('~~PDFNS>(?:.*?)\|(?:.*?)~~', $mode, 'plugin_dw2pdf_exportlink');
43    }
44
45    /**
46     * Handler to prepare matched data for the rendering process
47     *
48     * @param   string       $match   The text matched by the patterns
49     * @param   int          $state   The lexer state for the match
50     * @param   int          $pos     The character position of the matched text
51     * @param   Doku_Handler $handler The Doku_Handler object
52     * @return  bool|array Return an array with all data you want to use in render, false don't add an instruction
53     */
54    public function handle($match, $state, $pos, Doku_Handler $handler) {
55        global $ID;
56        $ns = substr($match,8,strpos($match,'|')-8);
57        $id = $ns . ':start';
58        resolve_pageid(getNS($ID),$id,$exists);
59        $ns = getNS($id);
60        $title = substr($match,strpos($match,'|')+1,-2);
61        $link = '?do=export_pdfns&book_ns=' . $ns . '&book_title=' . $title;
62
63        // check if there is an ampersand in the title
64        $amp = strpos($title,'&');
65        if ($amp !== false) {
66            $title = substr($title,0,$amp);
67        }
68
69        return array('link' => $link, 'title' => sprintf($this->getLang('export_ns'),$ns,$title),$state, $pos);
70    }
71
72    /**
73     * Handles the actual output creation.
74     *
75     * @param string        $mode     output format being rendered
76     * @param Doku_Renderer $renderer the current renderer object
77     * @param array         $data     data created by handler()
78     * @return  boolean                 rendered correctly? (however, returned value is not used at the moment)
79     */
80    public function render($mode, Doku_Renderer $renderer, $data) {
81        if($mode == 'xhtml') {
82            $renderer->internallink($data['link'],$data['title']);
83            return true;
84        }
85        return false;
86    }
87
88}
89