Class: Documatic::OpenDocumentText::Template

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/documatic/open_document_text/template.rb

Direct Known Subclasses

Partial

Constant Summary collapse

STYLE_NAME =

RE_STYLES match positions

1
STYLE_TYPE =
2
TYPE =

RE_ERB match positions

5
ERB_CODE =
6
ROW_START =
1
ROW_END =
11
ITEM_START =
2
ITEM_END =
10
PARA_START =
3
PARA_END =
9
SPAN_END =
4
SPAN_START =
8
TABLE_ROW =

Match types:

1
PARAGRAPH =
2
INLINE_CODE =
3
VALUE =
4
DTC =

Abbrevs

Documatic::OpenDocumentText::Component

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Template

class << self



98
99
100
101
102
# File 'lib/documatic/open_document_text/template.rb', line 98

def initialize(filename)
  @filename = filename
  @jar = Zip::ZipFile.open(@filename)
  return true
end

Instance Attribute Details

#contentObject

The template’s content component. This is an instance of Documatic::Component, instantiated from the compiled (embedded Ruby) version of ‘content.xml’.



14
15
16
# File 'lib/documatic/open_document_text/template.rb', line 14

def content
  @content
end

#content_erbObject

Compiled text, to be written to ‘content.erb’



24
25
26
# File 'lib/documatic/open_document_text/template.rb', line 24

def content_erb
  @content_erb
end

#content_rawObject

The raw contents of ‘content.xml’.



22
23
24
# File 'lib/documatic/open_document_text/template.rb', line 22

def content_raw
  @content_raw
end

#jarObject

The template’s JAR file (i.e. an instance of Zip::ZipFile)



20
21
22
# File 'lib/documatic/open_document_text/template.rb', line 20

def jar
  @jar
end

#stylesObject

The template’s styles component. This is an instance of Documatic::Component, instantiated from the compiled (embedded Ruby) version of ‘styles.xml’.



18
19
20
# File 'lib/documatic/open_document_text/template.rb', line 18

def styles
  @styles
end

#styles_erbObject

Compiled text, to be written to ‘styles.erb’



28
29
30
# File 'lib/documatic/open_document_text/template.rb', line 28

def styles_erb
  @styles_erb
end

#styles_rawObject

The raw contents of ‘styles.xml’



26
27
28
# File 'lib/documatic/open_document_text/template.rb', line 26

def styles_raw
  @styles_raw
end

Class Method Details

.process_template(args, &block) ⇒ Object

Process a template and save it to an output file.

The argument is a hash with the keys :options and :data. :options should contain an object that responds to #template_file and #output_file. #template_file is the path and filename to the OpenDocument template to be used; #output_file is where the processed results will be stored. The #template_file must exist, and the #output_file path must either exist or the current process must be able to create it.

An optional block can be provided to this method. The block will be passed the template currently being processed (i.e. an instance of Documatic::OpenDocumentText::Template). The block can peform manipulation of the template directly by e.g. accessing the template’s JAR or the content or styles components. The template will be saved after the block exits.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/documatic/open_document_text/template.rb', line 76

def process_template(args, &block)
  if args[:options] && args[:options].template_file &&
      args[:options].output_file
    output_dir = File.dirname(args[:options].output_file)
    File.directory?(output_dir) || FileUtils.mkdir_p(output_dir)
    FileUtils.cp(args[:options].template_file, args[:options].output_file)
    template = self.new(args[:options].output_file)
    template.process :data => args[:data], :options => args[:options],
    :template => template, :master => template
    template.save
    if block
      block.call(template)
      template.save
    end
    template.close
  else
    raise ArgumentError, 'Need to specify both :template_file and :output_file in options'
  end
end

Instance Method Details

#add_image(full_path) ⇒ Object

Add an image to the current template. The argument is the path and filename of the image to be added to the template. This can be an absolute path or a relative path to the current directory. Returns the basename of file if it exists; otherwise an ArgumentError exception is raised.



181
182
183
184
185
186
187
188
189
# File 'lib/documatic/open_document_text/template.rb', line 181

def add_image(full_path)
  if File.exists?(full_path)
    image = File.basename(full_path)
    self.images[image] = full_path
    return image
  else
    raise ArgumentError, 'Attempted to add non-existent image to template'
  end
end

#add_partial(full_path, partial) ⇒ Object



195
196
197
# File 'lib/documatic/open_document_text/template.rb', line 195

def add_partial(full_path, partial)
  self.partials[full_path] = partial
end

#closeObject



142
143
144
145
146
# File 'lib/documatic/open_document_text/template.rb', line 142

def close
  self.jar.remove('documatic/master/styles.erb')
  self.jar.remove('documatic/master/content.erb')
  self.jar.close
end

#compileObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/documatic/open_document_text/template.rb', line 148

def compile
  # Read the raw files
  @content_raw = pretty_xml('content.xml')
  @styles_raw = pretty_xml('styles.xml')
  
  @content_erb = self.erbify(@content_raw)
  @styles_erb = self.erbify(@styles_raw)

  # Create 'documatic/master/' in zip file
  self.jar.find_entry('documatic/master') || self.jar.mkdir('documatic/master')
  
  self.jar.get_output_stream('documatic/master/content.erb') do |f|
    f.write @content_erb
  end
  self.jar.get_output_stream('documatic/master/styles.erb') do |f|
    f.write @styles_erb
  end
end

#imagesObject

Returns a hash of images added during the processing of the current template. (This method is not intended to be called directly by application developers: it is used indirectly by the #image helper to add images from within an OpenDocument template.)



172
173
174
# File 'lib/documatic/open_document_text/template.rb', line 172

def images
  @images ||= Hash.new
end

#partialsObject



191
192
193
# File 'lib/documatic/open_document_text/template.rb', line 191

def partials
  @partials ||= Hash.new
end

#process(local_assigns = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/documatic/open_document_text/template.rb', line 104

def process(local_assigns = {})
  # Compile this template, if not compiled already.
  self.jar.find_entry('documatic/master') || self.compile
  # Process the styles (incl. headers and footers).
  # This is conditional because partials don't need styles.erb.
  @styles = DTC.new(self.jar.read('documatic/master/styles.erb') )
  @styles.process(local_assigns)
  # Process the main (body) content.
  @content = DTC.new( self.jar.read('documatic/master/content.erb') )
  @content.process(local_assigns)
  # Merge styles from any partials into the main template
  if self.partials.keys.length > 0
    @content.merge_partial_styles(self.partials.values)
  end
  # Copy any images into this jar
  if images.length > 0
    self.jar.find_entry('Pictures') || self.jar.mkdir('Pictures')
    images.keys.each do |filename|
      path = images.delete(filename)
      self.jar.add("Pictures/#{filename}", path)
    end
  end
end

#saveObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/documatic/open_document_text/template.rb', line 128

def save
  # Gather all the styles from the partials, add them to the master's styles.
  # Put the body into the document.
  self.jar.get_output_stream('content.xml') do |f|
    f.write self.content.to_s
  end

  if self.styles
    self.jar.get_output_stream('styles.xml') do |f|
      f.write self.styles.to_s
    end
  end
end