1<?php
2/**
3 * Display Title Dokuwiki Plugin (Syntax Component)
4 *
5 * Description: The Display Title Plugin can display the id or title of the current page or the title of an arbitrary page.
6 *
7 * Syntax: <<display id>>
8 *         <<display title>>
9 *         <<display title ID>>
10 *         ID   (Optional): Arbitrary local wiki page id as would be passed to normal local wiki links.
11 *
12 * @license    The MIT License (https://opensource.org/licenses/MIT)
13 * @author     Jay Jeckel <jeckelmail@gmail.com>
14 *
15 * Copyright (c) 2016 Jay Jeckel
16 * Licensed under the MIT license: https://opensource.org/licenses/MIT
17 * Permission is granted to use, copy, modify, and distribute the work.
18 * Full license information available in the project LICENSE file.
19 */
20
21if (!defined('DOKU_INC')) { die(); }
22
23class syntax_plugin_displaytitle extends DokuWiki_Syntax_Plugin
24{
25    function getInfo() { return confToHash(dirname(__FILE__) . '/plugin.info.txt'); }
26
27    function getType() { return 'substition'; }
28
29    function getSort() { return 5; }
30
31    function connectTo($mode)
32    {
33        $pattern = '<<' . 'display' . '\s' . '(?:' . 'title' . '|' . 'id' . ')' . '>>';
34        $this->Lexer->addSpecialPattern($pattern, $mode, 'plugin_displaytitle');
35
36        $pattern = '<<' . 'display' . '\s' . 'title' . '\s' . '.+?' . '>>';
37        $this->Lexer->addSpecialPattern($pattern, $mode, 'plugin_displaytitle');
38    }
39
40    function handle($match, $state, $pos, Doku_Handler $handler)
41    {
42        global $ID;
43
44        list($key, $id) = explode(' ', trim(substr($match, 9, -2)), 2);
45        if (empty($id) || strlen(trim($id)) == 0) { $id = $ID; }
46
47        if ($key == 'id') { return $id; }
48
49        $meta = p_get_metadata($id);
50        return (empty($meta) || !isset($meta['title']) ? $id : $meta['title']);
51    }
52
53    function render($mode, Doku_Renderer $renderer, $data)
54    {
55        if ($mode != 'xhtml') { return false; }
56
57        $renderer->doc .= $data;
58        return true;
59    }
60}
61
62//Setup VIM: ex: et ts=4 enc=utf-8 :
63?>