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