sandrods-odf-report 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/odf-report.rb +53 -16
- data/odf-report.gemspec +1 -1
- data/test/test.odt +0 -0
- data/test/test.rb +22 -19
- metadata +3 -2
data/lib/odf-report.rb
CHANGED
@@ -3,10 +3,10 @@ require 'zip/zipfilesystem'
|
|
3
3
|
require 'fileutils'
|
4
4
|
|
5
5
|
class ODFReport
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(template_name, &block)
|
8
8
|
@template = template_name
|
9
|
-
@data={:values=>{}, :tables=>{}}
|
9
|
+
@data={:values=>{}, :tables=>{}, :images => {} }
|
10
10
|
|
11
11
|
@tmp_dir = Dir.tmpdir + "/" + random_filename(:prefix=>'odt_')
|
12
12
|
Dir.mkdir(@tmp_dir) unless File.exists? @tmp_dir
|
@@ -27,11 +27,15 @@ class ODFReport
|
|
27
27
|
yield(row, item)
|
28
28
|
@data[:tables][table_tag] << row
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_image(name, path)
|
34
|
+
@data[:images][name] = path
|
31
35
|
end
|
32
36
|
|
33
37
|
def generate(dest = nil)
|
34
|
-
|
38
|
+
|
35
39
|
if dest
|
36
40
|
|
37
41
|
FileUtils.cp(@template, dest)
|
@@ -45,14 +49,21 @@ class ODFReport
|
|
45
49
|
end
|
46
50
|
|
47
51
|
%w(content.xml styles.xml).each do |content_file|
|
48
|
-
|
49
|
-
update_file_from_zip(new_file, content_file) do |txt|
|
50
52
|
|
51
|
-
|
52
|
-
replace_tables!(txt)
|
53
|
+
update_file_from_zip(new_file, content_file) do |txt|
|
53
54
|
|
55
|
+
replace_fields!(txt)
|
56
|
+
replace_tables!(txt)
|
57
|
+
replace_image_refs!(txt)
|
54
58
|
end
|
55
|
-
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
unless @data[:images].empty?
|
63
|
+
image_dir_name = "Pictures"
|
64
|
+
dir = File.join("#{@tmp_dir}", image_dir_name)
|
65
|
+
add_image_files_to_dir(dir)
|
66
|
+
add_dir_to_zip(new_file, dir, image_dir_name)
|
56
67
|
end
|
57
68
|
|
58
69
|
new_file
|
@@ -61,13 +72,26 @@ class ODFReport
|
|
61
72
|
|
62
73
|
private
|
63
74
|
|
75
|
+
def add_image_files_to_dir(dir)
|
76
|
+
FileUtils.mkdir(dir)
|
77
|
+
@data[:images].each_pair do |name, path|
|
78
|
+
FileUtils.cp(path, File.join(dir, File.basename(path)))
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def add_dir_to_zip(zip_file, dir, entry)
|
83
|
+
Zip::ZipFile.open(zip_file, true) do |z|
|
84
|
+
Dir["#{dir}/**/*"].each { |f| z.add("#{entry}/#{File.basename(f)}", f) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
64
88
|
def update_file_from_zip(zip_file, content_file, &block)
|
65
89
|
|
66
|
-
Zip::ZipFile.open(zip_file) do |z|
|
90
|
+
Zip::ZipFile.open(zip_file) do |z|
|
67
91
|
cont = "#{@tmp_dir}/#{content_file}"
|
68
|
-
|
92
|
+
|
69
93
|
z.extract(content_file, cont)
|
70
|
-
|
94
|
+
|
71
95
|
txt = ''
|
72
96
|
|
73
97
|
File.open(cont, "r") do |f|
|
@@ -90,6 +114,19 @@ private
|
|
90
114
|
hash_gsub!(content, @data[:values])
|
91
115
|
end
|
92
116
|
|
117
|
+
def replace_image_refs!(content)
|
118
|
+
@data[:images].each_pair do |image_name, path|
|
119
|
+
#Set the new image path
|
120
|
+
new_path = File.join("Pictures", File.basename(path))
|
121
|
+
#Search for the image
|
122
|
+
image_rgx = Regexp.new("draw:name=\"#{image_name}\".*?><draw:image.*?xlink:href=\"([^\s]*)\" .*?/></draw:frame>")
|
123
|
+
content_match = content.match(image_rgx)
|
124
|
+
if content_match
|
125
|
+
replace_path = content_match[1]
|
126
|
+
content.gsub!(content_match[0], content_match[0].gsub(replace_path, new_path))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
93
130
|
|
94
131
|
def replace_tables!(content)
|
95
132
|
|
@@ -114,8 +151,8 @@ private
|
|
114
151
|
|
115
152
|
unless row_match.empty?
|
116
153
|
|
117
|
-
# If there more than one line in the table, takes the second entry (row_match[1])
|
118
|
-
# since the first one represents the column header.
|
154
|
+
# If there more than one line in the table, takes the second entry (row_match[1])
|
155
|
+
# since the first one represents the column header.
|
119
156
|
# If there just one line, takes the first line. Besides, since the entry is an Array itself,
|
120
157
|
# takes the entry's first element ( entry[0] )
|
121
158
|
model_row = (row_match[1] || row_match[0])[0]
|
@@ -136,7 +173,7 @@ private
|
|
136
173
|
hash_gsub!(tmp_row, _values)
|
137
174
|
|
138
175
|
new_rows << tmp_row
|
139
|
-
end
|
176
|
+
end
|
140
177
|
|
141
178
|
# replace back the lines into the table
|
142
179
|
table.gsub!("[ROW_#{table_name}]", new_rows)
|
@@ -178,4 +215,4 @@ private
|
|
178
215
|
s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
|
179
216
|
end
|
180
217
|
|
181
|
-
end
|
218
|
+
end
|
data/odf-report.gemspec
CHANGED
data/test/test.odt
CHANGED
Binary file
|
data/test/test.rb
CHANGED
@@ -1,36 +1,39 @@
|
|
1
1
|
require '../lib/odf-report'
|
2
2
|
|
3
3
|
col1 = []
|
4
|
-
col1 << {:
|
5
|
-
col1 << {:
|
6
|
-
col1 << {:
|
4
|
+
col1 << {:name=>"name 01", :id=>"01", :address=>"this is address 01"}
|
5
|
+
col1 << {:name=>"name 03", :id=>"03", :address=>"this is address 03"}
|
6
|
+
col1 << {:name=>"name 02", :id=>"02", :address=>"this is address 02"}
|
7
|
+
col1 << {:name=>"name 04", :id=>"04", :address=>"this is address 04"}
|
7
8
|
|
8
9
|
col2 = []
|
9
|
-
col2 << {:
|
10
|
-
col2 << {:
|
11
|
-
col2 << {:
|
10
|
+
col2 << {:name=>"josh harnet", :id=>"02", :address=>"testing <&> ", :phone=>99025668, :zip=>"90420-002"}
|
11
|
+
col2 << {:name=>"sandro", :id=>"45", :address=>"address with &", :phone=>88774451, :zip=>"90490-002"}
|
12
|
+
col2 << {:name=>"ellen bicca", :id=>"77", :address=>"<address with escaped html>", :phone=>77025668, :zip=>"94420-002"}
|
12
13
|
|
13
14
|
report = ODFReport.new("test.odt") do |r|
|
14
15
|
|
15
|
-
r.add_field("
|
16
|
+
r.add_field("HEADER_FIELD", "This &field was in the HEADER")
|
16
17
|
|
17
18
|
r.add_field("TAG_01", "New tag")
|
18
19
|
r.add_field("TAG_02", "TAG-2 -> New tag")
|
19
20
|
|
20
|
-
r.add_table("
|
21
|
-
row["
|
22
|
-
row["
|
23
|
-
row["
|
21
|
+
r.add_table("TABLE_01", col1) do |row, item|
|
22
|
+
row["FIELD_01"] = item[:id]
|
23
|
+
row["FIELD_02"] = item[:name]
|
24
|
+
row["FIELD_03"] = item[:address]
|
24
25
|
end
|
25
26
|
|
26
|
-
r.add_table("
|
27
|
-
row["
|
28
|
-
row["
|
29
|
-
row["
|
30
|
-
row["
|
31
|
-
row["
|
27
|
+
r.add_table("TABLE_02", col2) do |row, item|
|
28
|
+
row["FIELD_04"] = item[:id]
|
29
|
+
row["FIELD_05"] = item[:name]
|
30
|
+
row["FIELD_06"] = item[:address]
|
31
|
+
row["FIELD_07"] = item[:phone]
|
32
|
+
row["FIELD_08"] = item[:zip]
|
32
33
|
end
|
33
|
-
|
34
|
+
|
35
|
+
r.add_image("graphics1", File.join(Dir.pwd, 'piriapolis.jpg'))
|
36
|
+
|
34
37
|
end
|
35
38
|
|
36
|
-
report.generate("result.odt")
|
39
|
+
report.generate("result.odt")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sandrods-odf-report
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sandro Duarte
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- Manifest
|
45
45
|
has_rdoc: true
|
46
46
|
homepage: ""
|
47
|
+
licenses:
|
47
48
|
post_install_message:
|
48
49
|
rdoc_options:
|
49
50
|
- --line-numbers
|
@@ -69,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
70
|
requirements: []
|
70
71
|
|
71
72
|
rubyforge_project: odf-report
|
72
|
-
rubygems_version: 1.
|
73
|
+
rubygems_version: 1.3.5
|
73
74
|
signing_key:
|
74
75
|
specification_version: 2
|
75
76
|
summary: Generates ODF files, given a template (.odt) and data, replacing tags
|