Skip to content

Commit

Permalink
Fixed bug in bugfix of #166
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Nov 27, 2017
1 parent 1bbd40a commit 5cfcb73
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 79 deletions.
158 changes: 79 additions & 79 deletions lib/earmark/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ defmodule Earmark.Context do
}

defstruct options: %Earmark.Options{},
links: Map.new,
rules: nil,
footnotes: Map.new,
value: []
links: Map.new,
rules: nil,
footnotes: Map.new,
value: []

##############################################################################
# Handle adding option specific rules and processors #
Expand All @@ -39,9 +39,9 @@ defmodule Earmark.Context do
Convenience method to get a context with cleared value and messages
"""
def clear(%__MODULE__{} = ctx) do
ctx
|> set_value([])
|> put_in([:options, :messages], [])
with empty_value <- set_value(ctx, []) do
%{empty_value | options: %{ empty_value.options | messages: []}}
end
end

@doc false
Expand All @@ -64,86 +64,86 @@ defmodule Earmark.Context do


@link_text ~S{(?:\[[^]]*\]|[^][]|\])*}
@href ~S{\s*<?(.*?)>?(?:\s+['"](.*?)['"])?\s*} #"

@code ~r{^
(`+) # $1 = Opening run of `
(.+?) # $2 = The code block
(?<!`)
\1 # Matching closer
(?!`)
@href ~S{\s*<?(.*?)>?(?:\s+['"](.*?)['"])?\s*} #"

@code ~r{^
(`+) # $1 = Opening run of `
(.+?) # $2 = The code block
(?<!`)
\1 # Matching closer
(?!`)
}xs


defp basic_rules do
[
escape: ~r{^\\([\\`*\{\}\[\]()\#+\-.!_>])},
autolink: ~r{^<([^ >]+(@|:\/)[^ >]+)>},
url: ~r{\z\A}, # noop

tag: ~r{
^<!--[\s\S]*?--> |
^<\/?\w+(?: "[^"<]*" | # < inside an attribute is illegal, luckily
'[^'<]*' |
[^'"<>])*?>}x,

inline_ial: ~r<^\s*\{:\s*(.*?)\s*}>,
link: ~r{^!?\[(#{@link_text})\]\(#{@href}\)},
reflink: ~r{^!?\[(#{@link_text})\]\s*\[([^]]*)\]},
nolink: ~r{^!?\[((?:\[[^]]*\]|[^][])*)\]},
strong: ~r{^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)},
em: ~r{^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)},
code: @code,
br: ~r<^ {2,}\n(?!\s*$)>,
text: ~r<^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)>,

strikethrough: ~r{\z\A} # noop
]
end

defp rules_for(options) do
rule_updates = if options.gfm do
rules = [
escape: ~r{^\\([\\`*\{\}\[\]()\#+\-.!_>~|])},
url: ~r{^(https?:\/\/[^\s<]+[^<.,:;\"\')\]\s])},
strikethrough: ~r{^~~(?=\S)([\s\S]*?\S)~~},
text: ~r{^[\s\S]+?(?=[\\<!\[_*`~]|https?://| \{2,\}\n|$)}
defp basic_rules do
[
escape: ~r{^\\([\\`*\{\}\[\]()\#+\-.!_>])},
autolink: ~r{^<([^ >]+(@|:\/)[^ >]+)>},
url: ~r{\z\A}, # noop

tag: ~r{
^<!--[\s\S]*?--> |
^<\/?\w+(?: "[^"<]*" | # < inside an attribute is illegal, luckily
'[^'<]*' |
[^'"<>])*?>}x,

inline_ial: ~r<^\s*\{:\s*(.*?)\s*}>,
link: ~r{^!?\[(#{@link_text})\]\(#{@href}\)},
reflink: ~r{^!?\[(#{@link_text})\]\s*\[([^]]*)\]},
nolink: ~r{^!?\[((?:\[[^]]*\]|[^][])*)\]},
strong: ~r{^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)},
em: ~r{^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)},
code: @code,
br: ~r<^ {2,}\n(?!\s*$)>,
text: ~r<^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)>,

strikethrough: ~r{\z\A} # noop
]
if options.breaks do
break_updates = [
br: ~r{^ *\n(?!\s*$)},
text: ~r{^[\s\S]+?(?=[\\<!\[_*`~]|https?://| *\n|$)}
]
Keyword.merge(rules, break_updates)
else
rules
end
else
if options.pedantic do
[
strong: ~r{^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)},
em: ~r{^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)}
end

defp rules_for(options) do
rule_updates = if options.gfm do
rules = [
escape: ~r{^\\([\\`*\{\}\[\]()\#+\-.!_>~|])},
url: ~r{^(https?:\/\/[^\s<]+[^<.,:;\"\')\]\s])},
strikethrough: ~r{^~~(?=\S)([\s\S]*?\S)~~},
text: ~r{^[\s\S]+?(?=[\\<!\[_*`~]|https?://| \{2,\}\n|$)}
]
if options.breaks do
break_updates = [
br: ~r{^ *\n(?!\s*$)},
text: ~r{^[\s\S]+?(?=[\\<!\[_*`~]|https?://| *\n|$)}
]
Keyword.merge(rules, break_updates)
else
rules
end
else
[]
if options.pedantic do
[
strong: ~r{^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)},
em: ~r{^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)}
]
else
[]
end
end
footnote = if options.footnotes, do: ~r{^\[\^(#{@link_text})\]}, else: ~r{\z\A}
rule_updates = Keyword.merge(rule_updates, [footnote: footnote])
Keyword.merge(basic_rules(), rule_updates)
|> Enum.into(%{})
end
footnote = if options.footnotes, do: ~r{^\[\^(#{@link_text})\]}, else: ~r{\z\A}
rule_updates = Keyword.merge(rule_updates, [footnote: footnote])
Keyword.merge(basic_rules(), rule_updates)
|> Enum.into(%{})
end

# Smartypants transformations convert quotes to the appropriate curly
# variants, and -- and ... to – and …
defp smartypants(text) do
text
|> replace(~r{--}, "—")
|> replace(~r{(^|[-—/\(\[\{"”“\s])'}, "\\1‘")
|> replace(~r{\'}, "’")
|> replace(~r{(^|[-—/\(\[\{\s])\"}, "\\1“")
|> replace(~r{"}, "”")
|> replace(~r{\.\.\.}, "…")
end
# Smartypants transformations convert quotes to the appropriate curly
# variants, and -- and ... to – and …
defp smartypants(text) do
text
|> replace(~r{--}, "—")
|> replace(~r{(^|[-—/\(\[\{"”“\s])'}, "\\1‘")
|> replace(~r{\'}, "’")
|> replace(~r{(^|[-—/\(\[\{\s])\"}, "\\1“")
|> replace(~r{"}, "”")
|> replace(~r{\.\.\.}, "…")
end

end
14 changes: 14 additions & 0 deletions test/regressions/i165_context_clear_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Regressions.I165ContextClearTest do
use ExUnit.Case

alias Earmark.Context
alias Earmark.Options

test "clearing, clears value and messages" do
context = %Context{options: %Options{messages: [{:error, 'not so good', 42}]}, value: 43}
cleared_context = Context.clear( context )

assert cleared_context.value == []
assert cleared_context.options.messages == []
end
end

0 comments on commit 5cfcb73

Please sign in to comment.