Current section

Files

Jump to
commonmark src commonmark@internal@parser@helpers.erl
Raw

src/commonmark@internal@parser@helpers.erl

-module(commonmark@internal@parser@helpers).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([ol_marker/1, ul_marker/1, trim_indent/2, determine_indent/1, parse_autolink/1, indent_pattern/1]).
-spec ol_marker(binary()) -> commonmark@ast:ordered_list_marker().
ol_marker(Marker) ->
case Marker of
<<"."/utf8>> ->
period_list_marker;
<<")"/utf8>> ->
bracket_list_marker;
_ ->
erlang:error(#{gleam_error => panic,
message => (<<"Invalid ordered list marker: "/utf8,
Marker/binary>>),
module => <<"commonmark/internal/parser/helpers"/utf8>>,
function => <<"ol_marker"/utf8>>,
line => 13})
end.
-spec ul_marker(binary()) -> commonmark@ast:unordered_list_marker().
ul_marker(Marker) ->
case Marker of
<<"*"/utf8>> ->
asterisk_list_marker;
<<"-"/utf8>> ->
dash_list_marker;
<<"+"/utf8>> ->
plus_list_marker;
_ ->
erlang:error(#{gleam_error => panic,
message => (<<"Invalid unordered list marker: "/utf8,
Marker/binary>>),
module => <<"commonmark/internal/parser/helpers"/utf8>>,
function => <<"ul_marker"/utf8>>,
line => 22})
end.
-spec do_trim_indent(binary(), integer(), integer()) -> binary().
do_trim_indent(Line, N, Removed) ->
case Line of
_ when Removed >= N ->
Line;
<<" "/utf8, Rest/binary>> ->
do_trim_indent(Rest, N, Removed + 1);
<<"\t"/utf8, Rest@1/binary>> ->
Next_tab_stop = Removed + (4 - (Removed rem 4)),
do_trim_indent(Rest@1, N, Removed + Next_tab_stop);
_ ->
Line
end.
-spec trim_indent(binary(), integer()) -> binary().
trim_indent(Line, N) ->
do_trim_indent(Line, N, 0).
-spec determine_indent(gleam@option:option(binary())) -> integer().
determine_indent(Indent) ->
case Indent of
none ->
0;
{some, S} ->
gleam@string:length(S)
end.
-spec parse_autolink(binary()) -> commonmark@ast:inline_node().
parse_autolink(Href) ->
_assert_subject = gleam@regex:from_string(
<<"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"/utf8>>
),
{ok, Email_regex} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"commonmark/internal/parser/helpers"/utf8>>,
function => <<"parse_autolink"/utf8>>,
line => 62})
end,
_assert_subject@1 = gleam@regex:from_string(
<<"^[a-zA-Z][-a-zA-Z+.]{1,31}:"/utf8>>
),
{ok, Uri_regex} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"commonmark/internal/parser/helpers"/utf8>>,
function => <<"parse_autolink"/utf8>>,
line => 66})
end,
case {gleam@regex:check(Email_regex, Href),
gleam@regex:check(Uri_regex, Href)} of
{true, _} ->
{email_autolink, Href};
{_, true} ->
{uri_autolink, Href};
{false, false} ->
{plain_text, <<<<"<"/utf8, Href/binary>>/binary, ">"/utf8>>}
end.
-spec indent_pattern(integer()) -> binary().
indent_pattern(Indent) ->
case Indent of
0 ->
<<""/utf8>>;
I when I >= 4 ->
<<"(?: {0,3}\t| )"/utf8, (indent_pattern(I - 4))/binary>>;
I@1 ->
<<<<" {"/utf8, (gleam@int:to_string(I@1))/binary>>/binary,
"}"/utf8>>
end.