Syntax Highlight con HTML Purifier y GeSHi
Martes 4 de Marzo del 2008 - Desarrollo Web, Programación
En Syntax Highlighting and Allowing HTML in Comments, Jay Pipes nos explica cómo han utilizado las librerias PHP HTMLPurifier de Edward Yang y GeSHi de Nigel McNie para crear la funcionalidad de Sytax Highlight en el nuevo MySQL Forge.
Además nos facilitan las dos elegantes funciones ( clean_and_codify y syntax_highlight ) que han acabado utilizando en "vivo":
PHP:
-
/**
-
* Highlights the text as code in the supplied language
-
*
-
* @return string The marked up code
-
* @param subject The text to markup
-
* @param language The language to use for highlighting
-
*/
-
/* Format the code with GeSHi */
-
include_once(APP_DIR . '/opt/geshi/geshi.php');
-
$geshi= new GeSHi($subject, $language);
-
$geshi->enable_classes();
-
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
-
return $geshi->parse_code();
-
}
-
-
/**
-
* Returns a cleaned and syntax-highlighted string of HTML
-
*
-
* @return string Cleaned and codified text
-
* @param subject The text to cut into code pieces
-
*/
-
$original= $subject;
-
$code_regex= '/[\[\<]code\s*(lang|language)\=[\"\'](\w+)[\"\'][\]\>]([\D\S]+?)[\[\<]\/code[\]\>]/';
-
$code_delimiter= "CODECODECODE";
-
-
/* First split the text into code and non-code blocks */
-
$code_sample= $code_matches[3];
-
$entire_code_string= $code_matches[0];
-
, 'text'=>$code_sample);
-
}
-
-
/*
-
* Assume two consecutive newlines are a paragraph.
-
*/
-
/* Normalize Newlines */
-
-
/*
-
* Next, do the same thing with markup sections
-
* We use HTMLPurifier here for safe checks with some allowed
-
* tags for ease of use
-
*/
-
include_once(APP_DIR . '/opt/htmlpurifier/library/HTMLPurifier.auto.php');
-
$config = HTMLPurifier_Config::createDefault();
-
$config->set('HTML', 'Doctype', 'XHTML 1.0 Transitional');
-
$config->set('HTML', 'AllowedElements', 'a,em,blockquote,p,code,pre,strong,b');
-
$config->set('HTML', 'AllowedAttributes', 'a.href,a.title');
-
$config->set('HTML', 'TidyLevel', 'light'); // should be enough since we don't allow many elements. this really just cleans up dangling elements...
-
$purifier= new HTMLPurifier();
-
$subject= $purifier->purify($subject, $config);
-
-
/*
-
* Now $subject should contain CleanMarkup\n|||CODE|||\nCleanMarkup...
-
* We now replace the code sections by passing an executable string
-
* to the regex parser (the /e option) and using the syntax_highlight
-
* function to do the grunt work
-
*/
-
$i= 0;
-
if ($num_code_pieces> 0) {
-
$replacement= "TextDecorator::syntax_highlight(trim(\$code_pieces[\$i]['text'], \"\r\n \"), \$code_pieces[\$i++]['lang']);";
-
}
-
return $subject;
-
}
Términos relacionados: