User:Gatoatigrado/sidebarcode
Appearance
code
[edit]<?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 */
main function
[edit]function quick_parse_allow_html($text) { global $nowiki_comment_array;
ignore all nowiki tags
[edit]$text = preg_replace('/\\<nowiki\\>([^<]*)\\<\\/nowiki\\>/e', "create_nowiki_comment(\"$1\")", $text);
replace http links
[edit]$text = preg_replace('/\\[(http[\\w:?=@&\\/#._;\\-%]*) ([^\\]]*)\\]/e', "create_html_link('$1', '$2')", $text);
replace #if statements
[edit]$text = preg_replace('/{{#if\\: {{{(\\w*)}}}\\|([^|}]*)(\\|[^}]*)?}}/e', "calc_if(\"$1\", \"$2\", \"$3\")", $text);
substitute variables - somewhat time expensive
[edit]$text = preg_replace("/{{{(\\w*)}}}/e", "wfMsg('$1')", $text);
break page into headers
[edit]$divide_by_header = preg_split("/^=====(.+)=====\\s*$/m", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); array_walk($divide_by_header, 'trim_value');
if there is no text before the header, erase that part, else add a noheader section
[edit]if(count($divide_by_header) % 2 == 1) { array_unshift($divide_by_header, '<!-- noheader -->'); } for($header = 0; $header < count($divide_by_header) / 2; $header++) {
start additional parsing function
[edit]$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; }
additional parsing function
[edit]function parse_text($text) {
create lists
[edit]$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) {
need to work on this
[edit]return $iftrue; } function trim_value(&$value) { $value = trim($value); } function rtrim_value(&$value) { $value = rtrim($value); } ?>
input
[edit]===== ===== 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]}}
output
[edit]- 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> ) ) )
what needs to be done
[edit]- 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