Skip to content

Commit

Permalink
Rename EarmarkParser to Earmark.Parser to avoid conflicts (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored Sep 26, 2023
1 parent 164127f commit 7fed587
Show file tree
Hide file tree
Showing 60 changed files with 211 additions and 207 deletions.
2 changes: 1 addition & 1 deletion bench/simple_data_bench.exs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ defmodule Bench.SimpleDataBench do
end

bench "01 as_ast|>transform" do
{:ok, ast, []} = EarmarkParser.as_ast(@readme_md)
{:ok, ast, []} = Earmark.Parser.as_ast(@readme_md)
Earmark.Transform.transform(ast)
end
end
28 changes: 14 additions & 14 deletions lib/earmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ defmodule Earmark do
### Abstract Syntax Tree and Rendering
The AST generation has now been moved out to [`EarmarkParser`](https://github.com/robertdober/earmark_parser)
The AST generation has now been moved out to [`Earmark.Parser`](https://github.com/robertdober/earmark_parser)
which is installed as a dependency.
This brings some changes to this documentation and also deprecates the usage of `Earmark.as_ast`
Earmark takes care of rendering the AST to HTML, exposing some AST Transformation Tools and providing a CLI as escript.
Therefore you will not find a detailed description of the supported Markdown here anymore as this is done in
[here](https://hexdocs.pm/earmark_parser/EarmarkParser.html)
[here](https://hexdocs.pm/earmark_parser/Earmark.Parser.html)
#### Earmark.as_ast
WARNING: This is just a proxy towards `EarmarkParser.as_ast` and is deprecated, it will be removed in version 1.5!
WARNING: This is just a proxy towards `Earmark.Parser.as_ast` and is deprecated, it will be removed in version 1.5!
Replace your calls to `Earmark.as_ast` with `EarmarkParse.as_ast` as soon as possible.
**N.B.** If all you use is `Earmark.as_ast` consider _only_ using `EarmarkParser`.
**N.B.** If all you use is `Earmark.as_ast` consider _only_ using `Earmark.Parser`.
Also please refer yourself to the documentation of [`EarmarkParser`](https://hexdocs.pm/earmark_parser/EarmarkParser.html)
Also please refer yourself to the documentation of [`Earmark.Parser`](https://hexdocs.pm/earmark_parser/Earmark.Parser.html)
The function is described below and the other two API functions `as_html` and `as_html!` are now based upon
the structure of the result of `as_ast`.
{:ok, ast, []} = EarmarkParser.as_ast(markdown)
{:ok, ast, deprecation_messages} = EarmarkParser.as_ast(markdown)
{:error, ast, error_messages} = EarmarkParser.as_ast(markdown)
{:ok, ast, []} = Earmark.Parser.as_ast(markdown)
{:ok, ast, deprecation_messages} = Earmark.Parser.as_ast(markdown)
{:error, ast, error_messages} = Earmark.Parser.as_ast(markdown)
#### Earmark.as_html
Expand All @@ -72,11 +72,11 @@ defmodule Earmark do
{status, html_doc, errors} = Earmark.as_html(markdown, options)
html_doc = Earmark.as_html!(markdown, options)
{status, ast, errors} = EarmarkParser.as_ast(markdown, options)
{status, ast, errors} = Earmark.Parser.as_ast(markdown, options)
### Rendering
All options passed through to `EarmarkParser.as_ast` are defined therein, however some options concern only
All options passed through to `Earmark.Parser.as_ast` are defined therein, however some options concern only
the rendering of the returned AST
These are:
Expand Down Expand Up @@ -148,7 +148,7 @@ defmodule Earmark do
```elixir
markdown
|> EarmarkParser.as_ast
|> Earmark.Parser.as_ast
|> Earmark.Transform.map_ast(fun)
|> Earmark.Transform.transform
```
Expand Down Expand Up @@ -221,21 +221,21 @@ defmodule Earmark do
"""

alias Earmark.{Internal, Options, Transform}
alias Earmark.EarmarkParserProxy, as: Proxy
alias Earmark.Earmark.ParserProxy, as: Proxy

defdelegate as_ast!(markdown, options \\ []), to: Internal
defdelegate as_html(lines, options \\ []), to: Internal
defdelegate as_html!(lines, options \\ []), to: Internal

@doc """
DEPRECATED call `EarmarkParser.as_ast` instead
DEPRECATED call `Earmark.Parser.as_ast` instead
"""
def as_ast(lines, options \\ %Options{}) do
{status, ast, messages} = _as_ast(lines, options)

message =
{:warning, 0,
"DEPRECATION: Earmark.as_ast will be removed in version 1.5, please use EarmarkParser.as_ast, which is of the same type"}
"DEPRECATION: Earmark.as_ast will be removed in version 1.5, please use Earmark.Parser.as_ast, which is of the same type"}

messages1 = [message | messages]
{status, ast, messages1}
Expand Down
14 changes: 7 additions & 7 deletions lib/earmark/earmark_parser_proxy.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
defmodule Earmark.EarmarkParserProxy do
defmodule Earmark.Earmark.ParserProxy do
@moduledoc ~S"""
This acts as a proxy to adapt to changes in `EarmarkParser`'s API
This acts as a proxy to adapt to changes in `Earmark.Parser`'s API
If no changes are needed it can delegate `as_ast` to `EarmarkParser`
If no changes are needed it can delegate `as_ast` to `Earmark.Parser`
If changes are needed they will be realised in this modules `as_ast`
function.
For that reason `EarmarkParser.as_ast/*` **SHALL NOT** be invoked
For that reason `Earmark.Parser.as_ast/*` **SHALL NOT** be invoked
anywhere else in this code base
"""

@doc ~S"""
An adapter to `EarmarkParser.as_ast/*`
An adapter to `Earmark.Parser.as_ast/*`
"""
def as_ast(input, options)
def as_ast(input, options) when is_list(options) do
EarmarkParser.as_ast(input, options |> Keyword.delete(:smartypants) |> Keyword.delete(:messages))
Earmark.Parser.as_ast(input, options |> Keyword.delete(:smartypants) |> Keyword.delete(:messages))
end
def as_ast(input, options) when is_map(options) do
EarmarkParser.as_ast(input, options |> Map.delete(:smartypants) |> Map.delete(:messages))
Earmark.Parser.as_ast(input, options |> Map.delete(:smartypants) |> Map.delete(:messages))
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/earmark/internal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ defmodule Earmark.Internal do
"""

alias Earmark.{Error, Message, Options, SysInterface, Transform}
alias Earmark.EarmarkParserProxy, as: Proxy
alias Earmark.Earmark.ParserProxy, as: Proxy
import Message, only: [emit_messages: 2]

@doc ~S"""
A wrapper to extract the AST from a call to `EarmarkParser.as_ast` if a tuple `{:ok, result, []}` is returned,
A wrapper to extract the AST from a call to `Earmark.Parser.as_ast` if a tuple `{:ok, result, []}` is returned,
raise errors otherwise
iex(1)> as_ast!(["Hello %% annotated"], annotations: "%%")
Expand Down
6 changes: 3 additions & 3 deletions lib/earmark/options.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Earmark.Options do
@moduledoc """
This is a superset of the options that need to be passed into `EarmarkParser.as_ast/2`
This is a superset of the options that need to be passed into `Earmark.Parser.as_ast/2`
The following options are proper to `Earmark` only and therefore explained in detail
Expand All @@ -11,10 +11,10 @@ defmodule Earmark.Options do
- `smartypants`: boolean use [Smarty Pants](https://daringfireball.net/projects/smartypants/) in the output
- `ignore_strings`, `postprocessor` and `registered_processors`: processors that modify the AST returned from
EarmarkParser.as_ast/`2` before rendering (`post` because preprocessing is done on the markdown, e.g. `eex`)
Earmark.Parser.as_ast/`2` before rendering (`post` because preprocessing is done on the markdown, e.g. `eex`)
Refer to the moduledoc of Earmark.`Transform` for details
All other options are passed onto EarmarkParser.as_ast/`2`
All other options are passed onto Earmark.Parser.as_ast/`2`
"""

defstruct annotations: nil,
Expand Down
4 changes: 2 additions & 2 deletions lib/earmark/restructure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Earmark.Restructure do
Walks an AST and allows you to process it (storing details in acc) and/or
modify it as it is walked.
items is the AST you got from EarmarkParser.as_ast()
items is the AST you got from Earmark.Parser.as_ast()
acc is the initial value of an accumulator that is passed to both
process_item_fn and process_list_fn and accumulated. If your functions
Expand Down Expand Up @@ -52,7 +52,7 @@ defmodule Earmark.Restructure do
...(1)>
...(1)> text
...(1)> """
...(1)> {:ok, ast, []} = EarmarkParser.as_ast(markdown)
...(1)> {:ok, ast, []} = Earmark.Parser.as_ast(markdown)
...(1)> Restructure.walk_and_modify_ast(ast, nil, italics_maker, comment_remover)
{[
{"p", [],
Expand Down
14 changes: 7 additions & 7 deletions lib/earmark/transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Earmark.Transform do

alias Earmark.Options
alias Earmark.TagSpecificProcessors, as: TSP
alias Earmark.EarmarkParserProxy, as: Proxy
alias Earmark.Earmark.ParserProxy, as: Proxy
alias __MODULE__.Pop

@compact_tags ~w[a code em strong del]
Expand All @@ -14,7 +14,7 @@ defmodule Earmark.Transform do
@moduledoc ~S"""
#### Structure Conserving Transformers
For the convenience of processing the output of `EarmarkParser.as_ast` we expose two structure conserving
For the convenience of processing the output of `Earmark.Parser.as_ast` we expose two structure conserving
mappers.
##### `map_ast`
Expand Down Expand Up @@ -280,20 +280,20 @@ defmodule Earmark.Transform do
@doc ~S"""
This is a structure conserving transformation
iex(11)> {:ok, ast, _} = EarmarkParser.as_ast("- one\n- two\n")
iex(11)> {:ok, ast, _} = Earmark.Parser.as_ast("- one\n- two\n")
...(11)> map_ast(ast, &(&1))
[{"ul", [], [{"li", [], ["one"], %{}}, {"li", [], ["two"], %{}}], %{}}]
A more useful transformation
iex(12)> {:ok, ast, _} = EarmarkParser.as_ast("- one\n- two\n")
iex(12)> {:ok, ast, _} = Earmark.Parser.as_ast("- one\n- two\n")
...(12)> fun = fn {_, _, _, _}=n -> Earmark.AstTools.merge_atts_in_node(n, class: "private")
...(12)> string -> string end
...(12)> map_ast(ast, fun)
[{"ul", [{"class", "private"}], [{"li", [{"class", "private"}], ["one"], %{}}, {"li", [{"class", "private"}], ["two"], %{}}], %{}}]
However the usage of the `ignore_strings` option renders the code much simpler
iex(13)> {:ok, ast, _} = EarmarkParser.as_ast("- one\n- two\n")
iex(13)> {:ok, ast, _} = Earmark.Parser.as_ast("- one\n- two\n")
...(13)> map_ast(ast, &Earmark.AstTools.merge_atts_in_node(&1, class: "private"), true)
[{"ul", [{"class", "private"}], [{"li", [{"class", "private"}], ["one"], %{}}, {"li", [{"class", "private"}], ["two"], %{}}], %{}}]
"""
Expand All @@ -305,15 +305,15 @@ defmodule Earmark.Transform do
This too is a structure perserving transformation but a value is passed to the mapping function as an accumulator, and the mapping
function needs to return the new node and the accumulator as a tuple, here is a simple example
iex(14)> {:ok, ast, _} = EarmarkParser.as_ast("- 1\n\n2\n- 3\n")
iex(14)> {:ok, ast, _} = Earmark.Parser.as_ast("- 1\n\n2\n- 3\n")
...(14)> summer = fn {"li", _, [v], _}=n, s -> {v_, _} = Integer.parse(v); {n, s + v_}
...(14)> n, s -> {n, s} end
...(14)> map_ast_with(ast, 0, summer, true)
{[{"ul", [], [{"li", [], ["1"], %{}}], %{}}, {"p", [], ["2"], %{}}, {"ul", [], [{"li", [], ["3"], %{}}], %{}}], 4}
or summing all numbers
iex(15)> {:ok, ast, _} = EarmarkParser.as_ast("- 1\n\n2\n- 3\n")
iex(15)> {:ok, ast, _} = Earmark.Parser.as_ast("- 1\n\n2\n- 3\n")
...(15)> summer = fn {_, _, _, _}=n, s -> {n, s}
...(15)> n, s -> {n_, _} = Integer.parse(n); {"*", s+n_} end
...(15)> map_ast_with(ast, 0, summer)
Expand Down
Loading

0 comments on commit 7fed587

Please sign in to comment.