Skip to content

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Jan 23, 2016
1 parent c4a7998 commit c9827f2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Highlighter/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function __construct(array $config = []) {
* @return string
*/
protected function _prepare($string) {
if ($this->_config['tabsToSpaces']) {
$string = preg_replace('/\t/', str_repeat(' ', $this->_config['tabsToSpaces']), $string);
if ($this->_config['tabToSpaces']) {
$string = preg_replace('/\t/', str_repeat(' ', $this->_config['tabToSpaces']), $string);
}
return $string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Highlighter/JsHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class JsHighlighter extends Highlighter {
*/
protected $_defaultConfig = [
'escape' => true,
'tabsToSpaces' => 4,
'tabToSpaces' => 4,
'templates' => [
'code' => '<pre><code{{attr}}>{{content}}</code></pre>'
],
Expand Down
5 changes: 3 additions & 2 deletions src/Highlighter/PhpHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class PhpHighlighter extends Highlighter {
protected $_defaultConfig = [
'templates' => [
'code' => '<pre{{attr}}>{{content}}</pre>'
]
],
'prefix' => 'language-'
];

/**
Expand All @@ -23,7 +24,7 @@ public function highlight($text, array $options = []) {

$options += $this->_config;

$attr = ['class' => 'lang-' . $options['lang']];
$attr = ['class' => $options['prefix'] . $options['lang']];

$options['attr'] = $this->templater()->formatAttributes($attr);
$options['content'] = str_replace(["\r\n", "\n", "\r"], '', $string);
Expand Down
9 changes: 8 additions & 1 deletion src/View/Helper/HighlighterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class HighlighterHelper extends Helper {
protected $_defaultConfig = [
'highlighter' => '\Markup\Highlighter\PhpHighlighter',
'debug' => null, // Enable caching mode
//'autoParse' => false,
];

/**
Expand All @@ -38,6 +37,14 @@ public function __construct(View $View, array $config = []) {
}

/**
* Highlight a string.
*
* Options, depending on the specific highlighter class used:
* - templates
* - escape (defaults to true)
* - tabToSpaces (defaults to 4)
* - prefix (defaults to `language-`)
*
* @param string $text
* @param array $options
* @return string
Expand Down
19 changes: 16 additions & 3 deletions tests/TestCase/View/Helper/HighlighterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,20 @@ public function testHighlight() {
TEXT;

$result = $this->Highlighter->highlight($text, ['lang' => 'php']);
$expected = '<pre class="lang-php"><code><span style="color: #000000">$key&nbsp;=&nbsp;\'string\'&nbsp;.&nbsp;$this-&gt;something-&gt;do(true);&nbsp;//&nbsp;Some&nbsp;comment</span></code></pre>';
$expected = '<pre class="language-php"><code><span style="color: #000000">$key&nbsp;=&nbsp;\'string\'&nbsp;.&nbsp;$this-&gt;something-&gt;do(true);&nbsp;//&nbsp;Some&nbsp;comment</span></code></pre>';
$this->assertSame($expected, $result);
}

/**
* @return void
*/
public function testHighlightPrefix() {
$text = <<<'TEXT'
$key = 'string' . $this->something->do(true);
TEXT;

$result = $this->Highlighter->highlight($text, ['lang' => 'php', 'prefix' => 'l-']);
$expected = '<pre class="l-php"><code><span style="color: #000000">$key&nbsp;=&nbsp;\'string\'&nbsp;.&nbsp;$this-&gt;something-&gt;do(true);</span></code></pre>';
$this->assertSame($expected, $result);
}

Expand Down Expand Up @@ -117,13 +130,13 @@ public function testHighlightIndentation() {
TEXT;

$result = $this->Highlighter->highlight($text, ['lang' => 'php']);
$expected = '<pre class="lang-php"><code><span style="color: #000000">if&nbsp;($foo)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(true)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;doSth();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span></code></pre>';
$expected = '<pre class="language-php"><code><span style="color: #000000">if&nbsp;($foo)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(true)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;doSth();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span></code></pre>';
$this->assertEquals($expected, $result);

$this->Highlighter->config('highlighter', 'Markup\Highlighter\JsHighlighter');

$result = $this->Highlighter->highlight($text, ['lang' => 'php']);
$expected = '<pre class="lang-php"><code><span style="color: #000000">if&nbsp;($foo)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(true)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;doSth();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span></code></pre>';
$expected = '<pre class="language-php"><code><span style="color: #000000">if&nbsp;($foo)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(true)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;doSth();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span></code></pre>';
$this->assertSame($expected, $result);

}
Expand Down

0 comments on commit c9827f2

Please sign in to comment.