Packages
floki
0.11.0
0.38.4
0.38.3
0.38.2
0.38.1
0.38.0
0.37.1
0.37.0
0.36.3
0.36.2
0.36.1
0.36.0
0.35.4
0.35.3
0.35.2
0.35.1
0.35.0
0.34.3
0.34.2
0.34.1
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.1
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.1
0.23.0
0.22.0
0.21.0
0.20.4
0.20.3
0.20.2
0.20.1
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.2
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Floki is a simple HTML parser that enables search for nodes using CSS selectors.
Current section
86 Versions
Jump to
Current section
86 Versions
Compare versions
7
files changed
+36
additions
-14
deletions
| @@ -100,7 +100,7 @@ Add Floki in your `mix.exs`, as a dependency: | |
| 100 100 | ```elixir |
| 101 101 | defp deps do |
| 102 102 | [ |
| 103 | - {:floki, "~> 0.10.1"} |
| 103 | + {:floki, "~> 0.11.0"} |
| 104 104 | ] |
| 105 105 | end |
| 106 106 | ``` |
| @@ -18,8 +18,8 @@ | |
| 18 18 | {<<"maintainers">>,[<<"Philip Sampaio Silva">>]}. |
| 19 19 | {<<"name">>,<<"floki">>}. |
| 20 20 | {<<"requirements">>, |
| 21 | - [[{<<"app">>,<<"mochiweb_html">>}, |
| 22 | - {<<"name">>,<<"mochiweb_html">>}, |
| 21 | + [[{<<"app">>,<<"mochiweb">>}, |
| 22 | + {<<"name">>,<<"mochiweb">>}, |
| 23 23 | {<<"optional">>,false}, |
| 24 24 | {<<"requirement">>,<<"~> 2.15">>}]]}. |
| 25 | - {<<"version">>,<<"0.10.1">>}. |
| 25 | + {<<"version">>,<<"0.11.0">>}. |
| @@ -74,7 +74,7 @@ defmodule Floki do | |
| 74 74 | Parser.parse(html) |
| 75 75 | end |
| 76 76 | |
| 77 | - @self_closing_tags ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "mete", "param", "source", "track", "wbr"] |
| 77 | + @self_closing_tags ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"] |
| 78 78 | |
| 79 79 | @doc """ |
| 80 80 | Converts HTML tree to raw HTML. |
| @@ -149,6 +149,16 @@ defmodule Floki do | |
| 149 149 | Finder.find(html_tree, selector) |
| 150 150 | end |
| 151 151 | |
| 152 | + def transform(html_tree_list, transformation) when is_list(html_tree_list) do |
| 153 | + html_tree_list |> Enum.map(fn |
| 154 | + html_tree -> |
| 155 | + Finder.apply_transformation(html_tree, transformation) |
| 156 | + end) |
| 157 | + end |
| 158 | + def transform(html_tree, transformation) do |
| 159 | + Finder.apply_transformation(html_tree, transformation) |
| 160 | + end |
| 161 | + |
| 152 162 | @doc """ |
| 153 163 | Returns the text nodes from a HTML tree. |
| 154 164 | By default, it will perform a deep search through the HTML tree. |
| @@ -30,6 +30,18 @@ defmodule Floki.Finder do | |
| 30 30 | find_selectors(html_tree, [selector]) |
| 31 31 | end |
| 32 32 | |
| 33 | + def apply_transformation({name, attrs, rest}, transformation) do |
| 34 | + {new_name, new_attrs} = transformation.({name, attrs}) |
| 35 | + |
| 36 | + new_rest = Enum.map(rest, fn(html_tree) -> |
| 37 | + apply_transformation(html_tree, transformation) |
| 38 | + end) |
| 39 | + |
| 40 | + {new_name, new_attrs, new_rest} |
| 41 | + end |
| 42 | + |
| 43 | + def apply_transformation(other, _transformation), do: other |
| 44 | + |
| 33 45 | defp find_selectors(html_tree, selectors) do |
| 34 46 | html_tree |
| 35 47 | |> traverse([], selectors, []) |
| @@ -1,4 +1,6 @@ | |
| 1 1 | defmodule Floki.SelectorParser do |
| 2 | + require Logger |
| 3 | + |
| 2 4 | @moduledoc """ |
| 3 5 | Parses a list of tokens returned from `SelectorTokenizer` and transfor into a `Selector`. |
| 4 6 | """ |
| @@ -59,8 +61,7 @@ defmodule Floki.SelectorParser do | |
| 59 61 | do_parse(t, %{selector | combinator: combinator}) |
| 60 62 | end |
| 61 63 | defp do_parse([{:unknown, _, unknown}|t], selector) do |
| 62 | - # TODO: find a better way to notify unknown tokens |
| 63 | - IO.puts("Unknown token #{inspect unknown}. Ignoring.") |
| 64 | + Logger.warn("Unknown token #{inspect unknown}. Ignoring.") |
| 64 65 | |
| 65 66 | do_parse(t, selector) |
| 66 67 | end |
| @@ -84,8 +85,7 @@ defmodule Floki.SelectorParser do | |
| 84 85 | consume_attribute(:done, t, attr_selector) |
| 85 86 | end |
| 86 87 | defp consume_attribute(:consuming, [unknown|t], attr_selector) do |
| 87 | - # TODO: find a better way to notify unknown tokens |
| 88 | - IO.puts("Unknown token #{inspect unknown}. Ignoring.") |
| 88 | + Logger.warn("Unknown token #{inspect unknown}. Ignoring.") |
| 89 89 | consume_attribute(:consuming, t, attr_selector) |
| 90 90 | end |
Loading more files…