Skip to content

Commit

Permalink
Fixes #19 Special characters within inline code span should be escaped.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzykhys committed Nov 30, 2013
1 parent b7cb888 commit 412c8b2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Ciconia/Extension/Core/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,19 @@ public function processCodeSpan(Text $text)
return;
}

$chars = ['\\\\', '`', '\*', '_', '{', '}', '\[', '\]', '\(', '\)', '>', '#', '\+', '\-', '\.', '!'];
$chars = implode('|', $chars);

/** @noinspection PhpUnusedParameterInspection */
$text->replace('{
(`+) # $1 = Opening run of `
(.+?) # $2 = The code block
(?<!`)
\1 # Matching closer
(?!`)
}x', function (Text $w, Text $b, Text $code) {
}x', function (Text $w, Text $b, Text $code) use ($chars) {
$code->trim()->escapeHtml(ENT_NOQUOTES);
$code->replace(sprintf('/(?<!\\\\)(%s)/', $chars), '\\\\${1}');

return $this->getRenderer()->renderCodeSpan($code);
});
Expand Down
2 changes: 1 addition & 1 deletion src/Ciconia/Extension/Core/EscaperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getName()
protected function generateSpecialCharsHash()
{
$hashes = array();
$chars = array('\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!');
$chars = array('\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!');

foreach ($chars as $char) {
$hashes[$char] = sprintf('{escape:md5(%s)}', md5($char));
Expand Down
5 changes: 5 additions & 0 deletions test/Ciconia/Resources/core/em-codespan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`user_name` `user_name`

`_EOF_` `_EOF_`

`__DIR__` `__DIR__`
5 changes: 5 additions & 0 deletions test/Ciconia/Resources/core/em-codespan.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p><code>user_name</code> <code>user_name</code></p>

<p><code>_EOF_</code> <code>_EOF_</code></p>

<p><code>__DIR__</code> <code>__DIR__</code></p>

0 comments on commit 412c8b2

Please sign in to comment.