Permalink
Browse files

Removed css indent functionality and fixed link resolving on anchors

without paths (fixes #1)
  • Loading branch information...
1 parent f80611a commit 88c20b5391b133ddaa93e12d9d3505c03fcfedeb @orangemug orangemug committed Jan 18, 2012
Showing with 73 additions and 64 deletions.
  1. +41 −4 lib/epub/file.rb
  2. +0 −2 lib/epub/item.rb
  3. +3 −12 lib/epub/item/css.rb
  4. +15 −44 lib/epub/item/html.rb
  5. +12 −0 lib/epub/manifest.rb
  6. +1 −1 lib/epub/metadata.rb
  7. +1 −1 lib/epub/zip_file.rb
View
@@ -11,15 +11,21 @@ class File
def initialize(path)
@path = path
- if type == :zip
+ case type
+ when :zip
@file = ZipFile.new(path)
- else
+ when :filesystem
@file = FileSystem.new(path)
+ when :nofile
+ raise "File not valid"
+ @file = ZipFile.new(path)
+ build_skeleton
end
@opf_xml = @file.read_xml(opf_path)
end
+
def self.extract(filepath, extract_path=nil)
if block_given?
Dir.mktmpdir do |outdir|
@@ -34,6 +40,35 @@ def self.extract(filepath, extract_path=nil)
end
end
+
+
+ def build_skeleton
+ @file.mkdir "META-INF"
+ @file.mkdir "OEBPS"
+
+ container_xml = <<END
+<?xml version="1.0"?>
+<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
+ <rootfiles>
+ <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
+ </rootfiles>
+</container>
+END
+
+ content_opf = <<END
+<?xml version="1.0" encoding="UTF-8"?>
+<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookID" version="2.0">
+ <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
+ </metadata>
+ <manifest></manifest>
+ <spine toc="ncx"></spine>
+</package>
+END
+
+ @file.write("META-INF/container.xml", container_xml)
+ @file.write("OEBPS/content.opf", content_opf)
+ end
+
# Flattens the directory structure, for example this:
#
# /
@@ -200,10 +235,12 @@ def to_s
private
def type
- if ::File.file?(@path)
+ if ::File.directory?(@path)
+ return :filesystem
+ elsif ::File.file?(@path)
return :zip
else
- return :filesystem
+ return :nofile
end
end
View
@@ -4,8 +4,6 @@ module Epub
class Item
attr_reader :type
- STYLESHEET_PREFIX = "epub"
-
# Initialize with a manifest id
#
# Item.new(epub, {
View
@@ -19,15 +19,6 @@ def normalize!
sass = css_to_sass(data)
- # Indent all lines with two spaces
- sass = sass.gsub(/\n/, "\n ")
-
- # Get stylesheet name
- stylesheet_name = "#{STYLESHEET_PREFIX}-#{hash}"
-
- # Add wrapper
- sass = ".#{stylesheet_name}\n " + sass
-
# Resolve the images in the sass to there new location
new_sass = ""
@@ -82,9 +73,6 @@ def normalize!
sass = new_sass
- # Replace body css rule and add it to the above rule using the sass '&'
- sass = sass.gsub(" body\n", " &\n")
-
sass = convert_fonts(sass)
# Parse SASS
@@ -116,6 +104,9 @@ def css_to_sass(css_data)
# Write the css
file.write css_data
+ # Rewind back to the start
+ file.rewind
+
# Use the std sass command line to convert a CSS file to SASS
command = "sass-convert #{file.path}"
sass = `#{command}`
View
@@ -80,46 +80,6 @@ def remove_inline_stylesheets(html)
end
- def add_base_stylesheet(html)
- # TODO: Add the base css here instead
- @epub.manifest.css.each do |css|
- # Add <link rel="stylesheet" href="css/book.css" type="text/css">
- css_link = Nokogiri::XML::Node.new "link", html
- css_link['rel'] = "stylesheet"
- css_link['href'] = css.normalized_hashed_path(:relative_to => self)
- css_link['type'] = "text/css"
-
- html.css("head").children.last.add_next_sibling(css_link)
- end
- end
-
- # Indent by stylesheet and remove all linked stylesheets
- # TODO: This should also extract inline <style></style>
- def indent_by_stylesheet_class(html)
- stylesheets = html.xpath(STYLESHEET_XPATH)
- if stylesheets.size
-
- css_classes = []
- css_str = stylesheets.each do |l|
- href = l[:href]
- href = Pathname.new(href).cleanpath.to_s
-
- # Get the epub item
- item = get_item(href)
-
- raise "Missing item" if !item
-
- css_classes << "#{STYLESHEET_PREFIX}-#{item.hash}"
- end
-
- css_class_str = css_classes.join(" ")
-
- body = html.search('body').first
- body_class = body.attributes['class']
- body['class'] = "#{body_class} #{css_class_str}"
- end
- end
-
def remove_scripts(html)
html.css('script').each do |node|
node.remove
@@ -134,6 +94,8 @@ def change_hrefs(html)
'href'
when 'img'
'src'
+ when 'link'
+ 'href'
end
orig_href = node.attributes[attr_name].to_s
@@ -146,16 +108,25 @@ def change_hrefs(html)
end
if internal_link?(src.to_s)
- # Change internal links
- item = get_item(src.to_s)
- if item
- new_path = item.normalized_hashed_path(:relative_to => self)
+ linked_item = nil
+
+ if src.path == ""
+ # If its just an anchor like '#this' just set to the current file
+ linked_item = self
+ else
+ linked_item = self
+ end
+
+ # Change link
+ if linked_item
+ new_path = linked_item.normalized_hashed_path(:relative_to => self)
new_path = URI(new_path)
if src.fragment
new_path.fragment = src.fragment
end
+ log "Changing #{src.to_s} to #{new_path.to_s}"
node[attr_name] = new_path.to_s
else
log "No item in manifest for #{src}"
View
@@ -114,6 +114,18 @@ def [](key)
end
+ # TODO
+ # def add(id, content)
+ # item = Nokogiri::XML::Node.new "item", @xmldoc.first
+ # item['id'] = id
+ # item['href'] = "bla.html"
+ # item['media-type'] = "text/css"
+ #
+ # @xmldoc.first.add_child(item)
+ # puts @xmldoc
+ # end
+
+
def path_from_id(key)
xpath = OPF_ITEM_XPATH % key
nodes = @xmldoc.xpath(xpath)
View
@@ -68,7 +68,7 @@ def []=(k,v)
# Node doesn't exist create it
node = Nokogiri::XML::Node.new "dc:title", doc
node.content = v
- doc.css("*").last.add_previous_sibling(node)
+ doc.add_child(node)
end
@epub.save_opf!(doc, OPF_XPATH)
View
@@ -207,7 +207,7 @@ def extract(filepath, extract_filepath)
#
# @yield [Zip::ZipFile] zip file
def zip_open
- Zip::ZipFile.open(@filepath) do |zip|
+ Zip::ZipFile.open(@filepath, true) do |zip|
yield(zip)
end
end

0 comments on commit 88c20b5

Please sign in to comment.