Packages
earmark
1.4.41
1.5.0-pre1
retired
1.5.0-pre
retired
1.4.49
retired
1.4.48
retired
1.4.47
retired
1.4.46
retired
1.4.45
retired
1.4.44
retired
1.4.43
retired
1.4.42
retired
1.4.41
retired
1.4.40
retired
1.4.39
retired
1.4.38
retired
1.4.37
retired
1.4.36
retired
1.4.35
retired
1.4.34
retired
1.4.33
retired
1.4.32
retired
1.4.31
retired
1.4.30
retired
1.4.29
retired
1.4.28
retired
1.4.27
retired
1.4.26
retired
1.4.25
retired
1.4.24
retired
1.4.23
retired
1.4.22
retired
1.4.21
retired
1.4.20
retired
1.4.19
retired
1.4.18
retired
1.4.17
retired
1.4.16
retired
1.4.16-pre2
retired
1.4.16-pre1
retired
1.4.16-pre
retired
1.4.15
retired
1.4.14
retired
1.4.13
retired
1.4.12
retired
1.4.10
retired
1.4.9
retired
1.4.8
retired
1.4.7
retired
1.4.6
retired
1.4.5
retired
1.4.4
retired
1.4.3
retired
1.4.2
retired
1.4.1
retired
1.4.0
retired
1.3.6
retired
1.3.5
retired
1.3.4
retired
1.3.3
retired
1.3.2
retired
1.3.1
retired
1.3.0
retired
1.2.6
retired
1.2.5
retired
1.2.4
retired
1.2.3
retired
1.2.2
retired
1.2.1
retired
1.2.0
retired
1.1.1
retired
1.1.0
retired
1.0.3
retired
1.0.2
retired
1.0.1
retired
1.0.0
retired
0.2.1
retired
0.2.0
retired
0.1.19
retired
0.1.18
retired
0.1.17
retired
0.1.16
retired
0.1.15
retired
0.1.14
retired
0.1.13
retired
0.1.12
retired
0.1.10
retired
0.1.9
retired
0.1.8
retired
0.1.7
retired
0.1.6
retired
0.1.5
retired
0.1.4
retired
0.1.3
retired
0.1.2
retired
0.1.1
retired
0.1.0
retired
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as a command-line tool (run mix escript.build first). Output generation is pluggable.
Retired package: Deprecated - Earmark is no longer maintained. Migrate to a replacement, for example MDEx (https://hex.pm/packages/mdex).
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/earmark_parser/enum/ext.ex
defmodule EarmarkParser.Enum.Ext do
@moduledoc ~S"""
Some extensions of Enum functions
"""
@doc ~S"""
`reduce_with_end` is like `Enum.reduce` for lists, but the reducer function is called for
each element of the list with the tuple `{:element, element}` and the accumulator and once
more at the end with `:end` and the accumulator
iex(1)> reducer =
...(1)> fn {:element, nil}, {partial, result} -> {[], [Enum.sum(partial)|result]}
...(1)> {:element, val}, {partial, result} -> {[val|partial], result}
...(1)> :end, {partial, result} -> [Enum.sum(partial)|result] |> Enum.reverse
...(1)> end
...(1)> [1, 2, nil, 4, 1, 0, nil, 3, 2, 2]
...(1)> |> reduce_with_end({[], []}, reducer)
[3, 5, 7]
**N.B.** that in the treatment of `:end` we can change the shape of the accumulator w/o any
penalty concerning the complexity of the reducer function
"""
def reduce_with_end(collection, initial_acc, reducer_fn)
def reduce_with_end([], acc, reducer_fn) do
reducer_fn.(:end, acc)
end
def reduce_with_end([ele|rest], acc, reducer_fn) do
reduce_with_end(rest, reducer_fn.({:element, ele}, acc), reducer_fn)
end
@doc ~S"""
Like map_reduce but reversing the list
iex(2)> replace_nil_and_count = fn ele, acc ->
...(2)> if ele, do: {ele, acc}, else: {"", acc + 1}
...(2)> end
...(2)> ["y", nil, "u", nil, nil, "a", nil] |> reverse_map_reduce(0, replace_nil_and_count)
{ ["", "a", "", "", "u", "", "y"], 4 }
"""
def reverse_map_reduce(list, initial, fun) do
_reverse_map_reduce(list, initial, [], fun)
end
# Helpers {{{
defp _reverse_map_reduce(list, acc, result, fun)
defp _reverse_map_reduce([], acc, result, _fun), do: {result, acc}
defp _reverse_map_reduce([fst|rst], acc, result, fun) do
{new_ele, new_acc} = fun.(fst, acc)
_reverse_map_reduce(rst, new_acc, [new_ele|result], fun)
end
# }}}
end
# SPDX-License-Identifier: Apache-2.0