Skip to content

Commit

Permalink
Simplify generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tolyo committed Nov 21, 2023
1 parent 0bcc5fb commit a330a5d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build:

lint:
npx prettier ./generator --write

setup:
npx playwright install
11 changes: 2 additions & 9 deletions generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,10 @@ const fs = require("fs");
import Nitroux.Utils
${extractedElements
.map((e) => {
if (voidElements.includes(e)) {
return `
@spec ${e}(String.t() | Nitroux.Types.GlobalAttributes.t) :: binary
def ${e}(attrs), do: "${e}" |> tag(attrs, false)
`;
} else {
return `
@spec ${e}(String.t() | Nitroux.Types.GlobalAttributes.t) :: binary
def ${e}(attrs), do: "${e}" |> tag(attrs)
`;
}
def ${e}(attrs), do: "${e}" |> tag(attrs ${voidElements.includes(e) ? ", false" : ""})
`
})
.join("\n")}
end
Expand Down
18 changes: 10 additions & 8 deletions generator/global-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ fs.readFile(filePath, "utf8", (err, data) => {
# AUTO GENERATED DO NOT EDIT
@type t :: [${attributeList
.map((e) => {
return `
.map((e) => {
return `
${e.name}: ${e.name},
`;
})
.join("")}]
})
.join("")}]
${attributeList
.map((e) => {
.map((e) => {
return `
@typedoc """
${e.description}
"""
@type ${e.name} :: ${e.ty == "String" ? "String.t()" : "bool"}
@type ${e.name} :: ${
e.ty == "String" ? "String.t()" : "bool"
}
`;
})
.join("\n")}
})
.join("\n")}
end
`;
fs.writeFileSync(outputFilePath, wrappedElement);
Expand Down
3 changes: 2 additions & 1 deletion lib/nitroux/utils.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Nitroux.Utils do
@type tag :: String.t()
@spec tag(any, Nitroux.Types.GlobalAttributes.t() | [tag] | tag, any) :: <<_::24, _::_*8>>
@spec tag(any, Nitroux.Types.GlobalAttributes.t() | [tag] | tag, any) :: tag()
@doc """
Generates dynamic open and closing tags around content
iex> Nitroux.Utils.tag("div", ["hello", " ", "world"])
Expand All @@ -18,6 +18,7 @@ defmodule Nitroux.Utils do
def tag(name, attrs, container \\ true)
def tag(name, attrs, false), do: "<#{name}#{add_attributes(attrs)}/>"
def tag(name, [], _container), do: name |> tag("", true)

def tag(name, [{_, _} | _t] = keywordlist, _container),
do: "<#{name}#{add_attributes(keywordlist)}>#{Keyword.get(keywordlist, :html, "")}</#{name}>"

Expand Down

0 comments on commit a330a5d

Please sign in to comment.