Created
April 20, 2014 18:31
-
-
Save ondrejmirtes/11121330 to your computer and use it in GitHub Desktop.
FSHL Handler for Texy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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