File: demo-geshi.php

package info (click to toggle)
php-texy 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 680 kB
  • ctags: 1,299
  • sloc: php: 4,754; makefile: 4
file content (100 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

/**
 * This demo shows how combine Texy! with syntax highlighter GESHI
 *       - define user callback (for /--code elements)
 *       - load language, highlight and return stylesheet + html output
 */


// include libs
require_once dirname(__FILE__) . 'Texy.php';


$geshiPath = dirname(__FILE__).'/geshi/';
@include_once $geshiPath . 'geshi.php';


if (!class_exists('Geshi')) {
	die('DOWNLOAD <a href="http://qbnz.com/highlighter/">GESHI</a> AND UNPACK TO GESHI 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();
	}

	$texy = $invocation->getTexy();

	global $geshiPath;

	if ($lang == 'html') $lang = 'html4strict';
	$content = Texy::outdent($content);
	$geshi = new GeSHi($content, $lang, $geshiPath.'geshi/');

	// GeSHi could not find the language
	if ($geshi->error) {
		return $invocation->proceed();
	}

	// do syntax-highlighting
	$geshi->set_encoding('UTF-8');
	$geshi->set_header_type(GESHI_HEADER_PRE);
	$geshi->enable_classes();
	$geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true);
	$geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true);
	$geshi->set_code_style('color: #000020;', 'color: #000020;');
	$geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
	$geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');

	// save generated stylesheet
	$texy->styleSheet .= $geshi->get_stylesheet();

	$content = $geshi->parse_code();

	// check buggy GESHI, it sometimes produce not UTF-8 valid code :-((
	$content = iconv('UTF-8', 'UTF-8//IGNORE', $content);

	// protect output is in HTML
	$content = $texy->protect($content, Texy::CONTENT_BLOCK);

	$el = TexyHtml::el();
	$el->setText($content);
	return $el;
}


$texy = new Texy();
$texy->addHandler('block', 'blockHandler');

// prepare CSS stylesheet
$texy->styleSheet = 'pre { padding:10px } ';

// 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">'. $texy->styleSheet . '</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>';