User Tools

Site Tools


comp:en:usrfrndl

Daily Cartoon

I want the daily cartoon of userfriendly.org show up on one of my wiki pages. Since it has another URL every day, I try to do it via a syntax plugin. Unfortunately, everything works fine on preview, but the page is garbled after saving :-(

You can reproduce this when you save the code in a file named syntax.php in the lib/plugins/userfriendly directory of your DokuWiki installation. Then, place {~userfriendly~} in the page text (I have it on a separate line with one blank line above and one underneath) and see…

Here's the code, maybe you can give me some hints what I am doing wrong.

<?php
/**
 * userfriendly plugin: shows the daily cartoon from userfriendly.org
 *
/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Werner Flamme <w.flamme@web.de>
 * @date       2009-01-17
 */
 
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_userfriendly extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo()
    {
        return array(
            'author' => 'Werner Flamme',
            'email'  => 'w.flamme@web.de',
            'date'   => '2009-01-17',
            'name'   => 'Userfriendly.org Cartoon Plugin',
            'desc'   => 'Shows the daily cartoon from userfriendly org ' .
                        'as shown on http://ars.userfriendly.org/cartoons/?id=<date>',
            'url'    => 'http://wiki.splitbrain.org/doku.php?id=plugin:userfriendly'
        );
    } // function getInfo
 
    /**
    * What kind of syntax are we?
    */
    function getType()
    {
        return 'substition';
    } // function getType
 
    /**
    * What kind of plugin are we?
    */
    function getPType()
    {
        return 'block';
    } // function getPType
 
    /**
    * Where to sort in?
    */
    function getSort()
    {
        return 300;
    } // function getSort
 
    /**
    * Connect pattern to lexer
    */
    function connectTo($mode)
    {
        $this->Lexer->addSpecialPattern('{~userfriendly~}', $mode, 'plugin_userfriendly');
    } // function connectTo
 
    /**
     * Handle the match
    */
    function handle($match, $state, $pos) 
    {
        return array();
    } // function handle
 
    /**
     * Create output
     @param $mode       current mode of DokuWiki (see http://wiki.splitbrain.org/plugin:tutorial)
     @param $renderer   DokuWiki's rendering object
     @param $data       the data between '\<userfriendly' and '/>' tags (not looked at)
     @var   $pagearr    the starting page of userfriendly.org (as array)
     @var   $line       that's what $pagearr consists of
     @var   $srcpos     position of the line where SRC= starts
     @var   $srclen     length of the URL after SRC=
     @var   $picid      the URL of the daily cartoon
     @var   $linkstart  constant expression to start the link tag with
     @var   $imgalt     constant expression for the alt attribute of the img tag
     @var   $myPrep     the text prepared to output
     @return true, if rendering happens, false in all other cases
    */
    function render($mode, &$renderer, $data)
    {
        if ($mode == 'xhtml') {
            $pagearr = file("http://userfriendly.org/");
            if (!is_array($pagearr))
                return -1;
            foreach ($pagearr as $line) {
                if (substr($line, 0, 50) == '<A href="http://ars.userfriendly.org/cartoons/?id=') {
                    $srcpos = strpos($line, 'SRC="http://www.userfriendly.org/cartoons/');
                    $srclen = strlen($line) - $srcpos - 10;
                    $picid = substr($line, $srcpos + 4, $srclen);
                    $linkstart = '<a href="http://userfriendly.org/">';
                    $imgalt    = 'userfriendly.org daily cartoon';
                } // if URL in this line
            } // foreach ($pagearr as $line)
 
// debug line
$renderer->doc .= "<p>{$linkstart}This was *YOUR* link here.</a></p>\n";
 
            $myPrep = '<p><a href="http://userfriendly.org/">' .
                      '<img src=' . $picid .' alt="' . $imgalt .
                      '" title="' .  $imgalt .'" /></a></p>'. "\n";
 
// debug line
$renderer->doc .= "\n <p> " . htmlentities($myPrep) . '</p>';
 
// the next line causes the strange behaviour
//            $renderer->doc .= "\n" . $myPrep;
 
            return true;
       } // if ($mode == 'xhtml')
        return false;
    } // function render
 
} // class syntax_plugin_userfriendly
 
 
//Setup VIM: ex: et ts=4 enc=utf-8 :
comp/en/usrfrndl.txt · Last modified: 2009-01-17 2335 by werner

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki