1<?php
2/**
3 * DokuWiki Plugin croissant (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Adrian Lang <lang@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_croissant extends DokuWiki_Syntax_Plugin {
13    function getType() {
14        return 'substition';
15    }
16
17    function getPType() {
18        return 'normal';
19    }
20
21    function getSort() {
22        return 1;
23    }
24
25    function connectTo($mode) {
26        $this->Lexer->addSpecialPattern('~~bc:.*?~~',$mode,'plugin_croissant');
27        $this->Lexer->addSpecialPattern('~~nobc~~',$mode,'plugin_croissant');
28    }
29
30    function handle($match, $state, $pos, Doku_Handler $handler){
31        if ($match == '~~nobc~~') {
32            return null;
33        }
34        return trim(substr($match, 5, -2));
35    }
36
37    function render($mode, Doku_Renderer $renderer, $data) {
38        if($mode === 'metadata') {
39            if (blank($data)) {
40                $renderer->meta['plugin_croissant_nobc'] = true;
41            }
42            $renderer->meta['plugin_croissant_bctitle'] = $data;
43        }
44        return true;
45    }
46
47    /**
48     * Greatly cleaned up copy&paste from tpl_youarehere with custom titles
49     */
50    function tpl($sep=' &raquo; ') {
51        global $ID;
52        global $lang;
53
54        if (p_get_metadata($ID, 'plugin_croissant_nobc') === true) {
55            return;
56        }
57
58        $parts = explode(':', $ID);
59
60        echo '<span class="plugin_croissant">';
61        echo '<span class="bchead">'.$lang['youarehere'].': </span>';
62
63        // always print the startpage
64        array_unshift($parts, '');
65
66        // print intermediate namespace links
67        $part = $page = '';
68        $count = count($parts);
69        for($i = 0; $i < $count; ++$i) {
70            $old_page = $page;
71            $part .= $parts[$i];
72            if ($i < $count - 1) {
73                $part .= ':';
74            }
75            $page = $part;
76            resolve_pageid('', $page, $exists);
77            if ($page !== $old_page) {
78                echo $sep;
79                tpl_pagelink(':' . $page, p_get_metadata($page, 'plugin_croissant_bctitle'));
80            }
81        }
82        echo '</span>';
83
84        return true;
85    }
86}
87