Skip to content

Commit

Permalink
new example syntax highlighting/demo-fshl-alt.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 9, 2008
1 parent eadc6ce commit bd2f995
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 15 deletions.
6 changes: 3 additions & 3 deletions changelog.cs.texy
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ rev. 185
- implementován Nette_Object z Nette Frameworku. Nahradil TexyBase

rev. 181
- TexyHtml::$children je nyní private. K potomkům se přistupuje přes ArrayAcces interface ("viz":http://www.latrine.cz/nhtml-pomocnik-php-programatora#comment-12140)
- TexyHtml::$children je nyní private. K potomkům se přistupuje přes ArrayAcces interface ("viz":http://phpfashion.com/nette-web-html-pomocnik-php-programatora#comment-12140)

rev. 180
- nevkládá `­` do URL

rev. 179
- POZOR: přejmenováno TexyHtml::add() -> create(), TexyHtml::addChild() -> add() ("důvody":http://www.latrine.cz/nhtml-pomocnik-php-programatora#comment-12100)
- POZOR: přejmenováno TexyHtml::add() -> create(), TexyHtml::addChild() -> add() ("důvody":http://phpfashion.com/nette-web-html-pomocnik-php-programatora#comment-12100)
- verze pro PHP4: emulace třídy Exception a throw
- chytřejší detekce emailů a URL v textu

Expand Down Expand Up @@ -261,7 +261,7 @@ htmlOutputModule

TexyDOM a zděděné třídy
-----------------------
- odstraněno, náhradou je TexyHtml (obdoba NHtml, viz http://www.latrine.cz/nhtml-pomocnik-php-programatora)
- odstraněno, náhradou je TexyHtml (obdoba NHtml, viz http://phpfashion.com/nette-web-html-pomocnik-php-programatora)
- vlastnost TexyHtml::$xhtml - přepínač mezi XHTML a HTML výstupem
- ve všech handlerech se nyní operuje s elementy reprezentovanými tímto objektem, manipulace je tedy extrémně snadná

Expand Down
2 changes: 1 addition & 1 deletion examples/Flash movie/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function imageHandler($invocation, $image, $link)
$movie = htmlSpecialChars($movie);
$altContent = htmlSpecialChars($image->modifier->title);

// @see http://www.latrine.cz/how-to-correctly-insert-a-flash-into-xhtml
// @see http://phpfashion.com/how-to-correctly-insert-a-flash-into-xhtml
$code = '
<!--[if !IE]> -->
<object type="application/x-shockwave-flash" data="'.$movie.'" '.$dimensions.'>
Expand Down
8 changes: 4 additions & 4 deletions examples/HTML filtering/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function doIt($texy)

echo '<h2>Enable custom tags</h2>';
$texy->allowedTags =
array( // enable only tags <myExtraTag> with attribute & <strong>
'myExtraTag' => array('attr1'),
'strong' => array(),
);
array( // enable only tags <myExtraTag> with attribute & <strong>
'myExtraTag' => array('attr1'),
'strong' => array(),
);
doIt($texy);
128 changes: 128 additions & 0 deletions examples/syntax highlighting/demo-fshl-alt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/**
* TEXY! THIRD PARTY SYNTAX HIGHLIGHTING
* --------------------------------------
*
* This demo shows how combine Texy! with syntax highlighter FSHL
* - define user callback (for /--code elements)
*
* @author David Grudl (http://davidgrudl.com)
* @version $Revision$ $Date$
*/


// include libs
require_once dirname(__FILE__).'/../../texy/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;
}



/**
* Pattern handler for PHP & JavaScript block syntaxes
*
* @param TexyBlockParser
* @param array regexp matches
* @param string pattern name
* @return TexyHtml|string|FALSE
*/
function codeBlockHandler($parser, $matches, $name)
{
list($content) = $matches;
$lang = $name === 'phpBlockSyntax' ? 'PHP' : 'HTML';

$fshl = new fshlParser('HTML_UTF8', P_TAB_INDENT);
$texy = $parser->getTexy();
$content = $fshl->highlightString($lang, $content);
$content = $texy->protect($content, Texy::CONTENT_BLOCK);

$elPre = TexyHtml::el('pre');
$elPre->attrs['class'] = strtolower($lang);

$elCode = $elPre->create('code', $content);

return $elPre;
}



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

// add new syntax: <?php ... ? >
$texy->registerBlockPattern(
'codeBlockHandler',
'#^<\\?php\n.+\n\\?>$#ms', // block patterns must be multiline and line-anchored
'phpBlockSyntax'
);

// add new syntax: <script ...> ... </script>
$texy->registerBlockPattern(
'codeBlockHandler',
'#^<script(?: type=.?text/javascript.?)?>\n.+\n</script>$#ms', // block patterns must be multiline and line-anchored
'scriptBlockSyntax'
);

// processing
$text = file_get_contents('sample2.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>';
9 changes: 5 additions & 4 deletions examples/syntax highlighting/demo-fshl.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
@include_once $fshlPath . 'fshl.php';


if (!class_exists('fshlParser'))
if (!class_exists('fshlParser')) {
die('DOWNLOAD <a href="http://hvge.sk/scripts/fshl/">FSHL</a> AND UNPACK TO FSHL FOLDER FIRST!');
}



Expand All @@ -43,14 +44,14 @@ function blockHandler($invocation, $blocktype, $content, $lang, $modifier)
$lang = strtoupper($lang);
if ($lang == 'JAVASCRIPT') $lang = 'JS';

$parser = new fshlParser('HTML_UTF8', P_TAB_INDENT);
if (!$parser->isLanguage($lang)) {
$fshl = new fshlParser('HTML_UTF8', P_TAB_INDENT);
if (!$fshl->isLanguage($lang)) {
return $invocation->proceed();
}

$texy = $invocation->getTexy();
$content = Texy::outdent($content);
$content = $parser->highlightString($lang, $content);
$content = $fshl->highlightString($lang, $content);
$content = $texy->protect($content, Texy::CONTENT_BLOCK);

$elPre = TexyHtml::el('pre');
Expand Down
3 changes: 2 additions & 1 deletion examples/syntax highlighting/demo-geshi.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
@include_once $geshiPath . 'geshi.php';


if (!class_exists('Geshi'))
if (!class_exists('Geshi')) {
die('DOWNLOAD <a href="http://qbnz.com/highlighter/">GESHI</a> AND UNPACK TO GESHI FOLDER FIRST!');
}



Expand Down
33 changes: 33 additions & 0 deletions examples/syntax highlighting/sample2.texy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Syntax highlighting in Texy!
****************************



How use clone in PHP4
---------------------

This example show how to implement PHP5 `clone` construction in older PHP4

<?php
if (((int) phpversion() < 5) && (!function_exists('clone')))
eval('function &clone($obj) { return $obj; }');

$obj = new Object();
$dolly = clone ($obj);
?>




And Now for Something Completely Different:
-------------------------------------------

<script type="text/javascript">
// function trim() in JavaScript

function trim(s) {
while (s.substr(0, 1) == ' ') s = s.substr(1);
while (s.substr(s.length-1, 1) == ' ') s = s.substr(0, s.length-2);
return s;
}
</script>
2 changes: 1 addition & 1 deletion readme.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ HTML kód najdete v adresáři /icons/

-----
Chcete-li další informace, navštivte autorův weblog:
http://www.latrine.cz/
http://phpfashion.com
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ Choose buttons and look at exemplary HTML code in directory /icons/

-----
For more information, visit the author's weblog (in czech language):
http://www.latrine.cz/
http://phpfashion.com

0 comments on commit bd2f995

Please sign in to comment.