1<?php
2/**
3 * DokuWiki Plugin htmlmetatags (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Heiko Heinz <heiko.heinz@soft2c.de>
7 * @author  Eric Maeker <eric@maeker.fr>
8 */
9
10// must be run within Dokuwiki
11if (! defined ( 'DOKU_INC' ))
12	die ();
13
14class syntax_plugin_htmlmetatags_syntax extends DokuWiki_Syntax_Plugin {
15	/**
16	 *
17	 * @return string Syntax mode type
18	 */
19	public function getType() {
20		return 'substition';
21	}
22	/**
23	 *
24	 * @return string Paragraph type
25	 */
26	public function getPType() {
27		return 'block';
28	}
29	/**
30	 *
31	 * @return int Sort order - Low numbers go before high numbers
32	 */
33	public function getSort() {
34		return 110;
35	}
36
37	/**
38	 * Connect lookup pattern to lexer.
39	 *
40	 * @param string $mode
41	 *        	Parser mode
42	 */
43	public function connectTo($mode) {
44		if ($mode == 'base') {
45			$this->Lexer->addSpecialPattern ( '{{htmlmetatags>.+?}}', $mode, 'plugin_htmlmetatags_syntax' );
46		}
47		// $this->Lexer->addEntryPattern('<FIXME>',$mode,'plugin_htmlmetatags_syntax');
48	}
49
50	// public function postConnect() {
51	// $this->Lexer->addExitPattern('</FIXME>','plugin_htmlmetatags_syntax');
52	// }
53
54	/**
55	 * Handle matches of the htmlmetatags syntax
56	 *
57	 * @param string $match
58	 *        	The match of the syntax
59	 * @param int $state
60	 *        	The state of the handler
61	 * @param int $pos
62	 *        	The position in the document
63	 * @param Doku_Handler $handler
64	 *        	The handler
65	 * @return array Data for the renderer
66	 */
67	public function handle($match, $state, $pos, Doku_Handler $handler) {
68		// Remove all linefeeds before parsing attributes
69		$match = str_replace ( PHP_EOL, "", $match );
70
71		// remove the plugin-activator string
72		$match = str_replace ( "{{htmlmetatags>", "", $match );
73		$match = str_replace ( "}}", "", $match );
74
75		// Explode match into attributes array using 'metatag-' as mask
76		return explode ( "metatag-", $match );
77	}
78
79	/**
80	 * Render xhtml output or metadata
81	 *
82	 * usage: {{htmlmetatags>metatag-keywords:(apfel,bananne,birne) metatag-description:(Allgemeiner Obstbauer)}}
83	 *
84	 * @param string $mode
85	 *        	Renderer mode (supported modes: xhtml)
86	 * @param Doku_Renderer $renderer
87	 *        	The renderer
88	 * @param array $data
89	 *        	The data from the handler() function
90	 * @return bool If rendering was successful.
91	 */
92	public function render($mode, Doku_Renderer $renderer, $data) {
93		global $ID;
94
95		switch ($mode) {
96			case 'metadata' :
97
98              /*
99               * e.g.
100               * data[0]="keywords=(apfel, bananne, birne) "
101               * data[1]="og:description=Allgemeiner Obstbauer"
102               */
103              for($i = 0; $i < sizeof ( $data ); $i ++) {
104					$mt = explode ( "=", $data [$i] );
105					$size = sizeof ( $mt );
106					// If attributes as value
107					if (sizeof ( $mt ) == 2) {
108						$name = trim ( $mt [0] );
109						$content = trim ( preg_replace ( "/\((.*?)\)\s*$/", "\\1", $mt [1] ) );
110						// Test if attribute name is a media files and get media file absolute URL
111						if (substr ( $name, 0, 6 ) === 'media-') {
112							$name = substr ( $name, 6 );
113							$content = ml ( $content, '', true, '&amp;', true );
114						}
115						// Send result to renderer
116						if (! empty ( $content )) {
117							if ($name == "keywords") {
118								if (! empty ( $renderer->meta ['htmlmetatags'] [$name] ))
119									$renderer->meta ["htmlmetatags"] [$name] .= ', ' . $content;
120								else
121									$renderer->meta ["htmlmetatags"] [$name] .= $content;
122							} else
123								if (! empty ( $renderer->meta ['htmlmetatags'] [$name] ))
124									$renderer->meta ["htmlmetatags"] [$name] .= $content;
125								else
126									$renderer->meta ["htmlmetatags"] [$name] = $content;
127						}
128					}
129				}
130				return true;
131		}
132
133		return false;
134	}
135}
136
137// vim:ts=4:sw=4:et:
138