1<?php
2
3/**
4 * Webcomics Plugin
5 *
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Christoph Lang <calbity@gmx.de>
8 */
9
10// based on http://wiki.splitbrain.org/plugin:tutorial
11
12// must be run within Dokuwiki
13if (! defined('DOKU_INC')) die();
14
15if (! defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
16require_once (DOKU_PLUGIN . 'syntax.php');
17
18/**
19 * All DokuWiki plugins to extend the parser/rendering mechanism
20 * need to inherit from this class
21 */
22class syntax_plugin_webcomics extends DokuWiki_Syntax_Plugin
23{
24
25  function getInfo ()
26  {
27    return array(
28      'author' => 'Christoph Lang',
29      'email' => 'calbity@gmx.de',
30      'date' => '2013-09-17',
31      'name' => 'Webcomics Plugin',
32      'desc' => 'It displays various Webcomics. Based on Dilbert Plugin.',
33      'url' => 'http://www.christophs-blog.de/dokuwiki-plugins/'
34    );
35  }
36
37  private function _listhd ($type)
38  {
39    require_once (DOKU_INC . 'inc/HTTPClient.php');
40
41    $urls = array(
42      # "GARFIELD" => 'http://feeds.hafcom.nl/garfield.xml',
43      # "CYANIDE" => 'http://pipes.yahoo.com/pipes/pipe.run?_id=9b91d1900e14d1caff163aa6fa1b24bd&_render=rss',
44      "XKCD" => 'http://xkcd.com/rss.xml',
45      "DILBERT" => 'http://pipes.yahoo.com/pipes/pipe.run?_id=1fdc1d7a66bb004a2d9ebfedfb3808e2&_render=rss',
46      "SHACKLES" => 'http://feeds2.feedburner.com/virtualshackles',
47    );
48
49    if ( ! isset($urls[$type]) ) {
50      return "[{$type} - no url given]";
51    }
52
53    $ch    = new DokuHTTPClient();
54    $piece = $ch->get($urls[$type]);
55    $xml   = simplexml_load_string($piece);
56
57    preg_match("/src=\"(.[^\"]*)\"/i", $xml->channel->item->description, $matches);
58
59    if ( ! empty($matches) ) {
60
61      return '<img src="' . $matches[1] . '" alt=""/>';
62    }
63
64    return "[{$type} - couldn't parse image tag]";
65  }
66
67  function connectTo ($mode)
68  {
69    $this->Lexer->addSpecialPattern('\[XKCD\]', $mode, 'plugin_webcomics');
70    $this->Lexer->addSpecialPattern('\[GARFIELD\]', $mode, 'plugin_webcomics');
71    $this->Lexer->addSpecialPattern('\[DILBERT\]', $mode, 'plugin_webcomics');
72    $this->Lexer->addSpecialPattern('\[SHACKLES\]', $mode, 'plugin_webcomics');
73    $this->Lexer->addSpecialPattern('\[CYANIDE\]', $mode, 'plugin_webcomics');
74  }
75
76  function getType ()
77  {
78    return 'substition';
79  }
80
81  function getSort ()
82  {
83    return 667;
84  }
85
86  function handle ($match, $state, $pos, &$handler)
87  {
88    $match = str_replace(array("[", "]"), array("", ""), $match);
89    return array($match, $state, $pos);
90  }
91
92  function render ($mode, &$renderer, $data)
93  {
94    if ($mode == 'xhtml')
95    {
96      $renderer->doc .= $this->_listhd($data[0]);
97      return true;
98    }
99
100    return false;
101  }
102}