1<?php
2/**
3 * DokuWiki Plugin fontawesome (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Mikhail Medvedev <mmedvede@cs.uml.edu>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_fontawesome_fontawesome extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'substition';
18    }
19
20    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
21    /**
22     * @return string Paragraph type
23     */
24    public function getPType() {
25        return 'normal';
26    }
27    /**
28     * @return int Sort order - Low numbers go before high numbers
29     */
30    public function getSort() {
31        return 100;
32    }
33
34    /**
35     * Connect lookup pattern to lexer.
36     *
37     * @param string $mode Parser mode
38     */
39    public function connectTo($mode) {
40        $this->Lexer->addEntryPattern('<fa',$mode,'plugin_fontawesome_fontawesome');
41//        $this->Lexer->addEntryPattern('<FIXME>',$mode,'plugin_fontawesome_fontawesome');
42    }
43
44    public function postConnect() {
45        $this->Lexer->addExitPattern('>','plugin_fontawesome_fontawesome');
46    }
47
48    /**
49     * Handle matches of the fontawesome syntax
50     *
51     * @param string $match The match of the syntax
52     * @param int    $state The state of the handler
53     * @param int    $pos The position in the document
54     * @param Doku_Handler    $handler The handler
55     * @return array Data for the renderer
56     */
57    public function handle($match, $state, $pos, Doku_Handler $handler){
58        $data = array();
59
60        if ($state == DOKU_LEXER_UNMATCHED){
61            $words=explode(" ",$match);
62
63            $data['type'] = array_shift($words);
64            $data['data'] = join(" ", $words);
65        }
66
67        return array($state,$data);
68    }
69
70    /**
71     * Render xhtml output or metadata
72     *
73     * @param string         $mode      Renderer mode (supported modes: xhtml)
74     * @param Doku_Renderer  $renderer  The renderer
75     * @param array          $data      The data from the handler() function
76     * @return bool If rendering was successful.
77     */
78    public function render($mode, Doku_Renderer $renderer, $data) {
79        if($mode != 'xhtml') return false;
80
81        list($state,$match)=$data;
82        if ($state == DOKU_LEXER_UNMATCHED){
83            switch($match['type']){
84            case 'icon':
85                $tag='i';
86                break;
87            }
88
89            $renderer->doc .= '<'.$tag.' class="'. $renderer->_xmlEntities($match['data']) .'"></'.$tag.'>';
90            return true;
91        }
92
93        return false;
94    }
95}
96
97// vim:ts=4:sw=4:et:
98