1+ <?php
2+
3+ /**
4+ * -----------------------------------------
5+ * TEXY! THIRD PARTY SYNTAX HIGHLIGHTING
6+ * -----------------------------------------
7+ *
8+ * Copyright (c) 2004-2005, David Grudl <[email protected] >. All rights reserved. 9+ * Web: http://www.texy.info/
10+ *
11+ * This program is free software; you can redistribute it and/or modify
12+ * it under the terms of the GNU General Public License as published by
13+ * the Free Software Foundation; either version 2 of the License, or
14+ * (at your option) any later version.
15+ *
16+ * This program is distributed in the hope that it will be useful,
17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+ * GNU General Public License for more details.
20+ *
21+ */
22+
23+
24+ /**
25+ * This demo shows how combine Texy! with syntax highlighter GESHI
26+ * - define user callback (for /--code elements)
27+ * - load language, highlight and retun stylesheet + html output
28+ */
29+
30+
31+ // check required version
32+ if (version_compare (phpversion (), '4.3.3 ' , '< ' ))
33+ die ('Texy! requires PHP version 4.3.3 or higher ' );
34+
35+
36+ $ texyPath = '../../texy/ ' ;
37+ $ geshiPath = dirname (__FILE__ ).'/geshi/ ' ;
38+
39+
40+ // include libs
41+ require_once ($ texyPath . 'texy.php ' );
42+ include_once ($ geshiPath .'geshi.php ' );
43+
44+
45+
46+
47+
48+ // this is user callback function for processing blocks
49+ //
50+ // /---code lang
51+ // ......
52+ // ......
53+ // \---
54+ //
55+ // $element is TexyCodeBlockElement object
56+ // $element->content - content of element
57+ // $element->htmlSafe - is content HTML safe?
58+ // $element->tag - parent HTML tag, default value is 'pre'
59+ // $element->type - type of content: code | samp | kbd | var | dfn (or empty value)
60+ // $element->lang - language (optional)
61+ //
62+ // Syntax highlighter changes $element->content and sets $element->htmlSafe to true
63+ //
64+ function myUserFunc (&$ element ) {
65+ global $ geshiPath ;
66+
67+ if ($ element ->lang == 'html ' ) $ element ->lang = 'html4strict ' ;
68+ $ geshi = new GeSHi ($ element ->content , $ element ->lang , $ geshiPath .'geshi/ ' );
69+
70+ if ($ geshi ->error ) // GeSHi could not find the language, nothing to do
71+ return ;
72+
73+ // do syntax-highlighting
74+ $ geshi ->set_encoding (TEXY_UTF8 ? 'UTF-8 ' : 'ISO-8859-1 ' );
75+ $ geshi ->set_header_type (GESHI_HEADER_PRE );
76+ $ geshi ->enable_classes ();
77+ $ geshi ->set_overall_style ('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0; ' , true );
78+ $ geshi ->set_line_style ('font: normal normal 95% \'Courier New \', Courier, monospace; color: #003030; ' , 'font-weight: bold; color: #006060; ' , true );
79+ $ geshi ->set_code_style ('color: #000020; ' , 'color: #000020; ' );
80+ $ geshi ->set_link_styles (GESHI_LINK , 'color: #000060; ' );
81+ $ geshi ->set_link_styles (GESHI_HOVER , 'background-color: #f0f000; ' );
82+
83+ // save generated stylesheet
84+ $ element ->texy ->styleSheet .= $ geshi ->get_stylesheet ();
85+
86+ $ out = $ geshi ->parse_code ();
87+ if (TEXY_UTF8 ) // double-check buggy GESHI, it sometimes produce not UTF-8 valid code :-((
88+ if ($ out !== utf8_encode (utf8_decode ($ out ))) return ;
89+
90+ $ element ->setContent ($ out , true );
91+ }
92+
93+
94+
95+
96+
97+
98+ $ texy = &new Texy ();
99+
100+ // set user callback function for /-- code blocks
101+ $ texy ->modules ['TexyBlockModule ' ]->userFunction = 'myUserFunc ' ;
102+ // prepare CSS stylesheet
103+ $ texy ->styleSheet = 'pre { padding:10px } ' ;
104+
105+ // processing
106+ $ text = file_get_contents ('sample.texy ' );
107+ $ html = $ texy ->process ($ text ); // that's all folks!
108+
109+ // echo Geshi Stylesheet
110+ echo '<style type="text/css"> ' . $ texy ->styleSheet . '</style> ' ;
111+ echo '<title> ' . $ texy ->headings ->title . '</title> ' ;
112+ // echo formated output
113+ echo $ html ;
114+
115+ // and echo generated HTML code
116+ echo '<hr /> ' ;
117+ echo '<pre> ' ;
118+ echo htmlSpecialChars ($ html );
119+ echo '</pre> ' ;
120+
121+ ?>
0 commit comments