xref: /plugin/include/syntax/header.php (revision 48f69f8ea1c319ebd94399cb423b8aa990cddd93)
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        list($headline, $lvl, $page, $sect, $flags) = $data;
40        $hid = $renderer->_headerToLink($headline);
41        if ($mode == 'xhtml') {
42            $renderer->toc_additem($hid, $headline, $lvl);
43            $url = ($sect) ? wl($page) . '#' . $sect : wl($page);
44            $renderer->doc .= DOKU_LF.'<h' . $lvl;
45            if($flags['taglogos']) {
46                $tag = $this->_get_firsttag($page);
47                if($tag) {
48                    $renderer->doc .= ' class="include_firsttag__' . $tag . '"';
49                }
50            }
51            $headline = $renderer->_xmlEntities($headline);
52            $renderer->doc .= ' id="'.$hid.'"><a href="' . $url . '" title="' . $headline . '">';
53            $renderer->doc .= $headline;
54            $renderer->doc .= '</a></h' . $lvl . '>' . DOKU_LF;
55            return true;
56        } elseif($mode == 'metadata') {
57            $renderer->toc_additem($hid, $headline, $lvl);
58            return true;
59        }
60        return false;
61    }
62
63    /**
64     * Optionally add a CSS class for the first tag
65     *
66     * @author Michael Klier <chi@chimeric.de>
67     */
68    function _get_firsttag($page) {
69        if(plugin_isdisabled('tag') || (!$taghelper =& plugin_load('helper', 'tag'))) {
70            return false;
71        }
72        $subject = p_get_metadata($page, 'subject');
73        if (is_array($subject)) {
74            $tag = $subject[0];
75        } else {
76            list($tag, $rest) = explode(' ', $subject, 2);
77        }
78        if($tag) {
79            return $tag;
80        } else {
81            return false;
82        }
83    }
84}
85// vim:ts=4:sw=4:et:
86