Packages
earmark_parser
1.4.16-pre
1.4.46
1.4.45
1.4.44
1.4.43
1.4.42
1.4.41
1.4.40
1.4.39
1.4.38
1.4.37
1.4.36
1.4.35
1.4.34
1.4.33
1.4.32
1.4.31
1.4.30
1.4.29
1.4.28
1.4.27
1.4.26
1.4.25
1.4.24
1.4.23
1.4.22
1.4.21
1.4.20
1.4.20-pre
1.4.19
1.4.18
1.4.17
1.4.16
1.4.16-pre2
1.4.16-pre1
1.4.16-pre
1.4.15
1.4.13
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
AST parser and generator for Markdown
Current section
Files
Jump to
Current section
Files
lib/earmark_parser/helpers.ex
defmodule EarmarkParser.Helpers do
@moduledoc false
@doc """
Expand tabs to multiples of 4 columns
"""
def expand_tabs(line) do
Regex.replace(~r{(.*?)\t}, line, &expander/2)
end
defp expander(_, leader) do
extra = 4 - rem(String.length(leader), 4)
leader <> pad(extra)
end
@doc """
Remove newlines at end of line and optionally annotations
"""
# def remove_line_ending(line, annotation \\ nil)
def remove_line_ending(line, nil) do
_trim_line({line, nil})
end
def remove_line_ending(line, annotation) do
case Regex.run(annotation, line) do
nil -> _trim_line({line, nil})
match -> match |> tl() |> List.to_tuple |> _trim_line()
end
end
defp _trim_line({line, annot}), do: {line |> String.trim_trailing("\n") |> String.trim_trailing("\r"), annot}
defp pad(1), do: " "
defp pad(2), do: " "
defp pad(3), do: " "
defp pad(4), do: " "
@doc """
`Regex.replace` with the arguments in the correct order
"""
def replace(text, regex, replacement, options \\ []) do
Regex.replace(regex, text, replacement, options)
end
@doc """
Replace <, >, and quotes with the corresponding entities. If
`encode` is true, convert ampersands, too, otherwise only
convert non-entity ampersands.
"""
@amp_rgx ~r{&(?!#?\w+;)}
def escape(html), do: _escape(Regex.replace(@amp_rgx, html, "&"))
defp _escape(html) do
html
|> String.replace("<", "<")
|> String.replace(">", ">")
|> String.replace("\"", """)
|> String.replace("'", "'")
end
end
# SPDX-License-Identifier: Apache-2.0