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/1, block_to_html/1]).
-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()) -> binary().
inline_to_html(Inline) ->
case Inline of
{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 inline_to_html/1),
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 inline_to_html/1),
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 inline_to_html/1),
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()) -> binary().
block_to_html(Block) ->
case Block of
{link_reference, _, _} ->
<<""/utf8>>;
{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 inline_to_html/1
),
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} ->
<<<<"<p>"/utf8,
(begin
_pipe@4 = Contents@3,
_pipe@5 = gleam@list:map(_pipe@4, fun inline_to_html/1),
gleam@string:join(_pipe@5, <<""/utf8>>)
end)/binary>>/binary,
"</p>\n"/utf8>>;
{html_block, Html} ->
<<Html/binary, "\n"/utf8>>;
{block_quote, Contents@4} ->
<<<<"<blockquote>"/utf8,
(begin
_pipe@6 = Contents@4,
_pipe@7 = gleam@list:map(_pipe@6, fun block_to_html/1),
gleam@string:join(_pipe@7, <<""/utf8>>)
end)/binary>>/binary,
"</blockquote>\n"/utf8>>;
{ordered_list, Items, 1, _} ->
<<<<"<ol>"/utf8,
(begin
_pipe@8 = Items,
_pipe@11 = gleam@list:map(
_pipe@8,
fun(Item) ->
<<<<"<li>"/utf8,
(begin
_pipe@9 = erlang:element(2, Item),
_pipe@10 = gleam@list:map(
_pipe@9,
fun block_to_html/1
),
gleam@string:join(
_pipe@10,
<<""/utf8>>
)
end)/binary>>/binary,
"</li>"/utf8>>
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,
"\">"/utf8>>/binary,
(begin
_pipe@12 = Items@1,
_pipe@15 = gleam@list:map(
_pipe@12,
fun(Item@1) ->
<<<<"<li>"/utf8,
(begin
_pipe@13 = erlang:element(2, Item@1),
_pipe@14 = gleam@list:map(
_pipe@13,
fun block_to_html/1
),
gleam@string:join(
_pipe@14,
<<""/utf8>>
)
end)/binary>>/binary,
"</li>"/utf8>>
end
),
gleam@string:join(_pipe@15, <<""/utf8>>)
end)/binary>>/binary,
"</ol>/n"/utf8>>;
{unordered_list, Items@2, _} ->
<<<<"<ul>"/utf8,
(begin
_pipe@16 = Items@2,
_pipe@19 = gleam@list:map(
_pipe@16,
fun(Item@2) ->
<<<<"<li>"/utf8,
(begin
_pipe@17 = erlang:element(2, Item@2),
_pipe@18 = gleam@list:map(
_pipe@17,
fun block_to_html/1
),
gleam@string:join(
_pipe@18,
<<""/utf8>>
)
end)/binary>>/binary,
"</li>"/utf8>>
end
),
gleam@string:join(_pipe@19, <<""/utf8>>)
end)/binary>>/binary,
"</ul>/n"/utf8>>
end.