Skip to content

Instantly share code, notes, and snippets.

@dg
Created September 5, 2012 20:36
Show Gist options
  • Save dg/3644321 to your computer and use it in GitHub Desktop.
Save dg/3644321 to your computer and use it in GitHub Desktop.
Texy on nette.org
<?php
/**
* Texy parser for wiki page.
*/
class Parser extends Nette\Object
{
/**
* @return void
*/
public function parse($text)
{
$texy = new Texy;
$texy->setOutputMode(Texy::HTML4_TRANSITIONAL);
$texy->linkModule->root = '';
$texy->alignClasses['left'] = 'left';
$texy->alignClasses['right'] = 'right';
$texy->emoticonModule->class = 'smiley';
$texy->headingModule->top = 1;
$texy->headingModule->generateID = TRUE;
$texy->tabWidth = 4;
$texy->tableModule->evenClass = 'alt';
$texy->dtd['body'][1]['style'] = TRUE;
$texy->allowed['longwords'] = FALSE;
$texy->allowed['block/html'] = FALSE;
$texy->phraseModule->tags['phrase/strong'] = 'b';
$texy->phraseModule->tags['phrase/em'] = 'i';
$texy->phraseModule->tags['phrase/em-alt'] = 'i';
$texy->addHandler('block', array($this, 'blockHandler'));
}
/**
* 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
*/
public function blockHandler($invocation, $blocktype, $content, $lang, $modifier)
{
if ($blocktype === 'block/php' || $blocktype === 'block/neon' || $blocktype === 'block/javascript' || $blocktype === 'block/js' || $blocktype === 'block/css' || $blocktype === 'block/html' || $blocktype === 'block/htmlcb') {
list(, $lang) = explode('/', $blocktype);
} elseif ($blocktype !== 'block/code') {
return $invocation->proceed($blocktype, $content, $lang, $modifier);
}
$lang = strtoupper($lang);
if ($lang == 'JAVASCRIPT') $lang = 'JS';
if ($lang == 'HTML') $lang = 'HTMLCB';
$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'] = 'src-' . strtolower($lang);
$elCode = $elPre->create('code', $content);
return $elPre;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment