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