Skip to content

Commit

Permalink
struct typespecs
Browse files Browse the repository at this point in the history
  • Loading branch information
bradhanks committed Jan 19, 2024
1 parent b212c4b commit f053e3d
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 59 deletions.
55 changes: 27 additions & 28 deletions lib/earmark/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,6 @@ defmodule Earmark.Options do
All other options are passed onto Earmark.Parser.as_ast/`2`
"""

defstruct annotations: nil,
breaks: false,
code_class_prefix: nil,
compact_output: false,
# Internal—only override if you're brave
eex: false,
escape: true,
file: nil,
footnote_offset: 1,
footnotes: false,
gfm: true,
gfm_tables: false,
ignore_strings: false,
inner_html: false,
line: 1,
mapper: &Earmark.pmap/2,
mapper_with_timeout: &Earmark.pmap/3,
messages: [],
pedantic: false,
postprocessor: nil,
pure_links: true,
sub_sup: false,
registered_processors: [],
smartypants: true,
template: false,
timeout: nil,
wikilinks: false

@type t :: %__MODULE__{
annotations: String.t() | nil,
breaks: boolean(),
Expand Down Expand Up @@ -74,6 +46,33 @@ defmodule Earmark.Options do
timeout: integer() | nil,
wikilinks: boolean()
}
defstruct annotations: nil,
breaks: false,
code_class_prefix: nil,
compact_output: false,
# Internal—only override if you're brave
eex: false,
escape: true,
file: nil,
footnote_offset: 1,
footnotes: false,
gfm: true,
gfm_tables: false,
ignore_strings: false,
inner_html: false,
line: 1,
mapper: &Earmark.pmap/2,
mapper_with_timeout: &Earmark.pmap/3,
messages: [],
pedantic: false,
postprocessor: nil,
pure_links: true,
sub_sup: false,
registered_processors: [],
smartypants: true,
template: false,
timeout: nil,
wikilinks: false

@doc ~S"""
Make a legal and normalized Option struct from, maps or keyword lists
Expand Down
26 changes: 15 additions & 11 deletions lib/earmark/tag_specific_processors.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Earmark.TagSpecificProcessors do

@moduledoc """
This struct represents a list of tuples `{tag, function}` from which a postprocessing function
can be constructed
Expand All @@ -21,16 +20,18 @@ defmodule Earmark.TagSpecificProcessors do
...(2)> make_postprocessor(tsp).({"x", [], nil, nil})
{"x", [], nil, nil}
"""

@type t :: %__MODULE__{tag_functions: list()}
defstruct tag_functions: []

@doc """
Constructs a postprocessor function from this struct which will find the function associated
to the tag of the node, and apply the node to it if such a function was found.
"""
def make_postprocessor(%__MODULE__{tag_functions: tfs}) do
fn {_, _, _, _}=node -> _postprocess(node, tfs)
other -> other end
fn
{_, _, _, _} = node -> _postprocess(node, tfs)
other -> other
end
end

@doc """
Expand All @@ -41,22 +42,25 @@ defmodule Earmark.TagSpecificProcessors do
"""
def new, do: %__MODULE__{}
def new({_, _}=tf), do: %__MODULE__{tag_functions: [tf]}
def new({_, _} = tf), do: %__MODULE__{tag_functions: [tf]}
def new(tfs), do: %__MODULE__{tag_functions: tfs}


@doc """
Prepends a tuple {tag, function} to the list of such tuples.
"""
def prepend_tag_function(tsp, tag, function), do: prepend_tag_function(tsp, {tag, function})
def prepend_tag_function(%__MODULE__{tag_functions: tfs}=tsp, tf) do
%{tsp | tag_functions: [tf|tfs]}

def prepend_tag_function(%__MODULE__{tag_functions: tfs} = tsp, tf) do
%{tsp | tag_functions: [tf | tfs]}
end

defp _postprocess({t, _, _, _}=node, tfs) do
fun = tfs
|> Enum.find_value(fn {t_, f} -> if t == t_, do: f end)
defp _postprocess({t, _, _, _} = node, tfs) do
fun =
tfs
|> Enum.find_value(fn {t_, f} -> if t == t_, do: f end)

if fun, do: fun.(node), else: node
end
end

# SPDX-License-Identifier: Apache-2.0
Loading

0 comments on commit f053e3d

Please sign in to comment.