Creating new block tags and parameters?

Anonymous
2011-01-06
2013-04-17

  • Anonymous
    2011-01-06

    I have searched in vain for hours now searching for a way to create new block tags, and additionally to give them parameters.

    For example, I want to do syntax highlighting for programming/scripting languages.  I would like to create a new php tag, such as:

    Here's how to markup some basic PHP:
    <php>
    <?php
    echo 'The answer is 42.';
    ?>
    </php>
    

    I'd honestly rather do something like:

    Here's how to markup some basic PHP:
    <code lang='php'>
    <?php
    echo 'The answer is 42.';
    ?>
    </code>
    

    How can I do this?

     
  • Mario Salzer
    Mario Salzer
    2011-01-07

    probably best per post-processing:

        $ewiki_plugins = "geshi";
        function geshi(&$html) {
               $html =  preg_replace_callback("/&lt;code(*)&gt;(.+?)&lt;/code&gt;/", "geshi2", $html);
        }
        function geshi2($matches) {
               $options = $matches;
               $html = html_entity_decode($matches);
               // …
               return $html;
        }

     

  • Anonymous
    2011-01-10

    What file would you suggest I put this in?

     

  • Anonymous
    2011-01-10

    Nevermind, figured that out.  Also, you forgot to escape the closing slash for the tag.  Regex should be:

    $html =  preg_replace_callback("/&lt;code([^&]*)&gt;(.+?)&lt;\/code&gt;/", "geshi2", $html);
    
     

  • Anonymous
    2011-01-10

    Problem:  doing this on the final render means that if my code contains characters used in WikiMarkup then they will mess up the rendering.  So any code having an array will mess things up.

     
  • Mario Salzer
    Mario Salzer
    2011-01-11

    Oh well, yes. This output already has all WikiMarkup applied.

    But there's maybe another option. Not well tested, and I'm not sure if it'll work:

    $ewiki_plugins["format_block"]["php"] = "geshi2";
    $ewiki_config["format_block"]["php"] = array("&lt;code", "&lt;/code&gt;", "php", 0x0000);
    

    Note the absence of the closing bracket for "&lt;code". The flags might also need 0x0400 or something.

    All relies on the arcane ewiki_format() function, which was thouroughly misoptimized and convoluted.

     
  • Mario Salzer
    Mario Salzer
    2011-01-11

    As example, here is the _format_html() after which to model the geshi2 interface:

    function ewiki_format_geshi2(&$str, &$in, &$iii, &$s) {
       // reverse htmlentitites, if flag 0x0400 not set
       $str = "<span class=\"markup html\">" . $str . "\n</span>\n";
    }
    

    But the setting parameters are not very relevant, you only need to modify $str.

     

  • Anonymous
    2011-01-11

    Okay, so I might be able to do this with this method.  I have a problem, though.  My function ewiki_format_geshi2 never seems to be called, or it doesn't work as I expect or something.  What I have:

    $ewiki_plugins["format_block"]["geshi"] = "ewiki_format_geshi2";
    $ewiki_config["format_block"]["geshi"] = array("&lt;code ", "&lt;/code&gt;", false, 0x0000);
    function ewiki_format_geshi2(&$str, &$in, &$iii, &$s){
        $str .= " &lt;- I parsed that.  Yeah, that's cool.";
    }
    

    I've tried changing false to php, removing it altogether, removing the last two parameters… I can't seem to get anything to call my function.

     
  • Mario Salzer
    Mario Salzer
    2011-01-12

    Hmm, I'm not sure.

    But the "false" should definitely match the plugin_block-registration name "geshi". Maybe you try make it match "

    " and "
    

    " instead to reduce the ambiguity with "&lt" html prefixes.

    Also on closer look the registration might actually need:

        $ewiki_plugins["format_block"]["geshi"][] = "ewiki_format_geshi2";
    

    Look into doc/INTERNALS for a few other notes.
    However take in mind that this is not really maintained atm.

     
  • Mario Salzer
    Mario Salzer
    2011-01-12

    Oh not helpful SF forum.
    I meant above that you should make it match something like "" and ""

     

  • Anonymous
    2011-01-13

    I thought I had posted another reply, but apparently I did not.  All I needed to do was change:

    $ewiki_plugins["format_block"]["geshi"] = "ewiki_format_geshi2";
    

    to:

    $ewiki_plugins["format_block"]["geshi"] = array("ewiki_format_geshi2");
    

    As for how useful this implementation is… I parse out the parameters based on the > symbol.  If a user doesn't include that symbol, it would break normally anyway, so they aren't out much, I'd say.  I don't like it, but it works:

    $ewiki_plugins["format_block"]["geshi"] = array("ewiki_format_geshi");
    $ewiki_config["format_block"]["geshi"] = array("&lt;code ", "&lt;/code&gt;", false, 0x0000);
    function ewiki_format_geshi(&$str, &$in, &$iii, &$s){
        $structure = explode("&gt;", $str, 2);
        $options =
            preg_replace(
                '/[\',\"]/',
                '',
                explode("=",trim($structure[0]))
            );
        require_once('/var/www-includes/geshi/geshi.php'); 
        $geshi = new GeSHi(html_entity_decode($structure[1]), $options[1]);
        $str = "<div class='code'>".$geshi->parse_code()."</div>\n";
    }
    

    If you see any optimizations or fixes to bugs or anything, I'd appreciated it.