Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

finished refactoring to modules

  • Loading branch information...
commit 7c628eaddaef6a518467af22609dccf17084264d 1 parent 395361e
@invisiblellama authored
View
1  .gitignore
@@ -1,3 +1,4 @@
pkg
tmp
.eprj
+*.epub
View
2  bin/repub
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#!/usr/bin/env ruby -w
require File.expand_path(
File.join(File.dirname(__FILE__), %w[.. lib repub]))
View
103 lib/repub.rb
@@ -1,47 +1,56 @@
-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__)
+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
+
+#
+# TODO
+#
+class Object
+ def if_blank(value)
+ self.nil? || self.empty? ? value : self
+ end
+end
+
+$:.unshift Repub.libpath
+#Repub.require_all_libs_relative_to(__FILE__)
View
7 lib/repub/app.rb
@@ -25,9 +25,11 @@ def initialize(args)
def run
#p options
puts "Source:\t\t#{options[:url]}"
- puts "Output path:\t#{options[:output_path]}"
- write(parse(fetch))
+ res = write(parse(fetch))
+
+ puts "Output path:\t#{res.output_path}"
+ puts "Output file:\t#{res.output_file}"
# Repub::Fetcher.get(options) do |cache|
# Repub::Parser.parse(cache, options) do |parser|
@@ -49,6 +51,7 @@ def parse_options(args)
:url => '',
:css => '',
:output_path => Dir.getwd,
+ :output_file => '',
:helper => 'wget',
:metadata => {},
:verbosity => 0,
View
300 lib/repub/app/writer.rb
@@ -1,152 +1,148 @@
-require 'rubygems'
-require 'builder'
-require 'fileutils'
-require 'repub/epub'
-
-module Repub
- class App
- module Writer
-
- class WriterException < RuntimeError; end
-
- def write(parser)
- Helper.new(options).write(parser)
- end
-
- class Helper
- include Epub
-
- def initialize(options)
- @options = options
- end
-
- def write(parser)
- @parser = parser
-
- # Initialize content.opf
- @content = Content.new(@parser.uid)
- # Default title is the parsed one
- @content.metadata.title = @parser.title
- if @options[:metadata]
- # Override metadata values specified in options
- @content.metadata.members.each do |m|
- m = m.to_sym
- next if m == :identifier # do not allow to override uid
- @content.metadata[m] = @options[:metadata][m] if @options[:metadata][m]
- end
- end
-
- # Initialize toc.ncx
- @toc = Toc.new(@parser.uid)
- # TOC title is the same as in content
- @toc.title = @content.metadata.title
-
- # Output filename is the same as title
- # TODO : add an option
- @output_filename = @content.metadata.title + '.epub'
-
- # Prepend output path if it was given in options
- if @options[:output_path]
- output_path = File.join(File.expand_path(@options[:output_path]), @output_filename)
- else
- output_path = @output_filename
- end
-
- # Write EPUB
- FileUtils.mkdir_p(output_path)
- FileUtils.chdir(output_path) do
- write_meta_inf
- write_mime_type
- write_assets
- write_content
- write_toc
- make_epub
- end
-
- # FileUtils.rm_r(output_path)
- @output_filename
- end
-
- private
-
- MetaInf = 'META-INF'
-
- def write_meta_inf
- FileUtils.mkdir_p(MetaInf)
- FileUtils.chdir(MetaInf) do
- Epub::Container.new.save
- end
- end
-
- def write_mime_type
- File.open('mimetype', 'w') do |f|
- f << 'application/epub+zip'
- end
- end
-
- # TODO : refactor this mess
- def write_assets
- # copy html
- @parser.cache.assets[:documents].each do |a|
- if @options[:css].empty?
- # copy file verbatim
- FileUtils.cp(File.join(@parser.cache.path, a), '.')
- else
- # custom css - fix css references
- doc = Hpricot(open(File.join(@parser.cache.path, a)))
- doc.search('//link[@rel="stylesheet"]') do |link|
- link[:href] = File.basename(@options[:css])
- end
- File.open(a, 'w') do |f|
- f << doc.to_html
- end
- end
- @content.add_document(a)
- end
- # copy css
- if @options[:css].empty?
- # if no custom css, copy from assets
- @parser.cache.assets[:stylesheets].each do |a|
- FileUtils.cp(File.join(@parser.cache.path, a), '.')
- @content.add_stylesheet(a)
- end
- else
- # otherwise copy custom css instead
- FileUtils.cp(@options[:css], '.')
- @content.add_stylesheet(File.basename(@options[:css]))
- end
- # copy images
- @parser.cache.assets[:images].each do |a|
- FileUtils.cp(File.join(@parser.cache.path, a), '.')
- @content.add_image(a)
- end
- end
-
- def write_content
- @content.save
- end
-
- def write_toc
- add_nav_points(@toc.nav_map, @parser.toc)
- @toc.save
- end
-
- def add_nav_points(nav_collection, toc)
- toc.each do |t|
- nav_point = nav_collection.add_nav_point(t.title, t.src)
- add_nav_points(nav_point, t.subitems) if t.subitems
- end
- end
-
- def make_epub
- # TODO : use rubyzip lib
- system("zip -X9 '#{@output_filename}' mimetype >/dev/null")
- system("zip -Xr9D '#{@output_filename}' * -xi mimetype >/dev/null")
- #
- FileUtils.mv(@output_filename, '..')
- end
- end
-
- end
- end
-end
+#require 'rubygems'
+#require 'builder'
+require 'fileutils'
+require 'tmpdir'
+require 'repub/epub'
+
+module Repub
+ class App
+ module Writer
+
+ class WriterException < RuntimeError; end
+
+ def write(parser)
+ Helper.new(options).write(parser)
+ end
+
+ class Helper
+ include Epub
+
+ attr_reader :output_path
+ attr_reader :output_file
+
+ def initialize(options)
+ @options = options
+ end
+
+ def write(parser)
+ @parser = parser
+
+ # Initialize content.opf
+ @content = Content.new(@parser.uid)
+ # Default title is the parsed one
+ @content.metadata.title = @parser.title
+ if @options[:metadata]
+ # Override metadata values specified in options
+ @content.metadata.members.each do |m|
+ m = m.to_sym
+ next if m == :identifier # do not allow to override uid
+ @content.metadata[m] = @options[:metadata][m] if @options[:metadata][m]
+ end
+ end
+
+ # Initialize toc.ncx
+ @toc = Toc.new(@parser.uid)
+ # TOC title is the same as in content
+ @toc.title = @content.metadata.title
+
+ # Setup output filename and path
+ @output_path = File.expand_path(@options[:output_path].if_blank('.'))
+ @output_file = (@options[:output_file].if_blank(@content.metadata.title)) + '.epub'
+
+ # Write EPUB
+ Dir.mktmpdir(App::name) do |tmp|
+ FileUtils.chdir(tmp) do
+ write_meta_inf
+ write_mime_type
+ write_assets
+ write_content
+ write_toc
+ make_epub
+ end
+ end
+ self
+ end
+
+ private
+
+ MetaInf = 'META-INF'
+
+ def write_meta_inf
+ FileUtils.mkdir_p(MetaInf)
+ FileUtils.chdir(MetaInf) do
+ Epub::Container.new.save
+ end
+ end
+
+ def write_mime_type
+ File.open('mimetype', 'w') do |f|
+ f << 'application/epub+zip'
+ end
+ end
+
+ # TODO : refactor this mess
+ def write_assets
+ # copy html
+ @parser.cache.assets[:documents].each do |a|
+ if @options[:css].empty?
+ # copy file verbatim
+ FileUtils.cp(File.join(@parser.cache.path, a), '.')
+ else
+ # custom css - fix css references
+ doc = Hpricot(open(File.join(@parser.cache.path, a)))
+ doc.search('//link[@rel="stylesheet"]') do |link|
+ link[:href] = File.basename(@options[:css])
+ end
+ File.open(a, 'w') do |f|
+ f << doc.to_html
+ end
+ end
+ @content.add_document(a)
+ end
+ # copy css
+ if @options[:css].empty?
+ # if no custom css, copy from assets
+ @parser.cache.assets[:stylesheets].each do |a|
+ FileUtils.cp(File.join(@parser.cache.path, a), '.')
+ @content.add_stylesheet(a)
+ end
+ else
+ # otherwise copy custom css instead
+ FileUtils.cp(@options[:css], '.')
+ @content.add_stylesheet(File.basename(@options[:css]))
+ end
+ # copy images
+ @parser.cache.assets[:images].each do |a|
+ FileUtils.cp(File.join(@parser.cache.path, a), '.')
+ @content.add_image(a)
+ end
+ end
+
+ def write_content
+ @content.save
+ end
+
+ def write_toc
+ add_nav_points(@toc.nav_map, @parser.toc)
+ @toc.save
+ end
+
+ def add_nav_points(nav_collection, toc)
+ toc.each do |t|
+ nav_point = nav_collection.add_nav_point(t.title, t.src)
+ add_nav_points(nav_point, t.subitems) if t.subitems
+ end
+ end
+
+ def make_epub
+ # TODO : use rubyzip lib
+ system("zip -X9 '#{@output_file}' mimetype >/dev/null")
+ system("zip -Xr9D '#{@output_file}' * -xi mimetype >/dev/null")
+ #
+ FileUtils.mv(@output_file, @output_path)
+ end
+ end
+
+ end
+ end
+end
View
5 test/epub/test_container.rb
@@ -1,14 +1,15 @@
require 'test/unit'
require 'rubygems'
require 'hpricot'
-require 'repub'
+require 'repub/epub'
class TestContainer < Test::Unit::TestCase
def test_container_create
c = Repub::Epub::Container.new
s = c.to_xml
doc = Hpricot(s)
- puts s
+ #puts s
+
assert_not_nil(doc.search('rootfile'))
end
end
View
23 test/epub/test_content.rb
@@ -1,12 +1,13 @@
require 'test/unit'
require 'rubygems'
require 'hpricot'
-require 'repub'
+require 'repub/epub'
class TestContent < Test::Unit::TestCase
def test_manifest_create
x = Repub::Epub::Content.new('some-name')
s = x.to_xml
+ #puts s
doc = Hpricot(s)
# manifest was created
@@ -24,20 +25,18 @@ def test_manifest_create
def test_manifest
x = Repub::Epub::Content.new('some-name')
x.add_page_template
- x.add_css 'style.css'
- x.add_css 'more-style.css'
- x.add_img ' logo.jpg '
- x.add_img ' image.png'
- x.add_img 'picture.jpeg '
- x.add_html 'intro.html', 'intro'
- x.add_html 'chapter-1.html'
- x.add_html 'glossary.html', 'glossary'
-
+ x.add_stylesheet 'style.css'
+ x.add_stylesheet 'more-style.css'
+ x.add_image ' logo.jpg '
+ x.add_image ' image.png'
+ x.add_image 'picture.jpeg '
+ x.add_document 'intro.html', 'intro'
+ x.add_document 'chapter-1.html'
+ x.add_document 'glossary.html', 'glossary'
s = x.to_xml
+ #puts s
doc = Hpricot(s)
- puts s
-
# manifest was created
assert_not_nil(doc.search('manifest'))
# has 2 stylesheets
View
10 test/epub/test_toc.rb
@@ -1,14 +1,15 @@
require 'test/unit'
require 'rubygems'
require 'hpricot'
-require 'repub'
+require 'repub/epub'
class TestToc < Test::Unit::TestCase
def test_toc_create
x = Repub::Epub::Toc.new('some-name')
s = x.to_xml
#puts s
- #doc = Hpricot(s)
+ doc = Hpricot(s)
+ # TODO
end
def test_toc
@@ -21,7 +22,8 @@ def test_toc
p11 = p1.add_nav_point('Chapter 1-1', 'chapter-1-1.html')
p12 = p1.add_nav_point('Chapter 1-2', 'chapter-1-2.html')
s = x.to_xml
- puts s
- #doc = Hpricot(s)
+ #puts s
+ doc = Hpricot(s)
+ # TODO
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.