cure-odf-report 0.5.1b

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +4 -0
  4. data/Gemfile +4 -0
  5. data/MIT-LICENSE +20 -0
  6. data/Manifest +12 -0
  7. data/README.textile +223 -0
  8. data/Rakefile +9 -0
  9. data/lib/odf-report.rb +16 -0
  10. data/lib/odf-report/actions/remove_section.rb +22 -0
  11. data/lib/odf-report/field.rb +88 -0
  12. data/lib/odf-report/file.rb +50 -0
  13. data/lib/odf-report/images.rb +44 -0
  14. data/lib/odf-report/nested.rb +62 -0
  15. data/lib/odf-report/parser/default.rb +91 -0
  16. data/lib/odf-report/report.rb +103 -0
  17. data/lib/odf-report/section.rb +64 -0
  18. data/lib/odf-report/table.rb +88 -0
  19. data/lib/odf-report/text.rb +43 -0
  20. data/lib/odf-report/version.rb +3 -0
  21. data/odf-report.gemspec +31 -0
  22. data/spec/fields_spec.rb +77 -0
  23. data/spec/result/specs.odt +0 -0
  24. data/spec/result/tables.rb +38 -0
  25. data/spec/spec_helper.rb +45 -0
  26. data/spec/specs.odt +0 -0
  27. data/spec/tables_spec.rb +39 -0
  28. data/test/fields_inside_text_test.rb +38 -0
  29. data/test/nested_tables_test.rb +43 -0
  30. data/test/sections_test.rb +44 -0
  31. data/test/sub_sections_test.rb +58 -0
  32. data/test/table_headers_test.rb +41 -0
  33. data/test/tables_test.rb +67 -0
  34. data/test/templates/piriapolis.jpg +0 -0
  35. data/test/templates/rails.png +0 -0
  36. data/test/templates/test_fields_inside_text.odt +0 -0
  37. data/test/templates/test_nested_tables.odt +0 -0
  38. data/test/templates/test_sections.odt +0 -0
  39. data/test/templates/test_sub_sections.odt +0 -0
  40. data/test/templates/test_table_headers.odt +0 -0
  41. data/test/templates/test_tables.odt +0 -0
  42. data/test/templates/test_text.odt +0 -0
  43. data/test/text_test.rb +56 -0
  44. metadata +204 -0
@@ -0,0 +1,43 @@
1
+ module ODFReport
2
+
3
+ class Text < Field
4
+
5
+ attr_accessor :parser
6
+
7
+ def replace!(doc, data_item = nil)
8
+
9
+ return unless node = find_text_node(doc)
10
+
11
+ text_value = get_value(data_item)
12
+
13
+ @parser = Parser::Default.new(text_value, node)
14
+
15
+ @parser.paragraphs.each do |p|
16
+ node.before(p)
17
+ end
18
+
19
+ node.remove
20
+
21
+ end
22
+
23
+ private
24
+
25
+ def find_text_node(doc)
26
+ texts = doc.xpath(".//text:p[text()='#{to_placeholder}']")
27
+ if texts.empty?
28
+ texts = doc.xpath(".//text:p/text:span[text()='#{to_placeholder}']")
29
+ if texts.empty?
30
+ texts = nil
31
+ else
32
+ texts = texts.first.parent
33
+ end
34
+ else
35
+ texts = texts.first
36
+ end
37
+
38
+ texts
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,3 @@
1
+ module ODFReport
2
+ VERSION = "0.5.1b"
3
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "odf-report/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{cure-odf-report}
7
+ s.version = ODFReport::VERSION
8
+
9
+ s.authors = ["Sandro Duarte"]
10
+ s.description = %q{Generates ODF files, given a template (.odt) and data, replacing tags}
11
+ s.email = %q{sandrods@gmail.com}
12
+ s.has_rdoc = false
13
+ s.homepage = %q{http://sandrods.github.com/odf-report/}
14
+ s.rubygems_version = %q{1.3.7}
15
+ s.summary = %q{Generates ODF files, given a template (.odt) and data, replacing tags}
16
+
17
+ s.files = `git ls-files -z`.split("\x0")
18
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "bundler", "~> 1.6"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "rspec", "~> 3.0.0"
25
+ s.add_development_dependency "faker"
26
+ s.add_development_dependency "launchy"
27
+
28
+ s.add_runtime_dependency('rubyzip', "~> 1.1.0")
29
+ s.add_runtime_dependency('nokogiri', ">= 1.5.0")
30
+
31
+ end
@@ -0,0 +1,77 @@
1
+ RSpec.describe "Fields" do
2
+
3
+ before(:context) do
4
+
5
+ @field_01 = Faker::Company.name
6
+ @field_02 = Faker::Name.name
7
+
8
+ @itens_01 = Item.get_list(3)
9
+
10
+ report = ODFReport::Report.new("spec/specs.odt") do |r|
11
+
12
+ r.add_field(:field_01, @field_01)
13
+ r.add_field(:field_02, @field_02)
14
+
15
+ r.add_table('TABLE_01', @itens_01) do |t|
16
+ t.add_column(:column_01, :id)
17
+ t.add_column(:column_02, :name)
18
+ end
19
+
20
+ r.add_section('SECTION_01', @itens_01) do |t|
21
+ t.add_field(:s01_field_01, :id)
22
+ t.add_field(:s01_field_02, :name)
23
+ end
24
+
25
+ end
26
+
27
+ report.generate("spec/result/specs.odt")
28
+
29
+ @data = Inspector.new("spec/result/specs.odt")
30
+
31
+ end
32
+
33
+
34
+ it "simple fields replacement" do
35
+
36
+ expect(@data.text).not_to match(/\[FIELD_01\]/)
37
+ expect(@data.text).not_to match(/\[FIELD_02\]/)
38
+
39
+ expect(@data.text).to match @field_01
40
+ expect(@data.text).to match @field_02
41
+
42
+ end
43
+
44
+ it "table columns replacement" do
45
+
46
+ table = @data.xml.xpath(".//table:table[@table:name='TABLE_01']").to_s
47
+
48
+ @itens_01.each do |item|
49
+
50
+ expect(table).not_to match(/\[COLUMN_01\]/)
51
+ expect(table).not_to match(/\[COLUMN_02\]/)
52
+ expect(table).to match(/\[COLUMN_03\]/)
53
+
54
+ expect(table).to match(item.id)
55
+ expect(table).to match(item.name)
56
+
57
+ end
58
+
59
+ end
60
+
61
+ it "section fields replacement" do
62
+
63
+ expect(@data.text).not_to match(/\[S01_FIELD_01\]/)
64
+ expect(@data.text).not_to match(/\[S01_FIELD_02\]/)
65
+
66
+ @itens_01.each_with_index do |item, idx|
67
+
68
+ section = @data.xml.xpath(".//text:section[@text:name='SECTION_01_#{idx+1}']").to_s
69
+
70
+ expect(section).to match(item.id)
71
+ expect(section).to match(item.name)
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
Binary file
@@ -0,0 +1,38 @@
1
+ RSpec.describe "Tables" do
2
+
3
+ before(:context) do
4
+
5
+ report = ODFReport::Report.new("spec/specs.odt") do |r|
6
+
7
+ r.add_table('TABLE_02', [], skip_if_empty: true) do |t|
8
+ t.add_column(:column_01, :id)
9
+ t.add_column(:column_02, :name)
10
+ end
11
+
12
+ r.add_section('TABLE_03', []) do |t|
13
+ t.add_field(:s01_field_01, :id)
14
+ t.add_field(:s01_field_02, :name)
15
+ end
16
+
17
+ end
18
+
19
+ report.generate("spec/result/specs.odt")
20
+
21
+ @data = Inspector.new("spec/result/specs.odt")
22
+
23
+ end
24
+
25
+ context "with Empty collection" do
26
+
27
+ it "should remove table if required" do
28
+
29
+ table2 = @data.xml.xpath(".//table:table[@table:name='TABLE_02']")
30
+ table3 = @data.xml.xpath(".//table:table[@table:name='TABLE_03']")
31
+
32
+ expect(table2).to be_nil
33
+ expect(table3).not_to be_nil
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,45 @@
1
+ require './lib/odf-report'
2
+ require 'faker'
3
+
4
+ I18n.enforce_available_locales = false
5
+
6
+ class Item
7
+ attr_accessor :id, :name, :subs
8
+ def initialize(_id, _name, _subs=[])
9
+ @name = _name
10
+ @id = _id
11
+ @subs = _subs
12
+ end
13
+
14
+ def self.get_list(quant = 3)
15
+ r = []
16
+ (1..quant).each do |i|
17
+ r << Item.new(Faker::Number.number(10), Faker::Name.name)
18
+ end
19
+ r
20
+ end
21
+
22
+ end
23
+
24
+ class Inspector
25
+
26
+ def initialize(file)
27
+ @content = nil
28
+ Zip::File.open(file) do |f|
29
+ @content = f.get_entry('content.xml').get_input_stream.read
30
+ end
31
+ end
32
+
33
+ def xml
34
+ @xml ||= Nokogiri::XML(@content)
35
+ end
36
+
37
+ def text
38
+ @text ||= xml.to_s
39
+ end
40
+
41
+ end
42
+
43
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
44
+ RSpec.configure do |config|
45
+ end
Binary file
@@ -0,0 +1,39 @@
1
+ RSpec.describe "Tables" do
2
+
3
+ before(:context) do
4
+
5
+ report = ODFReport::Report.new("spec/specs.odt") do |r|
6
+
7
+ r.add_table('TABLE_02', []) do |t|
8
+ t.add_column(:column_01, :id)
9
+ t.add_column(:column_02, :name)
10
+ end
11
+
12
+ r.add_table('TABLE_03', [], skip_if_empty: true) do |t|
13
+ t.add_column(:column_01, :id)
14
+ t.add_column(:column_02, :name)
15
+ end
16
+
17
+ end
18
+
19
+ report.generate("spec/result/specs.odt")
20
+
21
+ @data = Inspector.new("spec/result/specs.odt")
22
+
23
+ end
24
+
25
+ context "with Empty collection" do
26
+
27
+ it "should not remove table" do
28
+ table2 = @data.xml.xpath(".//table:table[@table:name='TABLE_02']")
29
+ expect(table2).not_to be_empty
30
+ end
31
+
32
+ it "should remove table if required" do
33
+ table3 = @data.xml.xpath(".//table:table[@table:name='TABLE_03']")
34
+ expect(table3).to be_empty
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,38 @@
1
+ # coding: UTF-8
2
+ require './lib/odf-report'
3
+ require 'faker'
4
+
5
+
6
+ @html = <<-HTML
7
+ <p>Uniquely promote installed base total linkage via emerging deliverables com <strong>[EVENTO_TEXTO_CARTA]</strong>, unleash cross-media collaboration <strong>[FUNCAO]</strong> [EVENTO_NOME].</p>
8
+
9
+ <p>Progressively fashion diverse portals nº <strong>[NUMERO_SECAO]</strong> do local <strong>[NOME_LOCAL]</strong>, situado na <strong>[ENDERECO_LOCAL]</strong> methodologies </p>
10
+
11
+ <p>Assertively orchestrate market positioning bandwidth rather than fully researched total linkage. Interactively architect granular e-markets via clicks-and-mortar ROI. Uniquely aggregate compelling.</p>
12
+
13
+ <p>Compellingly facilitate functionalized interfaces before wireless models. Compellingly morph parallel systems whereas combinado com o artigo 63, § 2º da Lei nº 9.504/97, abaixo mencionados:</p>
14
+
15
+ <p style="margin-left:1.76cm;"><em>Art. 120 - § 1º Compellingly envisioneer high standards in niches without best-of-breed leadership. Phosfluorescently unleash go forward methodologies after bricks-and-clicks niches. Authoritatively. </em></p>
16
+
17
+ <p style="margin-left:1.76cm;"><em>Art. 63 - [...] § 2º Enthusiastically parallel task user friendly functionalities whereas exceptional leadership. </em></p>
18
+
19
+ <p>Credibly enable multifunctional strategic theme areas and premium communities.</p>
20
+
21
+ HTML
22
+
23
+
24
+
25
+ report = ODFReport::Report.new("test/templates/test_fields_inside_text.odt") do |r|
26
+
27
+ r.add_text(:body, @html)
28
+
29
+ r.add_field('EVENTO_TEXTO_CARTA', Faker::Lorem.sentence)
30
+ r.add_field('FUNCAO', Faker::Lorem.word)
31
+ r.add_field('EVENTO_NOME', Faker::Company.name)
32
+
33
+ r.add_field('NUMERO_SECAO', Faker::Number.number(3))
34
+ r.add_field('NOME_LOCAL', Faker::Company.name)
35
+ r.add_field('ENDERECO_LOCAL', Faker::Address.street_address)
36
+ end
37
+
38
+ report.generate("test/result/test_fields_inside_text.odt")
@@ -0,0 +1,43 @@
1
+ # coding: UTF-8
2
+ require './lib/odf-report'
3
+ require 'faker'
4
+ require 'launchy'
5
+
6
+
7
+ class Item
8
+ attr_accessor :name, :sid, :children
9
+ def initialize(_name, _sid, _children=[])
10
+ @name=_name
11
+ @sid=_sid
12
+ @children=_children
13
+ end
14
+ end
15
+
16
+ @items = []
17
+ @items << Item.new("LOST", '007', %w(sawyer juliet hurley locke jack freckles))
18
+ @items << Item.new("ALIAS", '302', %w(sidney sloane jack michael marshal))
19
+ @items << Item.new("GREY'S ANATOMY", '220', %w(meredith christina izzie alex george))
20
+ @items << Item.new("BREAKING BAD", '556', %w(pollos gus mike heisenberg))
21
+
22
+
23
+ report = ODFReport::Report.new("test/templates/test_nested_tables.odt") do |r|
24
+
25
+ r.add_field("TAG_01", Time.now)
26
+ r.add_field("TAG_02", "TAG-2 -> New tag")
27
+
28
+ r.add_table("TABLE_MAIN", @items) do |s|
29
+
30
+ s.add_column('NAME') { |i| i.name }
31
+
32
+ s.add_column('SID', :sid)
33
+
34
+ s.add_table('TABLE_S1', :children, :header=>true) do |t|
35
+ t.add_column('NAME1') { |item| "-> #{item}" }
36
+ t.add_column('INV') { |item| item.to_s.reverse.upcase }
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ report.generate("test/result/test_nested_tables.odt")
@@ -0,0 +1,44 @@
1
+ require './lib/odf-report'
2
+ require 'ostruct'
3
+ require 'faker'
4
+ require 'launchy'
5
+
6
+
7
+ class Item
8
+ attr_accessor :name, :sid, :children
9
+ def initialize(_name, _sid, _children=[])
10
+ @name=_name
11
+ @sid=_sid
12
+ @children=_children
13
+ end
14
+ end
15
+
16
+
17
+ @items = []
18
+ @items << Item.new("LOST", '007', %w(sawyer juliet hurley locke jack freckles))
19
+ @items << Item.new("ALIAS", '302', %w(sidney sloane jack michael marshal))
20
+ @items << Item.new("GREY'S ANATOMY", '220', %w(meredith christina izzie alex george))
21
+ @items << Item.new("BREAKING BAD", '556', %w(pollos gus mike heisenberg))
22
+
23
+
24
+ report = ODFReport::Report.new("test/templates/test_sections.odt") do |r|
25
+
26
+ r.add_field("TAG_01", Time.now)
27
+ r.add_field("TAG_02", "TAG-2 -> New tag")
28
+
29
+ r.add_section("SECTION_01", @items) do |s|
30
+
31
+ s.add_field('NAME') { |i| i.name }
32
+
33
+ s.add_field('SID', :sid)
34
+
35
+ s.add_table('TABLE_S1', :children, :header=>true) do |t|
36
+ t.add_column('NAME1') { |item| "-> #{item}" }
37
+ t.add_column('INV') { |item| item.to_s.reverse.upcase }
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ report.generate("test/result/test_sections.odt")
@@ -0,0 +1,58 @@
1
+ require './lib/odf-report'
2
+ require 'ostruct'
3
+ require 'faker'
4
+ require 'launchy'
5
+
6
+
7
+ class Item
8
+ attr_accessor :name, :sid, :children, :subs
9
+ def initialize(_name, _sid, _children=[], _subs=[])
10
+ @name=_name
11
+ @sid=_sid
12
+ @children=_children
13
+ @subs=_subs
14
+ end
15
+ end
16
+
17
+
18
+
19
+ subs1 = []
20
+ subs1 << Item.new("SAWYER", 1, %w(Your bones don't break))
21
+ subs1 << Item.new("HURLEY", 2, %w(Your cells react to bacteria and viruses))
22
+ subs1 << Item.new("LOCKE", 3, %W(Do you see any Teletubbies in here))
23
+
24
+ subs2 = []
25
+ subs2 << Item.new("SLOANE", 21, %w(Praesent hendrerit lectus sit amet))
26
+ subs2 << Item.new("JACK", 22, %w(Donec nec est eget dolor laoreet))
27
+ subs2 << Item.new("MICHAEL", 23, %W(Integer elementum massa at nulla placerat varius))
28
+
29
+ @items = []
30
+ @items << Item.new("LOST", '007', [], subs1)
31
+ @items << Item.new("GREY'S ANATOMY", '220', nil)
32
+ @items << Item.new("ALIAS", '302', nil, subs2)
33
+ @items << Item.new("BREAKING BAD", '556', [])
34
+
35
+
36
+ report = ODFReport::Report.new("test/templates/test_sub_sections.odt") do |r|
37
+
38
+ r.add_field("TAG_01", Time.now)
39
+ r.add_field("TAG_02", "TAG-2 -> New tag")
40
+
41
+ r.add_section("SECTION_01", @items) do |s|
42
+
43
+ s.add_field('NAME') { |i| i.name }
44
+
45
+ s.add_field('SID', :sid)
46
+
47
+ s.add_section('SUB_01', :subs) do |r|
48
+ r.add_field("FIRST_NAME", :name)
49
+ r.add_table('IPSUM_TABLE', :children, :header => true) do |t|
50
+ t.add_column('IPSUM_ITEM') { |i| i }
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ report.generate("test/result/test_sub_sections.odt")