1<?php
2/**
3 * Charter Plugin
4 *
5 * Renders customized charts using the pChart library.
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Gina Haeussge <osd@foosel.net>
9 */
10
11if (!defined('DOKU_INC'))
12    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
13if (!defined('DOKU_PLUGIN'))
14    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15require_once (DOKU_PLUGIN . 'syntax.php');
16
17class syntax_plugin_charter extends DokuWiki_Syntax_Plugin {
18
19    function getInfo() {
20        return array (
21            'author' => 'Gina Haeussge',
22            'email' => 'osd@foosel.net',
23            'date' => @file_get_contents(DOKU_PLUGIN.'charter/VERSION'),
24            'name' => 'Charter Plugin (syntax component)',
25            'desc' => 'Renders customized charts using the pChart library',
26            'url' => 'http://foosel.org/snippets/dokuwiki/charter',
27        );
28    }
29
30    function getType() {
31        return 'substition';
32    }
33
34    function getSort() {
35        return 123;
36    }
37
38    function connectTo($mode) {
39        $this->Lexer->addSpecialPattern('<charter>.*?</charter>', $mode, 'plugin_charter');
40    }
41
42    function handle($match, $state, $pos, & $handler) {
43    	global $ID;
44
45    	$match = trim($match);
46    	$lines = explode("\n", substr($match, 9, -10));
47    	if (trim($lines[0]) === '')
48    		array_shift($lines);
49    	if (trim($lines[-1]) === '')
50    		array_pop($lines);
51
52    	$flags = array();
53    	$data = array();
54    	$indata = false;
55    	foreach ($lines as $line) {
56    		$line = trim($line);
57    		if ($line === '') { // begin data field
58    			$indata = true;
59    		} else {
60    			if ($indata) { // reading in data
61    				array_push($data, $line);
62    			} else { // reading in flags
63    				list($name, $value) = explode('=', $line, 2);
64    				$flags[trim($name)] = trim($value);
65    			}
66    		}
67    	}
68
69    	if (isset($flags['title'])) {
70	    	$mediaid = getNS($ID) . ':chart-' . cleanID($flags['title'], false, true) . '.png';
71    	} else {
72    		$mediaid = getNS($ID) . ':chart-' . cleanID('notitle_' . md5($match)) . '.png';
73    	}
74    	$filename = mediaFN($mediaid);
75
76    	$helper =& plugin_load('helper', 'charter');
77    	$helper->setFlags($flags);
78    	$helper->setData($data);
79
80    	io_makeFileDir($filename);
81    	io_lock($filename);
82    	$helper->render($filename);
83    	io_unlock($filename);
84
85    	return array($mediaid, $helper->flags);
86    }
87
88    function render($mode, & $renderer, $indata) {
89    	list($mediaid, $flags) = $indata;
90
91        if ($mode == 'xhtml') {
92        	$renderer->doc .= '<img src="'.ml($mediaid, 'cache=nocache').'" alt="'.$flags['title'].'" width="'.$flags['size']['width'].'" height="'.$flags['size']['height'].'" class="media'.$flags['align'].'" />';
93        	return true;
94        }
95
96        // unsupported $mode
97        return false;
98    }
99}
100
101//Setup VIM: ex: et ts=4 enc=utf-8 :