User:Gatoatigrado/sidebarcode

code
&lt;?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
function quick_parse_allow_html($text) { 	global $nowiki_comment_array;

ignore all nowiki tags
$text = preg_replace('/\\&lt;nowiki\\&gt;([^&lt;]*)\\&lt;\\/nowiki\\&gt;/e', "create_nowiki_comment(\"$1\")", $text);

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

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

substitute variables - somewhat time expensive
$text = preg_replace("//e", "wfMsg('$1')", $text);

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');

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, '&lt;!-- noheader --&gt;'); } 	for($header = 0; $header &lt; count($divide_by_header) / 2; $header++) {

start additional parsing function
$array1[$header] = array('headerinfo' =&gt; parse_text($divide_by_header[$header * 2]), 						'wikitext' =&gt; parse_text($divide_by_header[$header * 2 + 1]) ); } 	$result = "&lt;div&gt;"; $result .= "&lt;pre&gt;".htmlspecialchars(print_r($array1, true))."&lt;/pre&gt;"; $result .= "&lt;pre&gt;".htmlspecialchars(print_r($divide_by_header, true))."&lt;/pre&gt;"; $result .= "&lt;/div&gt;"; return $result; }

additional parsing function
function parse_text($text) {

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 &lt; count($array3);) { 		if(substr($array3[$current], 0, 1) === "*") { 			$result[$current_result] = array('type' =&gt; 'list', 'items' =&gt; 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 "&lt;!-- nowiki(".count($nowiki_comment_array).") --&gt;"; } function create_html_link($href, $text) { 	return "&lt;a href=\"$href\"&gt;$text&lt;/a&gt;"; } function calc_if($conditional, $iftrue, $iffalse) {

need to work on this
return $iftrue; }

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

input
===== ===== navigation ===== ===== * | *|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 ===== * link1 * [image:http://nothing.jpg]

output

 * This is an array, which can be used as any page would want.

Array (    [0] =&gt; Array         ( [headerinfo] =&gt; Array (                    [0] =&gt; ===== navigation =====                 ) [wikitext] =&gt; Array (                    [0] =&gt; Array                         ( [type] =&gt; list [items] =&gt; Array (                                    [0] =&gt; * Main Page|another mediawiki link at MediaWiki:Someothermediawikilink                                     [1] =&gt; *|style=&quot;margin-bottom:6px;&quot; text                                     [2] =&gt; * nothing                                 ) )                    [1] =&gt; Features                     [2] =&gt; Array                         ( [type] =&gt; list [items] =&gt; Array (                                    [0] =&gt; * fast - 10x regular parsing                                     [1] =&gt; * variable substitution                                 ) )                    [3] =&gt; Lacking                     [4] =&gt; Array                         ( [type] =&gt; list [items] =&gt; Array (                                    [0] =&gt; * No temp]l[ate [substitution]                                     [1] =&gt; * No brackets inside links                                 ) )                    [5] =&gt; other text here.                 ) )    [1] =&gt; Array         ( [headerinfo] =&gt; Array (                    [0] =&gt; reference pages                 ) [wikitext] =&gt; Array (                    [0] =&gt; Array                         ( [type] =&gt; list [items] =&gt; Array (                                    [0] =&gt; * &lt;a href=&quot;http://nothing&quot;&gt;link1&lt;/a&gt;                                     [1] =&gt; * [image:http://nothing.jpg]                                 ) )                    [1] =&gt; &lt;a href=&quot;http://localhost/wiki1/index.php/Main_Page&quot;&gt;main page&lt;/a&gt;                 ) ) )

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