-module(docs@utils@codeblock). -compile([no_auto_import, nowarn_unused_vars]). -export([process_code/1, codeblock/2]). -spec count_leading_spaces(binary(), integer()) -> integer(). count_leading_spaces(Line, Count) -> case gleam@string:pop_grapheme(Line) of {ok, {<<" "/utf8>>, Rest}} -> count_leading_spaces(Rest, Count + 1); _ -> Count end. -spec process_code(binary()) -> binary(). process_code(Code) -> Code@1 = begin _pipe = Code, gleam@string:trim(_pipe) end, Min_leading_spaces = begin _pipe@1 = Code@1, _pipe@2 = gleam@string:split(_pipe@1, <<"\n"/utf8>>), gleam@list:fold( _pipe@2, 0, fun(Acc, Line) -> case {Acc, count_leading_spaces(Line, 0)} of {0, Count} -> Count; {_, 0} -> Acc; {_, Count@1} -> gleam@int:min(Acc, Count@1) end end ) end, _pipe@3 = Code@1, _pipe@4 = gleam@string:split(_pipe@3, <<"\n"/utf8>>), _pipe@5 = gleam@list:map( _pipe@4, fun(Line@1) -> case gleam@string:pop_grapheme(Line@1) of {ok, {<<" "/utf8>>, _}} -> gleam@string:slice( Line@1, Min_leading_spaces, gleam@string:length(Line@1) - Min_leading_spaces ); _ -> Line@1 end end ), gleam@string:join(_pipe@5, <<"\n"/utf8>>). -spec codeblock(binary(), binary()) -> sprocket@element:element(). codeblock(Language, Body) -> sprocket@html:'div'( [sprocket@html@attributes:class( <<"not-prose overflow-x-auto text-sm"/utf8>> )], [sprocket@html:ignored( sprocket@html:pre( [], [sprocket@html:code_text( [sprocket@html@attributes:class( <<<<"language-"/utf8, Language/binary>>/binary, " rounded-lg"/utf8>> )], process_code(Body) )] ) )] ).