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 $format
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($format, Doku_Renderer $renderer, $data) {
93		switch ($format) {
94			case 'metadata' :
95				/*
96				 * e.g.
97				 * data[0]="keywords=(apfel, bananne, birne) "
98				 * data[1]="og:description=Allgemeiner Obstbauer"
99				 */
100				for ($i = 0; $i < sizeof ( $data ); $i ++) {
101					$mt = explode ( "=", $data [$i] );
102					// If attributes as value
103					if (sizeof ( $mt ) == 2) {
104						$name = trim ( $mt [0] );
105						$content = trim ( preg_replace ( "/\((.*?)\)\s*$/", "\\1", $mt [1] ) );
106						// Test if attribute name is a media files and get media file absolute URL
107						if (substr ( $name, 0, 6 ) === 'media-') {
108							$name = substr ( $name, 6 );
109							$content = ml ( $content, '', true, '&amp;', true );
110						}
111						// Send result to renderer
112						if (! empty ( $content )) {
113							if ($name == "keywords") {
114								if (! empty ( $renderer->meta ['htmlmetatags'] [$name] ))
115									$renderer->meta ["htmlmetatags"] [$name] .= ', ' . $content;
116								else
117									$renderer->meta ["htmlmetatags"] [$name] = $content;
118							} else
119								if (! empty ( $renderer->meta ['htmlmetatags'] [$name] ))
120									$renderer->meta ["htmlmetatags"] [$name] .= $content;
121								else
122									$renderer->meta ["htmlmetatags"] [$name] = $content;
123						}
124					}
125				}
126				return true;
127		}
128
129		return false;
130	}
131}
132
133// vim:ts=4:sw=4:et:
134