Not logged in. · Lost password · Register
Forum: General Help and Support Development RSS
Parsing markup in plugin output
Avatar
DavidN #1
Member for a month · 2 posts
Group memberships: Members
Show profile · Link to this post
Subject: Parsing markup in plugin output
Hi! I'm attempting to write a plugin for personal use on a wiki which will generate links. I'm trying to find the correct way to allow parsing the output from the plugin as markup - the eventual aim is for the plugin to generate a link with an icon. Outputting HTML works fine, but is there a way to output wiki markup and then have that output parsed?

Here's my class so far:

class syntax_plugin_zchar extends DokuWiki_Syntax_Plugin {

    function getType(){
        return 'container';
    }
 
    function getSort(){
        return 999;
    }

    function connectTo($mode) {
      $this->Lexer->addEntryPattern('<zchar>',$mode,'plugin_zchar');
    }
 
    function postConnect() {
      $this->Lexer->addExitPattern('</zchar>','plugin_zchar');
    }

    function getChars() {
        return [
            'anna' => 'Anna Arbuckle',
            'bob' => 'Bob Bobberson',
            'clive' => 'Clive Clark',
            'david' => 'David Dennis',
        ];
    }
   
    function handle($match, $state, $pos, &$handler){
        $result = ['text' => ''];
        switch ($state) {
          case DOKU_LEXER_ENTER :
          case DOKU_LEXER_MATCHED :
          case DOKU_LEXER_EXIT :
          case DOKU_LEXER_SPECIAL :
            break;
          case DOKU_LEXER_UNMATCHED :
            $values['key'] = $match;
            $chars = $this->getChars();
            $char = $chars[$values['key']];
            if (empty($char)) {
                $result['text'] = "(Person '" . $data['key'] . "' not found)";
            }
            else {
                $result['text'] = '[[person:' . $char . ']]';
            }
        }
        return $result;
    }
 
    function render($mode, &$renderer, $data) {
        if($mode != 'xhtml') { return false; }
        if(empty($data)) { return false; }
        $renderer->doc .= $data['text'];
        return true;
    }
}

With this, <zchar>anna</zchar> will output [[person:Anna Arbuckle]] literally on the page. How can I instead have that interpreted as a link - parse the output of the plugin as markup?

Thanks!
Avatar
LarsDW223 #2
Member since Sep 2014 · 454 posts · Location: Paderborn
Group memberships: Members
Show profile · Link to this post
Have a look at this code snippet:

$instructions = p_get_instructions($markup);
$html = p_render('xhtml', $instructions, $info);

The function p_get_instructions will parse the wiki markup and p_render will generate the output from it - in this case for 'xhtml'.
Avatar
DavidN #3
Member for a month · 2 posts
Group memberships: Members
Show profile · Link to this post
Thanks so much! That was the exact magic I was missing - I've now expanded this to automatically create links for people, locations and roles that are pulled from a database, and it's so much easier to maintain :)
Avatar
andi (Administrator) #4
User title: splitbrain
Member since May 2006 · 3509 posts · Location: Berlin Germany
Group memberships: Administrators, Members
Show profile · Link to this post
In reply to post #2
Quote by LarsDW223 on 2019-12-23, 07:32:
$instructions = p_get_instructions($markup);
$html = p_render('xhtml', $instructions, $info);

Don't. Unless you have a very very good reason to do this and your plugin is only rarely used on a page. Doing this instantiates a completely new parser/rendering cycle (within the current one) and is bound to become a performance nightmare.

There is no reason for your plugin to *create* wiki syntax. If you want to *accept* wiki syntax provided by users, then use the proper mode handling mechanisms as described at https://www.dokuwiki.org/devel:syntax_plugins#key_concepts
Read this if you don't get any useful answers.
Lies dies wenn du keine hilfreichen Antworten bekommst.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2020-01-18, 07:16:01 (UTC +01:00)