xref: /plugin/chordsheets/syntax.php (revision 3d7a173336f953ab1994ced194cc3bfbcfb037d6)
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; }
19*3d7a1733SAndreas Pazureck    public function connectTo($mode)
20*3d7a1733SAndreas Pazureck    {
21*3d7a1733SAndreas Pazureck        $this->Lexer->addEntryPattern('<chordSheet.*?>(?=.*?</chordSheet>)',$mode,'plugin_chordsheets');
22*3d7a1733SAndreas Pazureck        $this->Lexer->addSpecialPattern('%.*?\[\w+\]', $mode,'plugin_chordsheets');
23*3d7a1733SAndreas Pazureck    }
24edb40cceSAndreas Pazureck    public function postConnect() { $this->Lexer->addExitPattern('</chordSheet>','plugin_chordsheets'); }
25edb40cceSAndreas Pazureck
26edb40cceSAndreas Pazureck    /**
27edb40cceSAndreas Pazureck     * Handle the match
28edb40cceSAndreas Pazureck     */
29edb40cceSAndreas Pazureck    public function handle($match, $state, $pos, Doku_Handler $handler){
30edb40cceSAndreas Pazureck        switch ($state) {
31edb40cceSAndreas Pazureck          case DOKU_LEXER_ENTER :
32edb40cceSAndreas Pazureck                $re = '/^<chordSheet.*?([-+]?\d+)>/';
33edb40cceSAndreas Pazureck                $transpose = 0;
34edb40cceSAndreas Pazureck                preg_match($re, $match, $matches, PREG_OFFSET_CAPTURE, 0);
35edb40cceSAndreas Pazureck                if(count($matches) > 0) {
36edb40cceSAndreas Pazureck                    $transpose = $matches[1];
37edb40cceSAndreas Pazureck                }
38edb40cceSAndreas Pazureck                return array($state, $transpose);
39edb40cceSAndreas Pazureck
40edb40cceSAndreas Pazureck          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
41edb40cceSAndreas Pazureck          case DOKU_LEXER_EXIT :       return array($state, '');
42*3d7a1733SAndreas Pazureck          case DOKU_LEXER_SPECIAL:     return array($state, $match);
43edb40cceSAndreas Pazureck        }
44edb40cceSAndreas Pazureck        return array();
45edb40cceSAndreas Pazureck    }
46edb40cceSAndreas Pazureck
47edb40cceSAndreas Pazureck    /**
48edb40cceSAndreas Pazureck     * Create output
49edb40cceSAndreas Pazureck     */
50edb40cceSAndreas Pazureck    public function render($mode, Doku_Renderer $renderer, $data) {
51edb40cceSAndreas Pazureck        // $data is what the function handle() return'ed.
52edb40cceSAndreas Pazureck        if($mode == 'xhtml'){
53edb40cceSAndreas Pazureck            /** @var Doku_Renderer_xhtml $renderer */
54edb40cceSAndreas Pazureck            list($state,$match) = $data;
55edb40cceSAndreas Pazureck            switch ($state) {
56edb40cceSAndreas Pazureck                case DOKU_LEXER_ENTER :
57edb40cceSAndreas Pazureck                    list($transpose) = $match;
58edb40cceSAndreas Pazureck                    $id = mt_rand();
59cad2dd57SAndreas Pazureck                    $renderer->doc .= '<div class="cSheetButtonBar"><span class=cSheetButtons><button onclick="cSheetExportToWord('.$id.')">Export to Word</button></span></div>';
60edb40cceSAndreas Pazureck                    $renderer->doc .= '<div class="song-with-chords" id="'.$id.'" data-transpose="'.$transpose.'">';
61cad2dd57SAndreas Pazureck                    // $renderer->doc .= 'Filter: <form class="searchtable" onsubmit="return false;"><input class="searchtable" name="filtertable" b="searchtable.filterall(this, \''.$id.'\')" type="text"></form>';
62edb40cceSAndreas Pazureck
63edb40cceSAndreas Pazureck                case DOKU_LEXER_UNMATCHED :
64edb40cceSAndreas Pazureck                    $renderer->doc .= $renderer->_xmlEntities($match);
65edb40cceSAndreas Pazureck                    break;
66edb40cceSAndreas Pazureck                case DOKU_LEXER_EXIT :
67edb40cceSAndreas Pazureck                    $renderer->doc .= "</div>";
68edb40cceSAndreas Pazureck                    break;
69*3d7a1733SAndreas Pazureck                case DOKU_LEXER_SPECIAL:
70*3d7a1733SAndreas Pazureck                    $renderer->doc .= '<span class="jtab">'.$match.'</span>';
71*3d7a1733SAndreas Pazureck                break;
72edb40cceSAndreas Pazureck            }
73edb40cceSAndreas Pazureck            return true;
74edb40cceSAndreas Pazureck        }
75edb40cceSAndreas Pazureck        return false;
76edb40cceSAndreas Pazureck    }
77edb40cceSAndreas Pazureck}