Class: Documatic::OpenDocumentSpreadsheet::Template

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

Constant Summary collapse

STYLE_NAME =

RE_STYLES match positions

1
STYLE_TYPE =
2
ROW_START =

RE_ERB match positions

1
TYPE =
2
ERB_CODE =
3
ROW_END =
4
DSC =

Abbrevs

Documatic::OpenDocumentSpreadsheet::Component

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Template

class << self



57
58
59
60
61
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 57

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

Instance Attribute Details

#contentObject

Returns the value of attribute content.



12
13
14
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 12

def content
  @content
end

#content_erbObject

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



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

def content_erb
  @content_erb
end

#content_rawObject

The raw contents of ‘content.xml’.



16
17
18
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 16

def content_raw
  @content_raw
end

#jarObject

Returns the value of attribute jar.



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

def jar
  @jar
end

#stylesObject

Returns the value of attribute styles.



13
14
15
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 13

def styles
  @styles
end

Class Method Details

.process_template(args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 35

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.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

#closeObject



79
80
81
82
83
84
85
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 79

def close
  # To get rid of an annoying message about corrupted files in OOCalc 3.2.0
  # we most remove the compiled content before we close our ODS file.
  self.jar.remove('documatic/master/content.erb')
  # Now we can safely close our document.
  self.jar.close
end

#compileObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 87

def compile
  # Read the raw files
  @content_raw = regularise_styles( self.jar.read('content.xml') )
  @content_erb = self.erbify(@content_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
end

#process(local_assigns = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 63

def process(local_assigns = {})
  # Compile this template, if not compiled already.
  self.jar.find_entry('documatic/master') || self.compile
  # Process the main (body) content.
  @content = DSC.new( self.jar.read('documatic/master/content.erb') )
  @content.process(local_assigns)
end

#saveObject



71
72
73
74
75
76
77
# File 'lib/documatic/open_document_spreadsheet/template.rb', line 71

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
end