1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
<?php
/**
* This demo shows how combine Texy! with syntax highlighter FSHL
* - define user callback (for /--code elements)
*/
// include libs
require_once dirname(__FILE__) . 'Texy.php';
$fshlPath = dirname(__FILE__).'/fshl/';
@include_once $fshlPath . 'fshl.php';
if (!class_exists('fshlParser')) {
die('DOWNLOAD <a href="http://hvge.sk/scripts/fshl/">FSHL</a> AND UNPACK TO FSHL FOLDER FIRST!');
}
/**
* User handler for code block
*
* @param TexyHandlerInvocation handler invocation
* @param string block type
* @param string text to highlight
* @param string language
* @param TexyModifier modifier
* @return TexyHtml
*/
function blockHandler($invocation, $blocktype, $content, $lang, $modifier)
{
if ($blocktype !== 'block/code') {
return $invocation->proceed();
}
$lang = strtoupper($lang);
if ($lang == 'JAVASCRIPT') $lang = 'JS';
$fshl = new fshlParser('HTML_UTF8', P_TAB_INDENT);
if (!$fshl->isLanguage($lang)) {
return $invocation->proceed();
}
$texy = $invocation->getTexy();
$content = Texy::outdent($content);
$content = $fshl->highlightString($lang, $content);
$content = $texy->protect($content, Texy::CONTENT_BLOCK);
$elPre = TexyHtml::el('pre');
if ($modifier) $modifier->decorate($texy, $elPre);
$elPre->attrs['class'] = strtolower($lang);
$elCode = $elPre->create('code', $content);
return $elPre;
}
$texy = new Texy();
$texy->addHandler('block', 'blockHandler');
// processing
$text = file_get_contents('sample.texy');
$html = $texy->process($text); // that's all folks!
// echo Geshi Stylesheet
header('Content-type: text/html; charset=utf-8');
echo '<style type="text/css">'. file_get_contents($fshlPath.'styles/COHEN_style.css') . '</style>';
echo '<title>' . $texy->headingModule->title . '</title>';
// echo formated output
echo $html;
// and echo generated HTML code
echo '<hr />';
echo '<pre>';
echo htmlSpecialChars($html);
echo '</pre>';
|