Skip to content

Commit

Permalink
Adding some code from box
Browse files Browse the repository at this point in the history
  • Loading branch information
clone1018 committed Mar 25, 2017
1 parent 9151155 commit bc0071f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/simplates/pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ defmodule Simplates.Pagination do

import Simplates.Simplate, only: [config: 1]

@script_regex ~r/^\<script\>(?P<raw>.+?)^\<\/script\>/sim
@template_regex ~r/^\<template\>(?P<raw>.+?)^\<\/template\>/sim

def parse_pages(raw) do
script = parse_scripts(Floki.find(raw, "script"))
templates = parse_templates(Floki.find(raw, "template"))
raw = "<root>" <> raw <> "</root>"
script = parse_scripts(Floki.find(raw, "root > script"))
templates = parse_templates(Floki.find(raw, "root > template"))

%{code: script, templates: templates}
end
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Simplates.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:sweet_xml, "~> 0.6.5"},
{:floki, "~> 0.14.0"},
{:mime, "~> 1.1"},
# Plug is only needed for type resolution for now
Expand Down
11 changes: 7 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%{"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
"earmark": {:hex, :earmark, "1.1.1", "433136b7f2e99cde88b745b3a0cfc3fbc81fe58b918a09b40fce7f00db4d8187", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"earmark": {:hex, :earmark, "0.1.19", "ffec54f520a11b711532c23d8a52b75a74c09697062d10613fa2dbdf8a9db36e", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.11.5", "0dc51cb84f8312162a2313d6c71573a9afa332333d8a332bb12540861b9834db", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"excoveralls": {:hex, :excoveralls, "0.6.2", "0e993d096f1fbb6e70a3daced5c89aac066bda6bce57829622aa2d1e2b338cfb", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"exquery": {:hex, :exquery, "0.0.11", "af9a865ee55abb6a14a3d6402595e9fcc55f5923a29a21fa8b3b9dd1d328c869", [:mix], [{:earmark, "~> 0.1.13", [hex: :earmark, optional: false]}, {:ex_doc, "~> 0.7", [hex: :ex_doc, optional: false]}]},
"floki": {:hex, :floki, "0.14.0", "91a6be57349e10a63cf52d7890479a19012cef9185fa93c305d4fe42e6a50dee", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
Expand All @@ -11,5 +12,7 @@
"mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"mochiweb": {:hex, :mochiweb, "2.15.0", "e1daac474df07651e5d17cc1e642c4069c7850dc4508d3db7263a0651330aacc", [:rebar3], []},
"plug": {:hex, :plug, "1.3.0", "6e2b01afc5db3fd011ca4a16efd9cb424528c157c30a44a0186bcc92c7b2e8f3", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
"mochiweb_html": {:hex, :mochiweb_html, "2.15.0", "d7402e967d7f9f2912f8befa813c37be62d5eeeddbbcb6fe986c44e01460d497", [:rebar3], []},
"plug": {:hex, :plug, "1.3.1", "aaf54675428a393370ec1f4c865e3fcf42608686960764beac93c5abeba9e655", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"sweet_xml": {:hex, :sweet_xml, "0.6.5", "dd9cde443212b505d1b5f9758feb2000e66a14d3c449f04c572f3048c66e6697", [:mix], []}}
84 changes: 84 additions & 0 deletions test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
defmodule XmlNode do
require Record
Record.defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")

def from_string(xml_string, options \\ [quiet: true]) do
{doc, []} =
xml_string
|> :binary.bin_to_list
|> :xmerl_scan.string(options)

doc
end

def all(node, path) do
for child_element <- xpath(node, path) do
child_element
end
end

def first(node, path), do: node |> xpath(path) |> take_one
defp take_one([head | _]), do: head
defp take_one(_), do: nil

def node_name(nil), do: nil
def node_name(node), do: elem(node, 1)

def attr(node, name), do: node |> xpath('./@#{name}') |> extract_attr
defp extract_attr([xmlAttribute(value: value)]), do: List.to_string(value)
defp extract_attr(_), do: nil

def text(node), do: node |> xpath('./text()') |> extract_text
defp extract_text([xmlText(value: value)]), do: List.to_string(value)
defp extract_text(_x), do: nil

defp xpath(nil, _), do: []
defp xpath(node, path) do
:xmerl_xpath.string(to_char_list(path), node)
end
end

doc = XmlNode.from_string(
"""
<root>
<template>
This test ensures anything inside this template tag is ignored entirely by the parser.
<script>s
food = "bar";
</script>
<template>
I could be a handlebars template or something, who knows :
</template>
</template>
</root>
"""
)

IO.inspect(doc)

Enum.each(XmlNode.all(doc, "//root/template"), fn(node) ->
IO.inspect(node)
IO.puts "#{XmlNode.node_name(node)} id=#{XmlNode.attr(node, "id")} text=#{XmlNode.text(node)}"
end)

IO.puts(
doc
|> XmlNode.first("//child[@id='2']")
|> XmlNode.text
)

IO.puts(
doc
|> XmlNode.first("//child[@id='3']")
|> XmlNode.text
)

IO.puts(
doc
|> XmlNode.first("//root")
|> XmlNode.first("template")
|> XmlNode.text
)
11 changes: 11 additions & 0 deletions test/simplates/fake-www/ignore-contents.spt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
This test ensures anything inside this template tag is ignored entirely by the parser.

<script>
food = "bar";
</script>

<template>
I could be a handlebars template or something, who knows :
</template>
</template>
26 changes: 26 additions & 0 deletions test/simplates/pagination_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,31 @@ defmodule Simplates.PaginationTest do
assert String.trim(pages.templates["media/type"].raw) == "Hello, world! I have SOME code and a specline!"
end

test "parser ignores any non-root level tags" do
pages = Pagination.parse_pages(~s(<template>
This test ensures anything inside this template tag is ignored entirely by the parser.
<script>
food = "bar";
</script>
<template>
I could be a handlebars template or something, who knows :
</template>
</template>))

IO.inspect(pages)

assert map_size(pages.templates) == 1
end


test "parser handles two template tags" do
pages = Pagination.parse_pages(~s(<template type="text/html"></template><template type="text/plain"></template>))

assert map_size(pages.templates) == 2
assert pages.templates["text/html"]
assert pages.templates["text/plain"]
end

end

0 comments on commit bc0071f

Please sign in to comment.