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