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