Skip to content

Commit

Permalink
improves ruby linting of readme
Browse files Browse the repository at this point in the history
* removes dependency on nokogiri
* fixes 4xx errors not affecting linting results
  • Loading branch information
arempe93 committed Aug 1, 2016
1 parent a4d2495 commit 922fa51
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
1 change: 0 additions & 1 deletion Gemfile
@@ -1,6 +1,5 @@
source 'https://rubygems.org'

gem 'nokogiri'
gem 'parallel'
gem 'kramdown'
gem 'httparty'
Expand Down
6 changes: 1 addition & 5 deletions Gemfile.lock
Expand Up @@ -6,10 +6,7 @@ GEM
multi_xml (>= 0.5.2)
json (1.8.3)
kramdown (1.8.0)
mini_portile (0.6.2)
multi_xml (0.5.5)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
parallel (1.6.1)

PLATFORMS
Expand All @@ -18,8 +15,7 @@ PLATFORMS
DEPENDENCIES
httparty
kramdown
nokogiri
parallel

BUNDLED WITH
1.10.6
1.11.2
24 changes: 13 additions & 11 deletions dedup.rb
@@ -1,30 +1,32 @@
require 'parallel'
require 'nokogiri'
require 'rexml/document'
require 'open-uri'
require 'kramdown'

BASE_URI = ENV['BASE_URI'] || 'https://github.com/jondot/awesome-react-native'

doc = Nokogiri::HTML(Kramdown::Document.new(open('README.md').read).to_html)
links = doc.css('a').to_a
puts "Deduping #{links.count} links..."
html_readme = "<html>#{Kramdown::Document.new(open('README.md').read).to_html}</html>"
readme_doctree = REXML::Document.new(html_readme)
links = REXML::XPath.match(readme_doctree, '//a')

puts "Deduping #{links.size} links..."

map = {}
dups = []

links.each do |link|
uri = URI.join(BASE_URI, link.attr('href'))
if map[uri]
dups << link
end
map[uri] = link
href = link.attribute('href').to_s
uri = URI.join(BASE_URI, href)
if map[uri]
dups << href
end
map[uri] = href
end

unless dups.empty?
puts "\nDuplicate links:"
dups.each do |link|
puts "- #{link}"
puts `grep -nr '#{link.attr('href')}' README.md`
puts `grep -nr '#{link}' README.md`
end
puts "\nDone with errors."
exit(1)
Expand Down
36 changes: 22 additions & 14 deletions validate.rb
@@ -1,29 +1,37 @@
require 'parallel'
require 'nokogiri'
require 'open-uri'
require 'httparty'
require 'net/http'
require 'rexml/document'
require 'kramdown'
require 'httparty'

def check_link(uri)
code = HTTParty.head(uri, :follow_redirects => false).code
return code >= 200 && code < 400
HTTParty.head(uri).code.to_i.tap do |status|
raise "Request had status #{status}" if (400..422).include?(status)
end
end

BASE_URI = ENV['BASE_URI'] || 'https://github.com/jondot/awesome-react-native'

doc = Nokogiri::HTML(Kramdown::Document.new(open('README.md').read).to_html)
links = doc.css('a').to_a
puts "Validating #{links.count} links..."
html_readme = "<html>#{Kramdown::Document.new(open('README.md').read).to_html}</html>"
readme_doctree = REXML::Document.new(html_readme)
links = REXML::XPath.match(readme_doctree, '//a')

puts "Validating #{links.size} links..."

invalids = []
Parallel.each(links, :in_threads => 4) do |link|
Parallel.each(links, in_threads: 4) do |link|
href = link.attribute('href').to_s
begin
uri = URI.join(BASE_URI, link.attr('href'))
check_link(uri)
putc('.')
rescue
case check_link(URI.join(BASE_URI, href))
when (200...300)
putc('.')
when (300..302)
putc('w')
end
rescue => e
putc('F')
invalids << "#{link} (reason: #{$!})"
invalids << "#{href} (reason: #{e.message})"
end
end

Expand All @@ -36,4 +44,4 @@ def check_link(uri)
exit(1)
end

puts "\nDone."
# puts "\nDone."

0 comments on commit 922fa51

Please sign in to comment.