Skip to content

Commit 99be817

Browse files
committed
Show toc successfully
1 parent 30daf49 commit 99be817

File tree

6 files changed

+125
-6
lines changed

6 files changed

+125
-6
lines changed

apps/components_guide/lib/components_guide/application.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ defmodule ComponentsGuide.Application do
1212
%{
1313
id: :content_cache,
1414
start: {Cachex, :start_link, [:content_cache, []]}
15+
},
16+
%{
17+
id: :research_spec_cache,
18+
start: {Cachex, :start_link, [:research_spec_cache, []]}
1519
}
1620
# ComponentsGuide.Worker
1721
]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
defmodule ComponentsGuide.Research.Spec do
2+
alias ComponentsGuide.HTTPClient
3+
import Meeseeks.CSS
4+
import Meeseeks.XPath
5+
6+
@cache_name :research_spec_cache
7+
8+
defp read_cache(key) do
9+
value = Cachex.get(@cache_name, key)
10+
IO.puts("reading")
11+
IO.inspect(key)
12+
# IO.inspect(value)
13+
value
14+
end
15+
16+
defp write_cache(key, value) do
17+
Cachex.put(@cache_name, key, value)
18+
end
19+
20+
# defp fetch_url(url) when is_binary(url) do
21+
# key = {:fetch, url}
22+
# write_cache(key, :pending)
23+
# result = HTTPClient.get(url)
24+
# {:ok, response} = result
25+
# html = response.body
26+
# write_cache(key, {:html, html})
27+
# html
28+
# end
29+
30+
defp body({:fetch, url}) do
31+
result = HTTPClient.get(url)
32+
{:ok, response} = result
33+
html = response.body
34+
{:ok, html}
35+
end
36+
37+
defp body({:html_document, url}) do
38+
{:ok, html} = read({:fetch, url})
39+
document = Meeseeks.parse(html)
40+
{:ok, document}
41+
end
42+
43+
defp run(key) do
44+
tuple = {:ok, value} = body(key)
45+
write_cache(key, value)
46+
tuple
47+
end
48+
49+
defp read(key) do
50+
case read_cache(key) do
51+
{:ok, nil} ->
52+
run(key)
53+
54+
{:ok, value} ->
55+
{:ok, value}
56+
57+
other ->
58+
other
59+
end
60+
end
61+
62+
# defp html_string_for_url(url) when is_binary(url) do
63+
# with {:ok, result} <- read_cache({:fetch, url}),
64+
# {:html, value} <- result do
65+
# value
66+
# else
67+
# # Not in cache
68+
# {:ok, nil} ->
69+
# IO.puts("not in cache, fetching")
70+
# # fetch_url(url)
71+
# {:ok, html} = process({:fetch, url})
72+
# write_cache({:fetch, url}, {:html, html})
73+
# {:ok, html}
74+
75+
# _ ->
76+
# :err
77+
# end
78+
# end
79+
80+
# defp html_document_for_url(url) when is_binary(url) do
81+
# case read_cache({:html_document, url}) do
82+
# # Not in cache
83+
# {:ok, nil} ->
84+
# IO.puts("not in cache, parsing")
85+
# html = html_string_for_url(url)
86+
# document = Meeseeks.parse(html)
87+
# # :crypto.hash(:sha256,"I love Elixir")
88+
# write_cache({:html_document, url}, document)
89+
# document
90+
91+
# {:ok, document} ->
92+
# document
93+
94+
# _ ->
95+
# nil
96+
# end
97+
# end
98+
99+
def clear_search_cache() do
100+
Cachex.clear(@cache_name)
101+
end
102+
103+
def search_for(:whatwg_html_spec, query) when is_binary(query) do
104+
IO.puts("searching whatwg html spec")
105+
{:ok, document} = read({:html_document, "https://html.spec.whatwg.org/"})
106+
107+
# selector = css("#contents")
108+
selector = xpath("//*[@id='contents']/following-sibling::ol[1]")
109+
result = Meeseeks.one(document, selector)
110+
Meeseeks.html(result)
111+
end
112+
end

apps/components_guide/mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ defmodule ComponentsGuide.MixProject do
4141
{:httpotion, "~> 3.1.0"},
4242
{:jason, "~> 1.0"},
4343
{:tesla, "~> 1.3.0"},
44+
{:meeseeks, "~> 0.15.0"},
4445
]
4546
end
4647

apps/components_guide_web/lib/components_guide_web/controllers/research_controller.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule ComponentsGuideWeb.ResearchController do
22
use ComponentsGuideWeb, :controller
33

4-
alias ComponentsGuide.HTTPClient
4+
alias ComponentsGuide.Research.Spec
55

66
def index(conn, %{"q" => query}) do
77
query = query |> String.trim()
@@ -21,10 +21,7 @@ defmodule ComponentsGuideWeb.ResearchController do
2121
end
2222

2323
defp load_results(query) when is_binary(query) do
24-
url = "https://html.spec.whatwg.org/"
25-
result = HTTPClient.get(url)
26-
{:ok, response} = result
27-
html = response.body
28-
html
24+
# Spec.clear_search_cache()
25+
Spec.search_for(:whatwg_html_spec, query)
2926
end
3027
end

apps/components_guide_web/lib/components_guide_web/templates/research/index.html.eex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Results for
1313
<strong><%= @query %></strong>:
1414
</h1>
15+
<%#= raw @results %>
1516
<%= @results %>
1617
</div>
1718
</div>

mix.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], [], "hexpm", "6a8e5988872086f0506bef68311493551ac5beae7c06ba2a00d5e9f97a60f1c2"},
1414
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"},
1515
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
16+
"meeseeks": {:hex, :meeseeks, "0.15.0", "8ea95378a7cbaf8f17b7369c150bae6a8dc11c9793d9932deff0ce06c6a7e11a", [:mix], [{:meeseeks_html5ever, "~> 0.12.1", [hex: :meeseeks_html5ever, repo: "hexpm", optional: false]}], "hexpm", "3d3644ef0fd0eb4828f5d76fc8be413c1fa35ddbc22f2d424e99eea48a9b6013"},
17+
"meeseeks_html5ever": {:hex, :meeseeks_html5ever, "0.12.1", "718fab10d05b83204524a518b2b88caa37ba6a6e02f82e80d6a7bc47552fb54a", [:mix], [{:rustler, "~> 0.21.0", [hex: :rustler, repo: "hexpm", optional: false]}], "hexpm", "11489094637f49a26bad4610a9138352c8d229339d888169cb35b08cdfd8861a"},
1618
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"},
1719
"phoenix": {:hex, :phoenix, "1.4.6", "8535f4a01291f0fbc2c30c78c4ca6a2eacc148db5178ad76e8b2fc976c590115", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "17aa6f4909e41eebfa7589b61c71f0ebe8fdea997194fd85596e629bbf8d3e15"},
1820
"phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "fe15d9fee5b82f5e64800502011ffe530650d42e1710ae9b14bc4c9be38bf303"},
@@ -24,7 +26,9 @@
2426
"plug_cowboy": {:hex, :plug_cowboy, "2.0.2", "6055f16868cc4882b24b6e1d63d2bada94fb4978413377a3b32ac16c18dffba2", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "44a58653dda2b8cf69757c7f14c0920d4df0f79305ed8571bb93b2ea0a31a895"},
2527
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm", "73c1682f0e414cfb5d9b95c8e8cd6ffcfdae699e3b05e1db744e58b7be857759"},
2628
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
29+
"rustler": {:hex, :rustler, "0.21.0", "68cc4fc015d0b9541865ea78e78e9ef2dd91ee4be80bf543fd15791102a45aca", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "e5429378c397f37f1091a35593b153aee1925e197c6842d04648d802edb52f80"},
2730
"sleeplocks": {:hex, :sleeplocks, "1.1.1", "3d462a0639a6ef36cc75d6038b7393ae537ab394641beb59830a1b8271faeed3", [:rebar3], [], "hexpm", "84ee37aeff4d0d92b290fff986d6a95ac5eedf9b383fadfd1d88e9b84a1c02e1"},
2831
"tesla": {:hex, :tesla, "1.3.3", "26ae98627af5c406584aa6755ab5fc96315d70d69a24dd7f8369cfcb75094a45", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "2648f1c276102f9250299e0b7b57f3071c67827349d9173f34c281756a1b124c"},
32+
"toml": {:hex, :toml, "0.5.2", "e471388a8726d1ce51a6b32f864b8228a1eb8edc907a0edf2bb50eab9321b526", [:mix], [], "hexpm", "f1e3dabef71fb510d015fad18c0e05e7c57281001141504c6b69d94e99750a07"},
2933
"unsafe": {:hex, :unsafe, "1.0.1", "a27e1874f72ee49312e0a9ec2e0b27924214a05e3ddac90e91727bc76f8613d8", [:mix], [], "hexpm", "6c7729a2d214806450d29766abc2afaa7a2cbecf415be64f36a6691afebb50e5"},
3034
}

0 commit comments

Comments
 (0)