1<?php
2/**
3 * DokuWiki Ruby Plugin
4 *
5 * Provide a ruby annotation, which is used to indicate the pronunciation
6 * or meaning of the corresponding characters.
7 * This kind of annotation is often used in Japanese publications.
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author     Hokkaidoperson <dosankomali@yahoo.co.jp>
11 *
12 */
13
14// must be run within Dokuwiki
15if(!defined('DOKU_INC')) die();
16
17class syntax_plugin_ruby extends DokuWiki_Syntax_Plugin {
18
19    function getType(){
20        return 'substition';
21    }
22
23    function getSort(){
24        return 150;
25    }
26
27    /**
28     * Connect lookup pattern to lexer
29     */
30    function connectTo($mode) {
31        $this->Lexer->addSpecialPattern('\{\{ruby\|[^}]*\}\}', $mode, 'plugin_ruby');
32    }
33
34    /**
35     * Handle the match
36     */
37    function handle($match, $state, $pos, Doku_Handler $handler) {
38        // get ruby base and text component of a ruby annotation
39        $data = explode('|', substr($match, strlen('{{ruby|'), -2));
40        return $data;
41    }
42
43    /**
44     * Create output
45     */
46    function render($format, Doku_Renderer $renderer, $data) {
47        static $rp;
48        if (!isset($rp)) {
49            if (utf8_strlen($this->getConf('parentheses')) > 1) {
50                // get a pair of ruby parentheses
51                $rp[0] = utf8_substr($this->getConf('parentheses'), 0, 1);
52                $rp[1] = utf8_substr($this->getConf('parentheses'), 1, 1);
53            } else {
54                // set an empty array
55                $rp = array();
56            }
57        }
58
59        if ($format == 'xhtml') {
60            // create a Group-ruby annotation
61            $renderer->doc .= '<ruby>';
62            $renderer->doc .= '<rb>'.hsc($data[0]).'</rb>';
63            $renderer->doc .= isset($rp[0]) ? '<rp>'.hsc($rp[0]).'</rp>' : '';
64            $renderer->doc .= '<rt>'.hsc($data[1]).'</rt>';
65            $renderer->doc .= isset($rp[1]) ? '<rp>'.hsc($rp[1]).'</rp>' : '';
66            $renderer->doc .= '</ruby>';
67        }
68        if ($format == 'metadata') {
69            // when the format is "metadata" (abstract)
70            if ($renderer->capture) $renderer->doc .= hsc($data[0]) . hsc($rp[0]) . hsc($data[1]) . hsc($rp[1]);
71        }
72
73    }
74}
75