Commit e2e0b60f authored by twanvl's avatar twanvl

Custom php scripts used for the documentation on the website

parent 5908fb33
<?php
require_once('./modules/mse-drupal-modules/autoformat.inc');
/**
* Implementation of hook_help().
*/
function autoformat_help($section) {
switch ($section) {
case 'admin/modules#description':
// This description is shown in the listing at admin/modules.
return t('Automatic formating of text using the same tags as MediaWiki (wikipedia).');
}
}
/**
* Implementation of hook_filter_tips().
*/
function autoformat_filter_tips($delta, $format, $long = FALSE) {
if ($long) {
$tips = array(
'p' => 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
? '<pre>'. check_plain($tip[1]) .'</pre>'
: '<code>'. check_plain($tip[1]) .'</code>',
'class' => 'type'),
array('data' => autoformat($tip[1]), 'class' => 'get')
);
}
$output = t('Automatic formating of text using the same tags as <a href="http://en.wikipedia.org/wiki/Wikipedia:How_to_edit_a_page#Wiki_markup">MediaWiki (wikipedia)</a>.');
$output .= t('<p>This table provides examples for each formating tag that is enabled on this site.</p>');
$output .= theme('table', $header, $rows);
return $output;
} else {
return t('Automatic formating of text using the same tags as <a href="http://en.wikipedia.org/wiki/Wikipedia:How_to_edit_a_page#Wiki_markup">MediaWiki (wikipedia)</a>.');
}
}
/**
* 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
<?php
require_once('./modules/mse-drupal-modules/autoformat.inc');
global $nice_ff_names;
$nice_ff_names = array(
'function' => '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;
}
<?php
// Automatic formating of text using the same tags as MediaWiki (wikipedia).
require_once('./modules/mse-drupal-modules/highlight.inc');
// quick and dirty aliasses
global $nice_names;
$nice_names = array(
'type:double' => '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 .= '<h4>' . autoformat_line(substr($line,4,-4)) . "</h4>\n";
$state = '';
} elseif ($prefix == '' && preg_match('@^===.*===$@',$line)) {
// level 3 heading
$text .= '<h3>' . autoformat_line(substr($line,3,-3)) . "</h3>\n";
$state = '';
} elseif ($prefix == '' && (preg_match('@^==.*==$@',$line) || preg_match('@^--.*--$@',$line))) {
// level 2 heading
$text .= '<h2>' . autoformat_line(substr($line,2,-2)) . "</h2>\n";
$state = '';
// Lists
} elseif ($line{0} == '*') {
if ($state == '*') $text = substr($text,0,-5);
else $text .= '<ul>';
$i--;
$text .= '<li>' . autoformat_handle($i, $current_prefix . '*', false, true) . "</li>\n";
$text .= '</ul>';
$state = '*';
} elseif ($line{0} == '#') {
if ($state == '#') $text = substr($text,0,-5);
else $text .= '<ol>';
$i--;
$text .= '<li>' . autoformat_handle($i, $current_prefix . '#', false, true) . "</li>\n";
$text .= '</ol>';
$state = '#';
} elseif ($line{0} == ':') {
if ($state == ':') $text = substr($text,0,-10);
elseif ($state == ';') $text = substr($text,0,-5) . '<dt>';
else $text .= '<dl><dt>';
$i--;
$text .= "\n" . autoformat_handle($i, $current_prefix . ':', false);
$text .= '</dt></dl>';
$state = ':';
} elseif ($line{0} == ';') {
if ($state == ';') $text = substr($text,0,-10);
elseif ($state == ':') $text = substr($text,0,-5) . '<dd>';
else $text .= '<dl><dd>';
$i--;
$text .= "\n" . autoformat_handle($i, $current_prefix . ';', false);
$text .= '</dd></dl>';
$state = ';';
} elseif ($line{0} == '>') {
// source code
if ($state == '>') $text = substr($text,0,-6);
else $text .= '<pre>';
$text .= syntax_highlight(htmlspecialchars(substr($line, 1)));
$text .= "\n</pre>";
$state = '>';
} elseif ($line{0} == ']') {
// other code
if ($state == ']') $text = substr($text,0,-6);
else $text .= '<pre>';
$text .= htmlspecialchars(substr($line, 1));
$text .= "\n</pre>";
$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 .= '<p>';
}
$text .= autoformat_line($line) . "\n";
if ($first) {
$text .= '</p>';
}
$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 = '<table>';
foreach ($newrows as $k=>$r) {
$text .= $k %2 == 0 ? '<tr class="even">' : '<tr class="odd">';
$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 .= "</$td>";
}
}
$text .= '</tr>';
}
$text .= '</table>';
$autoformat_lines = $l;
return $text;
}
/**
* Expand formting tags inside a single line,
*/
function autoformat_line($line) {
$line = preg_replace("/'''(.*?)'''/", "<strong>\\1</strong>", $line);
$line = preg_replace("/''(.*?)''/", "<em>\\1</em>", $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 '<tt>' . syntax_highlight(htmlspecialchars($matches[1])) . '</tt>';
}
function autoformat_link($matches) {
return '<a href="' . autoformat_url($matches[1]) . '">' . autoformat_title($matches[1]) . $matches[2] . '</a>';
}
function autoformat_link_s($matches) {
return '<a href="' . autoformat_url($matches[1]) . '">' . $matches[2] . $matches[3] . '</a>';
}
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
<?php
// Syntax highlighting of script and reader code
function syntax_highlight($code) {
if (preg_match("@^(\s*#.*\n)*\s*[a-zA-Z0-9 _/,]+:[^=]@", $code)) {
return highlight_reader($code);
} else {
return highlight_script($code);
}
}
function highlight_reader($code) {
$ret = '';
$lines = explode("\n",$code);
$in_script = false;
for ($i = 0 ; $i < count($lines) ; ++$i) {
if ($i > 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 .= "<span class='hl-comment'>$rest</span>";
} 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] . "<span class='hl-key'>script:</span>" . highlight_script($matches[2]);
//} else if (preg_match("@\\s*rgb\\([0-9]+,[0-9]+,[0-9]+\\)\\s*@",$value,$matches)) {
// $value = highlight_script($value);
}
$ret .= "<span class='hl-key'>$key:</span>$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 .= "<span class='hl-kw'>" . $matches[0] . "</span>";
} else if (preg_match("@^(include file:)(.*)@",$code, $matches)) {
$ret .= "<span class='hl-key'>" . $matches[1] . "</span>" . $matches[2];
} else if (preg_match("@^[0-9][0-9.]*@",$code, $matches)) {
$ret .= "<span class='hl-num'>" . $matches[0] . "</span>";
} else if (preg_match("@^(\"|&quot;)(\\\\.|[^\\\\])*?(\"|&quot;)@",$code, $matches)) {
$ret .= "<span class='hl-str'>" . $matches[0] . "</span>";
} else if (preg_match("@^\\#.*@",$code, $matches)) {
$ret .= "<span class='hl-comment'>" . $matches[0] . "</span>";
} else if (preg_match("@^([-+*/=!.]|&lt;|&gt;)+|^:=@",$code, $matches)) {
$ret .= "<span class='hl-op'>" . $matches[0] . "</span>";
} else if (preg_match("@^[\\(\\)\\[\\]{},]+@",$code, $matches)) {
$ret .= "<span class='hl-paren'>" . $matches[0] . "</span>";
} else if (preg_match("@^[a-zA-Z_][a-zA-Z0-9_]*:@",$code, $matches)) {
$ret .= "<span class='hl-ckey'>" . $matches[0] . "</span>";
} 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
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