Permalink
Browse files

app: refactored to use mixins

  • Loading branch information...
1 parent 1123c2c commit 395361e34307078e7703687f4c0fb1fda65aa2b8 @invisiblellama committed Jun 15, 2009
Showing with 422 additions and 367 deletions.
  1. +11 −9 bin/repub
  2. +47 −46 lib/repub.rb
  3. +8 −3 lib/repub/app.rb
  4. +44 −42 lib/repub/app/fetcher.rb
  5. +108 −102 lib/repub/app/parser.rb
  6. +152 −129 lib/repub/app/writer.rb
  7. +3 −10 lib/repub/epub.rb
  8. +17 −5 test/test_fetcher.rb
  9. +32 −21 test/test_parser.rb
View
@@ -1,9 +1,11 @@
-#!/usr/bin/env ruby
-
-require File.expand_path(
- File.join(File.dirname(__FILE__), %w[.. lib repub]))
-
-#args = ['--version', '-s', 'http://www.berzinarchives.com/web/x/prn/p.html_1614431902.html']
-#args = ARGV << 'http://www.berzinarchives.com/web/x/prn/p.html_1614431902.html'
-
-Repub::App.run ARGV
+#!/usr/bin/env ruby
+
+require File.expand_path(
+ File.join(File.dirname(__FILE__), %w[.. lib repub]))
+
+require 'repub/app'
+
+#args = ['--version', '-s', 'http://www.berzinarchives.com/web/x/prn/p.html_1614431902.html']
+args = ARGV << 'http://www.berzinarchives.com/web/x/prn/p.html_1614431902.html'
+
+Repub::App.run args #ARGV
View
@@ -1,46 +1,47 @@
-module Repub
-
- # :stopdoc:
- VERSION = '0.0.2'
- LIBPATH = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
- PATH = File.dirname(LIBPATH) + File::SEPARATOR
- # :startdoc:
-
- # Returns the version string for the library.
- #
- def self.version
- VERSION
- end
-
- # Returns the library path for the module. If any arguments are given,
- # they will be joined to the end of the libray path using
- # <tt>File.join</tt>.
- #
- def self.libpath( *args )
- args.empty? ? LIBPATH : File.join(LIBPATH, args.flatten)
- end
-
- # Returns the lpath for the module. If any arguments are given,
- # they will be joined to the end of the path using
- # <tt>File.join</tt>.
- #
- def self.path( *args )
- args.empty? ? PATH : File.join(PATH, args.flatten)
- end
-
- # Utility method used to require all files ending in .rb that lie in the
- # directory below this file that has the same name as the filename passed
- # in. Optionally, a specific _directory_ name can be passed in such that
- # the _filename_ does not have to be equivalent to the directory.
- #
- def self.require_all_libs_relative_to( fname, dir = nil )
- dir ||= File.basename(fname, '.*')
- search_me = File.expand_path(
- File.join(File.dirname(fname), dir, '**', '*.rb'))
-
- Dir.glob(search_me).sort.each {|rb| require rb}
- end
-
-end
-
-Repub.require_all_libs_relative_to(__FILE__)
+module Repub
+
+ # :stopdoc:
+ VERSION = '0.0.2'
+ LIBPATH = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
+ PATH = File.dirname(LIBPATH) + File::SEPARATOR
+ # :startdoc:
+
+ # Returns the version string for the library.
+ #
+ def self.version
+ VERSION
+ end
+
+ # Returns the library path for the module. If any arguments are given,
+ # they will be joined to the end of the libray path using
+ # <tt>File.join</tt>.
+ #
+ def self.libpath( *args )
+ args.empty? ? LIBPATH : File.join(LIBPATH, args.flatten)
+ end
+
+ # Returns the lpath for the module. If any arguments are given,
+ # they will be joined to the end of the path using
+ # <tt>File.join</tt>.
+ #
+ def self.path( *args )
+ args.empty? ? PATH : File.join(PATH, args.flatten)
+ end
+
+ # Utility method used to require all files ending in .rb that lie in the
+ # directory below this file that has the same name as the filename passed
+ # in. Optionally, a specific _directory_ name can be passed in such that
+ # the _filename_ does not have to be equivalent to the directory.
+ #
+ def self.require_all_libs_relative_to( fname, dir = nil )
+ dir ||= File.basename(fname, '.*')
+ search_me = File.expand_path(
+ File.join(File.dirname(fname), dir, '**', '*.rb'))
+
+ Dir.glob(search_me).each {|rb| p rb; require rb}
+ end
+
+end
+
+$:.unshift Repub.libpath
+#Repub.require_all_libs_relative_to(__FILE__)
View
@@ -1,16 +1,20 @@
require 'optparse'
+require 'repub/app/fetcher'
+require 'repub/app/parser'
+require 'repub/app/writer'
module Repub
class App
# Mix-in actual functionality
- include Ttt, Fetcher #, Parser, Generator
+ include Fetcher, Parser, Writer
def self.name
File.basename($0)
end
def self.run(args)
+ #Fetcher.append_features(self)
self.new(args).run
end
@@ -23,7 +27,8 @@ def run
puts "Source:\t\t#{options[:url]}"
puts "Output path:\t#{options[:output_path]}"
- generate(parse(fetch))
+ write(parse(fetch))
+
# Repub::Fetcher.get(options) do |cache|
# Repub::Parser.parse(cache, options) do |parser|
# res = Repub::Writer.write(parser, cache, options)
@@ -47,7 +52,7 @@ def parse_options(args)
:helper => 'wget',
:metadata => {},
:verbosity => 0,
- :selector => Parser::Selectors
+ :selectors => Parser::Selectors
}
get_selector_values = lambda do
View
@@ -9,7 +9,7 @@ module Fetcher
class FetcherException < RuntimeError; end
def fetch
- FetchHelper.new(options[:helper]).fetch(options[:url])
+ Helper.new(options[:helper]).fetch(options[:url])
end
AssetTypes = {
@@ -18,6 +18,49 @@ def fetch
:images => %w[jpg jpeg png gif svg]
}
+ class Helper
+ Downloaders = {
+ :wget => { :cmd => 'wget', :options => '-nv -E -H -k -p -nH -nd' },
+ :httrack => { :cmd => 'httrack', :options => '-gB -r2 +*.css +*.jpg -*.xml -*.html' }
+ }
+
+ def initialize(name)
+ @downloader_path, @downloader_options = ENV['REPUB_HELPER'], ENV['REPUB_downloader_options']
+ begin
+ downloader = Downloaders[name.to_sym] rescue Downloaders[:wget]
+ @downloader_path ||= which(downloader[:cmd])
+ @downloader_options ||= downloader[:options]
+ rescue RuntimeError
+ raise FetcherException, "unknown helper '#{name}'"
+ end
+ end
+
+ def fetch(url)
+ raise FetcherException, "empty URL" if url.nil? || url.empty?
+ begin
+ URI.parse(url)
+ rescue
+ raise FetcherException, "invalid URL: #{url}"
+ end
+ cmd = "#{@downloader_path} #{@downloader_options} #{url}"
+ Cache.for_url(url) do |cache|
+ unless system(cmd) && !cache.empty?
+ raise FetcherException, "Fetch failed."
+ end
+ end
+ end
+
+ private
+
+ def which(cmd)
+ if !RUBY_PLATFORM.match('mswin')
+ cmd = `/usr/bin/which #{cmd}`.strip
+ raise FetcherException, "#{cmd}: helper not found." if cmd.empty?
+ end
+ cmd
+ end
+ end
+
class Cache
CACHE_ROOT = File.join(File.expand_path('~'), %w[.repub cache])
@@ -84,47 +127,6 @@ def initialize(url)
end
end
- class Helper
- HelperCommand = {
- :wget => { :cmd => 'wget', :options => '-nv -E -H -k -p -nH -nd' },
- :httrack => { :cmd => 'httrack', :options => '-gB -r2 +*.css +*.jpg -*.xml -*.html' }
- }
-
- def initialize(name)
- @helper_path, @helper_options = ENV['REPUB_HELPER'], ENV['REPUB_HELPER_OPTIONS']
- begin
- helper = HelperCommand[name.to_sym] rescue HelperCommand[:wget]
- @helper_path ||= which(helper[:cmd])
- @helper_options ||= helper[:options]
- rescue
- raise FetcherException, "unknown helper '#{name}'"
- end
- end
-
- def fetch(url)
- raise FetcherException, "empty URL" if url.nil? || url.empty?
- begin
- URI.parse(url)
- rescue
- raise FetcherException, "invalid URL: #{url}"
- end
- cmd = "#{@helper_path} #{@helper_options} #{url}"
- Cache.for_url(url) do |cache|
- unless system(cmd) && !cache.empty?
- raise FetcherException, "Fetch failed."
- end
- end
- end
-
- private
-
- def which
- res = `/usr/bin/which #{helper}`.strip
- raise FetcherException, "#{helper}: helper not found." if res.empty?
- res
- end
- end
-
end
end
end
Oops, something went wrong.

0 comments on commit 395361e

Please sign in to comment.