Class: Swordfish::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/swordfish/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

On initialization, set the nodes list to an empty array



28
29
30
31
# File 'lib/swordfish/document.rb', line 28

def initialize
  @nodes = []
  @images = {}
end

Instance Attribute Details

#imagesObject

Stored image assets



25
26
27
# File 'lib/swordfish/document.rb', line 25

def images
  @images
end

#nodesObject (readonly)

An array of all top-level elements in the document



24
25
26
# File 'lib/swordfish/document.rb', line 24

def nodes
  @nodes
end

Instance Method Details

#append(node) ⇒ Object

Pass in a node and append it to the nodes array



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

def append(node)
  if Swordfish::Node.constants.include? node.class.to_s.split('::').last.to_sym
    @nodes << node
  else
    raise ArgumentError, "Object is not a node"
  end
end

#get_image(name) ⇒ Object

Retrieve an image by name



43
44
45
# File 'lib/swordfish/document.rb', line 43

def get_image(name)
  @images[name]
end

#save_image(image, dest) ⇒ Object

Save an image to a specified directory



48
49
50
51
52
# File 'lib/swordfish/document.rb', line 48

def save_image(image, dest)
  @images[image].open
  File.open(dest, 'w') { |f| f.write(@images[image].read) }
  @images[image].close
end

#settings(opts = {}) ⇒ Object

Perform various destructive operations that may result in improved output



64
65
66
67
68
69
70
# File 'lib/swordfish/document.rb', line 64

def settings(opts = {})
  find_headers! if opts[:guess_headers]
  find_footnotes! if opts[:footnotes]
  clean_line_breaks! if opts[:smart_br]
  @generate_full_document = !!opts[:full_document]
  self
end

#to_htmlObject



72
73
74
75
76
77
78
79
80
# File 'lib/swordfish/document.rb', line 72

def to_html
  if @generate_full_document
    prefix = "<!DOCTYPE html><html><head><title></title></head><body>"
    suffix = "</body></html>"
    prefix + @nodes.map(&:to_html).join + suffix
  else
    @nodes.map(&:to_html).join
  end
end

#update_image_path(original_name, new_path) ⇒ Object

Change the value that an image should report its source to be



55
56
57
58
59
60
61
# File 'lib/swordfish/document.rb', line 55

def update_image_path(original_name, new_path)
  find_nodes_by_type(Swordfish::Node::Image).each do |image_node|
    if image_node.original_name == original_name
      image_node.path = new_path
    end
  end
end