User:Gatoatigrado/sidebarcode

From Wikipedia, the free encyclopedia

Contents

[edit] code

<?php
 
 /**
 * Lightweight, not error correcting, array output Wiki text parser.
 * Return Array
 * array { [header_info] { [header_n] { text, html } } [header_n] { [list] { items } [other_html] { asdf } [list] { items } } }
 *
 * @param $text String: wikitext
 * @return Array
 */

[edit] main function

function quick_parse_allow_html($text)
 {
        global $nowiki_comment_array;

[edit] ignore all nowiki tags

   $text = preg_replace('/\\<nowiki\\>([^<]*)\\<\\/nowiki\\>/e', "create_nowiki_comment(\"$1\")", $text);

[edit] replace http links

   $text = preg_replace('/\\[(http[\\w:?=@&\\/#._;\\-%]*) ([^\\]]*)\\]/e', "create_html_link('$1', '$2')", $text);

[edit] replace #if statements

   $text = preg_replace('/{{#if\\: {{{(\\w*)}}}\\|([^|}]*)(\\|[^}]*)?}}/e', "calc_if(\"$1\", \"$2\", \"$3\")", $text);

[edit] substitute variables - somewhat time expensive

   $text = preg_replace("/{{{(\\w*)}}}/e", "wfMsg('$1')", $text);

[edit] break page into headers

   $divide_by_header = preg_split("/^=====(.+)=====\\s*$/m", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        array_walk($divide_by_header, 'trim_value');

[edit] if there is no text before the header, erase that part, else add a noheader section

   if(count($divide_by_header) % 2 == 1)
        {
                array_unshift($divide_by_header, '<!-- noheader -->');
        }
        
        for($header = 0; $header < count($divide_by_header) / 2; $header++)
        {

[edit] start additional parsing function

                $array1[$header] = array('headerinfo' => parse_text($divide_by_header[$header * 2]),
                                                'wikitext' => parse_text($divide_by_header[$header * 2 + 1]) );
        }
        
        $result = "<div>";
        $result .= "<pre>".htmlspecialchars(print_r($array1, true))."</pre>";
        $result .= "<pre>".htmlspecialchars(print_r($divide_by_header, true))."</pre>";
        $result .= "</div>";
        return $result;
 }

[edit] additional parsing function

function parse_text($text)
 {

[edit] create lists

        $array3 = preg_split("/^(\\*.*)(?:\n)?/m", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        array_walk($array3, 'rtrim_value');
        $current_result = 0;
        for($current = 0; $current < count($array3);)
        {
                if(substr($array3[$current], 0, 1) === "*")
                {
                        $result[$current_result] = array('type' => 'list', 'items' => array());
                        array_push($result[$current_result]['items'], $array3[$current]);
                        $current++;
                        while(substr($array3[$current], 0, 1) === "*")
                        {
                                array_push($result[$current_result]['items'], $array3[$current]);
                                $current++;
                        }
                        $current_result++;
                }
                else
                {
                        $result[$current_result] = $array3[$current];
                        $current_result++;
                        $current++;
                }
        }
        return $result;
 }
 
 function create_nowiki_comment($text)
 {
        global $nowiki_comment_array;
        if(!isset($nowiki_comment_array)) { $nowiki_comment_array = array($text); }
        else { array_push($nowiki_comment_array, $text); }
        return "<!-- nowiki(".count($nowiki_comment_array).") -->";
 }
 
 function create_html_link($href, $text)
 {
        return "<a href=\"$href\">$text</a>";
 }
 
 function calc_if($conditional, $iftrue, $iffalse)
 {

[edit] need to work on this

   return $iftrue;
 }

 function trim_value(&$value)
 {
        $value = trim($value);
 }
 
 function rtrim_value(&$value)
 {
        $value = rtrim($value);
 }
 
 ?>

[edit] input

===== ===== navigation ===== =====
* {{{mainpage}}}|{{{someothermediawikilink}}}
*|style="margin-bottom:6px;" text
* nothing
Features
* fast - 10x regular parsing
* variable substitution
Lacking
* No temp]l[ate [substitution]
* No brackets inside links unless nowiki tags are used
other text here.

===== reference pages =====
* [http://nothing link1]
* [image:http://nothing.jpg]
{{#if: {{{uploadsenabled}}}|[http://localhost/wiki1/index.php/Main_Page main
page]}}

[edit] output

This is an array, which can be used as any page would want.
Array
 (
     [0] => Array
         (
             [headerinfo] => Array
                 (
                     [0] => ===== navigation =====
                 )
 
             [wikitext] => Array
                 (
                     [0] => Array
                         (
                             [type] => list
                             [items] => Array
                                 (
                                     [0] => * Main Page|another mediawiki link at MediaWiki:Someothermediawikilink
                                     [1] => *|style="margin-bottom:6px;" text
                                     [2] => * nothing
                                 )
 
                         )
 
                     [1] => Features
                     [2] => Array
                         (
                             [type] => list
                             [items] => Array
                                 (
                                     [0] => * fast - 10x regular parsing
                                     [1] => * variable substitution
                                 )
 
                         )
 
                     [3] => Lacking
                     [4] => Array
                         (
                             [type] => list
                             [items] => Array
                                 (
                                     [0] => * No temp]l[ate [substitution]
                                     [1] => * No brackets inside links
                                 )
 
                         )
 
                     [5] => other text here.
                 )
 
         )
 
     [1] => Array
         (
             [headerinfo] => Array
                 (
                     [0] => reference pages
                 )
 
             [wikitext] => Array
                 (
                     [0] => Array
                         (
                             [type] => list
                             [items] => Array
                                 (
                                     [0] => * <a href="http://nothing">link1</a>
 
                                     [1] => * [image:http://nothing.jpg]
                                 )
 
                         )
 
                     [1] => <a href="http://localhost/wiki1/index.php/Main_Page">main
 page</a>
                 )
 
         )
 
 )

[edit] what needs to be done

  • I am currently working on calc_if, which may be used to handle conditionals in the toolbox. It may simply return an array of the arguments so that the sidebar can be cached.
  • possible li styles, but html works for some things.
  • something to make this language neutral and cached.
  • stop using function for link if it remains simple