Commit a8429182 authored by rinpatch's avatar rinpatch

Format the code

parent 98b312c0
Pipeline #19292 failed with stage
in 56 seconds
......@@ -2,6 +2,7 @@ image: elixir:1.7
variables:
MIX_ENV: test
GIT_SUBMODULE_STRATEGY: recursive
cache:
key: ${CI_COMMIT_REF_SLUG}
......
......@@ -76,15 +76,16 @@ defmodule Myhtmlex do
3. Bonus: You can [open up in-memory references to parsed trees](https://hexdocs.pm/myhtmlex/Myhtmlex.html#open/1), without parsing + mapping erlang terms in one go
"""
@type tag() :: String.t | atom()
@type attr() :: {String.t, String.t}
@type tag() :: String.t() | atom()
@type attr() :: {String.t(), String.t()}
@type attr_list() :: [] | [attr()]
@type comment_node() :: {:comment, String.t}
@type comment_node3() :: {:comment, [], String.t}
@type tree() :: {tag(), attr_list(), tree()}
| {tag(), attr_list(), nil}
| comment_node()
| comment_node3()
@type comment_node() :: {:comment, String.t()}
@type comment_node3() :: {:comment, [], String.t()}
@type tree() ::
{tag(), attr_list(), tree()}
| {tag(), attr_list(), nil}
| comment_node()
| comment_node3()
@type format_flag() :: :html_atoms | :nil_self_closing | :comment_tuple3
defp module() do
......@@ -110,7 +111,7 @@ defmodule Myhtmlex do
iex> Myhtmlex.decode("<br>")
{"html", [], [{"head", [], []}, {"body", [], [{"br", [], []}]}]}
"""
@spec decode(String.t) :: tree()
@spec decode(String.t()) :: tree()
def decode(bin) do
decode(bin, format: [])
end
......@@ -144,7 +145,7 @@ defmodule Myhtmlex do
{:body, [], [{:comment, [], " a comment "}, {"unknown", [], nil}]}]}
"""
@spec decode(String.t, format: [format_flag()]) :: tree()
@spec decode(String.t(), format: [format_flag()]) :: tree()
def decode(bin, format: flags) do
module().decode(bin, flags)
end
......@@ -152,7 +153,7 @@ defmodule Myhtmlex do
@doc """
Returns a reference to an internally parsed myhtml_tree_t. (Nif only!)
"""
@spec open(String.t) :: reference()
@spec open(String.t()) :: reference()
def open(bin) do
Myhtmlex.Nif.open(bin)
end
......@@ -173,4 +174,3 @@ defmodule Myhtmlex do
Myhtmlex.Nif.decode_tree(ref, flags)
end
end
defmodule Myhtmlex.Nif do
@moduledoc false
@on_load { :init, 0 }
@on_load {:init, 0}
app = Mix.Project.config[:app]
app = Mix.Project.config()[:app]
def init do
path = :filename.join(:code.priv_dir(unquote(app)), 'myhtmlex')
......@@ -24,4 +24,3 @@ defmodule Myhtmlex.Nif do
def decode_tree(tree, flags)
def decode_tree(_, _), do: exit(:nif_library_not_loaded)
end
......@@ -3,8 +3,7 @@ defmodule Myhtmlex.Safe do
use Application
app = Mix.Project.config[:app]
app = Mix.Project.config()[:app]
defp random_sname, do: :crypto.strong_rand_bytes(4) |> Base.encode16(case: :lower)
......@@ -12,13 +11,20 @@ defmodule Myhtmlex.Safe do
def start(_type, _args) do
import Supervisor.Spec
unless Node.alive? do
Nodex.Distributed.up
unless Node.alive?() do
Nodex.Distributed.up()
end
myhtml_worker = Path.join(:code.priv_dir(unquote(app)), "myhtml_worker")
children = [
worker(Nodex.Cnode, [%{exec_path: myhtml_worker, sname: sname()}, [name: Myhtmlex.Safe.Cnode]])
worker(Nodex.Cnode, [
%{exec_path: myhtml_worker, sname: sname()},
[name: Myhtmlex.Safe.Cnode]
])
]
Supervisor.start_link(children, strategy: :one_for_one, name: Myhtmlex.Safe.Supervisor)
end
......@@ -30,6 +36,4 @@ defmodule Myhtmlex.Safe do
{:ok, res} = Nodex.Cnode.call(Myhtmlex.Safe.Cnode, {:decode, bin, flags})
res
end
end
......@@ -8,9 +8,9 @@ defmodule Myhtmlex.Mixfile do
elixir: "~> 1.5",
deps: deps(),
package: package(),
compilers: [:myhtmlex_make] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
compilers: [:myhtmlex_make] ++ Mix.compilers(),
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
name: "Myhtmlex",
description: """
A module to decode HTML into a tree,
......@@ -65,7 +65,8 @@ defmodule Myhtmlex.Mixfile do
# benchmarking helpers
{:benchfella, "~> 0.3.0", only: :dev},
# cnode helpers
{:nodex, git: "https://github.com/rinpatch/nodex", ref: "12ca7a2c5b5791f1e847d73ed646cf006d4c8ca8"}
{:nodex,
git: "https://github.com/rinpatch/nodex", ref: "12ca7a2c5b5791f1e847d73ed646cf006d4c8ca8"}
]
end
......@@ -83,24 +84,27 @@ defmodule Mix.Tasks.Compile.MyhtmlexMake do
]
def run(_) do
if match? {:win32, _}, :os.type do
IO.warn "Windows is not yet a target."
if match?({:win32, _}, :os.type()) do
IO.warn("Windows is not yet a target.")
exit(1)
else
{result, _error_code} = System.cmd("make",
@artifacts,
stderr_to_stdout: true,
env: [{"MIX_ENV", to_string(Mix.env)}]
)
IO.binwrite result
{result, _error_code} =
System.cmd(
"make",
@artifacts,
stderr_to_stdout: true,
env: [{"MIX_ENV", to_string(Mix.env())}]
)
IO.binwrite(result)
end
:ok
end
def clean() do
{result, _error_code} = System.cmd("make", ["clean"], stderr_to_stdout: true)
Mix.shell.info result
Mix.shell().info(result)
:ok
end
end
......@@ -16,11 +16,14 @@ defmodule Myhtmlex.NifTest do
test "open and decode_tree" do
ref = Myhtmlex.open(~s'text node')
assert is_reference(ref)
assert {:html, [], [
{:head, [], []},
{:body, [], [
"text node"
]}
]} = Myhtmlex.decode_tree(ref, format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
"text node"
]}
]} = Myhtmlex.decode_tree(ref, format: [:html_atoms])
end
end
......@@ -5,4 +5,3 @@ defmodule Myhtmlex.SafeTest do
assert {"html", _attrs, _children} = Myhtmlex.decode("<div> <!----> </div>")
end
end
defmodule MyhtmlexSharedTests do
defmacro __using__(opts) do
module = Keyword.fetch!(opts, :module)
quote do
use ExUnit.Case
doctest Myhtmlex
......@@ -11,109 +12,141 @@ defmodule MyhtmlexSharedTests do
end
test "builds a tree, formatted like mochiweb by default" do
assert {"html", [], [
{"head", [], []},
{"body", [], [
{"br", [], []}
]}
]} = Myhtmlex.decode("<br>")
assert {"html", [],
[
{"head", [], []},
{"body", [],
[
{"br", [], []}
]}
]} = Myhtmlex.decode("<br>")
end
test "builds a tree, html tags as atoms" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{:br, [], []}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:br, [], []}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms])
end
test "builds a tree, nil self closing" do
assert {"html", [], [
{"head", [], []},
{"body", [], [
{"br", [], nil},
{"esi:include", [], nil}
]}
]} = Myhtmlex.decode("<br><esi:include />", format: [:nil_self_closing])
assert {"html", [],
[
{"head", [], []},
{"body", [],
[
{"br", [], nil},
{"esi:include", [], nil}
]}
]} = Myhtmlex.decode("<br><esi:include />", format: [:nil_self_closing])
end
test "builds a tree, multiple format options" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{:br, [], nil}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms, :nil_self_closing])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:br, [], nil}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms, :nil_self_closing])
end
test "attributes" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{:span, [{"id", "test"}, {"class", "foo garble"}], []}
]}
]} = Myhtmlex.decode(~s'<span id="test" class="foo garble"></span>', format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:span, [{"id", "test"}, {"class", "foo garble"}], []}
]}
]} =
Myhtmlex.decode(~s'<span id="test" class="foo garble"></span>',
format: [:html_atoms]
)
end
test "single attributes" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{:button, [{"disabled", "disabled"}, {"class", "foo garble"}], []}
]}
]} = Myhtmlex.decode(~s'<button disabled class="foo garble"></span>', format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:button, [{"disabled", "disabled"}, {"class", "foo garble"}], []}
]}
]} =
Myhtmlex.decode(~s'<button disabled class="foo garble"></span>',
format: [:html_atoms]
)
end
test "text nodes" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
"text node"
]}
]} = Myhtmlex.decode(~s'<body>text node</body>', format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
"text node"
]}
]} = Myhtmlex.decode(~s'<body>text node</body>', format: [:html_atoms])
end
test "broken input" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{:a, [{"<", "<"}], [" asdf"]}
]}
]} = Myhtmlex.decode(~s'<a <> asdf', format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:a, [{"<", "<"}], [" asdf"]}
]}
]} = Myhtmlex.decode(~s'<a <> asdf', format: [:html_atoms])
end
test "namespaced tags" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{"svg:svg", [], [
{"svg:path", [], []},
{"svg:a", [], []}
]}
]}
]} = Myhtmlex.decode(~s'<svg><path></path><a></a></svg>', format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{"svg:svg", [],
[
{"svg:path", [], []},
{"svg:a", [], []}
]}
]}
]} = Myhtmlex.decode(~s'<svg><path></path><a></a></svg>', format: [:html_atoms])
end
test "custom namespaced tags" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
{"esi:include", [], nil}
]}
]} = Myhtmlex.decode(~s'<esi:include />', format: [:html_atoms, :nil_self_closing])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{"esi:include", [], nil}
]}
]} =
Myhtmlex.decode(~s'<esi:include />', format: [:html_atoms, :nil_self_closing])
end
test "html comments" do
assert {:html, [], [
{:head, [], []},
{:body, [], [
comment: " a comment "
]}
]} = Myhtmlex.decode(~s'<body><!-- a comment --></body>', format: [:html_atoms])
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
comment: " a comment "
]}
]} = Myhtmlex.decode(~s'<body><!-- a comment --></body>', format: [:html_atoms])
end
end # quote
end # defmacro __using__
end
end
# quote
end
# defmacro __using__
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment