Packages
earmark_parser
1.4.18
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/html_helpers.ex
defmodule EarmarkParser.Helpers.HtmlHelpers do
@moduledoc false
import EarmarkParser.Helpers.AttrParser
@simple_tag ~r{^<(.*?)\s*>}
@doc false
def augment_tag_with_ial(context, tag, ial, lnb) do
case Regex.run( @simple_tag, tag) do
nil ->
nil
["<code class=\"inline\">", "code class=\"inline\""] ->
tag = String.replace(tag, ~s{ class="inline"}, "")
add_attrs(context, tag, ial, [{"class", ["inline"]}], lnb)
_ ->
add_attrs(context, tag, ial, [], lnb)
end
end
##############################################
# add attributes to the outer tag in a block #
##############################################
@doc false
def add_attrs(context, text, attrs_as_string_or_map, default_attrs, lnb )
def add_attrs(context, text, nil, [], _lnb), do: {context, text}
def add_attrs(context, text, nil, default, lnb), do: add_attrs(context, text, %{}, default, lnb)
def add_attrs(context, text, attrs, default, lnb) when is_binary(attrs) do
{context1, attrs} = parse_attrs( context, attrs, lnb )
add_attrs(context1, text, attrs, default, lnb)
end
def add_attrs(context, text, attrs, default, _lnb) do
{context,
default
|> Map.new()
|> Map.merge(attrs, fn _k, v1, v2 -> v1 ++ v2 end)
|> attrs_to_string()
|> add_to(text)}
end
defp attrs_to_string(attrs) do
(for { name, value } <- attrs, do: ~s/#{name}="#{Enum.join(value, " ")}"/)
|> Enum.join(" ")
end
defp add_to(attrs, text) do
attrs = if attrs == "", do: "", else: " #{attrs}"
String.replace(text, ~r{\s?/?>}, "#{attrs}\\0", global: false)
end
end
# SPDX-License-Identifier: Apache-2.0