<?php
/**
 * Plugin Now: Inserts a timestamp.
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Carlo Perassi <carlo@perassi.org>
 */

// based on http://wiki.splitbrain.org/plugin:tutorial

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once(DOKU_PLUGIN . 'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_dil extends DokuWiki_Syntax_Plugin {
    function getInfo() {
        return array(
        'author'  => 'Carlo Perassi',
        'email'   => 'carlo@perassi.org',
        'date'    => '2009-02-09',
        'name'    => 'dil Plugin',
        'desc'    => 'It displays the Daily Dilbert. It uses the DilbertDailyStrip RSS feed. - 0.7',
        'url'     => 'http://perassi.org/2007/07/28/a-dilbert-plugin-for-dokuwiki/'
        );
    }

    private function _listhd() {
        require_once(DOKU_INC . 'inc/HTTPClient.php');

        $url = 'http://feedproxy.google.com/DilbertDailyStrip';

        $ch = new DokuHTTPClient();
        $piece = $ch->get($url);

        $xml = simplexml_load_string($piece);

        $pre  = 'http://dilbert.com/dyn/str_strip/';
        $post = '.gif';
        $a = explode($pre,  (string)$xml->channel->item->description);
        $b = explode($post, $a[1]);

        $feed_contents .= '<a href="' . $url . '/">' .
        '<img src="' . $pre . $b[0] . $post . '" alt="DIL"/></a>' .
        '<a href="http://perassi.org/2007/07/28/a-dilbert-plugin-for-dokuwiki/">dkdil</a>' . "\n";

        return $feed_contents;
    }

    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('\[DIL\]', $mode, 'plugin_dil');
    }

    function getType() { return 'substition'; }

    function getSort() { return 667; }

    function handle($match, $state, $pos, &$handler) {
        return array($match, $state, $pos);
    }

    function render($mode, &$renderer, $data) {

        if ($mode == 'xhtml') {
            $renderer->doc .= $this->_listhd();
            return true;
        }
        return false;
    }
}
