Current section

Files

Jump to
commonmark src commonmark@internal@html.erl
Raw

src/commonmark@internal@html.erl

-module(commonmark@internal@html).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([sanitize_href_property/1, sanitize_plain_text/1, inline_to_html/2, block_to_html/3]).
-spec sanitize_href_property(binary()) -> binary().
sanitize_href_property(Prop) ->
_pipe = Prop,
_pipe@1 = gleam@string:replace(_pipe, <<"&"/utf8>>, <<"&amp;"/utf8>>),
gleam@string:replace(_pipe@1, <<"\""/utf8>>, <<"%22"/utf8>>).
-spec sanitize_plain_text(binary()) -> binary().
sanitize_plain_text(Text) ->
_pipe = Text,
_pipe@1 = gleam@string:replace(_pipe, <<"&"/utf8>>, <<"&amp;"/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"<"/utf8>>, <<"&lt;"/utf8>>),
gleam@string:replace(_pipe@2, <<">"/utf8>>, <<"&gt;"/utf8>>).
-spec inline_to_html(
commonmark@ast:inline_node(),
gleam@dict:dict(binary(), commonmark@ast:reference_())
) -> binary().
inline_to_html(Inline, Refs) ->
case Inline of
{plain_text, Contents} ->
_pipe = Contents,
sanitize_plain_text(_pipe);
hard_line_break ->
<<"<br />\n"/utf8>>;
soft_line_break ->
<<"\n"/utf8>>;
{uri_autolink, Href} ->
<<<<<<<<"<a href=\""/utf8, (sanitize_href_property(Href))/binary>>/binary,
"\">"/utf8>>/binary,
Href/binary>>/binary,
"</a>"/utf8>>;
{email_autolink, Email} ->
<<<<<<<<"<a href=\"mailto:"/utf8,
(sanitize_href_property(Email))/binary>>/binary,
"\">"/utf8>>/binary,
Email/binary>>/binary,
"</a>"/utf8>>;
{code_span, Contents@1} ->
<<<<"<code>"/utf8,
(begin
_pipe@1 = Contents@1,
sanitize_plain_text(_pipe@1)
end)/binary>>/binary,
"</code>"/utf8>>;
{emphasis, Contents@2, _} ->
<<<<"<em>"/utf8,
(begin
_pipe@2 = Contents@2,
_pipe@3 = gleam@list:map(
_pipe@2,
fun(_capture) -> inline_to_html(_capture, Refs) end
),
gleam@string:join(_pipe@3, <<""/utf8>>)
end)/binary>>/binary,
"</em>"/utf8>>;
{strong_emphasis, Contents@3, _} ->
<<<<"<strong>"/utf8,
(begin
_pipe@4 = Contents@3,
_pipe@5 = gleam@list:map(
_pipe@4,
fun(_capture@1) ->
inline_to_html(_capture@1, Refs)
end
),
gleam@string:join(_pipe@5, <<""/utf8>>)
end)/binary>>/binary,
"</strong>"/utf8>>;
{strike_through, Contents@4} ->
<<<<"<s>"/utf8,
(begin
_pipe@6 = Contents@4,
_pipe@7 = gleam@list:map(
_pipe@6,
fun(_capture@2) ->
inline_to_html(_capture@2, Refs)
end
),
gleam@string:join(_pipe@7, <<""/utf8>>)
end)/binary>>/binary,
"</s>"/utf8>>;
{html_inline, Html} ->
Html;
{image, _, _} ->
<<"Image"/utf8>>;
{reference_link, _, _} ->
<<"Link"/utf8>>;
{link, _, _, _} ->
<<"Link"/utf8>>;
{named_entity, <<"amp"/utf8>>, _} ->
<<"&amp;"/utf8>>;
{named_entity, _, Cp} ->
gleam_stdlib:utf_codepoint_list_to_string(Cp);
{numeric_character_reference, Cp@1, _} ->
gleam_stdlib:utf_codepoint_list_to_string([Cp@1])
end.
-spec block_to_html(
commonmark@ast:block_node(),
gleam@dict:dict(binary(), commonmark@ast:reference_()),
boolean()
) -> binary().
block_to_html(Block, Refs, Tight) ->
case Block of
{code_block, none, _, Contents} ->
<<<<"<pre><code>"/utf8,
(begin
_pipe = Contents,
sanitize_plain_text(_pipe)
end)/binary>>/binary,
"</code></pre>\n"/utf8>>;
{code_block, {some, Info}, _, Contents@1} ->
<<<<<<<<"<pre><code class=\"language-"/utf8, Info/binary>>/binary,
"\">"/utf8>>/binary,
(begin
_pipe@1 = Contents@1,
sanitize_plain_text(_pipe@1)
end)/binary>>/binary,
"</code></pre>\n"/utf8>>;
{heading, Level, Contents@2} ->
<<<<<<<<<<<<"<h"/utf8, (gleam@int:to_string(Level))/binary>>/binary,
">"/utf8>>/binary,
(begin
_pipe@2 = Contents@2,
_pipe@3 = gleam@list:map(
_pipe@2,
fun(_capture) ->
inline_to_html(_capture, Refs)
end
),
gleam@string:join(_pipe@3, <<""/utf8>>)
end)/binary>>/binary,
"</h"/utf8>>/binary,
(gleam@int:to_string(Level))/binary>>/binary,
">\n"/utf8>>;
horizontal_break ->
<<"<hr />\n"/utf8>>;
{paragraph, Contents@3} when Tight ->
_pipe@4 = Contents@3,
_pipe@5 = gleam@list:map(
_pipe@4,
fun(_capture@1) -> inline_to_html(_capture@1, Refs) end
),
gleam@string:join(_pipe@5, <<""/utf8>>);
{paragraph, Contents@4} ->
<<<<"<p>"/utf8,
(begin
_pipe@6 = Contents@4,
_pipe@7 = gleam@list:map(
_pipe@6,
fun(_capture@2) ->
inline_to_html(_capture@2, Refs)
end
),
gleam@string:join(_pipe@7, <<""/utf8>>)
end)/binary>>/binary,
"</p>\n"/utf8>>;
{html_block, Html} ->
<<Html/binary, "\n"/utf8>>;
{block_quote, Contents@5} ->
<<<<"<blockquote>\n"/utf8,
(begin
_pipe@8 = Contents@5,
_pipe@9 = gleam@list:map(
_pipe@8,
fun(_capture@3) ->
block_to_html(_capture@3, Refs, false)
end
),
gleam@string:join(_pipe@9, <<""/utf8>>)
end)/binary>>/binary,
"</blockquote>\n"/utf8>>;
{ordered_list, Items, 1, _} ->
<<<<"<ol>\n"/utf8,
(begin
_pipe@10 = Items,
_pipe@11 = gleam@list:map(
_pipe@10,
fun(_capture@4) ->
list_item_to_html(_capture@4, Refs)
end
),
gleam@string:join(_pipe@11, <<""/utf8>>)
end)/binary>>/binary,
"</ol>\n"/utf8>>;
{ordered_list, Items@1, Start, _} ->
<<<<<<<<"<ol start=\""/utf8, (gleam@int:to_string(Start))/binary>>/binary,
"\">\n"/utf8>>/binary,
(begin
_pipe@12 = Items@1,
_pipe@13 = gleam@list:map(
_pipe@12,
fun(_capture@5) ->
list_item_to_html(_capture@5, Refs)
end
),
gleam@string:join(_pipe@13, <<""/utf8>>)
end)/binary>>/binary,
"</ol>\n"/utf8>>;
{unordered_list, Items@2, _} ->
<<<<"<ul>\n"/utf8,
(begin
_pipe@14 = Items@2,
_pipe@15 = gleam@list:map(
_pipe@14,
fun(_capture@6) ->
list_item_to_html(_capture@6, Refs)
end
),
gleam@string:join(_pipe@15, <<""/utf8>>)
end)/binary>>/binary,
"</ul>\n"/utf8>>
end.
-spec list_item_to_html(
commonmark@ast:list_item(),
gleam@dict:dict(binary(), commonmark@ast:reference_())
) -> binary().
list_item_to_html(Item, Refs) ->
case Item of
{list_item, []} ->
<<"<li></li>\n"/utf8>>;
{tight_list_item, []} ->
<<"<li></li>\n"/utf8>>;
{list_item, Contents} ->
<<<<"<li>\n"/utf8,
(begin
_pipe = Contents,
_pipe@1 = gleam@list:map(
_pipe,
fun(_capture) ->
block_to_html(_capture, Refs, false)
end
),
gleam@string:join(_pipe@1, <<""/utf8>>)
end)/binary>>/binary,
"</li>\n"/utf8>>;
{tight_list_item, Contents@1} ->
R = begin
_pipe@2 = Contents@1,
lists:reverse(_pipe@2)
end,
Rest = begin
_pipe@3 = R,
_pipe@4 = gleam@list:drop(_pipe@3, 1),
_pipe@5 = gleam@list:map(_pipe@4, fun(B) -> case B of
{paragraph, C} ->
block_to_html(
{paragraph,
gleam@list:concat(
[C, [soft_line_break]]
)},
Refs,
true
);
_ ->
block_to_html(B, Refs, true)
end end),
_pipe@6 = lists:reverse(_pipe@5),
gleam@string:join(_pipe@6, <<""/utf8>>)
end,
Last = case gleam@list:first(R) of
{ok, Block} ->
block_to_html(Block, Refs, true);
{error, _} ->
<<""/utf8>>
end,
<<<<<<"<li>"/utf8, Rest/binary>>/binary, Last/binary>>/binary,
"</li>\n"/utf8>>
end.