Skip to content

Commit

Permalink
Merge pull request #284 from pragdave/i279-csharp-code-blocks
Browse files Browse the repository at this point in the history
Fixes: #279;
  • Loading branch information
RobertDober committed Sep 23, 2019
2 parents 11d04b1 + 4bfda79 commit 518ded0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/earmark/line_scanner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ defmodule Earmark.LineScanner do
[_, spaces, code] = match
%Line.Indent{level: div(String.length(spaces), 4), content: code}

match = Regex.run(~r/^\s*(```|~~~)\s*([\w\-]*)\s*$/u, line) ->
match = Regex.run(~r/^\s*(```|~~~)\s*([^`\s]*)\s*$/u, line) ->
[_, fence, language] = match
%Line.Fence{delimiter: fence, language: language}
%Line.Fence{delimiter: fence, language: _attribute_escape(language)}

# Although no block tags I still think they should close a preceding para as do many other
# implementations.
Expand Down Expand Up @@ -218,6 +218,13 @@ defmodule Earmark.LineScanner do
end
end


defp _attribute_escape(string), do:
string
|> String.replace("&", "&")
|> String.replace("<", "&lt;")


@block_tags ~w< address article aside blockquote canvas dd div dl fieldset figcaption h1 h2 h3 h4 h5 h6 header hgroup li main nav noscript ol output p pre section table tfoot ul video>
|> Enum.into(MapSet.new())
defp block_tag?(tag), do: MapSet.member?(@block_tags, tag)
Expand Down
29 changes: 27 additions & 2 deletions test/acceptance/html/fenced_code_blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Acceptance.Html.FencedCodeBlocksTest do

import Support.Helpers, only: [as_html: 1, as_html: 2]

# describe "Fenced code blocks" do
describe "Fenced code blocks" do
test "no lang" do
markdown = "```\n<\n >\n```\n"
html = "<pre><code class=\"\">&lt;\n &gt;</code></pre>\n"
Expand Down Expand Up @@ -43,8 +43,33 @@ defmodule Acceptance.Html.FencedCodeBlocksTest do

assert as_html(markdown) == {:ok, html, messages}
end
end

# end
describe "do not make too many assumptions about programming language names" do
test "at least the existing ones shall work" do
markdown = "```c#\nI do not know c# code\n```\n"
html = "<pre><code class=\"c#\">I do not know c# code</code></pre>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end

test "and let us anticipate creative language designers too" do
markdown = "```42lang!\nassert x == 42\n```\n"
html = "<pre><code class=\"42lang!\">assert x == 42</code></pre>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end

test "be careful about what can go into an HTML attribute though" do
markdown = "```a<b&\nassert x == 42\n```\n"
html = "<pre><code class=\"a&lt;b&amp;\">assert x == 42</code></pre>\n"
messages = []

assert as_html(markdown) == {:ok, html, messages}
end
end
end

# SPDX-License-Identifier: Apache-2.0
31 changes: 31 additions & 0 deletions test/acceptance/html1/fenced_code_blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,38 @@ defmodule Acceptance.Html1.FencedCodeBlocksTest do

assert to_html1(markdown) == {:ok, html, messages}
end
end

describe "do not make too many assumptions about programming language names" do
test "at least the existing ones shall work" do
markdown = "```c#\nI do not know c# code\n```\n"
html = construct([
:pre,
{:code, ~s{class="c#"}, "I do not know c# code"}])
messages = []

assert to_html1(markdown) == {:ok, html, messages}
end

test "and let us anticipate creative language designers too" do
markdown = "```42lang!\nassert x == 42\n```\n"
html = construct([
:pre,
{:code, ~s{class="42lang!"}, "assert x == 42"}])
messages = []

assert to_html1(markdown) == {:ok, html, messages}
end

test "be careful about what can go into an HTML attribute though" do
markdown = "```a<b&\nassert x == 42\n```\n"
html = construct([
:pre,
{:code, ~s{class="a&lt;b&amp;"}, "assert x == 42"}])
messages = []

assert to_html1(markdown) == {:ok, html, messages}
end
end
end

Expand Down

0 comments on commit 518ded0

Please sign in to comment.