1<?php
2/**
3 * BookCreator plugin : Create a book from some pages.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Luigi Micco <l.micco@tiscali.it>
7 */
8
9class syntax_plugin_bookcreator_exportsaved extends DokuWiki_Syntax_Plugin
10{
11    /**
12     * @param string $mode
13     */
14    public function connectTo($mode)
15    {
16        $this->Lexer->addSpecialPattern('~~EXPORT.*?~~', $mode, 'plugin_bookcreator_exportsaved');
17    }
18
19    /**
20     * Syntax Type
21     *
22     * @return string
23     */
24    public function getType()
25    {
26        return 'substition';
27    }
28
29    /**
30     * Where to sort in?
31     */
32    public function getSort()
33    {
34        return 190;
35    }
36
37    /**
38     * Handler to prepare matched data for the rendering process
39     *
40     * @param string $match The text matched by the patterns
41     * @param int $state The lexer state for the match
42     * @param int $pos The character position of the matched text
43     * @param Doku_Handler $handler The Doku_Handler object
44     * @return  bool|array Return an array with all data you want to use in render, false don't add an instruction
45     */
46    public function handle($match, $state, $pos, Doku_Handler $handler)
47    {
48        $match = substr($match, 2, -2); // strip markup
49        [$type, $savedSelectionPage] = array_pad(explode(':', $match, 2), 2, '');
50        //type: EXPORT...
51        $type = strtolower(substr($type, 6));
52        $exporturl = [
53            'odt' => '?do=export_odtbook',
54            'text' => '?do=export_text',
55            'html' => '?do=export_html',
56            'pdf' => '?do=export_pdfbook'
57        ];
58        if (!in_array($type, array_keys($exporturl))) {
59            $type = 'pdf';
60        }
61        $link = $exporturl[$type];
62
63        [$savedSelectionPage, $linktitle] = array_pad(explode('|', $savedSelectionPage, 2), 2, '');
64        [$savedSelectionPage, $extraParameters] = array_pad(explode('&', $savedSelectionPage, 2), 2, '');
65
66        $ns = $this->getConf('save_namespace');
67        $savedSelectionPageid = cleanID($ns . ":" . $savedSelectionPage);
68        $savedSelectionPageid = substr($savedSelectionPageid, strlen($ns) + 1);
69
70        $link .= '&savedselection=' . $savedSelectionPageid . ($extraParameters ? '&' . $extraParameters : '');
71        if ($linktitle) {
72            $title = $linktitle;
73        } else {
74            $title = sprintf($this->getLang('exportselection'), $savedSelectionPage, $type);
75        }
76
77        return [
78            'link' => $link,
79            'title' => $title,
80            'type' => $type
81        ];
82    }
83
84
85    /**
86     * include a link to the requested export of the saved selection
87     *
88     * @param string $format render mode e.g. text, xhtml, meta,...
89     * @param Doku_Renderer &$renderer
90     * @param array $data return of handle()
91     * @return bool
92     */
93    public function render($format, Doku_Renderer $renderer, $data)
94    {
95        if ($format == 'xhtml' && !is_a($renderer, 'renderer_plugin_dw2pdf')) {
96            /** @var Doku_Renderer_xhtml $renderer */
97            $link = $renderer->internallink($data['link'], $data['title'], null, true);
98
99            // add class for adding file icons to the link
100            $pos = strpos($link, 'class="');
101            $link = substr_replace($link, 'mediafile mf_' . $data['type'] . ' ', $pos + 7, 0);
102            $renderer->doc .= $link;
103            return true;
104        }
105        return false;
106    }
107}
108