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