Packages
floki
0.14.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
8
files changed
+93
additions
-30
deletions
| @@ -5,6 +5,7 @@ | |
| 5 5 | [](https://hex.pm/packages/floki) |
| 6 6 | [](https://beta.hexfaktor.org/github/philss/floki) |
| 7 7 | [](http://inch-ci.org/github/philss/floki) |
| 8 | + [](https://ebertapp.io/github/philss/floki) |
| 8 9 | |
| 9 10 | Floki is a simple HTML parser that enables search for nodes using CSS selectors. |
| 10 11 | |
| @@ -102,7 +103,7 @@ Add Floki to your `mix.exs`: | |
| 102 103 | ```elixir |
| 103 104 | defp deps do |
| 104 105 | [ |
| 105 | - {:floki, "~> 0.13.2"} |
| 106 | + {:floki, "~> 0.14.0"} |
| 106 107 | ] |
| 107 108 | end |
| 108 109 | ``` |
| @@ -118,7 +119,47 @@ If you get this [kind of error](https://github.com/philss/floki/issues/35), | |
| 118 119 | you need to install the `erlang-dev` and `erlang-parsetools` packages in order get the `leex` module. |
| 119 120 | The packages names may be different depending on your OS. |
| 120 121 | |
| 121 | - ## More about the API |
| 122 | + ### Optional - Using http5ever as the HTML parser |
| 123 | + |
| 124 | + You can configure Floki to use [html5ever](https://github.com/servo/html5ever) as your HTML parser. |
| 125 | + This is recommended if you need [better performance](https://gist.github.com/philss/70b4b0294f29501c3c7e0f60338cc8bd) |
| 126 | + and a more accurate parser. However `html5ever` is being under active development and **may be unstable**. |
| 127 | + |
| 128 | + Since it's written in Rust, we need to install Rust and compile the project. Luckily we have have the |
| 129 | + [html5ever Elixir NIF](https://github.com/hansihe/html5ever_elixir) that makes the integration very easy. |
| 130 | + |
| 131 | + You still need to install Rust in your system. To do that, please |
| 132 | + [follow the instruction](https://www.rust-lang.org/en-US/install.html) presented in the official page. |
| 133 | + |
| 134 | + #### Installing html5ever |
| 135 | + |
| 136 | + After setup Rust, you need to add `html5ever` NIF to your dependency list: |
| 137 | + |
| 138 | + ```elixir |
| 139 | + defp deps do |
| 140 | + [ |
| 141 | + {:floki, "~> 0.14.0"}, |
| 142 | + {:html5ever, "~> 0.2.0"} |
| 143 | + ] |
| 144 | + end |
| 145 | + ``` |
| 146 | + |
| 147 | + Run `mix deps.get` and compiles the project with `mix compile` to make sure it works. |
| 148 | + |
| 149 | + Then you need to configure your app to use `html5ever`: |
| 150 | + |
| 151 | + ```elixir |
| 152 | + # in config/config.exs |
| 153 | + |
| 154 | + config :floki, :html_parser, Floki.HTMLParser.Html5ever |
| 155 | + ``` |
| 156 | + |
| 157 | + After that you are able to use `html5ever` as your HTML parser with Floki. |
| 158 | + |
| 159 | + To know more about NIFs, check the [Erlang docs](http://erlang.org/doc/man/erl_nif.html). |
| 160 | + And to know why Rust NIFs can be better, check the [Rustler](https://github.com/hansihe/rustler) project. |
| 161 | + |
| 162 | + ## More about Floki API |
| 122 163 | |
| 123 164 | To parse a HTML document, try: |
| 124 165 | |
| @@ -174,10 +215,6 @@ Floki.find(html, ".headline") | |
| 174 215 | # => "Floki" |
| 175 216 | ``` |
| 176 217 | |
| 177 | - ## References |
| 178 | - |
| 179 | - - https://www.w3.org/community/webed/wiki/CSS/Selectors |
| 180 | - |
| 181 218 | ## License |
| 182 219 | |
| 183 220 | Floki is under MIT license. Check the `LICENSE` file for more details. |
| @@ -7,16 +7,18 @@ | |
| 7 7 | [<<"lib/floki.ex">>,<<"lib/floki/attribute_selector.ex">>, |
| 8 8 | <<"lib/floki/combinator.ex">>,<<"lib/floki/deep_text.ex">>, |
| 9 9 | <<"lib/floki/filter_out.ex">>,<<"lib/floki/finder.ex">>, |
| 10 | - <<"lib/floki/flat_text.ex">>,<<"lib/floki/html_tree.ex">>, |
| 10 | + <<"lib/floki/flat_text.ex">>,<<"lib/floki/html_parser.ex">>, |
| 11 | + <<"lib/floki/html_parser/html5ever.ex">>, |
| 12 | + <<"lib/floki/html_parser/mochiweb.ex">>,<<"lib/floki/html_tree.ex">>, |
| 11 13 | <<"lib/floki/html_tree/comment.ex">>,<<"lib/floki/html_tree/html_node.ex">>, |
| 12 14 | <<"lib/floki/html_tree/id_seeder.ex">>,<<"lib/floki/html_tree/text.ex">>, |
| 13 | - <<"lib/floki/parser.ex">>,<<"lib/floki/selector.ex">>, |
| 14 | - <<"lib/floki/selector/pseudo_class.ex">>,<<"lib/floki/selector_parser.ex">>, |
| 15 | - <<"lib/floki/selector_tokenizer.ex">>,<<"src/floki_selector_lexer.xrl">>, |
| 16 | - <<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}. |
| 15 | + <<"lib/floki/selector.ex">>,<<"lib/floki/selector/pseudo_class.ex">>, |
| 16 | + <<"lib/floki/selector_parser.ex">>,<<"lib/floki/selector_tokenizer.ex">>, |
| 17 | + <<"src/floki_selector_lexer.xrl">>,<<"mix.exs">>,<<"README.md">>, |
| 18 | + <<"LICENSE">>]}. |
| 17 19 | {<<"licenses">>,[<<"MIT">>]}. |
| 18 20 | {<<"links">>, |
| 19 | - [{<<"Docs">>,<<"http://hexdocs.pm/floki">>}, |
| 21 | + [{<<"Docs">>,<<"https://hexdocs.pm/floki">>}, |
| 20 22 | {<<"GitHub">>,<<"https://github.com/philss/floki">>}]}. |
| 21 23 | {<<"maintainers">>,[<<"Philip Sampaio Silva">>]}. |
| 22 24 | {<<"name">>,<<"floki">>}. |
| @@ -25,4 +27,4 @@ | |
| 25 27 | {<<"name">>,<<"mochiweb">>}, |
| 26 28 | {<<"optional">>,false}, |
| 27 29 | {<<"requirement">>,<<"~> 2.15">>}]]}. |
| 28 | - {<<"version">>,<<"0.13.2">>}. |
| 30 | + {<<"version">>,<<"0.14.0">>}. |
| @@ -1,5 +1,5 @@ | |
| 1 1 | defmodule Floki do |
| 2 | - alias Floki.{Finder, Parser, FilterOut, HTMLTree} |
| 2 | + alias Floki.{Finder, HTMLParser, FilterOut, HTMLTree} |
| 3 3 | |
| 4 4 | @moduledoc """ |
| 5 5 | Floki is a simple HTML parser that enables search for nodes using CSS selectors. |
| @@ -69,7 +69,7 @@ defmodule Floki do | |
| 69 69 | @spec parse(binary) :: html_tree |
| 70 70 | |
| 71 71 | def parse(html) do |
| 72 | - Parser.parse(html) |
| 72 | + HTMLParser.parse(html) |
| 73 73 | end |
| 74 74 | |
| 75 75 | @self_closing_tags ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"] |
| @@ -0,0 +1,9 @@ | |
| 1 | + defmodule Floki.HTMLParser do |
| 2 | + @moduledoc false |
| 3 | + @default_parser Floki.HTMLParser.Mochiweb |
| 4 | + |
| 5 | + def parse(html) do |
| 6 | + parser = Application.get_env(:floki, :html_parser, @default_parser) |
| 7 | + parser.parse(html) |
| 8 | + end |
| 9 | + end |
| @@ -0,0 +1,15 @@ | |
| 1 | + defmodule Floki.HTMLParser.Html5ever do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + def parse(html) do |
| 5 | + case Code.ensure_loaded(Html5ever) do |
| 6 | + {:module, module} -> |
| 7 | + case module.parse(html) do |
| 8 | + {:ok, result} -> result |
| 9 | + {:error, error} -> {:error, error} |
| 10 | + end |
| 11 | + {:error, _reason} -> |
| 12 | + raise "Expected module Html5ever to be available." |
| 13 | + end |
| 14 | + end |
| 15 | + end |
Loading more files…