Packages

A Sketch runtime package, made to generate CSS!

Current section

Files

Jump to
sketch_css src sketch_css@module.erl
Raw

src/sketch_css@module.erl

-module(sketch_css@module).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/sketch_css/module.gleam").
-export([remove_pipes/1, rewrite_imports/1, rewrite_exposings/1, build_interface/3, build_stylesheet/2, from_path/2, reject_cycles/1, convert_style/1]).
-export_type([module_/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.
?MODULEDOC(false).
-type module_() :: {module, binary(), binary(), glance:module_(), binary()}.
-file("src/sketch_css/module.gleam", 40).
?DOC(false).
-spec remove_pipes(module_()) -> module_().
remove_pipes(Module) ->
Ast = sketch_css@module@pipes:remove(erlang:element(4, Module)),
_record = Module,
{module,
erlang:element(2, _record),
erlang:element(3, _record),
Ast,
erlang:element(5, _record)}.
-file("src/sketch_css/module.gleam", 48).
?DOC(false).
-spec rewrite_imports(module_()) -> module_().
rewrite_imports(Module) ->
Ast = sketch_css@module@imports:rewrite(erlang:element(4, Module)),
_record = Module,
{module,
erlang:element(2, _record),
erlang:element(3, _record),
Ast,
erlang:element(5, _record)}.
-file("src/sketch_css/module.gleam", 55).
?DOC(false).
-spec rewrite_exposings(module_()) -> module_().
rewrite_exposings(Module) ->
Ast = sketch_css@module@exposings:rewrite(erlang:element(4, Module)),
_record = Module,
{module,
erlang:element(2, _record),
erlang:element(3, _record),
Ast,
erlang:element(5, _record)}.
-file("src/sketch_css/module.gleam", 84).
?DOC(false).
-spec build_interface(binary(), binary(), list({binary(), binary()})) -> {binary(),
binary()}.
build_interface(Module_name, Content, Names) ->
_pipe@4 = gleam@list:fold(
Names,
{Content, []},
fun(Acc, Val) ->
{Content@1, Interface} = Acc,
{Fun_name, Gen_name} = Val,
Module_name@1 = gleam@string:replace(
Module_name,
<<"/"/utf8>>,
<<"-"/utf8>>
),
Class_name = gleam@string:join(
[Module_name@1, Fun_name],
<<"_"/utf8>>
),
_pipe = Content@1,
_pipe@1 = gleam@string:replace(_pipe, Gen_name, Class_name),
gleam@pair:new(
_pipe@1,
begin
Class_name@1 = gleam@string:join(
[<<"\""/utf8>>, Class_name, <<"\""/utf8>>],
<<""/utf8>>
),
_pipe@2 = [<<"pub"/utf8>>,
<<"const"/utf8>>,
Fun_name,
<<"="/utf8>>,
Class_name@1],
_pipe@3 = gleam@string:join(_pipe@2, <<" "/utf8>>),
gleam@list:prepend(Interface, _pipe@3)
end
)
end
),
gleam@pair:map_second(
_pipe@4,
fun(_capture) -> gleam@string:join(_capture, <<"\n\n"/utf8>>) end
).
-file("src/sketch_css/module.gleam", 107).
?DOC(false).
-spec build_stylesheet(
{module_(), sketch_css@module@stylesheet:style_sheet()},
sketch:style_sheet()
) -> {sketch:style_sheet(), list({binary(), binary()})}.
build_stylesheet(Module, Stylesheet) ->
At_rule = fun(A, B) -> sketch:at_rule(B, A) end,
Stylesheet@1 = gleam@list:fold(
erlang:element(4, (erlang:element(2, Module))),
Stylesheet,
At_rule
),
gleam@list:fold(
erlang:element(3, (erlang:element(2, Module))),
{Stylesheet@1, []},
fun(Stylesheet@2, Class) ->
{Stylesheet@3, Names} = Stylesheet@2,
{Class_name, Class@1} = Class,
{Stylesheet@4, Generated_class} = sketch:class_name(
Class@1,
Stylesheet@3
),
{Stylesheet@4,
gleam@list:key_set(Names, Class_name, Generated_class)}
end
).
-file("src/sketch_css/module.gleam", 120).
?DOC(false).
-spec parse_module(binary()) -> {ok, glance:module_()} | {error, snag:snag()}.
parse_module(Source) ->
_pipe = glance:module(Source),
_pipe@1 = snag:map_error(_pipe, fun gleam@string:inspect/1),
snag:context(_pipe@1, <<"Illegal Gleam file"/utf8>>).
-file("src/sketch_css/module.gleam", 27).
?DOC(false).
-spec from_path(binary(), binary()) -> {ok, module_()} | {error, snag:snag()}.
from_path(Path, Root) ->
gleam@result:'try'(
sketch_css@fs:read_file(Path),
fun(Content) ->
gleam@result:map(
parse_module(Content),
fun(Ast) ->
{module,
Path,
Content,
Ast,
begin
_pipe = Path,
_pipe@1 = gleam@string:replace(
_pipe,
<<Root/binary, "/"/utf8>>,
<<""/utf8>>
),
_pipe@2 = gleam@string:replace(
_pipe@1,
Root,
<<""/utf8>>
),
gleam@string:replace(
_pipe@2,
<<".gleam"/utf8>>,
<<""/utf8>>
)
end}
end
)
end
).
-file("src/sketch_css/module.gleam", 126).
?DOC(false).
-spec to_assoc(module_()) -> {binary(), glance:module_()}.
to_assoc(Module) ->
{erlang:element(5, Module), erlang:element(4, Module)}.
-file("src/sketch_css/module.gleam", 61).
?DOC(false).
-spec reject_cycles(list(module_())) -> {ok, list(module_())} |
{error, snag:snag()}.
reject_cycles(Modules) ->
Modules_ = gleam@list:map(Modules, fun to_assoc/1),
_pipe = gleam@list:try_map(
Modules_,
fun(_capture) ->
sketch_css@module@dependencies:reject_cycles(_capture, [], Modules_)
end
),
gleam@result:replace(_pipe, Modules).
-file("src/sketch_css/module.gleam", 68).
?DOC(false).
-spec convert_style(list(module_())) -> fun((list({binary(),
sketch_css@module@stylesheet:style_sheet()}), module_()) -> list({binary(),
sketch_css@module@stylesheet:style_sheet()})).
convert_style(Modules) ->
fun(Mods, Module) ->
Is_present = begin
_pipe = gleam@list:key_find(Mods, erlang:element(5, Module)),
gleam@result:is_ok(_pipe)
end,
gleam@bool:guard(
Is_present,
Mods,
fun() ->
M = to_assoc(Module),
Modules_ = gleam@list:map(Modules, fun to_assoc/1),
_pipe@1 = sketch_css@module@dependencies:all_imports(
M,
Modules_,
[]
),
_pipe@3 = gleam@list:filter(
_pipe@1,
fun(I) -> _pipe@2 = gleam@list:key_find(Mods, I),
gleam@result:is_error(_pipe@2) end
),
_pipe@4 = gleam@list:filter_map(
_pipe@3,
fun(I@1) ->
gleam@list:find(
Modules,
fun(M@1) -> erlang:element(5, M@1) =:= I@1 end
)
end
),
_pipe@5 = gleam@list:fold(_pipe@4, Mods, convert_style(Modules)),
sketch_css@module@stylesheet:convert(_pipe@5, to_assoc(Module))
end
)
end.