Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smartypants: Convert three hyphens to em dash #336

Merged
merged 2 commits into from
May 7, 2020
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ Where `html_doc` is an HTML representation of the markdown document and
* `smartypants`: boolean

Turns on smartypants processing, so quotes become curly, two
or four hyphens become en and em dashes, and so on. True by
or three hyphens become en and em dashes, and so on. True by
default.

So, to format the document in `original` and disable smartypants,
Expand Down
2 changes: 1 addition & 1 deletion bench/simple_data_bench.exs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ defmodule Bench.SimpleDataBench do
* `smartypants`: boolean

Turns on smartypants processing, so quotes become curly, two
or four hyphens become en and em dashes, and so on. True by
or three hyphens become en and em dashes, and so on. True by
default.

So, to format the document in `original` and disable smartypants,
Expand Down
2 changes: 1 addition & 1 deletion lib/earmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ defmodule Earmark do
* `smartypants`: boolean

Turns on smartypants processing, so quotes become curly, two
or four hyphens become en and em dashes, and so on. True by
or three hyphens become en and em dashes, and so on. True by
default.

So, to format the document in `original` and disable smartypants,
Expand Down
12 changes: 7 additions & 5 deletions lib/earmark/transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ defmodule Earmark.Transform do
if void?, do: " />", else: ">"
["<", tag, atts |> Enum.map(&make_att(&1, tag)), closer]
end

@dashes_rgx ~r{--}
@dbl1_rgx ~r{(^|[-—/\(\[\{"”“\s])'}

@em_dash_rgx ~r{---}
@en_dash_rgx ~r{--}
@dbl1_rgx ~r{(^|[-–—/\(\[\{"”“\s])'}
@single_rgx ~r{\'}
@dbl2_rgx ~r{(^|[-—/\(\[\{‘\s])\"}
@dbl2_rgx ~r{(^|[-—/\(\[\{‘\s])\"}
@dbl3_rgx ~r{"}
defp smartypants(text, options)
defp smartypants(text, %{smartypants: true}) do
text
|> replace(@dashes_rgx, "—")
|> replace(@em_dash_rgx, "—")
|> replace(@en_dash_rgx, "–")
|> replace(@dbl1_rgx, "\\1‘")
|> replace(@single_rgx, "’")
|> replace(@dbl2_rgx, "\\1“")
Expand Down
26 changes: 22 additions & 4 deletions test/acceptance/html/render_specific/smarty_pants_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Acceptance.Html.RenderSpecific.SmartyPantsTest do
use Support.AcceptanceTestCase

@en_dash "\u2013"
@em_dash "\u2014"
@open_sgl_smarty "\u2018"
@close_sgl_smarty "\u2019"
@open_dbl_smarty "\u201c"
Expand Down Expand Up @@ -30,7 +32,7 @@ defmodule Acceptance.Html.RenderSpecific.SmartyPantsTest do

assert as_html(markdown) == {:ok, html, messages}
end
test "paired single" do
test "paired single" do
markdown = "a 'single' quote"
html = para("a #{sgl_quote("single")} quote")
messages = []
Expand Down Expand Up @@ -63,8 +65,24 @@ defmodule Acceptance.Html.RenderSpecific.SmartyPantsTest do

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

test "en dash" do
markdown = "1947 -- 2020"
html = para("1947 #{@en_dash} 2020")
messages = []

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

test "em dash" do
markdown = "Earmark---A Pure Elixir Markdown Processor"
html = para("Earmark#{@em_dash}A Pure Elixir Markdown Processor")
messages = []

assert as_html(markdown) == {:ok, html, messages}
end
end
defp dbl_quote(str), do: Enum.join([@open_dbl_smarty, str, @close_dbl_smarty])
defp sgl_quote(str), do: Enum.join([@open_sgl_smarty, str, @close_sgl_smarty])

defp dbl_quote(str), do: Enum.join([@open_dbl_smarty, str, @close_dbl_smarty])
defp sgl_quote(str), do: Enum.join([@open_sgl_smarty, str, @close_sgl_smarty])
end
2 changes: 1 addition & 1 deletion test/fixtures/elixir_readmes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ Where `html_doc` is an HTML representation of the markdown document and
* `smartypants`: boolean

Turns on smartypants processing, so quotes become curly, two
or four hyphens become en and em dashes, and so on. True by
or three hyphens become en and em dashes, and so on. True by
default.

So, to format the document in `original` and disable smartypants,
Expand Down