Skip to content

Commit

Permalink
Merge pull request #15 from darvin/master
Browse files Browse the repository at this point in the history
Little book on Coffeescript recipe
  • Loading branch information
Daniel Choi committed Dec 20, 2012
2 parents 02ab795 + bf9f99e commit ca62a0c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions recipes/little_book_on_coffeescript.rb
@@ -0,0 +1,62 @@
require 'kindlefodder'

class LittleCoffeeScript < Kindlefodder

def get_source_files
@start_url = "http://arcturo.github.com/library/coffeescript/"
@start_doc = Nokogiri::HTML run_shell_command("curl -s #{@start_url}")

sections = extract_articles

File.open("#{output_dir}/sections.yml", 'w') {|f|
f.puts sections.to_yaml
}
end

def document
# download cover image
if !File.size?("cover.gif")
`curl -s 'http://akamaicovers.oreilly.com/images/0636920024309/lrg.jpg' > cover.jpg`
run_shell_command "convert cover.jpg -type Grayscale -resize '400x300>' cover.gif"
end
{
'title' => 'The Little Book on CoffeeScript',
'author' => 'Alex MacCaw',
'cover' => 'cover.gif',
'masthead' => nil
}
end


def extract_articles
@start_doc.search('ol.pages li a').map do |o|
puts o
title = o.inner_text

$stderr.puts "#{title}"

FileUtils::mkdir_p "#{output_dir}/articles"

{
title: title,
path: save_article_and_return_path(o[:href])
}
end
end
def save_article_and_return_path href, filename=nil
path = filename || "articles/" + href.sub(/^\//, '').sub(/\/$/, '').gsub('/', '.')
full_url = @start_url + href.sub(/^\//, '')
puts path, full_url
html = run_shell_command "curl -s #{full_url}"
article_doc = Nokogiri::HTML html
b = article_doc.at(".back")
b.remove if b
res = article_doc.at('#content').inner_html
File.open("#{output_dir}/#{path}", 'w') {|f| f.puts res}
path
end

end

LittleCoffeeScript.generate

0 comments on commit ca62a0c

Please sign in to comment.