1<?php
2/**
3 * DokuWiki Plugin passwordgrey (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Schreiber <m.schreiber@creatronics.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11  die();
12}
13
14class syntax_plugin_passwordgrey_passwordgrey extends DokuWiki_Syntax_Plugin {
15  /**
16   * @return string Syntax mode type
17   */
18  public function getType() {
19    return 'formatting';
20  }
21
22  /**
23   * @return string Paragraph type
24   */
25  public function getPType() {
26    return 'normal';
27  }
28
29  /**
30   * @return int Sort order - Low numbers go before high numbers
31   */
32  public function getSort() {
33    return 158;
34  }
35
36  /**
37   * Connect lookup pattern to lexer.
38   *
39   * @param string $mode Parser mode
40   */
41  public function connectTo($mode) {
42    $this->Lexer->addEntryPattern('<pwd>(?=.*?</pwd>)', $mode, 'plugin_passwordgrey_passwordgrey');
43  }
44
45  public function postConnect() {
46    $this->Lexer->addExitPattern('</pwd>', 'plugin_passwordgrey_passwordgrey');
47  }
48
49  /**
50   * Handle matches of the passwordgrey syntax
51   *
52   * @param string $match The match of the syntax
53   * @param int $state The state of the handler
54   * @param int $pos The position in the document
55   * @param Doku_Handler $handler The handler
56   *
57   * @return array Data for the renderer
58   */
59  public function handle($match, $state, $pos, Doku_Handler $handler) {
60    switch ($state) {
61      case DOKU_LEXER_ENTER :
62      {
63
64        return array($state, array('color:lightgrey', ''));
65      }
66
67      case DOKU_LEXER_UNMATCHED :
68        return array($state, $match);
69      case DOKU_LEXER_EXIT :
70        return array($state, '');
71    }
72    return array();
73  }
74
75  /**
76   * Render xhtml output or metadata
77   *
78   * @param string $mode Renderer mode (supported modes: xhtml)
79   * @param Doku_Renderer $renderer The renderer
80   * @param array $data The data from the handler() function
81   *
82   * @return bool If rendering was successful.
83   */
84  public function render($mode, Doku_Renderer $renderer, $data) {
85    if ($mode == 'xhtml') {
86      $r = rand(0, 100000);
87      list($state, $match) = $data;
88      switch ($state) {
89        case DOKU_LEXER_ENTER :
90          list($color, $background) = $match;
91
92          $renderer->doc .= '<label class="passwordgrey"><span style= "font-weight:bold" class="passwordgrey">';
93
94          break;
95
96        case DOKU_LEXER_UNMATCHED :
97          $renderer->doc .= $renderer->_xmlEntities($match);
98          break;
99        case DOKU_LEXER_EXIT :
100        {
101          $renderer->doc .= '</span></label>';
102          break;
103        }
104      }
105      return true;
106    }
107    if ($mode == 'odt') {
108      list($state, $match) = $data;
109      switch ($state) {
110        case DOKU_LEXER_ENTER :
111          list($color, $background) = $match;
112          if (class_exists('ODTDocument')) {
113            $renderer->_odtSpanOpenUseCSS(NULL, 'style="' . $color . ' ' . $background . '"');
114          }
115          break;
116
117        case DOKU_LEXER_UNMATCHED :
118          $renderer->cdata($match);
119          break;
120
121        case DOKU_LEXER_EXIT :
122          if (class_exists('ODTDocument')) {
123            $renderer->_odtSpanClose();
124          }
125          break;
126      }
127      return true;
128    }
129    return false;
130  }
131}
132