Skip to content

Commit

Permalink
Struct typespecs additions (pragdave#479)
Browse files Browse the repository at this point in the history
* Struct Typespecs Additions
  • Loading branch information
bradhanks committed Feb 4, 2024
1 parent a2749b5 commit 92b21df
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 31 deletions.
29 changes: 29 additions & 0 deletions lib/earmark/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ defmodule Earmark.Options do
All other options are passed onto Earmark.Parser.as_ast/`2`
"""

@type t :: %__MODULE__{
annotations: String.t() | nil,
breaks: boolean(),
code_class_prefix: String.t() | nil,
compact_output: boolean(),
eex: boolean(),
escape: boolean(),
file: String.t() | nil,
footnote_offset: non_neg_integer(),
footnotes: boolean(),
gfm: boolean(),
gfm_tables: boolean(),
ignore_strings: boolean(),
inner_html: boolean(),
line: non_neg_integer(),
mapper: (list(any()), function() -> {:ok, list(any())} | {:error, any()}),
mapper_with_timeout:
(list(any()), function(), integer() -> {:ok, list(any())} | {:error, any()}),
messages: list(Earmark.Error.t()) | [],
pedantic: boolean(),
postprocessor: function() | nil,
pure_links: boolean(),
sub_sup: boolean(),
registered_processors: list(any()),
smartypants: boolean(),
template: boolean(),
timeout: integer() | nil,
wikilinks: boolean()
}
defstruct annotations: nil,
breaks: false,
code_class_prefix: nil,
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 92b21df

Please sign in to comment.