|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * TEXY! THIRD PARTY SYNTAX HIGHLIGHTING |
| 5 | + * -------------------------------------- |
| 6 | + * |
| 7 | + * This demo shows how combine Texy! with syntax highlighter FSHL |
| 8 | + * - define user callback (for /--code elements) |
| 9 | + * |
| 10 | + * @author David Grudl (http://davidgrudl.com) |
| 11 | + * @version $Revision$ $Date$ |
| 12 | + */ |
| 13 | + |
| 14 | + |
| 15 | +// include libs |
| 16 | +require_once dirname(__FILE__).'/../../texy/texy.php'; |
| 17 | + |
| 18 | +$fshlPath = dirname(__FILE__).'/fshl/'; |
| 19 | +@include_once $fshlPath . 'fshl.php'; |
| 20 | + |
| 21 | + |
| 22 | +if (!class_exists('fshlParser')) { |
| 23 | + die('DOWNLOAD <a href="http://hvge.sk/scripts/fshl/">FSHL</a> AND UNPACK TO FSHL FOLDER FIRST!'); |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * User handler for code block |
| 30 | + * |
| 31 | + * @param TexyHandlerInvocation handler invocation |
| 32 | + * @param string block type |
| 33 | + * @param string text to highlight |
| 34 | + * @param string language |
| 35 | + * @param TexyModifier modifier |
| 36 | + * @return TexyHtml |
| 37 | + */ |
| 38 | +function blockHandler($invocation, $blocktype, $content, $lang, $modifier) |
| 39 | +{ |
| 40 | + if ($blocktype !== 'block/code') { |
| 41 | + return $invocation->proceed(); |
| 42 | + } |
| 43 | + |
| 44 | + $lang = strtoupper($lang); |
| 45 | + if ($lang == 'JAVASCRIPT') $lang = 'JS'; |
| 46 | + |
| 47 | + $fshl = new fshlParser('HTML_UTF8', P_TAB_INDENT); |
| 48 | + if (!$fshl->isLanguage($lang)) { |
| 49 | + return $invocation->proceed(); |
| 50 | + } |
| 51 | + |
| 52 | + $texy = $invocation->getTexy(); |
| 53 | + $content = Texy::outdent($content); |
| 54 | + $content = $fshl->highlightString($lang, $content); |
| 55 | + $content = $texy->protect($content, Texy::CONTENT_BLOCK); |
| 56 | + |
| 57 | + $elPre = TexyHtml::el('pre'); |
| 58 | + if ($modifier) $modifier->decorate($texy, $elPre); |
| 59 | + $elPre->attrs['class'] = strtolower($lang); |
| 60 | + |
| 61 | + $elCode = $elPre->create('code', $content); |
| 62 | + |
| 63 | + return $elPre; |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +/** |
| 69 | + * Pattern handler for PHP & JavaScript block syntaxes |
| 70 | + * |
| 71 | + * @param TexyBlockParser |
| 72 | + * @param array regexp matches |
| 73 | + * @param string pattern name |
| 74 | + * @return TexyHtml|string|FALSE |
| 75 | + */ |
| 76 | +function codeBlockHandler($parser, $matches, $name) |
| 77 | +{ |
| 78 | + list($content) = $matches; |
| 79 | + $lang = $name === 'phpBlockSyntax' ? 'PHP' : 'HTML'; |
| 80 | + |
| 81 | + $fshl = new fshlParser('HTML_UTF8', P_TAB_INDENT); |
| 82 | + $texy = $parser->getTexy(); |
| 83 | + $content = $fshl->highlightString($lang, $content); |
| 84 | + $content = $texy->protect($content, Texy::CONTENT_BLOCK); |
| 85 | + |
| 86 | + $elPre = TexyHtml::el('pre'); |
| 87 | + $elPre->attrs['class'] = strtolower($lang); |
| 88 | + |
| 89 | + $elCode = $elPre->create('code', $content); |
| 90 | + |
| 91 | + return $elPre; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +$texy = new Texy(); |
| 97 | +$texy->addHandler('block', 'blockHandler'); |
| 98 | + |
| 99 | +// add new syntax: <?php ... ? > |
| 100 | +$texy->registerBlockPattern( |
| 101 | + 'codeBlockHandler', |
| 102 | + '#^<\\?php\n.+\n\\?>$#ms', // block patterns must be multiline and line-anchored |
| 103 | + 'phpBlockSyntax' |
| 104 | +); |
| 105 | + |
| 106 | +// add new syntax: <script ...> ... </script> |
| 107 | +$texy->registerBlockPattern( |
| 108 | + 'codeBlockHandler', |
| 109 | + '#^<script(?: type=.?text/javascript.?)?>\n.+\n</script>$#ms', // block patterns must be multiline and line-anchored |
| 110 | + 'scriptBlockSyntax' |
| 111 | +); |
| 112 | + |
| 113 | +// processing |
| 114 | +$text = file_get_contents('sample2.texy'); |
| 115 | +$html = $texy->process($text); // that's all folks! |
| 116 | + |
| 117 | +// echo Geshi Stylesheet |
| 118 | +header('Content-type: text/html; charset=utf-8'); |
| 119 | +echo '<style type="text/css">'. file_get_contents($fshlPath.'styles/COHEN_style.css') . '</style>'; |
| 120 | +echo '<title>' . $texy->headingModule->title . '</title>'; |
| 121 | +// echo formated output |
| 122 | +echo $html; |
| 123 | + |
| 124 | +// and echo generated HTML code |
| 125 | +echo '<hr />'; |
| 126 | +echo '<pre>'; |
| 127 | +echo htmlSpecialChars($html); |
| 128 | +echo '</pre>'; |
0 commit comments