@@ -3,7 +3,60 @@ defmodule ComponentsGuide.Research.Spec do
3
3
4
4
def search_for ( :caniuse , query ) when is_binary ( query ) do
5
5
url = "https://cdn.jsdelivr.net/npm/[email protected] /data.json"
6
- { :ok , data } = Source . json_at ( url )
6
+ result = Source . json_at ( url )
7
+ process_search_for ( :caniuse , query , result )
8
+ end
9
+
10
+ def search_for ( :npm_downloads_last_month , query ) when is_binary ( query ) do
11
+ query = String . trim ( query )
12
+ url = "https://api.npmjs.org/downloads/point/last-month/#{ query } "
13
+ case Source . json_at ( url ) do
14
+ { :ok , % { "downloads" => downloads_count , "package" => name } } ->
15
+ % { downloads_count: downloads_count , name: name }
16
+
17
+ { :ok , % { "error" => _error_message } } ->
18
+ nil
19
+
20
+ _ ->
21
+ nil
22
+ end
23
+ end
24
+
25
+ def search_for ( :whatwg_html_spec , query ) when is_binary ( query ) do
26
+ # url = "https://html.spec.whatwg.org/"
27
+ url = "https://html.spec.whatwg.org/dev/"
28
+
29
+ case Source . html_document_at ( url ) do
30
+ { :ok , document } ->
31
+ document
32
+ |> Floki . find ( "body" )
33
+ |> Floki . find ( "a:fl-contains('#{ query } ')" )
34
+ |> Floki . raw_html ( )
35
+
36
+ _ ->
37
+ ""
38
+ end
39
+ end
40
+
41
+ def search_for ( :html_aria_spec , query ) when is_binary ( query ) do
42
+ url = "https://www.w3.org/TR/html-aria/"
43
+ result = Source . html_document_at ( url )
44
+ process_search_for ( :html_aria_spec , query , result )
45
+ end
46
+
47
+ def search_for ( :wai_aria_practices , query ) when is_binary ( query ) do
48
+ url = "https://www.w3.org/TR/wai-aria-practices/"
49
+ result = Source . html_document_at ( url )
50
+ process_search_for ( :wai_aria_practices , query , result )
51
+ end
52
+
53
+ def search_for ( :bundlephobia , query ) when is_binary ( query ) do
54
+ { :ok , data } = Source . json_at ( "https://bundlephobia.com/api/size?package=#{ query } " )
55
+
56
+ data
57
+ end
58
+
59
+ defp process_search_for ( :caniuse , query , { :ok , data } ) when is_binary ( query ) do
7
60
table = data [ "data" ]
8
61
9
62
if false do
@@ -24,39 +77,29 @@ defmodule ComponentsGuide.Research.Spec do
24
77
end
25
78
end
26
79
27
- def search_for ( :npm_downloads_last_month , query ) when is_binary ( query ) do
28
- query = String . trim ( query )
29
- url = "https://api.npmjs.org/downloads/point/last-month/#{ query } "
30
- { :ok , data } = Source . json_at ( url )
31
-
32
- case data do
33
- % { "downloads" => downloads_count , "package" => name } ->
34
- % { downloads_count: downloads_count , name: name }
35
-
36
- % { "error" => _error_message } ->
37
- nil
80
+ defp process_search_for ( :wai_aria_practices , query , { :ok , document } ) when is_binary ( query ) do
81
+ html_elements = document |> Floki . find ( "[id*='#{ query } ']" )
38
82
39
- _ ->
40
- nil
41
- end
42
- end
83
+ results =
84
+ Enum . map ( html_elements , fn el ->
85
+ heading = Floki . find ( el , "h1, h2, h3, h4" )
43
86
44
- def search_for ( :whatwg_html_spec , query ) when is_binary ( query ) do
45
- # url = "https://html.spec.whatwg.org/"
46
- url = "https://html.spec.whatwg.org/dev/"
87
+ % {
88
+ heading: heading |> Floki . text ( ) ,
89
+ html: el
90
+ }
91
+ end )
47
92
48
- { :ok , document } = Source . html_document_at ( url )
93
+ results
49
94
50
- document
51
- |> Floki . find ( "body" )
52
- |> Floki . find ( "a:fl-contains('#{ query } ')" )
53
- |> Floki . raw_html ( )
95
+ # document
96
+ # # |> Floki.find("#toc li a")
97
+ # # |> Floki.find("[href*='#{query}']")
98
+ # |> Floki.find("[id*='#{query}']")
99
+ # |> Floki.raw_html()
54
100
end
55
101
56
- def search_for ( :html_aria_spec , query ) when is_binary ( query ) do
57
- url = "https://www.w3.org/TR/html-aria/"
58
- { :ok , document } = Source . html_document_at ( url )
59
-
102
+ defp process_search_for ( :html_aria_spec , query , { :ok , document } ) do
60
103
html_elements =
61
104
document
62
105
|> Floki . find (
@@ -101,34 +144,5 @@ defmodule ComponentsGuide.Research.Spec do
101
144
end )
102
145
end
103
146
104
- def search_for ( :wai_aria_practices , query ) when is_binary ( query ) do
105
- url = "https://www.w3.org/TR/wai-aria-practices/"
106
- { :ok , document } = Source . html_document_at ( url )
107
-
108
- html_elements = document |> Floki . find ( "[id*='#{ query } ']" )
109
-
110
- results =
111
- Enum . map ( html_elements , fn el ->
112
- heading = Floki . find ( el , "h1, h2, h3, h4" )
113
-
114
- % {
115
- heading: heading |> Floki . text ( ) ,
116
- html: el
117
- }
118
- end )
119
-
120
- results
121
-
122
- # document
123
- # # |> Floki.find("#toc li a")
124
- # # |> Floki.find("[href*='#{query}']")
125
- # |> Floki.find("[id*='#{query}']")
126
- # |> Floki.raw_html()
127
- end
128
-
129
- def search_for ( :bundlephobia , query ) when is_binary ( query ) do
130
- { :ok , data } = Source . json_at ( "https://bundlephobia.com/api/size?package=#{ query } " )
131
-
132
- data
133
- end
147
+ defp process_search_for ( _id , _query , _ ) , do: [ ]
134
148
end
0 commit comments