Current section

86 Versions

Jump to

Compare versions

8 files changed
+93 additions
-30 deletions
  @@ -5,6 +5,7 @@
5 5 [![Hex.pm](https://img.shields.io/hexpm/dt/floki.svg)](https://hex.pm/packages/floki)
6 6 [![Deps Status](https://beta.hexfaktor.org/badge/all/github/philss/floki.svg)](https://beta.hexfaktor.org/github/philss/floki)
7 7 [![Inline docs](http://inch-ci.org/github/philss/floki.svg?branch=master)](http://inch-ci.org/github/philss/floki)
8 + [![Ebert](https://ebertapp.io/github/philss/floki.svg)](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…