1<?php
2  /**
3   * @license    http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
4   * @author     Francois Merciol <dokuplugin@merciol.fr>
5   *
6   * Plugin Glossary: manage forms for glossary
7   */
8
9if (!defined ('DOKU_INC'))
10    define ('DOKU_INC', realpath (dirname (__FILE__).'/../../../').'/');
11if (!defined ('DOKU_PLUGIN'))
12    define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
13require_once (DOKU_PLUGIN.'syntax.php');
14
15require_once (dirname (__FILE__).'/../glossary.class.php');
16
17// ============================================================
18class syntax_plugin_glossary_span extends DokuWiki_Syntax_Plugin {
19
20  // ============================================================
21  function getType () { return 'formatting'; }
22  function getPType () { return 'normal'; }
23  function getAllowedTypes() { return array ('formatting', 'substition', 'disabled'); }
24  function getSort () { return 195; }
25  function accepts ($mode) {
26    if ($mode == substr (get_class ($this), 7))
27      return true;
28    return parent::accepts ($mode);
29  }
30
31  function connectTo ($mode) {
32    $this->Lexer->addSpecialPattern ('<glossary[^/]*?/>', $mode, 'plugin_glossary_span');
33    $this->Lexer->addEntryPattern ('<glossary[^/]*?>(?=.*?</glossary>)', $mode, 'plugin_glossary_span');
34  }
35  function postConnect() {
36    $this->Lexer->addExitPattern ('</glossary>', 'plugin_glossary_span');
37  }
38
39  // ============================================================
40  function handle ($match, $state, $pos, Doku_Handler $handler) {
41    switch ($state) {
42    case DOKU_LEXER_SPECIAL :
43      $data = strtolower (trim (substr ($match, 9, -2)));
44      return array ($state, $data);
45    case DOKU_LEXER_ENTER:
46      $data = strtolower (trim (substr ($match, 9, -1)));
47      return array ($state, $data);
48
49    case DOKU_LEXER_UNMATCHED :
50      $handler->_addCall ('cdata', array ($match), $pos);
51      return false;
52
53    case DOKU_LEXER_EXIT :
54      return array ($state, '');
55    }
56    return false;
57  }
58
59  // ============================================================
60  function render ($mode, Doku_Renderer $renderer, $indata) {
61    if (empty ($indata))
62      return false;
63    if ($mode != 'xhtml')
64      false;
65    list ($state, $data) = $indata;
66    $data = trim ($data);
67    switch ($state) {
68    case DOKU_LEXER_SPECIAL :
69      $imgDir = DOKU_REL.'lib/plugins/glossary/images/';
70      if ($data == "help")
71	$renderer->doc .= '<img src="'.$imgDir.'help.png" />';
72      elseif ($data == "clock")
73	$renderer->doc .= '<img src="'.$imgDir.'clock.png" />';
74      elseif ($data == "face-smile")
75	$renderer->doc .= '<img src="'.$imgDir.'face-smile.png" />';
76      elseif ($data == "face-sad")
77	$renderer->doc .= '<img src="'.$imgDir.'face-sad.png" />';
78      elseif ($data == "stop")
79	$renderer->doc .= '<img src="'.$imgDir.'stop.png" />';
80      elseif ($data == "one-way")
81	$renderer->doc .= '<img src="'.$imgDir.'one-way.png" />';
82      elseif ($data == "eye")
83	$renderer->doc .= '<img src="'.$imgDir.'eye.png" />';
84      elseif ($data == "score")
85	$renderer->doc .= '<img src="'.$imgDir.'score-all.png" width="32" />';
86     elseif ($data == "search")
87	$renderer->doc .= '<img src="'.$imgDir.'search.png" />';
88      elseif (preg_match_all ("#(?<ns>.*)\s+(?<type>definition|proposal|poll)#", $data, $dumy) > 0) {
89	global $ID;
90	$ns = getNS ($ID);
91	if (count ($dumy ['ns']) > 0) {
92	  $ns = $dumy['ns'][0];
93	  if (($ns == '*') || ($ns == ':'))
94	    $ns = '';
95	  elseif ($ns == '.')
96	    $ns = getNS ($ID);
97	  else
98	    $ns = cleanID ($ns);
99	}
100	$glossary = new glossary ($this, $ns);
101	switch ($dumy['type'][0]) {
102	case "definition":
103	  $renderer->doc .= $glossary->getDefinitionSize ();
104	  break;
105	case "proposal":
106	  $renderer->doc .= $glossary->getProposalSize ();
107	  break;
108	case "poll":
109	  $renderer->doc .= $glossary->getPollSize ();
110	  break;
111	}
112      }
113      break;
114    case DOKU_LEXER_ENTER:
115      if ($data == "word")
116	$renderer->doc .= '<span class="glossaryWord">';
117      elseif ($data == "translate")
118	$renderer->doc .= '<span class="glossaryTranslate">';
119      else
120	$renderer->doc .= '<span>';
121      break;
122    case DOKU_LEXER_EXIT:
123      $renderer->doc .= "</span>";
124      break;
125    }
126    return true;
127  }
128
129  // ============================================================
130} // syntax_plugin_GLOSSARY
131?>
132