xref: /plugin/chordsheets/syntax.php (revision 4c1cd5de102ef29b15c9325de0df30fdbc7cc5a6)
1<?php
2/**
3 * DokuWiki Plugin chordsheets (Syntax Component)
4 *
5 * @license MIT
6 * @author  Andreas Pazureck <andreas@pazureck.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_chordsheets extends DokuWiki_Syntax_Plugin
15{
16    public function getType(){ return 'formatting'; }
17    public function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
18    public function getSort(){ return 158; }
19    public function connectTo($mode) { $this->Lexer->addEntryPattern('<chordSheet.*?>(?=.*?</chordSheet>)',$mode,'plugin_chordsheets'); }
20    public function postConnect() { $this->Lexer->addExitPattern('</chordSheet>','plugin_chordsheets'); }
21
22
23    /**
24     * Handle the match
25     */
26    public function handle($match, $state, $pos, Doku_Handler $handler){
27        switch ($state) {
28          case DOKU_LEXER_ENTER :
29                $re = '/^<chordSheet.*?([-+]?\d+)>/';
30                $transpose = 0;
31                preg_match($re, $match, $matches, PREG_OFFSET_CAPTURE, 0);
32                if(count($matches) > 0) {
33                    $transpose = $matches[1];
34                }
35                return array($state, $transpose);
36
37          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
38          case DOKU_LEXER_EXIT :       return array($state, '');
39        }
40        return array();
41    }
42
43    /**
44     * Create output
45     */
46    public function render($mode, Doku_Renderer $renderer, $data) {
47        // $data is what the function handle() return'ed.
48        if($mode == 'xhtml'){
49            /** @var Doku_Renderer_xhtml $renderer */
50            list($state,$match) = $data;
51            switch ($state) {
52                case DOKU_LEXER_ENTER :
53                    list($transpose) = $match;
54                    // $renderer->doc .= "<div style='$color $background'>";
55                    // break;
56                    $id = mt_rand();
57                    $renderer->doc .= '<div class="song-with-chords" id="'.$id.'" data-transpose="'.$transpose.'">';
58                    // $renderer->doc .= 'Filter: <form class="searchtable" onsubmit="return false;"><input class="searchtable" name="filtertable" onkeyup="searchtable.filterall(this, \''.$id.'\')" type="text"></form>';
59
60                case DOKU_LEXER_UNMATCHED :
61                    $renderer->doc .= $renderer->_xmlEntities($match);
62                    break;
63                case DOKU_LEXER_EXIT :
64                    $renderer->doc .= "</div>";
65                    break;
66            }
67            return true;
68        }
69        return false;
70    }
71
72    /**
73     * Validate color value $c
74     * this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
75     * three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
76     */
77    private function _isValid($c) {
78        $c = trim($c);
79
80        $pattern = "/^\s*(
81            ([a-zA-z]+)|                                #colorname - not verified
82            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
83            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
84            )\s*$/x";
85
86        if (preg_match($pattern, $c)) return trim($c);
87
88        return "";
89    }
90}
91
92