Skip to content

Commit

Permalink
Support pretty option (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella authored Nov 23, 2024
1 parent a6ee95d commit e516f7f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/live_view_native/stylesheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ defmodule LiveViewNative.Stylesheet do
def compile_string({class_or_list, style_list}) do
pretty = Application.get_env(:live_view_native_stylesheet, :pretty, false)

{class_or_list, style_list}
|> compile_ast()
|> :json.encode(&LiveViewNative.Stylesheet.Encoder.encode/2)
ast = compile_ast({class_or_list, style_list})

if Code.ensure_loaded?(:json) && pretty && Kernel.function_exported?(:json, :format, 2) do
:json.format(ast, &LiveViewNative.Stylesheet.Encoder.format/3)
else
:json.encode(ast, &LiveViewNative.Stylesheet.Encoder.encode/2)
end
|> IO.iodata_to_binary()
end

Expand Down
23 changes: 23 additions & 0 deletions lib/live_view_native/stylesheet/encoder.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
defmodule LiveViewNative.Stylesheet.Encoder do
def format({term, annotations, arguments}, encoder, state) do
annotations = Enum.into(annotations, %{}, &(&1))
format([term, annotations, arguments], encoder, state)
end

def format({name, value}, encoder, state) when is_atom(name),
do: :json.format_value(%{{name, value}}, encoder, state)

def format(atom, encoder, state) when is_atom(atom) do
cond do
Code.ensure_loaded?(atom) ->
"Elixir." <> module = Atom.to_string(atom)
:json.format_value(module, encoder, state)
true -> :json.format_value(atom, encoder, state)
end
end

def format(dynamic, encoder, state),
do: :json.format_value(dynamic, encoder, state)

def encode({term, annotations, arguments}, encoder) do
annotations = Enum.into(annotations, %{}, &(&1))
:json.encode_list([term, annotations, arguments], encoder)
end

def encode({name, value}, encoder) when is_atom(name),
do: :json.encode_map(%{{name, value}}, encoder)

def encode(atom, encoder) when is_atom(atom) do
cond do
Code.ensure_loaded?(atom) ->
Expand Down
2 changes: 1 addition & 1 deletion test/live_view_native_stylesheet_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule LiveViewNative.StylesheetTest do
test "can convert the output to a json string" do
output = MockSheet.compile_string(["complex"])

assert output == ~s({"complex":[["complex",{"line":33,"module":"MockSheet","file":"mock_sheet.ex"},[1,["complex_sub",{},[]],3]]]})
assert output == ~s({"complex":[["complex",{"line":33,"module":"MockSheet","file":"mock_sheet.ex"},[1,["complex_sub",{},[{"foo":"bar"}]],3]]]})
end

test "will not pattern match on nil or empty string" do
Expand Down
2 changes: 1 addition & 1 deletion test/support/mock_rules_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule MockRulesParser do
file: Keyword.get(opts, :file),
line: Keyword.get(opts, :line),
module: Keyword.get(opts, :module)
], [1, {:complex_sub, [], []}, 3]}
], [1, {:complex_sub, [], [{:foo, "bar"}]}, 3]}
end

defp parse_rule("rule-annotated", opts) do
Expand Down

0 comments on commit e516f7f

Please sign in to comment.