-module(glugify@unicode). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/glugify/unicode.gleam"). -export([validate_ascii_only/1, validate_ascii_or_unicode/2, transliterate_text/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -file("src/glugify/unicode.gleam", 117). -spec is_ascii_safe(binary()) -> boolean(). is_ascii_safe(Char) -> case string:length(Char) of 1 -> case gleam_stdlib:string_pop_grapheme(Char) of {ok, {Grapheme, <<""/utf8>>}} -> case gleam@string:to_utf_codepoints(Grapheme) of [Codepoint] -> Code = gleam_stdlib:identity(Codepoint), (Code >= 32) andalso (Code =< 126); _ -> false end; _ -> false end; _ -> false end. -file("src/glugify/unicode.gleam", 83). -spec validate_ascii_graphemes(list(binary())) -> {ok, nil} | {error, glugify@errors:slugify_error()}. validate_ascii_graphemes(Graphemes) -> case Graphemes of [] -> {ok, nil}; [Grapheme | Rest] -> case is_ascii_safe(Grapheme) of true -> validate_ascii_graphemes(Rest); false -> {error, {transliteration_failed, Grapheme}} end end. -file("src/glugify/unicode.gleam", 52). ?DOC( " Validates that text contains only ASCII characters.\n" " \n" " This function checks that all characters in the input are within\n" " the printable ASCII range (32-126). Non-ASCII characters will cause\n" " an error to be returned.\n" " \n" " ## Examples\n" " \n" " ```gleam\n" " validate_ascii_only(\"hello world\")\n" " // -> Ok(\"hello world\")\n" " \n" " validate_ascii_only(\"café\")\n" " // -> Error(TransliterationFailed(\"é\"))\n" " ```\n" ). -spec validate_ascii_only(binary()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. validate_ascii_only(Text) -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = validate_ascii_graphemes(_pipe@1), gleam@result:map(_pipe@2, fun(_) -> Text end). -file("src/glugify/unicode.gleam", 73). ?DOC( " Validates text based on Unicode allowance settings.\n" " \n" " If `allow_unicode` is `True`, all text is accepted.\n" " If `allow_unicode` is `False`, only ASCII text is accepted.\n" " \n" " ## Examples\n" " \n" " ```gleam\n" " validate_ascii_or_unicode(\"café\", True)\n" " // -> Ok(\"café\")\n" " \n" " validate_ascii_or_unicode(\"café\", False)\n" " // -> Error(TransliterationFailed(\"é\"))\n" " ```\n" ). -spec validate_ascii_or_unicode(binary(), boolean()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. validate_ascii_or_unicode(Text, Allow_unicode) -> case Allow_unicode of true -> {ok, Text}; false -> validate_ascii_only(Text) end. -file("src/glugify/unicode.gleam", 97). -spec transliterate_graphemes(list(binary()), list(binary())) -> {ok, list(binary())} | {error, glugify@errors:slugify_error()}. transliterate_graphemes(Graphemes, Acc) -> case Graphemes of [] -> {ok, lists:reverse(Acc)}; [Grapheme | Rest] -> case gleam_stdlib:map_get( glugify@internal@char_maps:combined_char_map(), Grapheme ) of {ok, Replacement} -> transliterate_graphemes(Rest, [Replacement | Acc]); {error, _} -> case is_ascii_safe(Grapheme) of true -> transliterate_graphemes(Rest, [Grapheme | Acc]); false -> {error, {transliteration_failed, Grapheme}} end end end. -file("src/glugify/unicode.gleam", 30). ?DOC( " Transliterates Unicode text to ASCII equivalents.\n" " \n" " This function converts accented characters and common symbols to their\n" " ASCII equivalents. Characters that cannot be transliterated will cause\n" " an error to be returned.\n" " \n" " ## Examples\n" " \n" " ```gleam\n" " transliterate_text(\"café\")\n" " // -> Ok(\"cafe\")\n" " \n" " transliterate_text(\"naïve\")\n" " // -> Ok(\"naive\")\n" " \n" " transliterate_text(\"Résumé\")\n" " // -> Ok(\"Resume\")\n" " ```\n" " \n" " ## Errors\n" " \n" " Returns `TransliterationFailed(char)` when a character cannot be converted.\n" ). -spec transliterate_text(binary()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}. transliterate_text(Text) -> _pipe = Text, _pipe@1 = gleam@string:to_graphemes(_pipe), _pipe@2 = transliterate_graphemes(_pipe@1, []), gleam@result:map( _pipe@2, fun(_capture) -> gleam@string:join(_capture, <<""/utf8>>) end ).