Class: Documatic::OpenDocumentText::Partial

Inherits:
Template
  • Object
show all
Defined in:
lib/documatic/open_document_text/partial.rb

Constant Summary

Constants inherited from Template

Template::DTC, Template::ERB_CODE, Template::INLINE_CODE, Template::ITEM_END, Template::ITEM_START, Template::PARAGRAPH, Template::PARA_END, Template::PARA_START, Template::ROW_END, Template::ROW_START, Template::SPAN_END, Template::SPAN_START, Template::STYLE_NAME, Template::STYLE_TYPE, Template::TABLE_ROW, Template::TYPE, Template::VALUE

Instance Attribute Summary collapse

Attributes inherited from Template

#content_raw, #jar, #styles_erb, #styles_raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Template

#add_image, #add_partial, #close, #images, #partials, process_template

Constructor Details

#initialize(filename, prefix_name = nil) ⇒ Partial

class << self



32
33
34
35
# File 'lib/documatic/open_document_text/partial.rb', line 32

def initialize(filename, prefix_name = nil)
  super filename
  @prefix = prefix_name || self.prefix
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/documatic/open_document_text/partial.rb', line 6

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/documatic/open_document_text/partial.rb', line 7

def filename
  @filename
end

Class Method Details

.add_partial(name, partial) ⇒ Object



10
11
12
# File 'lib/documatic/open_document_text/partial.rb', line 10

def add_partial(name, partial)
  self.cache[name] = partial
end

.cacheObject



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

def cache
  @cache ||= Hash.new
end

.cache_by_prefixObject



18
19
20
21
22
23
24
# File 'lib/documatic/open_document_text/partial.rb', line 18

def cache_by_prefix
  by_prefix = Hash.new
  self.cache.each_value do |partial|
    by_prefix[partial.prefix] = partial
  end
  by_prefix
end

.flush_cacheObject



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

def flush_cache
  @cache = Hash.new
end

Instance Method Details

#compileObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/documatic/open_document_text/partial.rb', line 43

def compile
  doc = REXML::Document.new( self.jar.read('content.xml') )
  style_names = Hash.new

  # Gather all auto style names from <office:automatic-styles>
  doc.root.each_element('office:automatic-styles/*') do |e|
    attr = e.attributes.get_attribute('style:name')
    attr && style_names[attr.value] = attr
  end

  # Replace all auto styles in the document's attributes with the
  # prefixed form.
  doc.each_element('//*') do |e|
    e.attributes.each_attribute do |attr|
      if style_names.has_key? attr.value
        e.add_attribute(attr.expanded_name, "#{self.prefix}_#{attr.value}")
      end
    end
  end

  # Create 'documatic/partial/' in zip file
  self.jar.find_entry('documatic/partial') ||
    self.jar.mkdir('documatic/partial')
  
  # Save the prefix in documatic/partial.txt
  self.jar.get_output_stream('documatic/partial/partial.txt') do |f|
    f.write self.prefix
  end

  # Set @styles, & save it in documatic/styles.xml
  @styles = doc.root.elements['office:automatic-styles']
  self.jar.get_output_stream('documatic/partial/styles.xml') do |f|
    f.write @styles.to_s
  end

  # Get body text, erbify it, keep it in @content and save it in
  # documatic/content.erb
  body_text = doc.root.elements['office:body/office:text']
  body_text.elements.delete('text:sequence-decls')
  body_text.elements.delete('office:forms')
  @content_erb = self.erbify( (body_text.elements.to_a.collect do |e|
                                 e.to_s ;
                               end ).join("\n") )
  self.jar.get_output_stream('documatic/partial/content.erb') do |f|
    f.write @content_erb
  end

end

#content_erbObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/documatic/open_document_text/partial.rb', line 108

def content_erb
  if not @content_erb
    if self.jar.find_entry('documatic/partial/content.erb')
      self.jar.read('documatic/partial/content.erb')
    else
      self.compile
    end
  end
  @content_erb
end

#prefixObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/documatic/open_document_text/partial.rb', line 96

def prefix
  if not @prefix
    if self.jar.find_entry('documatic/partial/partial.txt')
      @prefix = self.jar.read('documatic/partial/partial.txt')
    else
      @prefix = File.basename(self.filename, '.odt').gsub(/[^A-Za-z0-9_]/,
                                                          '_')
    end
  end
  return @prefix
end

#process(local_assigns = {}) ⇒ Object



37
38
39
40
41
# File 'lib/documatic/open_document_text/partial.rb', line 37

def process(local_assigns = {})
  self.jar.find_entry('documatic/partial') || self.compile
  @content = Documatic::OpenDocumentText::Component.new( self.content_erb )
  @content.process(local_assigns)
end

#saveObject

Partials aren’t saved in the same way that templates are: this is a no-op.



94
# File 'lib/documatic/open_document_text/partial.rb', line 94

def save ; end

#stylesObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/documatic/open_document_text/partial.rb', line 119

def styles
  if not @styles
    if self.jar.find_entry('documatic/partial/styles.xml')
      @styles = REXML::Document.new( self.jar.read('documatic/partial/styles.xml') ).root
    else
      self.compile
    end
  end
  @styles
end