Class: Documatic::OpenDocumentText::Partial
- Inherits:
-
Template
- Object
- Template
- Documatic::OpenDocumentText::Partial
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
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
#content ⇒ Object
Returns the value of attribute content.
6
7
8
|
# File 'lib/documatic/open_document_text/partial.rb', line 6
def content
@content
end
|
#filename ⇒ Object
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
|
.cache ⇒ Object
14
15
16
|
# File 'lib/documatic/open_document_text/partial.rb', line 14
def cache
@cache ||= Hash.new
end
|
.cache_by_prefix ⇒ Object
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_cache ⇒ Object
26
27
28
|
# File 'lib/documatic/open_document_text/partial.rb', line 26
def flush_cache
@cache = Hash.new
end
|
Instance Method Details
#compile ⇒ Object
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
doc.root.each_element('office:automatic-styles/*') do |e|
attr = e.attributes.get_attribute('style:name')
attr && style_names[attr.value] = attr
end
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
self.jar.find_entry('documatic/partial') ||
self.jar.mkdir('documatic/partial')
self.jar.get_output_stream('documatic/partial/partial.txt') do |f|
f.write self.prefix
end
@styles = doc.root.elements['office:automatic-styles']
self.jar.get_output_stream('documatic/partial/styles.xml') do |f|
f.write @styles.to_s
end
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_erb ⇒ Object
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
|
#prefix ⇒ Object
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
|
#save ⇒ Object
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
|
#styles ⇒ Object
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
|