From fe0de4893f0e887e4b1356fdcc2a71865c78c6ee Mon Sep 17 00:00:00 2001 From: twanvl Date: Fri, 13 Jul 2007 18:08:02 +0000 Subject: [PATCH] Custom php scripts used for the documentation on the website git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@566 0fc631ac-6414-0410-93d0-97cfa31319b6 --- tools/website/drupal/autoformat.module | 73 +++++ tools/website/drupal/fromfile.module | 120 ++++++++ .../drupal/mse-drupal-modules/autoformat.inc | 275 ++++++++++++++++++ .../drupal/mse-drupal-modules/highlight.inc | 88 ++++++ 4 files changed, 556 insertions(+) create mode 100644 tools/website/drupal/autoformat.module create mode 100644 tools/website/drupal/fromfile.module create mode 100644 tools/website/drupal/mse-drupal-modules/autoformat.inc create mode 100644 tools/website/drupal/mse-drupal-modules/highlight.inc diff --git a/tools/website/drupal/autoformat.module b/tools/website/drupal/autoformat.module new file mode 100644 index 00000000..4ca9b7fc --- /dev/null +++ b/tools/website/drupal/autoformat.module @@ -0,0 +1,73 @@ + array( t('Paragraphs'), "A single line break\nhas no effect.\n\nBut two line breaks start a new paragraph."), + 'h2' => array( t('Headings'), '=='. t('Section') ."==\n". + '==='. t('Subsection') ."===\n". + '===='. t('Subsection two') ."===="), + 'ul' => array( t('Unordered lists'), "* First list item\n* Second list item\n** A nested list item"), + 'ol' => array( t('Ordered lists'), "# First list item\n# Second list item\n## A nested list item"), + 'dl' => array( t('Definition lists'), ": A term\n; The defenition of that term\n; can span multiple lines\n: Another term\n; And another definition."), + 'mix' => array( t('List types can be mixed'), "* A list\n*: Contains a definition \n*; It is both:\n*;# Inside that list, and\n*;# Correctly formated"), + 'pre' => array( t('Preformated text'), " If you start with a space\n the formating is preserved"), + 'em' => array( t('Emphasis'), "''Emphasized text''"), + 'strong'=> array( t('Strong Emphasis'), "'''Strongly emphasized text'''"), + ); + $header = array(t('Description'), t('You Type'), t('You Get')); + foreach($tips as $tip) { + $rows[] = array( + array('data' => $tip[0], 'class' => 'description'), + array('data' => strpos($tip[1],"\n") !== false + ? '
'.  check_plain($tip[1]) .'
' + : ''. check_plain($tip[1]) .'', + 'class' => 'type'), + array('data' => autoformat($tip[1]), 'class' => 'get') + ); + } + $output = t('Automatic formating of text using the same tags as MediaWiki (wikipedia).'); + $output .= t('

This table provides examples for each formating tag that is enabled on this site.

'); + $output .= theme('table', $header, $rows); + return $output; + } else { + return t('Automatic formating of text using the same tags as MediaWiki (wikipedia).'); + } +} + +/** + * Implementation of hook_filter(). + */ +function autoformat_filter($op, $delta = 0, $format = -1, $text = '') { + switch ($op) { + case 'list': + return array( t('Automatic formating') ); + + case 'description': + return t('Automatic formating of text using the same tags as MediaWiki (wikipedia). HTML is untouched.'); + + case 'process': + return autoformat($text); + + default: + return $text; + } +} + +?> \ No newline at end of file diff --git a/tools/website/drupal/fromfile.module b/tools/website/drupal/fromfile.module new file mode 100644 index 00000000..413497dc --- /dev/null +++ b/tools/website/drupal/fromfile.module @@ -0,0 +1,120 @@ + 'Functions', + 'type' => 'Types' +); + +define('FROMFILE_DIR', 'doc'); + +/** + * @file + * Read pages from the filesystem + */ + +/** + * Implementation of hook_help(). + */ +function fromfile_help($section) { + switch ($section) { + case 'admin/modules#description': + return t('Read pages from the filesystem.'); + case 'admin/settings/search': + return t('From file path settings'); + } +} + +/** + * Implementation of hook_menu(). + */ +function fromfile_menu($may_cache) { + $items = array(); + + if ($may_cache) { + $items[] = array('path' => FROMFILE_DIR, + 'title' => t('fromfile'), + 'callback' => 'fromfile_page', + 'access' => user_access('access content'), + 'type' => MENU_SUGGESTED_ITEM); + } + + return $items; +} + + +/** + * Menu callback: show file + */ +function fromfile_page() { + global $nice_ff_names; + $path = variable_get('fromfile_path', ''); + if ($path=='') return drupal_not_found(); + $args = func_get_args(); + $file = implode('/', $args); + + // open file + $file = str_replace('.','',$file); + $file1 = "$path/$file.txt"; + $file2 = "$path/$file/index.txt"; + $file1 = str_replace('//','/',$file1); + $file2 = str_replace('//','/',$file2); + if (file_exists($file1)) { + $full_path = $file1; + } elseif (file_exists($file2)) { + $full_path = $file2; + } else { + return drupal_not_found(); + } + $raw = file($full_path); + + foreach($raw as $k=>$l) $raw[$k] = preg_replace("/\r?\n/","",$l); + + // format the file + $title = array_shift($raw); + $content = autoformat($raw); + + // breadcrumbs + $breadcrumbs = array(l(t('Home'), NULL)); + $path = FROMFILE_DIR; + for ($i = -1 ; $i < count($args) ; ++$i) { + $path .= '/' . $args[$i]; + if ($i == count($args) - 1) { + $name = $title; + $name = preg_replace("/.*: ?/","",$name); + } elseif ($i == -1) { + $name = 'Documentation'; + } else { + $name = ucfirst($args[$i]); + if (isset($nice_ff_names[$name])) $name = $nice_ff_names[$name]; + } + $breadcrumbs[] = l(t($name), $path); + } + + // done + drupal_set_title($title); + drupal_set_breadcrumb($breadcrumbs); + return $content; +} + + + +/** + * Menu callback; displays a module's settings page. + */ +function fromfile_settings() { + + // From file settings: + $form['fromfile'] = array( + '#type' => 'fieldset', '#title' => t('From file settings'), + '#collapsible' => TRUE, '#collapsed' => FALSE + ); + $form['fromfile']['fromfile_path'] = array( + '#type' => 'textfield', '#title' => t('Path'), '#default_value' => variable_get('fromfile_path', ''), + '#description' => t('Path for the fromfile module.'), '#required' => FALSE + ); + + return $form; +} diff --git a/tools/website/drupal/mse-drupal-modules/autoformat.inc b/tools/website/drupal/mse-drupal-modules/autoformat.inc new file mode 100644 index 00000000..4099ecfb --- /dev/null +++ b/tools/website/drupal/mse-drupal-modules/autoformat.inc @@ -0,0 +1,275 @@ + 'real number', + 'type:indexmap' => 'map', +); + + + +/** + * 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 .= ' '; + } + + // Determine line type + if (strlen($oldline) < strlen($prefix)) { + // empty line => break out of this level + 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 .= '

' . autoformat_line(substr($line,4,-4)) . "

\n"; + $state = ''; + } elseif ($prefix == '' && preg_match('@^===.*===$@',$line)) { + // level 3 heading + $text .= '

' . autoformat_line(substr($line,3,-3)) . "

\n"; + $state = ''; + } elseif ($prefix == '' && (preg_match('@^==.*==$@',$line) || preg_match('@^--.*--$@',$line))) { + // level 2 heading + $text .= '

' . autoformat_line(substr($line,2,-2)) . "

\n"; + $state = ''; + + // Lists + } elseif ($line{0} == '*') { + if ($state == '*') $text = substr($text,0,-5); + else $text .= ''; + $state = '*'; + } elseif ($line{0} == '#') { + if ($state == '#') $text = substr($text,0,-5); + else $text .= '
    '; + $i--; + $text .= '
  1. ' . autoformat_handle($i, $current_prefix . '#', false, true) . "
  2. \n"; + $text .= '
'; + $state = '#'; + } elseif ($line{0} == ':') { + if ($state == ':') $text = substr($text,0,-10); + elseif ($state == ';') $text = substr($text,0,-5) . '
'; + else $text .= '
'; + $i--; + $text .= "\n" . autoformat_handle($i, $current_prefix . ':', false); + $text .= '
'; + $state = ':'; + } elseif ($line{0} == ';') { + if ($state == ';') $text = substr($text,0,-10); + elseif ($state == ':') $text = substr($text,0,-5) . '
'; + else $text .= '
'; + $i--; + $text .= "\n" . autoformat_handle($i, $current_prefix . ';', false); + $text .= '
'; + $state = ';'; + } elseif ($line{0} == '>') { + // source code + if ($state == '>') $text = substr($text,0,-6); + else $text .= '
';
+			$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 = ']'; + + // 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 = ''; + foreach ($newrows as $k=>$r) { + $text .= $k %2 == 0 ? '' : ''; + $td = $rows[$k]['heading'] ? 'th' : 'td'; + foreach($r as $c) { + if (!$c['skip']) { + $text .= "<$td" + . ($c['cols'] > 1 ? ' colspan="'.$c['cols'].'"' : "") + . ($c['rows'] > 1 ? ' rowspan="'.$c['rows'].'"' : "") + . ">"; + $text .= autoformat($c['data'], false); + $text .= ""; + } + } + $text .= ''; + } + $text .= '
'; + $autoformat_lines = $l; + return $text; +} + + +/** + * Expand formting tags inside a single line, + */ +function autoformat_line($line) { + $line = preg_replace("/'''(.*?)'''/", "\\1", $line); + $line = preg_replace("/''(.*?)''/", "\\1", $line); + $line = preg_replace_callback("/@(.*?)@/", "autoformat_code", $line); + $line = preg_replace_callback("/\[\[(.*?)\|(.*?)]](s?)/", "autoformat_link_s", $line); + $line = preg_replace_callback("/\[\[(.*?)]](s?)/", "autoformat_link", $line); + return $line; +} + +function autoformat_code($matches) { + return '' . syntax_highlight(htmlspecialchars($matches[1])) . ''; +} + +function autoformat_link($matches) { + return '' . autoformat_title($matches[1]) . $matches[2] . ''; +} +function autoformat_link_s($matches) { + return '' . $matches[2] . $matches[3] . ''; +} + +function autoformat_url($url) { + if (preg_match("/^type:/",$url)) { + return url("doc/type/" . substr($url,5)); + } elseif (preg_match("/^fun:/",$url)) { + return url("doc/function/" . substr($url,4)); + } else { + return url($url); + } +} +function autoformat_title($url) { + global $nice_names; + if (isset($nice_names[$url])) { + return $nice_names[$url]; + } else { + return preg_replace("/.*:/","",$url); + } +} + +?> \ No newline at end of file diff --git a/tools/website/drupal/mse-drupal-modules/highlight.inc b/tools/website/drupal/mse-drupal-modules/highlight.inc new file mode 100644 index 00000000..00c751eb --- /dev/null +++ b/tools/website/drupal/mse-drupal-modules/highlight.inc @@ -0,0 +1,88 @@ + 0) $ret .= "\n"; + preg_match("@^(\s*)(.*)@",$lines[$i],$matches); + $indent = $matches[1]; + $ret .= $indent; + $rest = $matches[2]; + if ($in_script !== false) { + if (strlen($indent) <= strlen($in_script)) $in_script = false; + } + if ($in_script !== false) { + $ret .= highlight_script($rest); + } else { + if (preg_match("@^#@",$rest,$matches)) { + $ret .= "$rest"; + } else if (preg_match("@^([a-zA-Z0-9 _/,]+):(.*)@",$rest,$matches)) { + $key = $matches[1]; + $value = $matches[2]; + if (preg_match("@script|default@", $key)) $in_script = $indent; + if (strpos($value,"{")!==false) $in_script = $indent; + if ($in_script !== false) { + $value = highlight_script($value); + } else if (preg_match("@^(\s*)script:(.*)@",$value,$matches)) { + $value = $matches[1] . "script:" . highlight_script($matches[2]); + //} else if (preg_match("@\\s*rgb\\([0-9]+,[0-9]+,[0-9]+\\)\\s*@",$value,$matches)) { + // $value = highlight_script($value); + } + $ret .= "$key:$value"; + } else { + // not valid reader code + if (strpos($rest,"{")!==false) $in_script = substr($indent,0,-1); + if ($in_script !== false) $rest = highlight_script($rest); + $ret .= $rest; + } + } + } + return $ret; +} + +function highlight_script($code) { + $ret = ''; + while(strlen($code)) { + if (preg_match("@^(if|then|else|for|in|do|and|or|not|rgb)\b@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^(include file:)(.*)@",$code, $matches)) { + $ret .= "" . $matches[1] . "" . $matches[2]; + } else if (preg_match("@^[0-9][0-9.]*@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^(\"|")(\\\\.|[^\\\\])*?(\"|")@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^\\#.*@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^([-+*/=!.]|<|>)+|^:=@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^[\\(\\)\\[\\]{},]+@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^[a-zA-Z_][a-zA-Z0-9_]*:@",$code, $matches)) { + $ret .= "" . $matches[0] . ""; + } else if (preg_match("@^([a-zA-Z0-9_]+\s*|\s+|&#?[a-zA-Z0-9]+;)@",$code, $matches)) { + //$ret .= '[' . $matches[0] . ']'; + $ret .= $matches[0]; + } else { + // fallback + $matches = array($code[0]); + //$ret .= '{{{' . $matches[0] . '}}}'; + $ret .= $matches[0]; + } + $code = substr($code, strlen($matches[0])); + } + return $ret; +} + +?> \ No newline at end of file