For updated information and discussion, we recommend you view this page on the main server: click here
How Formatters work and why it is easy to write your own
See also
- Explanation for users
- Wanted Wikka formatters
- SyntaxHighlighter
- GeSHi - WikkaGeshiIntegration
- Explanation for users
- Wanted Wikka formatters
- SyntaxHighlighter
- GeSHi - WikkaGeshiIntegration
What php-code is used?
- In the above example the "ini" formatter was selected, so the file formatters/ini.php was included.
- Within this script you have access to a variable $text, a string that contains the code, content of the block.
- Within the formatter process $text (mostly string manipulations) and print the result to stdout.
- note: the ";line-number" is for the moment only used in the GeSHi code.
How do we get there?
- When you display a normal page, the default handlers/page/show.php handler will call at some point $this->Format($this->page["body"], "wakka") which includes the formatters/wakka.php code.
- Within formatters/wakka.php the %%(formatter[;line-number]) code %% is detected and
- In " else if (preg_match("/^%%(.*?)%%$/s", $thing, $matches)) " a new call to $wakka->Format($code, $language); is made.
- $language = "ini" in this example and $code is our content (and will become $text).
Code examples
- preg_replace patterns in $text: formatters/ini.php
<?php
/**
* INI language file for Wikka highlighting (configuration file).
*/
$text = htmlspecialchars($text, ENT_QUOTES);
$text = preg_replace("/([=,\|]+)/m","<span style=\"color:#4400DD\">\\1</span>",$text);
$text = preg_replace("/^([;#].+)$/m","<span style=\"color:#226622\">\\1</span>",$text);
$text = preg_replace("/([^\d\w#;:>])([;#].+)$/m","<span style=\"color:#226622\">\\2</span>",$text);
$text = preg_replace("/^(\[.*\])/m","<strong style=\"color:#AA0000;background:#EEE0CC\">\\1</strong>",$text);
print "<pre>".$text."</pre>";
?>
/**
* INI language file for Wikka highlighting (configuration file).
*/
$text = htmlspecialchars($text, ENT_QUOTES);
$text = preg_replace("/([=,\|]+)/m","<span style=\"color:#4400DD\">\\1</span>",$text);
$text = preg_replace("/^([;#].+)$/m","<span style=\"color:#226622\">\\1</span>",$text);
$text = preg_replace("/([^\d\w#;:>])([;#].+)$/m","<span style=\"color:#226622\">\\2</span>",$text);
$text = preg_replace("/^(\[.*\])/m","<strong style=\"color:#AA0000;background:#EEE0CC\">\\1</strong>",$text);
print "<pre>".$text."</pre>";
?>
- Do something with every line in the block:
<?php
foreach(split("\n", $text) as $n => $line){
if(preg_match("/^\s*$/",$line)) continue; // ignore empty lines
// process $line here...
}
?>
foreach(split("\n", $text) as $n => $line){
if(preg_match("/^\s*$/",$line)) continue; // ignore empty lines
// process $line here...
}
?>
CategoryEN - CategoryReview