'real number', 'type:int' => 'number', 'type:indexmap' => 'field map', 'type:vector2d' => '2D-vector', ); /** * Format $text, recognizes commands at the start of each line */ function autoformat($text, $first = true) { global $autoformat__lines; // Lines in the input $autoformat__lines = is_array($text) ? $text : explode("\n",$text); // Result text $i = 0; return autoformat__handle($i, '', $first); } function autoformat__handle(&$i, $prefix, $first, $fail_same = false) { global $autoformat__lines; $text = ''; $state = ''; // While not at the end... while ($i < count($autoformat__lines)) { $line = $autoformat__lines[$i++]; $len = min(strlen($line),strlen($prefix)); // Match prefix for ($j = 0 ; $j < $len ; ++$j) { if ($prefix{$j} != $line{$j} && $line{$j} != ' ') { $i--; if ($state == '|') $text .= autoformat__table($table); return $text; } } $oldline = $line; $line = substr($line, strlen($prefix)); // trim $current_prefix = $prefix; while ($line != '' && $line{0} == ' ') { $line = substr($line,1); $current_prefix .= ' '; } //$line = $line . '{' . $current_prefix . '|' . strlen($prefix) . substr($oldline, 0, strlen($prefix)) . '}'; // Determine line type if (strlen($oldline) < strlen($prefix)) { // empty line => break out of this level $i--; if ($state == '|') $text .= autoformat__table($table); return $text; } else if ($line == '') { // empty line => paragraph separator if ($state == '|') $text .= autoformat__table($table); $state = ''; $text .= "\n"; // Table } elseif ($line{0} == '|' || $line{0} == '!') { // other code if ($state != '|') $table = array(); $table[]= array( 'lines' => array(substr($line,1)), 'heading' => $line{0} == '!'); $state = '|'; } elseif ($line{0} == "\t" && $state=='|') { // continue cell $table[count($table)-1]['lines'][] = $line; } else { if ($state == '|') $text .= autoformat__table($table); // Headings if ($prefix == '' && preg_match('@^====.*====$@',$line)) { // level 4 heading $text .= '
';
if (substr($line,0,4)=='>>>>') { // always code, not escaped
$text .= highlight_script(substr($line, 4));
} else if (substr($line,0,3)=='>>>') { // not escaped
$text .= syntax_highlight(substr($line, 3));
} else if (substr($line,0,2)=='>>') { // always code
$text .= highlight_script(htmlspecialchars(substr($line, 2)));
} else {
$text .= syntax_highlight(htmlspecialchars(substr($line, 1)));
}
$text .= "\n";
$state = '>';
} elseif ($line{0} == ']') {
// other code
if ($state == ']') $text = substr($text,0,-6);
else $text .= ''; $text .= htmlspecialchars(substr($line, 1)); $text .= "\n"; $state = ']'; // Html } elseif (preg_match("@^?(pre|ul|ol|li|div|blockquote|>)@", $line)) { $line = preg_replace("@^<>@","",$line); $text .= $line . "\n"; $state = ''; // Just text } else if ($fail_same && $autoformat__lines[$i-1]{$len-1} != ' ' && $text != '') { // consecutive * and # lines are different items $i--; return $text; } else { // text if ($first) { if ($state == 'P') $text = substr($text,0,-4); else $text .= '
'; } $text .= autoformat__line($line) . "\n"; if ($first) { $text .= '
'; } $state = 'P'; }} //print_r("\n\n--------------[$prefix]---------------------\n$text\n"); } //print_r("\n\n==================[$prefix]=================\n$text"); //print_r("\n==================///=================\n"); if ($state == '|') $text .= autoformat__table($table); return $text; } /** * Format a table, given the rows */ function autoformat__table($rows) { foreach ($rows as $k=>$r) { // split lines into columns $cols = array(); foreach($r['lines'] as $l) { // split into columns $lcols = preg_split("/\t+/",$l); for ($i = 0 ; $i < count($lcols) ; ++$i) { $cols[$i] .= $lcols[$i] . "\n"; } } $rows2[$k] = $cols; } $newrows = array(); foreach ($rows2 as $y=>$r) { // colspan foreach($r as $x=>$c) { $data = trim($c); if ($data == "<<<" && $x > 0) { $skip = $newrows[$y][$x-1]['skip']; if ($skip == false) $skip = array($y,$x-1); $newrows[$y][$x]['skip'] = $skip; if ($y == $skip[0]) { $newrows[$skip[0]][$skip[1]]['cols'] += 1; } } elseif ($data == "^^^" && $y > 0) { $skip = $newrows[$y-1][$x]['skip']; if ($skip == false) $skip = array($y-1,$x); $newrows[$y][$x]['skip'] = $skip; if ($x == $skip[1]) { $newrows[$skip[0]][$skip[1]]['rows'] += 1; } } else { $newrows[$y][$x]['skip'] = false; $newrows[$y][$x]['data'] = $data; $newrows[$y][$x]['rows'] = 1; $newrows[$y][$x]['cols'] = 1; } } } global $autoformat__lines; $l = $autoformat__lines; $text = '