1<?php
2/**
3 * Provides a header instruction which renders a permalink to the blog post
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Gina Haeussge <gina@foosel.net>
7 * @author  Michael Klier <chi@chimeric.de>
8 */
9
10/**
11 * Class syntax_plugin_blogtng_header
12 */
13class syntax_plugin_blogtng_header extends DokuWiki_Syntax_Plugin {
14
15    /**
16     * Syntax Type
17     *
18     * @return string
19     */
20    function getType() {
21        return 'formatting';
22    }
23
24    /**
25     * Sort for applying this mode
26     *
27     * @return int
28     */
29    function getSort() {
30        return 50;
31    }
32
33    /**
34     * Handler to prepare matched data for the rendering process
35     *
36     * @param   string       $match   The text matched by the patterns
37     * @param   int          $state   The lexer state for the match
38     * @param   int          $pos     The character position of the matched text
39     * @param   Doku_Handler $handler The Doku_Handler object
40     * @return  bool|array Return an array with all data you want to use in render, false don't add an instruction
41     */
42    function handle($match, $state, $pos, Doku_Handler $handler) {
43        // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser
44    }
45
46    /**
47     * Renders a permalink header.
48     *
49     * Code heavily copied from the header renderer from inc/parser/xhtml.php, just
50     * added an href parameter to the anchor tag linking to the wikilink.
51     *
52     * @param string $format output format being rendered
53     * @param Doku_Renderer $renderer reference to the current renderer object
54     * @param array $data data created by handler()
55     * @return  boolean                       rendered correctly?
56     */
57    function render($format, Doku_Renderer $renderer, $data) {
58        global $ID;
59        list($text, $level) = $data;
60
61        if ($format == 'xhtml') {
62            /** @var Doku_Renderer_xhtml $renderer */
63            $hid = $renderer->_headerToLink($text,true);
64
65            //only add items within configured levels
66            $renderer->toc_additem($hid, $text, $level);
67
68            // write the header
69            $renderer->doc .= DOKU_LF.'<h'.$level.'><a name="'.$hid.'" id="'.$hid.'" href="'.wl($ID).'">';
70            $renderer->doc .= $renderer->_xmlEntities($text);
71            $renderer->doc .= "</a></h$level>".DOKU_LF;
72
73            return true;
74        }
75
76        // unsupported $mode
77        return false;
78    }
79}
80