Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a cache file to improve speed

  • Loading branch information...
commit f3c917a4f98ae313de14a6afa2c4135c014d75d0 1 parent bee5a52
zeta authored
Showing with 32 additions and 2 deletions.
  1. +1 −1  Html2Feedbooks.gemspec
  2. +27 −1 bin/html2fb.rb
  3. +4 −0 lib/conf.rb
View
2  Html2Feedbooks.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{Html2Feedbooks}
- s.version = '1.0.10'
+ s.version = '1.1.0'
s.date = %q{2009-04-28}
s.authors = ["Benoit Larroque"]
s.email = "zeta dot ben at gmail dot com"
View
28 bin/html2fb.rb
@@ -8,6 +8,7 @@
require 'feedbooks.rb'
require 'tmpdir'
require 'launchy'
+require 'digest/md5'
include HTML2FB
@@ -27,10 +28,13 @@
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
@@ -45,9 +49,31 @@
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 doc.toc.to_yaml
if options[:preview]
page=File.join(Dir.tmpdir(),Digest::MD5.hexdigest(url.to_s))+'.html'
View
4 lib/conf.rb
@@ -21,5 +21,9 @@ def initialize(file,conv)
def [](x)
@conf[x]
end
+
+ def to_h
+ @conf
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.