1<?php
2/**
3 * DokuWiki Plugin Abbr: abbr tag for abbreviation in Wiki text
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Satoshi Sahara <sahara.satoshi@gmail.com>
7 *
8 * SYNTAX:
9 *       Type 1: <abbr>whole phrase (shortened word)</abbr>
10 *       Type 2: <abbr>shortened word [whole phrase]</abbr>
11 *
12 * OUTPUT:
13 *       <abbr title="whole phrase">shortened word</abbr>
14 */
15
16if(!defined('DOKU_INC')) die();
17
18class syntax_plugin_abbr_abbreviation extends DokuWiki_Syntax_Plugin {
19
20    protected $special_pattern = '<abbr\b(?:\s+short)?>.*?</abbr>';
21
22    public function getType() { return 'formatting'; }
23    public function getSort() { return 65; }
24
25    public function connectTo($mode) {
26        $this->Lexer->addSpecialPattern($this->special_pattern,$mode,substr(get_class($this), 7));
27    }
28
29   /**
30    * Handle the match
31    */
32    public function handle($match, $state, $pos, Doku_Handler $handler) {
33
34        $match = substr($match, 6,-7); // drop markup
35        if (preg_match("/^short>/", $match)) {
36            $shortonly = true;
37            $match = substr($match, 6);
38        } else {
39            $shortonly = false;
40        }
41
42        if (preg_match("/(^.*)((((?>[^()]+)|(?R))*))$/u", $match, $matches)) {
43            // Type 1-Japanese 末尾に置いた「全角カッコ内」で省略語を指定(日本語向け)
44            $shortened = $matches[2];
45            $phrase = $matches[1];
46        } elseif (preg_match("/(^.*)\((((?>[^\(\)]+)|(?R))*)\)$/", $match, $matches)) {
47            // Type 1: shortened word will found in tailing ()
48            // ex. <abbr>HyperText Markup Language (HTML)</abbr>
49            $shortened = $matches[2];
50            $phrase = rtrim($matches[1]);
51        } elseif (preg_match("/(^.*)\[(((?>[^\[\]]+)|(?R))*)\]$/", $match, $matches)) {
52            // Type 2: whole phrase will found in tailing []
53            // ex. <abbr>HTML [HyperText Markup Language]</abbr>
54            $shortened = rtrim($matches[1]);
55            $phrase = $matches[2];
56        } elseif (strpos($match,'|') !== false) {
57            // Type 3: (experimental)
58            // ex. <abbr>HTML|HyperText Markup Language</abbr>
59            list($shortened, $phrase) = explode('|',$match,2);
60            $shortened = trim($shortened);
61            $phrase = trim($phrase);
62        } else {
63            //msg('shortend word not found in "'.$match.'"' ,2);
64            $shortonly = true;
65            $shortened = trim($match);
66            $phrase = $shortened;
67        }
68        return array($state, $shortonly, $shortened, $phrase);
69    }
70
71   /**
72    * Create output
73    */
74    public function render($format, Doku_Renderer $renderer, $data) {
75
76        if ($format != 'xhtml') return false;
77
78        list($state, $shortonly, $shortened, $phrase) = $data;
79        if ($shortonly) {
80            //$html = '<abbr>'.hsc($shortened).'</abbr>';
81            $html = hsc($shortened);
82        } else {
83            $html = '<abbr';
84            $html.= (empty($phrase)) ? '>' : ' title="'.hsc($phrase).'">';
85            $html.= hsc($shortened).'</abbr>';
86        }
87        $renderer->doc .= $html;
88        return true;
89    }
90}
91