1<?php
2/**
3 * DokuWiki Plugin code39 (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 *
7 * @auther dodotori @dokuwiki forum
8 * code based on mark2memorize by shimamu <cooklecurry@gmail.com>
9 */
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) {
13	die();
14}
15
16class syntax_plugin_code39 extends DokuWiki_Syntax_Plugin
17{
18	/**
19	 * @return string Syntax mode type
20	 */
21	public function getType()
22	{
23		return 'formatting';
24	}
25
26	/**
27	 * @return string Paragraph type
28	 */
29	public function getPType()
30	{
31		return 'normal';
32	}
33
34	function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
35
36	/**
37	 * @return int Sort order - Low numbers go before high numbers
38	 */
39	public function getSort()
40	{
41		return 100;
42	}
43
44	/**
45	 * Connect lookup pattern to lexer.
46	 *
47	 * @param string $mode Parser mode
48	 */
49	public function connectTo($mode)
50	{
51		$this->Lexer->addEntryPattern('<code39.*?>(?=.*?</code39>)', $mode, 'plugin_code39');
52	}
53
54	public function postConnect()
55	{
56		$this->Lexer->addExitPattern('</code39>', 'plugin_code39');
57	}
58
59	/**
60	 * Handle matches of the code39 syntax
61	 *
62	 * @param string       $match   The match of the syntax
63	 * @param int          $state   The state of the handler
64	 * @param int          $pos     The position in the document
65	 * @param Doku_Handler $handler The handler
66	 *
67	 * @return array Data for the renderer
68	 */
69	public function handle($match, $state, $pos, Doku_Handler $handler)
70	{
71		switch ($state) {
72		case DOKU_LEXER_ENTER :     return array($state, '');
73		case DOKU_LEXER_UNMATCHED : return array($state, $match);
74		case DOKU_LEXER_EXIT :      return array($state, '');
75		}
76		return array();
77	}
78
79	/**
80	 * Render xhtml output or metadata
81	 *
82	 * @param string        $mode     Renderer mode (supported modes: xhtml)
83	 * @param Doku_Renderer $renderer The renderer
84	 * @param array         $data     The data from the handler() function
85	 *
86	 * @return bool If rendering was successful.
87	 */
88	public function render($mode, Doku_Renderer $renderer, $data)
89	{
90         $renderer->doc .= "<link href='https://fonts.googleapis.com/css?family=Libre Barcode 39 Extended Text' rel='stylesheet'>";
91		if ($mode == 'xhtml') {
92			list($state, $match) = $data;
93			switch ($state) {
94			case DOKU_LEXER_ENTER :
95				$renderer->doc .= "<span class='code39'>*";
96				break;
97			case DOKU_LEXER_UNMATCHED :  $renderer->doc .= $renderer->_xmlEntities($match); break;
98			case DOKU_LEXER_EXIT :       $renderer->doc .= "*</span>"; break;
99			}
100			return true;
101		}
102		return false;
103	}
104}
105?>