Para quien le pueda interesar, no he podido resistirme a lo siguiente.
Código PHP:
<?php // magic!
/*
delphi2html PHP class v1.0b
(w) 2005 David Esperalta Calderón
Thanks very much to the people of ClubDelphi Forums
([url]http://www.clubdelphi.com[/url]) and specially to Román
from the idea and tutorial. ¡Thanks to all! ;-)
Comments are welcome in davidesperalta @ gmail.com
or in the ClubDelphi Forums (see url above)
*/
/*
// Example usage 1
//
$colorize = new delphi2html();
$file = file('./delphi_unit.pas');
foreach ($file as $line) $temp .= $line;
echo $colorize->execute($temp, true, true);
*/
/*
// Example usage 2
//
$colorize = new delphi2html();
$text = "type\n";
$text .= " TForm1 = class(TForm)\n";
$text .= " { Private declarations }\n";
$text .= " private\n";
$text .= " procedure AMethod(param: string);\n";
$text .= " end;\n";
echo $colorize->execute($text, true, true);
*/
class delphi2html {
// Style (CSS code)
//
var $css_style =
"<style type='text/css'>
.str { background-color: transparent; color: Green; }
.idn { background-color: transparent; color: Black; }
.res { background-color: transparent; color: Navy; font-weight: bold; }
.com { background-color: transparent; color: Gray; font-style: italic; }
</style>\n";
// Reserved keywords in Delphi
//
var $keybords = array (
"and", "array", "as", "asm", "begin", "case", "class", "const",
"constructor", "destructor", "dispinterface", "div", "do", "downto",
"else", "end", "except", "exports", "file", "finalization", "finally",
"for", "function", "goto", "if", "implementation", "in", "inherited",
"initialization", "inline", "interface", "is", "label", "library",
"mod", "nil", "not", "object", "of", "or", "out", "packed", "procedure",
"program", "property", "raise", "record", "repeat", "resourcestring",
"set", "shl", "shr", "string", "then", "threadvar", "to", "try", "type",
"unit", "until", "uses", "var", "while", "with", "xor", "private",
"protected", "public", "published", "automated"
);
// Regular Exp for strings, identifiers and comments tokens.
//
var $reg_exp = "";
var $reg_exp_strings = "('.*?')|";
var $reg_exp_identifiers = "([a-z_][a-z0-9_]*)|";
var $reg_exp_comments = "(\{.*?\}|\(\*.*?\*\)|//.*?\n|//.*\$)";
// Class constructor.
//
function delphi2html() {
$this->reg_exp = "#".
$this->reg_exp_strings.
$this->reg_exp_identifiers.
$this->reg_exp_comments ."#si";
}
// Callback function.
//
function replace($matches) {
switch ($matches[0])
{
case $matches[1]: // strings
return "<span class='str'>" .$matches[1]. "</span>";
break;
case $matches[2]: // identifiers
if (in_array($matches[2], $this->keybords))
return "<span class='res'>" .$matches[2]. "</span>";
else
return "<span class='idn'>" .$matches[2]. "</span>";
break;
case $matches[3]: // comments
return "<span class='com'>" .$matches[3]. "</span>";
break;
}
}
// The main public method: execute
//
// param: code, string: the delphi source code to colorize.
// param: add_css_code, boolean: add the CSS code in result.
// param: add_pre_tags, boolean: add the <pre></pre> HTML tags.
//
// return: HTML delphi colorized text.
//
function execute($code, $add_css_code = true, $add_pre_tags = true) {
$result = preg_replace_callback($this->reg_exp,
array($this, 'replace'), $code);
if(!$add_pre_tags) {
if(!$add_css_code) { return $result; }
else { return $this->css_style . $result; }
} else {
if(!$add_css_code) { return "<pre>" .$result. "</pre>"; }
else { return $this->css_style . "<pre>" .$result. "</pre>"; }
}
} // function execute
} // class delphi2html
?>
.