summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/include/syntax/header.php
blob: 3f14dcb0694b02023b69b17997c421837ae4c0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
 * Include plugin (permalink header component)
 *
 * Provides a header instruction which renders a permalink to the included page
 *
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author  Gina Haeussge <osd@foosel.net>
 * @author  Michael Klier <chi@chimeric.de>
 */

class syntax_plugin_include_header extends DokuWiki_Syntax_Plugin {

    function getType() {
        return 'formatting';
    }
    
    function getSort() {
        return 50;
    }

    function handle($match, $state, $pos, Doku_Handler $handler) {
        // this is a syntax plugin that doesn't offer any syntax, so there's nothing to handle by the parser
    }

    /**
     * Renders a permalink header.
     * 
     * Code heavily copied from the header renderer from inc/parser/xhtml.php, just
     * added an href parameter to the anchor tag linking to the wikilink.
     */
    function render($mode, Doku_Renderer $renderer, $data) {
        global $conf;

        list($headline, $lvl, $pos, $page, $sect, $flags) = $data;

        if ($mode == 'xhtml') {
            /** @var Doku_Renderer_xhtml $renderer */
            $hid = $renderer->_headerToLink($headline, true);
            $renderer->toc_additem($hid, $headline, $lvl);
            $url = ($sect) ? wl($page) . '#' . $sect : wl($page);
            $renderer->doc .= DOKU_LF.'<h' . $lvl;
            $classes = array();
            if($flags['taglogos']) {
                $tag = $this->_get_firsttag($page);
                if($tag) {
                    $classes[] = 'include_firsttag__' . $tag;
                }
            }
            // the include header instruction is always at the beginning of the first section edit inside the include
            // wrap so there is no need to close a previous section edit.
            if ($lvl <= $conf['maxseclevel']) {
                if (defined('SEC_EDIT_PATTERN')) { // for DokuWiki Greebo and more recent versions
                    $classes[] = $renderer->startSectionEdit($pos, array('target' => 'section', 'name' => $headline, 'hid' => $hid));
                } else {
                    $classes[] = $renderer->startSectionEdit($pos, 'section', $headline);
                }
            }
            if ($classes) {
                $renderer->doc .= ' class="'. implode(' ', $classes) . '"';
            }
            $headline = $renderer->_xmlEntities($headline);
            $renderer->doc .= ' id="'.$hid.'"><a href="' . $url . '" title="' . $headline . '">';
            $renderer->doc .= $headline;
            $renderer->doc .= '</a></h' . $lvl . '>' . DOKU_LF;
            return true;
        } else {
            $renderer->header($headline, $lvl, $pos);
        }
        return false;
    }

    /**
     * Optionally add a CSS class for the first tag
     *
     * @author Michael Klier <chi@chimeric.de>
     */
    function _get_firsttag($page) {
        if(plugin_isdisabled('tag') || (!plugin_load('helper', 'tag'))) {
            return false;
        }
        $subject = p_get_metadata($page, 'subject');
        if (is_array($subject)) {
            $tag = $subject[0];
        } else {
            list($tag, $rest) = explode(' ', $subject, 2);
        }
        if($tag) {
            return $tag;
        } else {
            return false;
        }
    }
}
// vim:ts=4:sw=4:et: