Current section
Files
Jump to
Current section
Files
src/hyphenation@internal@codegen.erl
-module(hyphenation@internal@codegen).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([main/0]).
-export_type([license/0, content/0]).
-type license() :: mit | lppl | gpl | other_free | unlicense.
-type content() :: patterns | exceptions.
-spec gen(binary(), content()) -> binary().
gen(Raw_str, C) ->
Constant = case C of
patterns ->
<<"patterns"/utf8>>;
exceptions ->
<<"exceptions"/utf8>>
end,
Transform = fun(Pattern) -> case C of
patterns ->
<<<<"\""/utf8, Pattern/binary>>/binary, "\","/utf8>>;
exceptions ->
<<<<<<<<<<"#(\""/utf8,
(gleam@string:replace(
Pattern,
<<"-"/utf8>>,
<<""/utf8>>
))/binary>>/binary,
"\","/utf8>>/binary,
"\""/utf8>>/binary,
Pattern/binary>>/binary,
"\"),"/utf8>>
end end,
Content = begin
_pipe = Raw_str,
_pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>),
_pipe@2 = gleam@list:filter(_pipe@1, fun(X) -> X /= <<""/utf8>> end),
_pipe@3 = gleam@list:map(_pipe@2, Transform),
gleam@string:join(_pipe@3, <<"\n"/utf8>>)
end,
gleam@string:join(
[<<<<"pub const "/utf8, Constant/binary>>/binary, " = ["/utf8>>,
Content,
<<"]"/utf8>>],
<<"\n"/utf8>>
).
-spec disclaimer(hyphenation@language:language()) -> binary().
disclaimer(L) ->
<<"// This file was generated from ./src/hyphenation/internal/codegen.gleam
// Data is sourced from:
// Patterns and Exceptions - https://ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/txt
// License Info - https://ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex"/utf8>>.
-spec copyright(hyphenation@language:language()) -> binary().
copyright(L) ->
case L of
english_us ->
<<"// Copyright (C) 1990, 2004, 2005 Gerard D.C. Kuiken"/utf8>>
end.
-spec license(hyphenation@language:language()) -> license().
license(L) ->
case L of
english_us ->
other_free
end.
-spec license_text(license()) -> binary().
license_text(L) ->
case L of
mit ->
<<"// License: MIT (https://opensource.org/licenses/MIT)
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// \"Software\"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE."/utf8>>;
other_free ->
<<"// Copying and distribution of this file, with or without modification,
// are permitted in any medium without royalty provided the copyright
// notice and this notice are preserved"/utf8>>;
_ ->
<<""/utf8>>
end.
-spec filepaths(hyphenation@language:language()) -> {binary(),
binary(),
binary()}.
filepaths(L) ->
Abbrev = erlang:element(2, hyphenation@internal@metadata:metadata(L)),
Patterns = <<<<"tex-hyphen/hyph-"/utf8, Abbrev/binary>>/binary,
".pat.txt"/utf8>>,
Exceptions = <<<<"tex-hyphen/hyph-"/utf8, Abbrev/binary>>/binary,
".hyp.txt"/utf8>>,
Out = <<<<"src/hyphenation/internal/data/"/utf8,
(gleam@string:replace(Abbrev, <<"-"/utf8>>, <<"_"/utf8>>))/binary>>/binary,
".gleam"/utf8>>,
{Patterns, Exceptions, Out}.
-spec main() -> {ok,
{ok, {ok, nil} | {error, simplifile:file_error()}} |
{error, simplifile:file_error()}} |
{error, simplifile:file_error()}.
main() ->
gleam@io:print(<<"from internal/codegen"/utf8>>),
Lang = english_us,
{Patterns_path, Exceptions_path, Out_path} = filepaths(Lang),
gleam@result:map(
simplifile:read(Patterns_path),
fun(Patterns_str) ->
gleam@result:map(
simplifile:read(Exceptions_path),
fun(Exceptions_str) ->
Patterns = gen(Patterns_str, patterns),
Exceptions = gen(Exceptions_str, exceptions),
Res = begin
_pipe = [disclaimer(Lang),
copyright(Lang),
license_text(license(Lang)),
Patterns,
Exceptions],
gleam@string:join(_pipe, <<"\n\n"/utf8>>)
end,
_assert_subject = simplifile:write(Out_path, Res),
{ok, nil} = case _assert_subject of
{ok, nil} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"hyphenation/internal/codegen"/utf8>>,
function => <<"main"/utf8>>,
line => 36})
end
end
)
end
).