1<?php
2/**
3 * Include plugin (permalink header component)
4 *
5 * Provides a header instruction which renders a permalink to the included page
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Gina Haeussge <osd@foosel.net>
9 * @author  Michael Klier <chi@chimeric.de>
10 */
11
12class syntax_plugin_include_header extends DokuWiki_Syntax_Plugin {
13
14    function getType() {
15        return 'formatting';
16    }
17
18    function getSort() {
19        return 50;
20    }
21
22    function handle($match, $state, $pos, Doku_Handler $handler) {
23        // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser
24    }
25
26    /**
27     * Renders a permalink header.
28     *
29     * Code heavily copied from the header renderer from inc/parser/xhtml.php, just
30     * added an href parameter to the anchor tag linking to the wikilink.
31     */
32    function render($mode, Doku_Renderer $renderer, $data) {
33        global $conf;
34
35        list($headline, $lvl, $pos, $page, $sect, $flags) = $data;
36
37        if ($mode == 'xhtml') {
38            /** @var Doku_Renderer_xhtml $renderer */
39            $hid = $renderer->_headerToLink($headline, true);
40            $renderer->toc_additem($hid, $headline, $lvl);
41            $url = ($sect) ? wl($page) . '#' . $sect : wl($page);
42            $renderer->doc .= DOKU_LF.'<h' . $lvl;
43            $classes = array();
44            if($flags['taglogos']) {
45                $tag = $this->_get_firsttag($page);
46                if($tag) {
47                    $classes[] = 'include_firsttag__' . $tag;
48                }
49            }
50            // the include header instruction is always at the beginning of the first section edit inside the include
51            // wrap so there is no need to close a previous section edit.
52            if ($lvl <= $conf['maxseclevel']) {
53                if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions
54                    $classes[] = $renderer->startSectionEdit($pos, array('target' => 'section', 'name' => $headline, 'hid' => $hid));
55                } else {
56                    $classes[] = $renderer->startSectionEdit($pos, 'section', $headline);
57                }
58            }
59            if ($classes) {
60                $renderer->doc .= ' class="'. implode(' ', $classes) . '"';
61            }
62            $headline = $renderer->_xmlEntities($headline);
63            $renderer->doc .= ' id="'.$hid.'"><a href="' . $url . '" title="' . $headline . '">';
64            $renderer->doc .= $headline;
65            $renderer->doc .= '</a></h' . $lvl . '>' . DOKU_LF;
66            return true;
67        } else {
68            $renderer->header($headline, $lvl, $pos);
69        }
70        return false;
71    }
72
73    /**
74     * Optionally add a CSS class for the first tag
75     *
76     * @author Michael Klier <chi@chimeric.de>
77     */
78    function _get_firsttag($page) {
79        if(plugin_isdisabled('tag') || (!plugin_load('helper', 'tag'))) {
80            return false;
81        }
82        $subject = p_get_metadata($page, 'subject');
83        if (is_array($subject)) {
84            $tag = $subject[0];
85        } else {
86            list($tag, $rest) = explode(' ', $subject, 2);
87        }
88        if($tag) {
89            return $tag;
90        } else {
91            return false;
92        }
93    }
94}
95// vim:ts=4:sw=4:et:
96