xref: /plugin/include/syntax/header.php (revision e33820a04ca98e1ad53a2bd3ea854283a44b88b5)
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                $classes[] = $renderer->startSectionEdit($pos, 'section', $headline);
60            }
61            if ($classes) {
62                $renderer->doc .= ' class="'. implode(' ', $classes) . '"';
63            }
64            $headline = $renderer->_xmlEntities($headline);
65            $renderer->doc .= ' id="'.$hid.'"><a href="' . $url . '" title="' . $headline . '">';
66            $renderer->doc .= $headline;
67            $renderer->doc .= '</a></h' . $lvl . '>' . DOKU_LF;
68            return true;
69        } else {
70            $renderer->header($headline, $lvl, $pos);
71        }
72        return false;
73    }
74
75    /**
76     * Optionally add a CSS class for the first tag
77     *
78     * @author Michael Klier <chi@chimeric.de>
79     */
80    function _get_firsttag($page) {
81        if(plugin_isdisabled('tag') || (!$taghelper =& plugin_load('helper', 'tag'))) {
82            return false;
83        }
84        $subject = p_get_metadata($page, 'subject');
85        if (is_array($subject)) {
86            $tag = $subject[0];
87        } else {
88            list($tag, $rest) = explode(' ', $subject, 2);
89        }
90        if($tag) {
91            return $tag;
92        } else {
93            return false;
94        }
95    }
96}
97// vim:ts=4:sw=4:et:
98