Navigation Menu

Skip to content

Commit

Permalink
Add line by line formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kadet1090 committed Aug 13, 2018
1 parent 28d8a3b commit 6aac402
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Config/formatters.php
@@ -1,7 +1,7 @@
<?php return [
'latex' => new \Kadet\Highlighter\Formatter\LatexFormatter(),
'html' => new \Kadet\Highlighter\Formatter\HtmlFormatter(),
'lhtml' => new \Kadet\Highlighter\Formatter\LineContainedHtmlFormatter(),
'lhtml' => new \Kadet\Highlighter\Formatter\HtmlFormatter(['lines' => ['enable' => true]]),
'cli' => new \Kadet\Highlighter\Formatter\CliFormatter(),
'debug' => new \Kadet\Highlighter\Formatter\DebugFormatter()
];
44 changes: 40 additions & 4 deletions Formatter/AbstractFormatter.php
Expand Up @@ -9,29 +9,65 @@

abstract class AbstractFormatter implements FormatterInterface
{
protected $_options = [];
protected $_line = 1;

public function __construct(array $options = [])
{
$this->_options = array_replace_recursive([
'lines' => [
'enable' => false,
'start' => 1,
'marked' => []
]
], $options);
}

public function format(Tokens $tokens)
{
$source = $tokens->getSource();

$result = '';
$this->_line = (int)$this->_options['lines']['start'];
$this->_options['lines']['max'] = substr_count($source, '\n') + $this->_options['lines']['start'];

$result = $this->_options['lines']['enable'] ? $this->formatLineStart($this->_line) : '';
$last = 0;

/** @var Token $token */
foreach ($tokens as $token) {
$result .= $this->content(substr($source, $last, $token->pos - $last));
$result .= $this->_content(substr($source, $last, $token->pos - $last));
$result .= $this->token($token);

$last = $token->pos;
}
$result .= $this->content(substr($source, $last));
$result .= $this->_content(substr($source, $last));

return $result;
return $result.($this->_options['lines']['enable'] ? $this->formatLineEnd($this->_line++) : '');
}

private function _content($text)
{
$content = $this->content($text);

return $this->_options['lines']['enable'] ? preg_replace_callback('/\R/u', function($feed) {
return $this->formatLineEnd($this->_line++).$feed[0].$this->formatLineStart($this->_line);
}, $content) : $content;
}

protected function content($text)
{
return $text;
}

protected function formatLineStart($line)
{
return null;
}

protected function formatLineEnd($line)
{
return null;
}

protected abstract function token(Token $token);
}
22 changes: 17 additions & 5 deletions Formatter/CliFormatter.php
Expand Up @@ -27,16 +27,18 @@
*/
class CliFormatter extends AbstractFormatter implements FormatterInterface
{
private $_styles;
private $_styles = [];

/**
* CliFormatter constructor.
*
* @param $styles
*/
public function __construct($styles = false)
public function __construct(array $options = [])
{
$this->_styles = $styles ?: include __DIR__.'/../Styles/Cli/Default.php';
parent::__construct(array_replace_recursive([
'styles' => include __DIR__.'/../Styles/Cli/Default.php'
], $options));

$this->_styles = $this->_options['styles'];
}

public function format(Tokens $tokens)
Expand All @@ -56,4 +58,14 @@ protected function token(Token $token)
? Console::open(is_callable($style) ? $style($token) : $style)
: Console::close();
}

protected function formatLineStart($line)
{
return str_pad($line, 5, ' ', STR_PAD_LEFT) . ' | '.Console::set(Console::current());
}

protected function formatLineEnd($line)
{
return Console::reset();
}
}
29 changes: 27 additions & 2 deletions Formatter/HtmlFormatter.php
Expand Up @@ -28,13 +28,17 @@ class HtmlFormatter extends AbstractFormatter implements FormatterInterface
protected $_prefix = '';
protected $_tag = 'span';

private $_stack;

/**
* HtmlFormatter constructor.
*
* @param array $options
*/
public function __construct($options = [])
public function __construct(array $options = [])
{
parent::__construct($options);

$options = array_merge([
'prefix' => 'kl-',
'tag' => 'span'
Expand All @@ -44,6 +48,12 @@ public function __construct($options = [])
$this->_prefix = $options['prefix'];
}

public function format(Tokens $tokens)
{
$this->_stack = [];
return parent::format($tokens);
}

protected function getOpenTag(Token $token)
{
return sprintf(
Expand All @@ -59,11 +69,26 @@ protected function getCloseTag()

protected function token(Token $token)
{
return $token->isStart() ? $this->getOpenTag($token) : $this->getCloseTag();
if ($token->isStart()) {
return $this->_stack[] = $this->getOpenTag($token);
} else {
array_pop($this->_stack);
return $this->getCloseTag();
}
}

protected function content($text)
{
return htmlspecialchars($text);
}

protected function formatLineStart($line)
{
return implode('', $this->_stack);
}

protected function formatLineEnd($line)
{
return str_repeat($this->getCloseTag(), count($this->_stack));
}
}
10 changes: 7 additions & 3 deletions Formatter/LatexFormatter.php
Expand Up @@ -32,11 +32,15 @@ class LatexFormatter extends AbstractFormatter implements FormatterInterface
/**
* LatexFormatter constructor.
*
* @param array|false $styles
* @param array $options
*/
public function __construct($styles = false)
public function __construct(array $options = [])
{
$this->_styles = $styles ?: include __DIR__.'/../Styles/Latex/Default.php';
parent::__construct(array_replace_recursive([
'styles' => include __DIR__.'/../Styles/Cli/Default.php'
], $options));

$this->_styles = $this->_options['styles'];
}

protected function token(Token $token)
Expand Down
42 changes: 0 additions & 42 deletions Formatter/LineContainedHtmlFormatter.php

This file was deleted.

2 changes: 1 addition & 1 deletion Parser/TokenFactory.php
Expand Up @@ -132,7 +132,7 @@ public function setRule($rule)
*/
public function setClass($class)
{
if (!is_a($class, 'Kadet\Highlighter\Parser\Token\Token', true)) {
if (!is_a($class, Token::class, true)) {
throw new \InvalidArgumentException('$class must extend Kadet\Highlighter\Parser\Token\Token');
}

Expand Down

0 comments on commit 6aac402

Please sign in to comment.