Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Ciconia/Extension/Gfm/FencedCodeBlockExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function processFencedCodeBlock(Text $text, array $options = [])
\1 # matched #1
)
}smx', function (Text $w, Text $fence, Text $lang, Text $code) use ($options) {
$rendererOptions = [];
$preRendererOptions = $codeRendererOptions = [];

if (!$lang->isEmpty()) {
if ($options['pygments'] && class_exists('KzykHys\Pygments\Pygments')) {
Expand All @@ -66,19 +66,24 @@ public function processFencedCodeBlock(Text $text, array $options = [])
return "\n\n" . $html . "\n\n";
}

$rendererOptions = [
$preRendererOptions = [
'attr' => [
'class' => 'prettyprint lang-' . $lang->lower()
]
];
$codeRendererOptions = [
'attr' => [
'class' => 'language-' . $lang->lower()
]
];
}

$code->escapeHtml(ENT_NOQUOTES);
$this->markdown->emit('detab', array($code));
$code->replace('/\A\n+/', '');
$code->replace('/\s+\z/', '');

return "\n\n" . $this->getRenderer()->renderCodeBlock($code, $rendererOptions) . "\n\n";
return "\n\n" . $this->getRenderer()->renderCodeBlock($code, $preRendererOptions, $codeRendererOptions) . "\n\n";
});
}

Expand Down
9 changes: 6 additions & 3 deletions src/Ciconia/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ public function renderHeader($content, array $options = array())
/**
* {@inheritdoc}
*/
public function renderCodeBlock($content, array $options = array())
public function renderCodeBlock($content, array $preOptions = array(), array $codeOptions = array())
{
if (!$content instanceof Text) {
$content = new Text($content);
}

$options = $this->createResolver()->resolve($options);
$resolver = $this->createResolver();
$preOptions = $resolver->resolve($preOptions);
$codeOptions = $resolver->resolve($codeOptions);

$tag = Tag::create('pre')
->setAttributes($options['attr'])
->setAttributes($preOptions['attr'])
->setText(
Tag::create('code')
->setAttributes($codeOptions['attr'])
->setText($content->append("\n"))
->render()
);
Expand Down
5 changes: 3 additions & 2 deletions src/Ciconia/Renderer/RendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public function renderHeader($content, array $options = array());
* @api
*
* @param string|Text $content
* @param array $options
* @param array $preOptions
* @param array $codeOptions
*
* @return string
*/
public function renderCodeBlock($content, array $options = array());
public function renderCodeBlock($content, array $preOptions = array(), array $codeOptions = array());

/**
* @api
Expand Down
2 changes: 1 addition & 1 deletion test/Ciconia/Resources/gfm/fenced-code-block-syntax.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
code block
</code></pre>

<pre class="prettyprint lang-php"><code>&lt;?php
<pre class="prettyprint lang-php"><code class="language-php">&lt;?php

class Test
{
Expand Down
2 changes: 1 addition & 1 deletion test/Ciconia/Resources/gfm/link-in-codeblock.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<pre class="prettyprint lang-php"><code>$validator-&gt;validate('http://www.example.com/');
<pre class="prettyprint lang-php"><code class="language-php">$validator-&gt;validate('http://www.example.com/');
</code></pre>