Packages

A robust, type-safe slugification library for Gleam that converts text into URL-friendly slugs

Current section

Files

Jump to
glugify src glugify@transformations.erl
Raw

src/glugify@transformations.erl

-module(glugify@transformations).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glugify/transformations.gleam").
-export([compose/1, normalize_unicode/0, transliterate_to_ascii/0]).
-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/transformations.gleam", 37).
?DOC(
" Composes multiple transformations into a single transformation.\n"
" \n"
" Transformations are applied in order from left to right.\n"
" If any transformation fails, the entire composition fails.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" let pipeline = compose([\n"
" normalize_unicode(),\n"
" transliterate_to_ascii()\n"
" ])\n"
" \n"
" pipeline(\"café\")\n"
" // -> Ok(\"cafe\")\n"
" ```\n"
).
-spec compose(
list(fun((binary()) -> {ok, binary()} |
{error, glugify@errors:slugify_error()}))
) -> fun((binary()) -> {ok, binary()} | {error, glugify@errors:slugify_error()}).
compose(Transformations) ->
fun(Input) ->
gleam@list:fold(
Transformations,
{ok, Input},
fun(Acc, Transform) -> gleam@result:'try'(Acc, Transform) end
)
end.
-file("src/glugify/transformations.gleam", 57).
?DOC(
" Creates a transformation that normalizes Unicode text.\n"
" \n"
" Currently this is a no-op transformation that passes text through unchanged.\n"
" In the future, this could implement Unicode normalization forms (NFC, NFD, etc.).\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" let normalizer = normalize_unicode()\n"
" normalizer(\"text\")\n"
" // -> Ok(\"text\")\n"
" ```\n"
).
-spec normalize_unicode() -> fun((binary()) -> {ok, binary()} |
{error, glugify@errors:slugify_error()}).
normalize_unicode() ->
fun(Text) -> {ok, Text} end.
-file("src/glugify/transformations.gleam", 73).
?DOC(
" Creates a transformation that transliterates Unicode to ASCII.\n"
" \n"
" This transformation converts accented characters and symbols to their\n"
" ASCII equivalents. Characters that cannot be transliterated will cause an error.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" let transliterator = transliterate_to_ascii()\n"
" transliterator(\"café\")\n"
" // -> Ok(\"cafe\")\n"
" ```\n"
).
-spec transliterate_to_ascii() -> fun((binary()) -> {ok, binary()} |
{error, glugify@errors:slugify_error()}).
transliterate_to_ascii() ->
fun glugify@unicode:transliterate_text/1.