Tu testujem použitie Texy! pre syntax highlighting kódu. Funguje to celkom fajn. Integroval som FSHL a ofarbovanie funguje výborne. Pokiaľ nie je integrovaný žiadny syntax highlighter, tak sa apoň PHP ofarbí cez štandardnú PHP funkciu highlight_string() (samozrejme pokiaľ je v texy.module syntax highlighting aktivovaný).
Modul texy.module je nainštalovaný v adresári modules/texy. Samotné Texy! musí byť rozbalené v podadresári modules/texy/texy (konkrétne obsah podadresára texy-1.1/texy z distribúcie Texy! ( texy-1.1.tar.gz), rozbalíte do adresára modules/texy/texy).
Kód texy.module
Obsah súboru texy.module
<?php
// $Id: texy.module,v 1.1 2006/09/03 15:13:14 havran Exp $
/**
* @file
* DRUPAL MODUL - Texy!
*/
/**
* Implementation of hook_help().
*/
function texy_help($section) {
switch ($section) {
case 'admin/help#texy':
$output = '<p>Texy!</p>';
return $output;
case 'admin/modules#description':
return 'Texy!';
}
}
/**
* Implementation of hook_menu().
*/
function texy_settings() {
$form = array();
$module_path = drupal_get_path('module', 'texy');
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('General Settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['general']['texy_use_syntaxhighlight'] = array(
'#type' => 'checkbox',
'#title' => t('Use code syntax highlighting'),
'#description' => t('Check if you want highlight code.'),
'#default_value' => variable_get('texy_use_syntaxhighlight', false),
);
$form['general']['texy_syntaxhighlighter_name'] = array(
'#type' => 'textfield',
'#size' => '12',
'#title' => t('Name for syntax highlighter module'),
'#description' => t('Syntax highlighter module which Texy! use for syntax
highlighting. Module must be installed in to <code>'.$module_path.'/lib/syntaxhighlightermodulename</code>
directory and there must be <code>'.$module_path.'/lib/syntaxhighlightermodulename.php</code> file,
with php code for Texy! implementation this highlighter module. For example I use FSHL module.
I have directory <code>'.$module_path.'/lib/fshl</code> and code <code>'.$module_path.'/lib/fshl.php</code>
and name for syntax highlighter module is set to <em>fshl</em>.'),
'#default_value' => variable_get('texy_syntaxhighlighter_name', ''),
);
return $form;
}
/**
* Implementation of hook_menu().
*/
function texy_menu($may_cache) {
$items = array();
if ($may_cache) {
}
else {
// trick for add styles
theme_add_style(drupal_get_path('module', 'texy') . '/lib/fshl/styles/COHEN_style.css');
}
return $items;
}
/**
* Implementation of hook_filter().
*/
function texy_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(0 => 'Texy! list');
case 'description':
switch ($delta) {
case 0:
return 'Texy! description.';
default:
return;
}
case 'process':
switch ($delta) {
case 0:
return _filter_texy($text);
default:
return $text;
}
case 'settings':
switch ($delta) {
default:
return;
}
default:
return $text;
}
}
/**
* Implementation of hook_filter_tips().
*/
function texy_filter_tips($delta, $format, $long = false) {
return 'Texy! tips';
}
function _filter_texy($text) {
// get base module path
$module_path = drupal_get_path('module', 'texy');
// include Texy!
require_once $module_path . '/texy/texy.php';
$texy = &new Texy();
// check for use syntax highlighter
if (variable_get('texy_use_syntaxhighlight', false)) {
$texy_syntaxhighlighter_name = variable_get('texy_syntaxhighlighter_name', '');
if ($texy_syntaxhighlighter_name == '') {
// set user callback function for /-- code blocks (PHP only)
$texy->blockModule->codeHandler = '_filter_texy_basic_highlighter';
}
else {
$texy_syntaxhighlighter = $module_path . '/lib/' . $texy_syntaxhighlighter_name . '.php';
if (file_exists($texy_syntaxhighlighter)) {
require_once $texy_syntaxhighlighter;
}
else {
drupal_set_message(t('Syntax highlighter <em>' . $texy_syntaxhighlighter_name . '</em> is not ready...'),'error');
}
}
}
// other OPTIONAL configuration
$texy->utf = TRUE; // enable UTF-8
$texy->imageModule->root = '_img/'; // specify image folder
$texy->headingModule->top = variable_get('texy_heading_base', 2);
$html = $texy->process($text);
return $html;
}
function _filter_texy_basic_highlighter(&$element) {
$lang = strtoupper($element->lang);
if ($lang != 'PHP'){
return;
}
$element->setContent(highlight_string("<?php\n" . decode_entities($element->content) . "\n?>", TRUE), TRUE);
}
Kód pre integráciu FSHL
Obsah súboru fshl.php:
<?php
// include syntaxhighlighter
require_once 'fshl/fshl.php';
require_once 'fshl/fshl-config.php';
// set user callback function for /-- code blocks
$texy->blockModule->codeHandler = '_filter_texy_fshl_highlighter';
function _filter_texy_fshl_highlighter(&$element) {
$lang = strtoupper($element->lang);
if ($lang == 'JAVASCRIPT') $lang = 'JS';
if (!in_array($lang,array('CPP', 'CSS', 'HTML', 'JAVA', 'PHP', 'JS', 'SQL'))) {
return;
}
//$parser_options = P_TAB_INDENT | P_LINE_COUNTER; // show tabs and line numbers
$parser_options = P_TAB_INDENT; // show tabs
$parser = new fshlParser($element->texy->utf ? 'HTML_UTF8' : 'HTML', $parser_options);
$element->setContent($parser->highlightString($lang, $element->content), TRUE);
}
FSHL je nakopírovaný v adresári texy/lib/fshl (konkrétne obsah adresára fshl-0.4.17/fshl (z FSHL distribúcie fshl-0.4.17.zip) do adresára texy/lib/fshl). V adresári texy/lib sa nachádza súbor fshl.php (obsah ktorého ste mohli vidieť vyššie).
Aha a ešte jedna dôležitá finta. Aby boli kódy pekne vyfarbené, musí byť samozrejme niekde aj nejaké CSS pre ich správne vyfarbovanie. Zatiaľ je použitá finta vo funkcii texy_menu() – aby sa potrebný štýl vložil zakaždým do stránky, je v časti ktorá sa nekešuje použitá funkcia theme_add_style(). Táto funkcia vkladá štýl potrebný pre FSHL do stránky. Zatiaľ je to neumelé ale pre moje účely to bohate postačuje.



Komentáre
Test
Poslať nový komentár