require 'uri'
url = "http://www.aoky.net/articles/why_poignant_guide_to_ruby/"
#url = "http://poignant.guide/book/"
uri = URI.parse(url)
url_host = uri.scheme + "://" + uri.host + '/articles/why_poignant_guide_to_ruby/'
html_0 = `curl -s #{url}index.html`
css = `curl -s #{url}guide.css`
require 'oga'
#require 'nokogiri'
doc = Oga.parse_html(html_0)
#doc = Nokogiri::HTML.parse(html_0)
atag = doc.css('a[href]')
url_list = []
link_list = []
atag.each_with_index do |tag,index|
if index < 39 && index != 2
link_list << "#{tag['href']}"
url_list << "#{url_host}#{tag['href']}"
end
end
url_list.map! do |x| x.sub(/\#.+/,'') end
url_list.uniq!
link_list.map! do |x| x.sub(/\#.+/,'') end
link_list.uniq!
html_list = []
system('mkdir manual')
system('mkdir manual/images')
fh0 = File.open('manual/guide.css',"w")
fh0.write css
fh0.close
fh0 = File.open('manual/index.html',"w")
fh0.write html_0
fh0.close
url_list.each_with_index do |x,index|
html = `curl -s #{x}`
html_list << "#{html}"
doc2 = Nokogiri::HTML.parse(html)
imgtag = doc2.css('img')
imgtag.each {|img|
image = img.attr('src')
imagefile = `curl -s "#{url_host}#{image}"`
filename = File.basename(image)
fh = File.open('manual/images/'+filename,"w")
fh.write imagefile
fh.close
}
end
url_list.each_with_index {|x,index|
xx = File.basename(x)
File.open('manual/' + xx,'w') do |fh|
fh.write html_list[index]
fh.close
end
}
url_list << url+"index.html"
url_list.each_with_index {|x,index|
xx = File.basename(x)
strings = ""
File.open("manual/#{xx}", mode = "rt"){ |rf|
strings = rf.read
rf.close
}
strings.gsub!(/<script\b[^>]*>([\s\S]*?)<\/script>/,'')
strings.gsub!(/images\//,'')
File.open("manual/#{xx}", mode = "w+"){ |wf|
wf.write strings
wf.close
}
}
system("zip" , "-rjD", "manga_ruby_manual.zip","./manual/")
system("rm" ,"-r", "./manual")