xref: /plugin/include/syntax/header.php (revision 45c6515cdda30db7aff2b975732c89c56425ca23)
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, &$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, &$renderer, $data) {
39        global $conf;
40
41        list($headline, $lvl, $pos, $page, $sect, $flags) = $data;
42        $hid = $renderer->_headerToLink($headline, true);
43        if ($mode == 'xhtml') {
44            $renderer->toc_additem($hid, $headline, $lvl);
45            $url = ($sect) ? wl($page) . '#' . $sect : wl($page);
46            $renderer->doc .= DOKU_LF.'<h' . $lvl;
47            $classes = array();
48            if($flags['taglogos']) {
49                $tag = $this->_get_firsttag($page);
50                if($tag) {
51                    $classes[] = 'include_firsttag__' . $tag;
52                }
53            }
54            // the include header instruction is always at the beginning of the first section edit inside the include
55            // wrap so there is no need to close a previous section edit.
56            if ($lvl <= $conf['maxseclevel']) {
57                $classes[] = $renderer->startSectionEdit($pos, 'section', $headline);
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        } elseif($mode == 'metadata') {
68            $renderer->toc_additem($hid, $headline, $lvl);
69            return true;
70        }
71        return false;
72    }
73
74    /**
75     * Optionally add a CSS class for the first tag
76     *
77     * @author Michael Klier <chi@chimeric.de>
78     */
79    function _get_firsttag($page) {
80        if(plugin_isdisabled('tag') || (!$taghelper =& plugin_load('helper', 'tag'))) {
81            return false;
82        }
83        $subject = p_get_metadata($page, 'subject');
84        if (is_array($subject)) {
85            $tag = $subject[0];
86        } else {
87            list($tag, $rest) = explode(' ', $subject, 2);
88        }
89        if($tag) {
90            return $tag;
91        } else {
92            return false;
93        }
94    }
95}
96// vim:ts=4:sw=4:et:
97