Skip to content
This repository
tree: 603b710096
Fetching contributors…

Cannot retrieve contributors at this time

executable file 90 lines (84 sloc) 2.319 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#!/usr/bin/env ruby
require 'rubygems'

require 'optparse'
require 'open-uri'
require 'tmpdir'
require 'launchy'
require 'digest/md5'
require 'Html2Feedbooks'

include HTML2FB

options = {}
options[:conf] = "conf.yaml"
options[:preview] = true
options[:conv] = true
OptionParser.new do |opts|
opts.banner = "Usage: html2fb [options] URL"

opts.on("-c", "--conf FILE", String,"Configuration file") do |f|
options[:conf] = f
end
opts.on("-s", "-s","Send to feedbooks") do |f|
options[:preview] = !f
end
opts.on("-nc", "--no-conv","No charset conversion") do |f|
options[:conv] = !f
end
opts.on("-C", "--cache", String,"Configuration file") do |f|
options[:cache] = !f
end
end.parse!
valid=false
entry=ARGV[0]
basedir=Dir.tmpdir+'/'
while !valid
url=nil
begin
url=Downloader.valid_url?(entry)
valid=true
rescue Exception => e
STDERR.puts 'Invalid URL' unless entry.nil? || entry==''
valid=false
puts e
end
print "URL : " if entry.nil? || entry==''
entry=STDIN.readline.strip unless valid
end
conf=Conf.new(options[:conf],options[:conv])
abridged_conf=conf.to_h.reject{|k,v| k=='fb'}
content=Downloader.download(url)
cache={}
ok=false
if options[:cache] && File.exists?(basedir+'.cache')
cache=Marshal.restore(File.open(basedir+'.cache','r'))
ok=Digest::MD5.hexdigest(content)==Digest::MD5.hexdigest(cache[:content])
abridged_conf.each do |k,v|
# puts (abridged_conf[k]==cache[:conf][k]).inspect
# puts (abridged_conf[k]).inspect
# puts (cache[:conf][k]).inspect
# puts "-_-_-_-_"
ok&&=abridged_conf[k]==cache[:conf][k]
end
end
#puts content.size
if options[:cache] && ok
puts "Using cache file"
doc=cache[:doc]
else
doc=Parser.new(conf).parse(content)
end

File.open(basedir+'.cache','w') do |e|
Marshal.dump({:url => url,:conf => abridged_conf, :content => content, :doc => doc},e)
end
puts "Writing cache File "

puts doc.toc.to_yaml
if options[:preview]
page=File.join(Dir.tmpdir(),Digest::MD5.hexdigest(url.to_s))+'.html'
f=File.open(page,'w')
f.write doc.to_html
f.close
puts "A preview of the parsed file should be opening in your webbrowser now"
puts "If nothing open you can open the file located at : #{page}"
puts "When happy with the parsed output rerun with -s option to send to Feedbooks.com"
Launchy.open(page)
else
doc.to_feedbooks(conf)
end
Something went wrong with that request. Please try again.