1<?php
2/**
3 * BBCode plugin: allows BBCode markup familiar from forum software
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <esther@kaffeehaus.ch>
7 * @author     Luis Machuca Bezzaza <luis.machuca@gulix.cl>
8 */
9
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_bbcode_size extends DokuWiki_Syntax_Plugin {
19
20    function getType() { return 'formatting'; }
21    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
22    function getSort() { return 105; }
23    function connectTo($mode) { $this->Lexer->addEntryPattern('\[size=.*?\](?=.*?\x5B/size\x5D)',$mode,'plugin_bbcode_size'); }
24    function postConnect() { $this->Lexer->addExitPattern('\[/size\]','plugin_bbcode_size'); }
25
26    /**
27     * Handle the match
28     */
29    function handle($match, $state, $pos, Doku_Handler $handler) {
30        switch ($state) {
31          case DOKU_LEXER_ENTER :
32            $match = substr($match, 6, -1);
33            if (preg_match('/".+?"/',$match)) $match = substr($match, 1, -1); // addition #1: unquote
34            if (preg_match('/^[0-6]$/',$match)) $match = self::_relsz(intval($match) ); // addition #2: relative size number
35            else if (preg_match('/^\d+$/',$match)) $match .= 'px';
36            return array($state, $match);
37
38          case DOKU_LEXER_UNMATCHED :
39            return array($state, $match);
40
41          case DOKU_LEXER_EXIT :
42            return array($state, '');
43
44        }
45        return array();
46    }
47
48    /**
49     * Create output
50     */
51    function render($mode, Doku_Renderer $renderer, $data) {
52        if($mode == 'xhtml') {
53            list($state, $match) = $data;
54            switch ($state) {
55              case DOKU_LEXER_ENTER :
56                $renderer->doc .= '<span style="font-size:'.$renderer->_xmlEntities($match).'">';
57                break;
58
59              case DOKU_LEXER_UNMATCHED :
60                $renderer->doc .= $renderer->_xmlEntities($match);
61                break;
62
63              case DOKU_LEXER_EXIT :
64                $renderer->doc .= '</span>';
65                break;
66
67            }
68            return true;
69        }
70        return false;
71    }
72
73    /**
74    * @fn      _relsz
75    * @brief   Returns a relative-size CSS keyword based on numbering.
76    * @author Luis Machuca Bezzaza <luis.machuca@gulix.cl>
77    *
78    * Provides a mapping to the series of size-related keywords in CSS 2.1
79    * (http://www.w3.org/TR/REC-CSS1/#font-size)
80    * Valid values are [0-6], with 3 for "medium" (as recommended by standard)
81    */
82    private function _relsz ($value) {
83        switch ($value) {
84          case 0:
85            return 'xx-small'; break;
86          case 1:
87            return 'x-small'; break;
88          case 2:
89            return 'small'; break;
90          case 4:
91            return 'large'; break;
92          case 5:
93            return 'x-large'; break;
94          case 6:
95            return 'xx-large'; break;
96          case 3:
97            return 'medium'; break;
98          default:
99            return false; break;
100        }
101    }
102
103}
104// vim:ts=4:sw=4:et:enc=utf-8: