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