Packages

Pure-Gleam implementation of KnuthLiang Hyphenation.

Current section

Files

Jump to
hyphenation src hyphenation.erl
Raw

src/hyphenation.erl

-module(hyphenation).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([hyphenate/2, hyphenate_delim/3, hyphenator/1]).
-export_type([hyphenator/0]).
-opaque hyphenator() :: {hyphenator,
binary(),
hyphenation@internal@patterns:patterns(),
integer(),
integer()}.
-spec merge_two(list(GFH), fun((GFH, GFH) -> GFH)) -> list(GFH).
merge_two(Ls, F) ->
case Ls of
[X, Y | Xs] ->
[F(X, Y) | Xs];
_ ->
Ls
end.
-spec hyphenate(binary(), hyphenator()) -> list(binary()).
hyphenate(S, H) ->
Hyphenated = hyphenation@internal@patterns:process(S, erlang:element(3, H)),
Is_min_left = case Hyphenated of
[] ->
true;
[X | _] ->
gleam@string:length(X) < erlang:element(4, H)
end,
Hyphenated@1 = begin
_pipe = case Is_min_left of
true ->
merge_two(Hyphenated, fun gleam@string:append/2);
false ->
Hyphenated
end,
gleam@list:reverse(_pipe)
end,
Is_min_right = case Hyphenated@1 of
[] ->
true;
[X@1 | _] ->
gleam@string:length(X@1) < erlang:element(5, H)
end,
Hyphenated@2 = begin
_pipe@1 = case Is_min_right of
true ->
merge_two(
Hyphenated@1,
fun(X@2, Y) -> gleam@string:append(Y, X@2) end
);
false ->
Hyphenated@1
end,
gleam@list:reverse(_pipe@1)
end,
Hyphenated@2.
-spec hyphenate_delim(binary(), hyphenator(), binary()) -> binary().
hyphenate_delim(S, H, Delim) ->
gleam@string:join(hyphenate(S, H), Delim).
-spec hyphenator(hyphenation@language:language()) -> hyphenator().
hyphenator(L) ->
Md = hyphenation@internal@metadata:metadata(L),
Patterns = hyphenation@language:patterns(L),
{hyphenator,
erlang:element(2, Md),
Patterns,
erlang:element(3, Md),
erlang:element(4, Md)}.