Current section

86 Versions

Jump to

Compare versions

4 files changed
+42 additions
-10 deletions
  @@ -1,6 +1,11 @@
1 - # Floki [![Build status](https://travis-ci.org/philss/floki.svg?branch=master)](https://travis-ci.org/philss/floki) [![Floki version](https://img.shields.io/hexpm/v/floki.svg)](https://hex.pm/packages/floki) [![Inline docs](http://inch-ci.org/github/philss/floki.svg?branch=master)](http://inch-ci.org/github/philss/floki)
1 + # Floki
2 2
3 - Floki is a simple HTML parser that enables search using CSS like selectors.
3 + [![Build status](https://travis-ci.org/philss/floki.svg?branch=master)](https://travis-ci.org/philss/floki)
4 + [![Floki version](https://img.shields.io/hexpm/v/floki.svg)](https://hex.pm/packages/floki)
5 + [![Hex.pm](https://img.shields.io/hexpm/dt/floki.svg)](https://hex.pm/packages/floki)
6 + [![Inline docs](http://inch-ci.org/github/philss/floki.svg?branch=master)](http://inch-ci.org/github/philss/floki)
7 +
8 + Floki is a simple HTML parser that enables search using query selectors like jQuery or CSS.
4 9
5 10 You can search elements by class, tag name and id.
6 11
  @@ -23,7 +28,7 @@ Assuming that you have the following HTML:
23 28 </html>
24 29 ```
25 30
26 - Here are some of the queries that you can perform (with return examples):
31 + Here are some queries that you can perform (with return examples):
27 32
28 33 ```elixir
29 34 Floki.find(html, "#content")
  @@ -31,16 +36,20 @@ Floki.find(html, "#content")
31 36 # => [{"p", [{"class", "headline"}], ["Floki"]},
32 37 # => {"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}]}
33 38
39 +
34 40 Floki.find(html, ".headline") # returns a list with the `p` element
35 41 # => [{"p", [{"class", "headline"}], ["Floki"]}]
36 42
43 +
37 44 Floki.find(html, "a")
38 45 # => [{"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]},
39 46 # => {"a", [{"href", "https://hex.pm/packages/floki"}], ["Hex package"]}]
40 47
48 +
41 49 Floki.find(html, "#content a")
42 50 # => [{"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]}]
43 51
52 +
44 53 Floki.find(html, ".headline, a")
45 54 # => [{"p", [{"class", "headline"}], ["Floki"]},
46 55 # => {"a", [{"href", "http://github.com/philss/floki"}], ["Github page"]},
  @@ -58,17 +67,32 @@ Example of node:
58 67 So even if the only child node is the element text, it is represented
59 68 inside a list.
60 69
61 - You can write a simple HTML crawler (with support of [HTTPoison](https://github.com/edgurgel/httpoison)) with a few lines of code:
70 + You can write a simple HTML crawler with Floki and [HTTPoison](https://github.com/edgurgel/httpoison):
62 71
63 72 ```elixir
64 73 html
65 74 |> Floki.find(".pages a")
66 75 |> Floki.attribute("href")
67 76 |> Enum.map(fn(url) -> HTTPoison.get!(url) end)
77 +
68 78 ```
69 79
70 80 It is simple as that!
71 81
82 + ## Installation
83 +
84 + You can install Floki by adding a dependency to your mix file (mix.exs):
85 +
86 + ```elixir
87 + defp deps do
88 + [
89 + {:floki, "~> 0.2"}
90 + ]
91 + end
92 + ```
93 +
94 + After that, run `mix deps.get`.
95 +
72 96 ## API
73 97
74 98 To parse a HTML document, try:
  @@ -2,7 +2,7 @@
2 2 {<<"build_tools">>,[<<"mix">>]}.
3 3 {<<"contributors">>,[<<"Philip Sampaio Silva">>]}.
4 4 {<<"description">>,
5 - <<"A HTML parser and searcher.\n\nYou can search inside HTML documents using CSS like selectors.\n">>}.
5 + <<"A HTML parser and searcher.\n\nYou can search inside HTML documents using CSS like selectors.">>}.
6 6 {<<"elixir">>,<<">= 1.0.0">>}.
7 7 {<<"files">>,
8 8 [<<"lib/floki.ex">>,<<"lib/floki/deep_text.ex">>,
  @@ -17,4 +17,4 @@
17 17 [{<<"app">>,<<"mochiweb">>},
18 18 {<<"optional">>,nil},
19 19 {<<"requirement">>,<<"~> 2.12.2">>}]}]}.
20 - {<<"version">>,<<"0.2.0">>}.
20 + {<<"version">>,<<"0.2.1">>}.
  @@ -42,8 +42,7 @@ defmodule Floki do
42 42 You can write a simple HTML crawler (with support of [HTTPoison](https://github.com/edgurgel/httpoison)) with a few lines of code:
43 43
44 44 html
45 - |> Floki.find(".pages")
46 - |> Floki.find("a")
45 + |> Floki.find(".pages a")
47 46 |> Floki.attribute("href")
48 47 |> Enum.map(fn(url) -> HTTPoison.get!(url) end)
49 48
  @@ -60,11 +59,20 @@ defmodule Floki do
60 59 iex> Floki.parse("<div class=js-action>hello world</div>")
61 60 {"div", [{"class", "js-action"}], ["hello world"]}
62 61
62 + iex> Floki.parse("<div>first</div><div>second</div>")
63 + [{"div", [], ["first"]}, {"div", [], ["second"]}]
64 +
63 65 """
64 66
67 + @floki_root_node "floki"
68 +
65 69 @spec parse(binary) :: html_tree
66 70
67 - def parse(html), do: :mochiweb_html.parse(html)
71 + def parse(html) do
72 + html = "<#{@floki_root_node}>#{html}</#{@floki_root_node}>"
73 + {@floki_root_node, [], parsed} = :mochiweb_html.parse(html)
74 + if length(parsed) == 1, do: hd(parsed), else: parsed
75 + end
68 76
69 77 @doc """
70 78 Finds elements inside a HTML tree or string.
  @@ -3,7 +3,7 @@ defmodule Floki.Mixfile do
3 3
4 4 def project do
5 5 [app: :floki,
6 - version: "0.2.0",
6 + version: "0.2.1",
7 7 elixir: ">= 1.0.0",
8 8 package: package,
9 9 description: description,