Skip to content

Instantly share code, notes, and snippets.

@ondrejmirtes
Created April 20, 2014 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondrejmirtes/11121330 to your computer and use it in GitHub Desktop.
Save ondrejmirtes/11121330 to your computer and use it in GitHub Desktop.
FSHL Handler for Texy
services:
- AmphibianDemo\Model\FshlHandler
-
class: Texy
setup:
- addHandler(block, [ @AmphibianDemo\Model\FshlHandler, handleBlock ])
- $tabWidth(4)
- $allowedClasses(TRUE)
- $allowedTags(TRUE)
- $allowedStyles(TRUE)
- $allowed([blocks: TRUE])
-
class: FSHL\Highlighter
arguments: [ ..., 2 ]
- FSHL\Output\Html
<?php
namespace AmphibianDemo\Model;
use FSHL\Highlighter;
use Texy;
use TexyHandlerInvocation;
use TexyHtml;
use TexyModifier;
class FshlHandler
{
/**
* @var \FSHL\Highlighter
*/
private $highlighter;
public function __construct(Highlighter $highlighter)
{
$this->highlighter = $highlighter;
}
/**
* @param \TexyHandlerInvocation $invocation
* @param string $blocktype
* @param string $content
* @param string $lang
* @param \TexyModifier $modifier
* @return string
*/
public function handleBlock(
TexyHandlerInvocation $invocation,
$blocktype,
$content,
$lang,
TexyModifier $modifier
)
{
if ($blocktype !== 'block/code') {
return $invocation->proceed();
}
$lang = strtolower($lang);
$lexerClass = '\FSHL\Lexer\\' . ucfirst($lang);
if (class_exists($lexerClass)) {
$this->highlighter->setLexer(new $lexerClass());
} else {
return $invocation->proceed();
}
$texy = $invocation->getTexy();
$content = Texy::outdent($content);
$content = $this->highlighter->highlight($content);
$content = $texy->protect($content, Texy::CONTENT_BLOCK);
$elPre = TexyHtml::el('pre');
$modifier->decorate($texy, $elPre);
$elPre->attrs['class'] = $lang;
$elPre->create('code', $content);
return $elPre->toString($texy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment