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