From e516f7f4c47c01dcd7b28d58a616c91099abf4d3 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Sat, 23 Nov 2024 10:49:57 -0500 Subject: [PATCH] Support pretty option (#91) --- lib/live_view_native/stylesheet.ex | 10 +++++++--- lib/live_view_native/stylesheet/encoder.ex | 23 ++++++++++++++++++++++ test/live_view_native_stylesheet_test.exs | 2 +- test/support/mock_rules_parser.ex | 2 +- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/live_view_native/stylesheet.ex b/lib/live_view_native/stylesheet.ex index d306ae3..13d1cf5 100644 --- a/lib/live_view_native/stylesheet.ex +++ b/lib/live_view_native/stylesheet.ex @@ -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 diff --git a/lib/live_view_native/stylesheet/encoder.ex b/lib/live_view_native/stylesheet/encoder.ex index 2b8ef06..62b4879 100644 --- a/lib/live_view_native/stylesheet/encoder.ex +++ b/lib/live_view_native/stylesheet/encoder.ex @@ -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) -> diff --git a/test/live_view_native_stylesheet_test.exs b/test/live_view_native_stylesheet_test.exs index 4a8047b..e1cb75c 100644 --- a/test/live_view_native_stylesheet_test.exs +++ b/test/live_view_native_stylesheet_test.exs @@ -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 diff --git a/test/support/mock_rules_parser.ex b/test/support/mock_rules_parser.ex index 60aa3db..060cb27 100644 --- a/test/support/mock_rules_parser.ex +++ b/test/support/mock_rules_parser.ex @@ -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