Skip to content

Commit 27a6e02

Browse files
authored
Merge pull request #2684 from cerredz/issuehunt-cyclejs-devdocs
Add Cycle.js documentation scraper
2 parents 1c5e5f1 + dc085c1 commit 27a6e02

6 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Docs
2+
class Cyclejs
3+
class CleanHtmlFilter < Filter
4+
def call
5+
return "<h1>Cycle.js</h1><p>A functional and reactive JavaScript framework for predictable code</p>" if root_page?
6+
css('br').remove
7+
8+
css('pre > code').each do |node|
9+
parent = node.parent
10+
parent['data-language'] = 'javascript'
11+
parent.content = node.content.strip
12+
end
13+
14+
css('table[style]', 'tr[style]', 'td[style]', 'th[style]').remove_attr('style')
15+
css('img').each do |node|
16+
node['alt'] = node['alt'].presence || ''
17+
end
18+
19+
doc
20+
end
21+
end
22+
end
23+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Docs
2+
class Cyclejs
3+
class EntriesFilter < Docs::EntriesFilter
4+
def get_name
5+
title = at_css('h1')
6+
name = title ? title.content.strip : subpath.sub(/\.html\z/, '').titleize
7+
name = 'Cycle.js' if root_page?
8+
name = 'API Reference' if slug == 'api/index'
9+
name.delete_suffix! ' - source'
10+
name
11+
end
12+
13+
def get_type
14+
slug.start_with?('api/') ? 'API' : 'Guide'
15+
end
16+
17+
def additional_entries
18+
css('h2[id], h3[id]').map do |node|
19+
name = node.content.strip
20+
name.sub!(/\A#\s*/, '')
21+
name.sub!(/\s+#\z/, '')
22+
[get_name + ': ' + name, node['id']]
23+
end
24+
end
25+
end
26+
end
27+
end

lib/docs/scrapers/cyclejs.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require 'redcarpet'
2+
3+
module Docs
4+
class Cyclejs < UrlScraper
5+
self.name = 'Cycle.js'
6+
self.slug = 'cyclejs'
7+
self.type = 'cyclejs'
8+
self.release = '23.1.0'
9+
self.base_url = 'https://cycle.js.org/'
10+
self.root_path = 'index.html'
11+
self.initial_paths = %w(
12+
getting-started.html
13+
model-view-intent.html
14+
streams.html
15+
drivers.html
16+
components.html
17+
basic-examples.html
18+
dialogue.html
19+
releases.html
20+
api/index.html
21+
api/run.html
22+
api/rxjs-run.html
23+
api/most-run.html
24+
api/dom.html
25+
api/html.html
26+
api/http.html
27+
api/history.html
28+
api/isolate.html
29+
api/state.html
30+
)
31+
32+
self.links = {
33+
home: 'https://cycle.js.org/',
34+
code: 'https://github.com/cyclejs/cyclejs'
35+
}
36+
37+
html_filters.push 'cyclejs/clean_html', 'cyclejs/entries'
38+
39+
options[:only_patterns] = [
40+
/\Aindex\.html\z/,
41+
/\Agetting-started\.html\z/,
42+
/\Amodel-view-intent\.html\z/,
43+
/\Astreams\.html\z/,
44+
/\Adrivers\.html\z/,
45+
/\Acomponents\.html\z/,
46+
/\Abasic-examples\.html\z/,
47+
/\Adialogue\.html\z/,
48+
/\Areleases\.html\z/,
49+
/\Aapi\//
50+
]
51+
52+
options[:download_images] = false
53+
options[:attribution] = <<-HTML
54+
&copy; 2014&ndash;present Cycle.js contributors.<br>
55+
Licensed under the MIT License.
56+
HTML
57+
58+
def get_latest_version(opts)
59+
get_npm_version('@cycle/dom', opts)
60+
end
61+
62+
private
63+
64+
def parse(response)
65+
document = Parser.new(response.body).html
66+
markdown = document.at_css('script#markdown')
67+
68+
return super unless markdown
69+
70+
html = markdown_renderer.render(markdown.content.strip)
71+
title = document.at_css('title').try(:content).try(:strip)
72+
[Parser.new("<html><head><title>#{title}</title></head><body>#{html}</body></html>").html, title]
73+
end
74+
75+
def markdown_renderer
76+
@markdown_renderer ||= Redcarpet::Markdown.new(
77+
Redcarpet::Render::HTML.new(with_toc_data: true),
78+
autolink: true,
79+
fenced_code_blocks: true,
80+
no_intra_emphasis: true,
81+
tables: true
82+
)
83+
end
84+
end
85+
end

public/icons/docs/cyclejs/16.png

638 Bytes
Loading
1.34 KB
Loading

public/icons/docs/cyclejs/SOURCE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://cycle.js.org/img/cyclejs_logo.svg

0 commit comments

Comments
 (0)