Skip to content
Browse files

add script for http://closure.org/

  • Loading branch information...
1 parent 0cfbca2 commit e71226e6ae731478f80c5e2e834f08e985ac7ab8 @youpy youpy committed Apr 4, 2012
Showing with 49 additions and 0 deletions.
  1. +49 −0 misc/closure.rb
View
49 misc/closure.rb
@@ -0,0 +1,49 @@
+%w/uri json nokogiri curb cgi/.each {|g| require g }
+
+root = 'http://clojure.org/'
+book = {
+ :title => 'Clojure',
+ :authors => [
+ 'Rich Hickey'
+ ],
+ :cover_image => 'http://clojure.org/space/showimage/clojure-icon.gif',
+ :content_xpath => '//div[@id="content_view"]',
+ :exclude_xpath => '//div[@id="toc"]',
+ :chapters => []
+}
+
+def curl(url)
+ c = Curl::Easy.new(url.to_s)
+ c.follow_location = true
+ c.perform
+ c.body_str
+end
+
+def sections(url)
+ sections = []
+ doc = Nokogiri::HTML(curl(url))
+ doc.xpath('//div[@id="toc"]//a').each do |a|
+ if a[:href] =~ /^#/
+ sections << {
+ :uri => url.to_s + a[:href],
+ :title => a.text
+ }
+ end
+ end
+
+ sections
+end
+
+doc = Nokogiri::HTML(curl(root))
+doc.xpath('//div[@class="WikiCustomNav WikiElement wiki"]//a').each do |a|
+ next if a[:href] =~ /^http/
+ chapter_url = URI(root) + a[:href]
+ chapter = {
+ :uri => chapter_url,
+ :title => a.text,
+ #:sections => sections(chapter_url)
+ }
+ book[:chapters] << chapter
+end
+
+puts book.to_json

0 comments on commit e71226e

Please sign in to comment.
Something went wrong with that request. Please try again.