1<?php
2/**
3 * FontSize2 Plugin: control the size of your text
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Thorsten Stratmann <thorsten.stratmann@web.de>
7 * @link       https://www.dokuwiki.org/plugin:fontsize2
8 * @version    0.3
9 */
10
11// must be run within Dokuwiki
12if(!defined('DOKU_INC')) die();
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_fontsize2 extends DokuWiki_Syntax_Plugin {
19
20    // What kind of syntax are we?
21    public function getType(){ return 'formatting'; }
22
23    // What kind of syntax do we allow (optional)
24    public function getAllowedTypes() {
25        return array('formatting', 'substition', 'disabled');
26    }
27
28    // What about paragraphs? (optional)
29    public function getPType(){ return 'normal'; }
30
31    // Where to sort in?
32    public function getSort(){ return 91; }
33
34
35    // Connect pattern to lexer
36    public function connectTo($mode) {
37        $this->Lexer->addEntryPattern('(?i)<fs(?: .+?)?>(?=.+</fs>)',$mode,'plugin_fontsize2');
38    }
39    public function postConnect() {
40        $this->Lexer->addExitPattern('(?i)</fs>','plugin_fontsize2');
41    }
42
43
44    // Handle the match
45    public function handle($match, $state, $pos, Doku_Handler $handler) {
46        switch ($state) {
47          case DOKU_LEXER_ENTER :
48            preg_match("/(?i)<fs (.+?)>/", $match, $fs);   // get the fontsize
49            if ( $this->_isValid($fs[1]) ) return array($state, $fs[1]);
50            break;
51          case DOKU_LEXER_MATCHED :
52            break;
53          case DOKU_LEXER_UNMATCHED :
54            return array($state, $match);
55            break;
56          case DOKU_LEXER_EXIT :
57            break;
58          case DOKU_LEXER_SPECIAL :
59            break;
60        }
61        return array($state, "1em");
62    }
63
64    // Create output
65    public function render($format, Doku_Renderer $renderer, $data) {
66        if($format == 'xhtml'){
67            list($state, $fs) = $data;
68            switch ($state) {
69              case DOKU_LEXER_ENTER :
70                $renderer->doc .= "<span style=\"font-size: $fs\">";
71                break;
72              case DOKU_LEXER_MATCHED :
73                break;
74              case DOKU_LEXER_UNMATCHED :
75                $renderer->doc .= $renderer->_xmlEntities($fs);
76                break;
77              case DOKU_LEXER_EXIT :
78                $renderer->doc .= "</span>";
79                break;
80              case DOKU_LEXER_SPECIAL :
81                break;
82            }
83            return true;
84        }
85        return false;
86    }
87
88    protected function _isValid($c) {
89        $c = trim($c);
90        $pattern = "/^([0-9]{1,4})\.[0-9](em|ex|px|%)|"
91                   ."^([0-9]{1,4}(em|ex|px|%))|"
92                   ."^(xx-small|x-small|small|medium|large|x-large|xx-large)/x";
93        if (preg_match($pattern, $c)) return true;
94    }
95}
96