Class: Bookbinder::DocumentProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/bookbinder/document_proxy.rb

Constant Summary collapse

XMLNS =
{
  'xhtml' => 'http://www.w3.org/1999/xhtml',
  'dc' => 'http://purl.org/dc/elements/1.1/',
  'dcterms' => 'http://purl.org/dc/terms/',
  'mathml' => 'http://www.w3.org/1998/Math/MathML',
  'svg' => 'http://www.w3.org/2000/svg',
  'ocf' => 'urn:oasis:names:tc:opendocument:xmlns:container',
  'opf' => 'http://www.idpf.org/2007/opf',
  'ncx' => 'http://www.daisy.org/z3986/2005/ncx/',
  'epub' => 'http://www.idpf.org/2007/ops'
}
XML_PREFIX =
{
  'ibooks' => 'http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/',
  'rendition' => 'http://www.idpf.org/vocab/rendition/#'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mthd, *args, &blk) ⇒ Object (protected)



167
168
169
# File 'lib/bookbinder/document_proxy.rb', line 167

def method_missing(mthd, *args, &blk)
  @doc.send(mthd, *args, &blk)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



21
22
23
# File 'lib/bookbinder/document_proxy.rb', line 21

def doc
  @doc
end

Instance Method Details

#add_namespace(namespace_label, default = false) ⇒ Object



90
91
92
# File 'lib/bookbinder/document_proxy.rb', line 90

def add_namespace(namespace_label, default = false)
  add_node_namespace(@doc.root, namespace_label, default)
end

#add_node_namespace(node, namespace_label, default = false) ⇒ Object



100
101
102
103
# File 'lib/bookbinder/document_proxy.rb', line 100

def add_node_namespace(node, namespace_label, default = false)
  xmlns = default ? nil : namespace_label
  node.add_namespace_definition(xmlns, XMLNS[namespace_label])
end

#add_node_prefix(node, prefix_label, prefix_attribute = 'prefix') ⇒ Object



106
107
108
109
110
# File 'lib/bookbinder/document_proxy.rb', line 106

def add_node_prefix(node, prefix_label, prefix_attribute = 'prefix')
  prefix = "#{prefix_label}: #{XML_PREFIX[prefix_label]}"
  prefixes = [node[prefix_attribute], prefix].compact
  node[prefix_attribute] = prefixes.join("\n")
end

#add_prefix(prefix_label, prefix_attribute = 'prefix') ⇒ Object



95
96
97
# File 'lib/bookbinder/document_proxy.rb', line 95

def add_prefix(prefix_label, prefix_attribute = 'prefix')
  add_node_prefix(@doc.root, prefix_label, prefix_attribute)
end

#build(&blk) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/bookbinder/document_proxy.rb', line 34

def build(&blk)
  builder = Nokogiri::XML::Builder.new { |x|
    @doc = x.doc
    yield(self, x)
  }
  self
end

#each(query, &blk) ⇒ Object

Iterates over the results of the search for the given CSS query.



70
71
72
# File 'lib/bookbinder/document_proxy.rb', line 70

def each(query, &blk)
  search(query).each(&blk)
end

#each_within(node, query, &blk) ⇒ Object



85
86
87
# File 'lib/bookbinder/document_proxy.rb', line 85

def each_within(node, query, &blk)
  search_within(node, query).each(&blk)
end

#find(query) ⇒ Object

Given a CSS query, returns the first result, or nil.



56
57
58
# File 'lib/bookbinder/document_proxy.rb', line 56

def find(query)
  @doc.at_css(query, node_namespaces(@doc.root))
end

#find_within(node, query) ⇒ Object



75
76
77
# File 'lib/bookbinder/document_proxy.rb', line 75

def find_within(node, query)
  node.at_css(query, node_namespaces(node))
end

#load(string) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/bookbinder/document_proxy.rb', line 24

def load(string)
  begin
    @doc = Nokogiri::XML(string)
  rescue
    @doc = Nokogiri::HTML(string)
  end
  self
end

#new_node(tag, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/bookbinder/document_proxy.rb', line 43

def new_node(tag, options = {})
  Nokogiri::XML::Node.new(tag, doc).tap { |node|
    yield(node)  if block_given?
    if parent = options[:append]
      parent = find(parent)  if parent.kind_of?(String)
      parent.add_child(node)
    end
  }
end

#search(query) ⇒ Object

Given a CSS query, returns all results, or an empty array.



63
64
65
# File 'lib/bookbinder/document_proxy.rb', line 63

def search(query)
  @doc.css(query, node_namespaces(@doc.root))
end

#search_within(node, query) ⇒ Object



80
81
82
# File 'lib/bookbinder/document_proxy.rb', line 80

def search_within(node, query)
  node.css(query, node_namespaces(node))
end

#to_strObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bookbinder/document_proxy.rb', line 113

def to_str
  if @doc.kind_of?(Nokogiri::HTML::Document)
    # Remove the old-style charset meta tag that Nokogiri auto-inserts.
    # This is nasty business, but apparently once again Nokogiri is
    # wrong and Markus Gylling knows best:
    # http://code.google.com/p/epubcheck/issues/detail?id=135#c3
    html = @doc.to_xhtml
    html.sub!(
      '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
      ''
    )
    html
  elsif @doc.kind_of?(Nokogiri::XML::Document)
    @doc.to_xml
  end
end