From 0ed5c127f9d104ef1ece2f5ba1ec7ccafbf41736 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 8 Nov 2019 21:11:06 -0500 Subject: [PATCH] minor refactoring --- lib/textile/parser.ex | 22 ++++------------------ lib/textile/parser_helpers.ex | 13 +++++++++++++ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/textile/parser.ex b/lib/textile/parser.ex index f5e6ff87..67c59b86 100644 --- a/lib/textile/parser.ex +++ b/lib/textile/parser.ex @@ -1,6 +1,5 @@ defmodule Textile.Parser do import Textile.ParserHelpers - import Phoenix.HTML alias Textile.{ Lexer, @@ -9,7 +8,6 @@ defmodule Textile.Parser do } defstruct [ - reductions: [], image_transform: nil ] @@ -129,7 +127,7 @@ defmodule Textile.Parser do {:ok, [markup: ~s|
|] ++ tree ++ [markup: ~s|
|], r2_tokens} else _ -> - {:ok, [text: ~s|[bq="#{author}"]|], r_tokens} + {:ok, [text: escape_nl2br(~s|[bq="#{author}"]|)], r_tokens} end end @@ -138,7 +136,7 @@ defmodule Textile.Parser do {:ok, [markup: ~s|
|] ++ tree ++ [markup: ~s|
|], r2_tokens} else _ -> - {:ok, [text: escape_html(open)], r_tokens} + {:ok, [text: escape_nl2br(open)], r_tokens} end end @@ -155,7 +153,7 @@ defmodule Textile.Parser do {:ok, [markup: ~s||] ++ tree ++ [markup: ~s||], r2_tokens} else _ -> - {:ok, [text: escape_html(open)], r_tokens} + {:ok, [text: escape_nl2br(open)], r_tokens} end end @@ -172,7 +170,7 @@ defmodule Textile.Parser do {:ok, [markup: ~s||] ++ tree ++ [markup: ~s||], r2_tokens} else _ -> - {:ok, [text: escape_html(start)], r_tokens} + {:ok, [text: escape_nl2br(start)], r_tokens} end end @@ -305,16 +303,4 @@ defmodule Textile.Parser do defp text(_parser, _tokens), do: {:error, "Expected text"} - - - defp escape_nl2br(text) do - text - |> String.split("\n", trim: true) - |> Enum.map(&escape_html(&1)) - |> Enum.join("
") - end - - defp escape_html(text) do - html_escape(text) |> safe_to_string() - end end \ No newline at end of file diff --git a/lib/textile/parser_helpers.ex b/lib/textile/parser_helpers.ex index 13a95d97..634b049f 100644 --- a/lib/textile/parser_helpers.ex +++ b/lib/textile/parser_helpers.ex @@ -1,4 +1,6 @@ defmodule Textile.ParserHelpers do + import Phoenix.HTML + defmacro attribute_parser(name, open_token, close_token, open_tag, close_tag) do quote do defp unquote(name)(parser, [{unquote(open_token), open} | r_tokens]) do @@ -23,4 +25,15 @@ defmodule Textile.ParserHelpers do do: {:error, "Expected #{unquote(name)} tag"} end end + + def escape_nl2br(text) do + text + |> String.split("\n", trim: true) + |> Enum.map(&escape_html(&1)) + |> Enum.join("
") + end + + def escape_html(text) do + html_escape(text) |> safe_to_string() + end end \ No newline at end of file