Commit a15c744d authored by twanvl's avatar twanvl

Some tweaks to the syntax highlighting and document formating

parent d1574f12
...@@ -8,6 +8,7 @@ require_once('./modules/mse-drupal-modules/highlight.inc'); ...@@ -8,6 +8,7 @@ require_once('./modules/mse-drupal-modules/highlight.inc');
global $nice_names; global $nice_names;
$nice_names = array( $nice_names = array(
'type:double' => 'real number', 'type:double' => 'real number',
'type:int' => 'number',
'type:indexmap' => 'map', 'type:indexmap' => 'map',
); );
...@@ -17,21 +18,21 @@ $nice_names = array( ...@@ -17,21 +18,21 @@ $nice_names = array(
* Format $text, recognizes commands at the start of each line * Format $text, recognizes commands at the start of each line
*/ */
function autoformat($text, $first = true) { function autoformat($text, $first = true) {
global $autoformat_lines; global $autoformat__lines;
// Lines in the input // Lines in the input
$autoformat_lines = is_array($text) ? $text : explode("\n",$text); $autoformat__lines = is_array($text) ? $text : explode("\n",$text);
// Result text // Result text
$i = 0; $i = 0;
return autoformat_handle($i, '', $first); return autoformat__handle($i, '', $first);
} }
function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { function autoformat__handle(&$i, $prefix, $first, $fail_same = false) {
global $autoformat_lines; global $autoformat__lines;
$text = ''; $text = '';
$state = ''; $state = '';
// While not at the end... // While not at the end...
while ($i < count($autoformat_lines)) { while ($i < count($autoformat__lines)) {
$line = $autoformat_lines[$i++]; $line = $autoformat__lines[$i++];
$len = min(strlen($line),strlen($prefix)); $len = min(strlen($line),strlen($prefix));
...@@ -39,7 +40,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -39,7 +40,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
for ($j = 0 ; $j < $len ; ++$j) { for ($j = 0 ; $j < $len ; ++$j) {
if ($prefix{$j} != $line{$j} && $line{$j} != ' ') { if ($prefix{$j} != $line{$j} && $line{$j} != ' ') {
$i--; $i--;
if ($state == '|') $text .= autoformat_table($table); if ($state == '|') $text .= autoformat__table($table);
return $text; return $text;
} }
} }
...@@ -55,11 +56,11 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -55,11 +56,11 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
// Determine line type // Determine line type
if (strlen($oldline) < strlen($prefix)) { if (strlen($oldline) < strlen($prefix)) {
// empty line => break out of this level // empty line => break out of this level
if ($state == '|') $text .= autoformat_table($table); if ($state == '|') $text .= autoformat__table($table);
return $text; return $text;
} else if ($line == '') { } else if ($line == '') {
// empty line => paragraph separator // empty line => paragraph separator
if ($state == '|') $text .= autoformat_table($table); if ($state == '|') $text .= autoformat__table($table);
$state = ''; $state = '';
$text .= "\n"; $text .= "\n";
...@@ -76,20 +77,20 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -76,20 +77,20 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
$table[count($table)-1]['lines'][] = $line; $table[count($table)-1]['lines'][] = $line;
} else { } else {
if ($state == '|') $text .= autoformat_table($table); if ($state == '|') $text .= autoformat__table($table);
// Headings // Headings
if ($prefix == '' && preg_match('@^====.*====$@',$line)) { if ($prefix == '' && preg_match('@^====.*====$@',$line)) {
// level 4 heading // level 4 heading
$text .= '<h4>' . autoformat_line(substr($line,4,-4)) . "</h4>\n"; $text .= '<h4>' . autoformat__line(substr($line,4,-4)) . "</h4>\n";
$state = ''; $state = '';
} elseif ($prefix == '' && preg_match('@^===.*===$@',$line)) { } elseif ($prefix == '' && preg_match('@^===.*===$@',$line)) {
// level 3 heading // level 3 heading
$text .= '<h3>' . autoformat_line(substr($line,3,-3)) . "</h3>\n"; $text .= '<h3>' . autoformat__line(substr($line,3,-3)) . "</h3>\n";
$state = ''; $state = '';
} elseif ($prefix == '' && (preg_match('@^==.*==$@',$line) || preg_match('@^--.*--$@',$line))) { } elseif ($prefix == '' && (preg_match('@^==.*==$@',$line) || preg_match('@^--.*--$@',$line))) {
// level 2 heading // level 2 heading
$text .= '<h2>' . autoformat_line(substr($line,2,-2)) . "</h2>\n"; $text .= '<h2>' . autoformat__line(substr($line,2,-2)) . "</h2>\n";
$state = ''; $state = '';
// Lists // Lists
...@@ -97,14 +98,14 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -97,14 +98,14 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
if ($state == '*') $text = substr($text,0,-5); if ($state == '*') $text = substr($text,0,-5);
else $text .= '<ul>'; else $text .= '<ul>';
$i--; $i--;
$text .= '<li>' . autoformat_handle($i, $current_prefix . '*', false, true) . "</li>\n"; $text .= '<li>' . autoformat__handle($i, $current_prefix . '*', false, true) . "</li>\n";
$text .= '</ul>'; $text .= '</ul>';
$state = '*'; $state = '*';
} elseif ($line{0} == '#') { } elseif ($line{0} == '#') {
if ($state == '#') $text = substr($text,0,-5); if ($state == '#') $text = substr($text,0,-5);
else $text .= '<ol>'; else $text .= '<ol>';
$i--; $i--;
$text .= '<li>' . autoformat_handle($i, $current_prefix . '#', false, true) . "</li>\n"; $text .= '<li>' . autoformat__handle($i, $current_prefix . '#', false, true) . "</li>\n";
$text .= '</ol>'; $text .= '</ol>';
$state = '#'; $state = '#';
} elseif ($line{0} == ':') { } elseif ($line{0} == ':') {
...@@ -112,7 +113,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -112,7 +113,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
elseif ($state == ';') $text = substr($text,0,-5) . '<dt>'; elseif ($state == ';') $text = substr($text,0,-5) . '<dt>';
else $text .= '<dl><dt>'; else $text .= '<dl><dt>';
$i--; $i--;
$text .= "\n" . autoformat_handle($i, $current_prefix . ':', false); $text .= "\n" . autoformat__handle($i, $current_prefix . ':', false);
$text .= '</dt></dl>'; $text .= '</dt></dl>';
$state = ':'; $state = ':';
} elseif ($line{0} == ';') { } elseif ($line{0} == ';') {
...@@ -120,7 +121,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -120,7 +121,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
elseif ($state == ':') $text = substr($text,0,-5) . '<dd>'; elseif ($state == ':') $text = substr($text,0,-5) . '<dd>';
else $text .= '<dl><dd>'; else $text .= '<dl><dd>';
$i--; $i--;
$text .= "\n" . autoformat_handle($i, $current_prefix . ';', false); $text .= "\n" . autoformat__handle($i, $current_prefix . ';', false);
$text .= '</dd></dl>'; $text .= '</dd></dl>';
$state = ';'; $state = ';';
} elseif ($line{0} == '>') { } elseif ($line{0} == '>') {
...@@ -139,7 +140,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -139,7 +140,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
$state = ']'; $state = ']';
// Just text // Just text
} else if ($fail_same && $autoformat_lines[$i-1]{$len-1} != ' ' && $text != '') { } else if ($fail_same && $autoformat__lines[$i-1]{$len-1} != ' ' && $text != '') {
// consecutive * and # lines are different items // consecutive * and # lines are different items
$i--; $i--;
return $text; return $text;
...@@ -149,7 +150,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -149,7 +150,7 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
if ($state == 'P') $text = substr($text,0,-4); if ($state == 'P') $text = substr($text,0,-4);
else $text .= '<p>'; else $text .= '<p>';
} }
$text .= autoformat_line($line) . "\n"; $text .= autoformat__line($line) . "\n";
if ($first) { if ($first) {
$text .= '</p>'; $text .= '</p>';
} }
...@@ -159,14 +160,14 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) { ...@@ -159,14 +160,14 @@ function autoformat_handle(&$i, $prefix, $first, $fail_same = false) {
} }
//print_r("\n\n==================[$prefix]=================\n$text"); //print_r("\n\n==================[$prefix]=================\n$text");
//print_r("\n==================///=================\n"); //print_r("\n==================///=================\n");
if ($state == '|') $text .= autoformat_table($table); if ($state == '|') $text .= autoformat__table($table);
return $text; return $text;
} }
/** /**
* Format a table, given the rows * Format a table, given the rows
*/ */
function autoformat_table($rows) { function autoformat__table($rows) {
foreach ($rows as $k=>$r) { foreach ($rows as $k=>$r) {
// split lines into columns // split lines into columns
$cols = array(); $cols = array();
...@@ -207,8 +208,8 @@ function autoformat_table($rows) { ...@@ -207,8 +208,8 @@ function autoformat_table($rows) {
} }
} }
global $autoformat_lines; global $autoformat__lines;
$l = $autoformat_lines; $l = $autoformat__lines;
$text = '<table>'; $text = '<table>';
foreach ($newrows as $k=>$r) { foreach ($newrows as $k=>$r) {
$text .= $k %2 == 0 ? '<tr class="even">' : '<tr class="odd">'; $text .= $k %2 == 0 ? '<tr class="even">' : '<tr class="odd">';
...@@ -226,7 +227,7 @@ function autoformat_table($rows) { ...@@ -226,7 +227,7 @@ function autoformat_table($rows) {
$text .= '</tr>'; $text .= '</tr>';
} }
$text .= '</table>'; $text .= '</table>';
$autoformat_lines = $l; $autoformat__lines = $l;
return $text; return $text;
} }
...@@ -234,42 +235,51 @@ function autoformat_table($rows) { ...@@ -234,42 +235,51 @@ function autoformat_table($rows) {
/** /**
* Expand formting tags inside a single line, * Expand formting tags inside a single line,
*/ */
function autoformat_line($line) { function autoformat__line($line) {
$line = preg_replace("/'''(.*?)'''/", "<strong>\\1</strong>", $line); $line = preg_replace("/'''(.*?)'''/", "<strong>\\1</strong>", $line);
$line = preg_replace("/''(.*?)''/", "<em>\\1</em>", $line); $line = preg_replace("/''(.*?)''/", "<em>\\1</em>", $line);
$line = preg_replace_callback("/@(.*?)@/", "autoformat_code", $line); $line = preg_replace_callback("/@(.*?)@/", "autoformat__code", $line);
$line = preg_replace_callback("/\[\[(.*?)\|(.*?)]](s?)/", "autoformat_link_s", $line); $line = preg_replace_callback("/\[\[(.*?)\|(.*?)]]/", "autoformat__link_s", $line);
$line = preg_replace_callback("/\[\[(.*?)]](s?)/", "autoformat_link", $line); $line = preg_replace_callback("/\[\[(.*?)]](s?)/", "autoformat__link", $line);
return $line; return $line;
} }
function autoformat_code($matches) { function autoformat__code($matches) {
return '<tt>' . syntax_highlight(htmlspecialchars($matches[1])) . '</tt>'; return '<tt>' . syntax_highlight(htmlspecialchars($matches[1])) . '</tt>';
} }
function autoformat_link($matches) { function autoformat__link($matches) {
return '<a href="' . autoformat_url($matches[1]) . '">' . autoformat_title($matches[1]) . $matches[2] . '</a>'; return '<a href="' . autoformat__url($matches[1]) . '">' . autoformat__title($matches[1], $matches[2]) . '</a>';
} }
function autoformat_link_s($matches) { function autoformat__link_s($matches) {
return '<a href="' . autoformat_url($matches[1]) . '">' . $matches[2] . $matches[3] . '</a>'; return '<a href="' . autoformat__url($matches[1]) . '">' . $matches[2] . '</a>';
} }
function autoformat_url($url) { function autoformat__url($url) {
if (preg_match("/^type:/",$url)) { if (preg_match("/^(type|fun|script|file):(.*)/i",$url,$matches)) {
return url("doc/type/" . substr($url,5)); $part = $matches[1];
} elseif (preg_match("/^fun:/",$url)) { if ($part == 'fun') $part = 'function';
return url("doc/function/" . substr($url,4)); $sub = str_replace(' ','_',strtolower($matches[2]));
return url('doc/' . $part . '/' . $sub);
} else { } else {
return url($url); return url($url);
} }
} }
function autoformat_title($url) { function autoformat__title($url, $s = '') {
global $nice_names; global $nice_names;
if (isset($nice_names[$url])) { if (isset($nice_names[$url])) {
return $nice_names[$url]; $url = $nice_names[$url];
} else if (preg_match("/.*:$/",$url)) {
$url = preg_replace("/:/","",$url);
} else if (!preg_match("@^http://@",$url)) {
$url = preg_replace("/.*:/","",$url);
}
if ($s == 's' && $url{strlen($url)-1}=='y') {
$url = substr($url,0,-1) . 'ies';
} else { } else {
return preg_replace("/.*:/","",$url); $url .= $s;
} }
return $url;
} }
?> ?>
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// Syntax highlighting of script and reader code // Syntax highlighting of script and reader code
function syntax_highlight($code) { function syntax_highlight($code) {
if (preg_match("@^(\s*#.*\n)*\s*[a-zA-Z0-9 _/,]+:[^=]@", $code)) { if (preg_match("@^(\s*#.*\n)*\s*[-a-zA-Z0-9 _/,]+:([^=]|$)@", $code)) {
return highlight_reader($code); return highlight_reader($code);
} else { } else {
return highlight_script($code); return highlight_script($code);
...@@ -55,11 +55,11 @@ function highlight_reader($code) { ...@@ -55,11 +55,11 @@ function highlight_reader($code) {
function highlight_script($code) { function highlight_script($code) {
$ret = ''; $ret = '';
while(strlen($code)) { while(strlen($code)) {
if (preg_match("@^(if|then|else|for|in|do|and|or|not|rgb)\b@",$code, $matches)) { if (preg_match("@^(if|then|else|for|in|do|and|or|xor|not|rgb)\b@",$code, $matches)) {
$ret .= "<span class='hl-kw'>" . $matches[0] . "</span>"; $ret .= "<span class='hl-kw'>" . $matches[0] . "</span>";
} else if (preg_match("@^(include file:)(.*)@",$code, $matches)) { } else if (preg_match("@^(include file:)(.*)@",$code, $matches)) {
$ret .= "<span class='hl-key'>" . $matches[1] . "</span>" . $matches[2]; $ret .= "<span class='hl-key'>" . $matches[1] . "</span>" . $matches[2];
} else if (preg_match("@^[0-9][0-9.]*@",$code, $matches)) { } else if (preg_match("@^([0-9][0-9.]*|true|false)@",$code, $matches)) {
$ret .= "<span class='hl-num'>" . $matches[0] . "</span>"; $ret .= "<span class='hl-num'>" . $matches[0] . "</span>";
} else if (preg_match("@^(\"|&quot;)(\\\\.|[^\\\\])*?(\"|&quot;)@",$code, $matches)) { } else if (preg_match("@^(\"|&quot;)(\\\\.|[^\\\\])*?(\"|&quot;)@",$code, $matches)) {
$ret .= "<span class='hl-str'>" . $matches[0] . "</span>"; $ret .= "<span class='hl-str'>" . $matches[0] . "</span>";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment