-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Mix task to convert HTML into Temple
- Loading branch information
Showing
8 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
elixir ref:v1.13.4 | ||
elixir 1.14.0-otp-25 | ||
erlang 25.0-rc2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
## Main | ||
|
||
### 0.9.0-rc.0 | ||
### 0.9.0 | ||
|
||
### Breaking Changes | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Mix.Tasks.Temple.Convert do | ||
use Mix.Task | ||
|
||
@shortdoc "A task to convert vanilla HTML into Temple syntax" | ||
|
||
def run(argv) do | ||
case argv do | ||
[] -> | ||
Mix.raise( | ||
"You need to provide the path to an HTML file you would like to convert to Temple syntax" | ||
) | ||
|
||
[file] -> | ||
file | ||
|> File.read!() | ||
|> Temple.Converter.convert() | ||
|> IO.puts() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
defmodule Temple.Converter do | ||
@boolean_attributes ~w[ | ||
allowfullscreen | ||
async | ||
autofocus | ||
autoplay | ||
checked | ||
controls | ||
default | ||
defer | ||
disabled | ||
formnovalidate | ||
ismap | ||
itemscope | ||
loop | ||
multiple | ||
muted | ||
nomodule | ||
novalidate | ||
open | ||
playsinline | ||
readonly | ||
required | ||
reversed | ||
selected | ||
truespeed | ||
] | ||
|
||
def convert(html) do | ||
html | ||
|> Floki.parse_fragment!() | ||
|> to_temple() | ||
|> :erlang.iolist_to_binary() | ||
|> Code.format_string!() | ||
|> :erlang.iolist_to_binary() | ||
end | ||
|
||
def to_temple([]) do | ||
[] | ||
end | ||
|
||
def to_temple([{tag, attrs, children} | rest]) do | ||
[ | ||
to_string(tag), | ||
" ", | ||
to_temple_attrs(attrs), | ||
" do\n", | ||
to_temple(children), | ||
"end\n" | ||
] ++ to_temple(rest) | ||
end | ||
|
||
def to_temple([{:comment, comment} | rest]) do | ||
[ | ||
comment | ||
|> String.split("\n") | ||
|> Enum.map_join("\n", fn line -> | ||
if String.trim(line) != "" do | ||
"# #{line}" | ||
else | ||
"" | ||
end | ||
end), | ||
"\n" | ||
] ++ to_temple(rest) | ||
end | ||
|
||
def to_temple([text | rest]) when is_binary(text) do | ||
[ | ||
text | ||
|> String.split("\n") | ||
|> Enum.map_join("\n", fn line -> | ||
if String.trim(line) != "" do | ||
escaped = String.replace(line, ~s|"|, ~s|\\"|) | ||
~s|"#{String.trim(escaped)}"| | ||
else | ||
"" | ||
end | ||
end), | ||
"\n" | ||
] ++ to_temple(rest) | ||
end | ||
|
||
defp to_temple_attrs([]) do | ||
"" | ||
end | ||
|
||
defp to_temple_attrs(attrs) do | ||
Enum.map_join(attrs, ", ", fn | ||
{attr, _value} when attr in @boolean_attributes -> | ||
to_attr_name(attr) <> ": true" | ||
|
||
{attr, value} -> | ||
~s|#{to_attr_name(attr)}: "#{value}"| | ||
end) | ||
end | ||
|
||
defp to_attr_name(name) do | ||
String.replace(name, "-", "_") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule Temple.ConverterTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Temple.Converter | ||
|
||
describe "convert/1" do | ||
test "converts basic html" do | ||
# html | ||
html = """ | ||
<div class="container" disabled aria-label="alice"> | ||
<!-- this is a comment --> | ||
I'm some content! | ||
</div> | ||
""" | ||
|
||
assert Converter.convert(html) === | ||
""" | ||
div class: "container", disabled: true, aria_label: "alice" do | ||
# this is a comment | ||
"I'm some content!" | ||
end | ||
""" | ||
|> String.trim() | ||
end | ||
|
||
test "multiline html comments" do | ||
# html | ||
html = """ | ||
<div > | ||
<!-- this is a comment | ||
and this is some multi | ||
stuff --> | ||
</div> | ||
""" | ||
|
||
assert Converter.convert(html) === | ||
""" | ||
div do | ||
# this is a comment | ||
# and this is some multi | ||
# stuff | ||
end | ||
""" | ||
|> String.trim() | ||
end | ||
|
||
test "script and style tag" do | ||
# html | ||
html = """ | ||
<script> | ||
console.log("ayy yoo"); | ||
</script> | ||
<style> | ||
.foo { | ||
color: red; | ||
} | ||
</style> | ||
""" | ||
|
||
assert Converter.convert(html) |> tap(&IO.puts/1) === | ||
""" | ||
script do | ||
"console.log(\\"ayy yoo\\");" | ||
end | ||
style do | ||
".foo {" | ||
"color: red;" | ||
"}" | ||
end | ||
""" | ||
|> String.trim() | ||
end | ||
end | ||
end |