1<?php
2/**
3 * Poldek Plugin:  query poldek for package info
4 *
5 * Add poldek tags to dokuwiki
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Elan Ruusamäe <glen@delfi.ee>
9 */
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 *
17 * @author     Elan Ruusamäe <glen@delfi.ee>
18 */
19class syntax_plugin_poldek extends DokuWiki_Syntax_Plugin {
20	/**
21	 * What kind of syntax are we?
22	 */
23	public function getType() {
24		return 'substition';
25	}
26
27	/**
28	 * Where to sort in?
29	 */
30	public function getSort() {
31		return 306;
32	}
33
34	/**
35	 * Connect pattern to lexer
36	 */
37	public function connectTo($mode) {
38		$this->Lexer->addSpecialPattern('\{\{poldek>.+?\}\}', $mode, 'plugin_poldek');
39	}
40
41
42	/**
43	 * Handle the match
44	 */
45	public function handle($match, $state, $pos, Doku_Handler $handler) {
46		$raw = substr($match, 9, -2);
47
48		$data = array('pkg' => $raw);
49		return $data;
50	}
51
52	/**
53	 * Create output
54	 */
55	public function render($format, Doku_Renderer $renderer, $data) {
56		if ($format == "metadata") {
57			// add packages to metadata
58			$packages = &$renderer->meta["plugin_" . $this->getPluginName()];
59			$packages[] = $data['pkg'];
60			$packages = array_unique($packages);
61			return true;
62		} elseif ($format != 'xhtml') {
63			return false;
64		}
65
66		$helper = $this->loadHelper($this->getPluginName(), true);
67		$renderer->doc .= $helper->ls($data['pkg']);
68
69		return true;
70	}
71
72	/**
73	 * FIXME: Somewhy Syntax plugins don't have this method. duplicate
74	 *
75	 * Loads a given helper plugin (if enabled)
76	 *
77	 * @author  Esther Brunner <wikidesign@gmail.com>
78	 *
79	 * @param   $name   name of plugin to load
80	 * @param   $msg    message to display in case the plugin is not available
81	 *
82	 * @return  object  helper plugin object
83	 */
84	function loadHelper($name, $msg){
85		if (!plugin_isdisabled($name)){
86			$obj =& plugin_load('helper',$name);
87		}else{
88			$obj = null;
89		}
90		if ($obj !== null && $msg) msg("Helper plugin $name is not available or invalid.",-1);
91		return $obj;
92	}
93}
94
95//Setup VIM: ex: noet ts=4 enc=utf-8 :
96