The new version VIM Color module has been patched for the drupal version 6.x by zhangyingda
| Project: | VIM Color |
| Version: | HEAD |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | zhangyingda |
| Status: | active |
I want to use the VIM Color Module in my drupal which version is 6.9, but it won't work. So I patched the code and it work fine now! I drop the code for php, and I only test it on C language. Now the module need three files only:
vimcolor.info
;$ld$
name=Vim Color
description=Allow users to color the code
package=Other
version="6.x-1.0-dev"
core="6.x"
project="vimcolor
datestamp="1204818604"vimcolor.css
div.codeblock {
padding: 5px;
margin-bottom:10px;
border: 1px solid #CCC;
background-color: #EEE;
}
div.codeblock pre {
line-height:0.5em;
}
.synComment { color: #0000FF }
.synConstant { color: #0000FF }
.synIdentifier { color: #008B8B }
.synStatement { color: #A52A2A ; font-weight: bold }
.synPreProc { color: #A020F0 }
.synType { color: #2E8B57 ; font-weight: bold }
.synSpecial { color: #6A5ACD }
.synUnderlined { color: #000000 ; text-decoration: underline }
.synError { color: #FFFFFF ; background: #FF0000 none }
.synTodo { color: #0000FF ; background: #FFFF00 none }vimcolor.module
<?php
// $Id: vimcolor.module,v 1.1 2005/04/26 15:40:57 arandeltac Exp $
/**
* Implementation of hook_filter_tips()
*/
function vimcolor_filter_tips($delta, $format, $long = FALSE) {
if ($long) {
return t(
To post pieces of code, surround them with <code type="language">...</code> tags.
Set the language to whatever programming language the code is written in (such as php, perl, cpp, etc).
If you do not specify a language type then the language will be guessed.
For PHP code, you can use <?php ...
?>');
}
else {
return t('You may post code using
..., ... (auto-detect), or <?php
...
?>}
}
/**
* Implementation of hook_init()
*
* Adds the vimcolor stylesheet to the page
*/
function vimcolor_init(){
drupal_add_css(drupal_get_path('module','vimcolor') .'/vimcolor.css','module','all',FALSE);
}
/**
* Processes chunk of escaped colde into HTML
*
*/
function vimcolor_process_color($text,$type) {
// test the $text is multiline
$multiline = ereg("[\n\r]", $text);
// Note, pay attention to odd preg_replace-with-/e behaviour on slashes
// Undo possible linebreak filter conversion
$text = preg_replace('@@', '', str_replace('"', '"', $text));
// Undo the escaping in the prepare step
$text = htmlspecialchars_decode($text);
// Trim leading and trailing linebreaks
$text = trim($text, "\r\n");
// Highlight as Code
$in_file = tempnam(ini_get('upload_tmp_dir'), 'pl');
$out_file = tempnam(ini_get('upload_tmp_dir'), 'htm');
$handle = fopen($in_file, "w");
fwrite($handle, $text);
fclose($handle);
if($type){
$type = '--filetype '.$type;
}
//system('/usr/bin/text-vimcolor --format html '.$type.' '.$in_file.' --output '.$out_file.' --full-page');
system('/usr/bin/text-vimcolor --format html '.$type.' '.$in_file.' --output '.$out_file);
$handle = fopen($out_file, "r");
$html = fread($handle, filesize($out_file));
fclose($handle);
$html=nl2br($html);
$html=''.$html.'';
if( $multiline ){
//$html = preg_replace('/\r/s','',$html);
//$html = preg_replace('/\n/s','',$html);
$html = ''. $html .'';
//$html = preg_replace('/ /s',' ',$html);
}else{
$html = ''. trim($html) .'';
}
return $html;
}
function vimcolor_fix_indent($text) {
return str_replace(' ', ' ', $text[0]);
}
function vimcolor_fix_spaces($text){
return preg_replace('@ (?! )@',' ',$text);
}
function vimcolor_escape($text) {
// check_plain return drupal_validate_utf8($text) ? htmlspecialchars($text, ENT_QUOTES) : '';
// Note, pay attention to odd preg_replace-with-/e behaviour on slashes
return check_plain(str_replace('"', '"', $text));
}
function vimcolor_filter($op, $delta = 0, $format = -1, $text = '') {
// The "list" operation provides the module an opportunity to declare both how
// many filters it defines and a human-readable name for each filter. Note that
// the returned name should be passed through t() for translation.
// Get ordered list of filters
if($op=='list'){
return array(t('C & C++ Code filter'));
}
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Allows users to post code verbatim using
, <code type="language">, and <?php ?> tags.');
case 'settings':
break;
case 'no cache':
return TRUE;
// Prepare to filter
case 'prepare':
$text = preg_replace('#<code type="([a-z]+)"?>(.*?)'.vimcolor_escape('\\2') .''", $text);/*
$text = preg_replace('#[\[<](\?php|%)(.+?)(\?|%)[\]>]#se', "'
'. vimcolor_escape('<?php
2
?>') .''", $text);*/
return $text;
// Filter
case "process":
$text = preg_replace('#(.*?)#se', "vimcolor_process_color('$2','$1')", $text);
return $text;
default:
return $text;
}
}
?>
| Attachment | Size |
|---|---|
| vimcolor.tar_.gz | 8.77 KB |
