xref: /plugin/chordsheets/syntax.php (revision cad2dd57df0f0b5ab4474311c899587159c7e1d3)
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     * Handle the match
24     */
25    public function handle($match, $state, $pos, Doku_Handler $handler){
26        switch ($state) {
27          case DOKU_LEXER_ENTER :
28                $re = '/^<chordSheet.*?([-+]?\d+)>/';
29                $transpose = 0;
30                preg_match($re, $match, $matches, PREG_OFFSET_CAPTURE, 0);
31                if(count($matches) > 0) {
32                    $transpose = $matches[1];
33                }
34                return array($state, $transpose);
35
36          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
37          case DOKU_LEXER_EXIT :       return array($state, '');
38        }
39        return array();
40    }
41
42    /**
43     * Create output
44     */
45    public function render($mode, Doku_Renderer $renderer, $data) {
46        // $data is what the function handle() return'ed.
47        if($mode == 'xhtml'){
48            /** @var Doku_Renderer_xhtml $renderer */
49            list($state,$match) = $data;
50            switch ($state) {
51                case DOKU_LEXER_ENTER :
52                    list($transpose) = $match;
53                    $id = mt_rand();
54                    $renderer->doc .= '<div class="cSheetButtonBar"><span class=cSheetButtons><button onclick="cSheetExportToWord('.$id.')">Export to Word</button></span></div>';
55                    $renderer->doc .= '<div class="song-with-chords" id="'.$id.'" data-transpose="'.$transpose.'">';
56                    // $renderer->doc .= 'Filter: <form class="searchtable" onsubmit="return false;"><input class="searchtable" name="filtertable" b="searchtable.filterall(this, \''.$id.'\')" type="text"></form>';
57
58                case DOKU_LEXER_UNMATCHED :
59                    $renderer->doc .= $renderer->_xmlEntities($match);
60                    break;
61                case DOKU_LEXER_EXIT :
62                    $renderer->doc .= "</div>";
63                    break;
64            }
65            return true;
66        }
67        return false;
68    }
69
70    /**
71     * Validate color value $c
72     * this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
73     * three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
74     */
75    private function _isValid($c) {
76        $c = trim($c);
77
78        $pattern = "/^\s*(
79            ([a-zA-z]+)|                                #colorname - not verified
80            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
81            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
82            )\s*$/x";
83
84        if (preg_match($pattern, $c)) return trim($c);
85
86        return "";
87    }
88}
89
90