serenity-odt 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rake'
2
2
  require 'spec/rake/spectask'
3
+ require 'rake/gempackagetask'
3
4
 
4
5
  task :default => [:spec]
5
6
 
@@ -7,7 +8,7 @@ Spec::Rake::SpecTask.new("spec")
7
8
 
8
9
  spec = Gem::Specification.new do |s|
9
10
  s.name = %q{serenity-odt}
10
- s.version = "0.1.0"
11
+ s.version = "0.1.1"
11
12
 
12
13
  s.authors = ["Tomas Kramar"]
13
14
  s.description = <<-EOF
@@ -16,7 +17,7 @@ spec = Gem::Specification.new do |s|
16
17
  Very similar to .erb files.
17
18
  EOF
18
19
  s.email = %q{kramar.tomas@gmail.com}
19
- s.files = Dir.glob('lib/**/*.rb') + %w{README.md Rakefile serenity-odt.gemspec LICENSE}
20
+ s.files = Dir.glob('lib/**/*.rb') + %w{README.md Rakefile LICENSE}
20
21
  s.has_rdoc = false
21
22
  s.require_paths = ["lib"]
22
23
  s.rubygems_version = %q{1.3.5}
@@ -26,11 +27,6 @@ spec = Gem::Specification.new do |s|
26
27
  s.add_development_dependency('rspec', '>= 1.2.9')
27
28
  end
28
29
 
29
- task :gemspec do
30
- File.open("serenity-odt.gemspec", "w") { |f| f << spec.to_ruby }
30
+ Rake::GemPackageTask.new(spec) do |p|
31
+ p.gem_spec = spec
31
32
  end
32
-
33
- task :gem => :gemspec do
34
- system "gem build serenity-odt.gemspec"
35
- end
36
-
data/lib/serenity.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'serenity/line'
3
3
  require 'serenity/debug'
4
4
  require 'serenity/node_type'
5
+ require 'serenity/escape_xml'
5
6
  require 'serenity/odteruby'
6
7
  require 'serenity/template'
7
8
  require 'serenity/xml_reader'
@@ -0,0 +1,14 @@
1
+ class String
2
+ def escape_xml
3
+ mgsub!([[/&/, '&amp;'], [/</, '&lt;'], [/>/, '&gt;']])
4
+ self
5
+ end
6
+
7
+ def mgsub!(key_value_pairs=[].freeze)
8
+ regexp_fragments = key_value_pairs.collect { |k,v| k }
9
+ gsub!(Regexp.union(*regexp_fragments)) do |match|
10
+ key_value_pairs.detect{|k,v| k =~ match}[1]
11
+ end
12
+ self
13
+ end
14
+ end
@@ -1,5 +1,3 @@
1
- require 'ruby-debug'
2
-
3
1
  module Serenity
4
2
  module Generator
5
3
  def render_odt template_path, output_path = output_name(template_path)
data/lib/serenity/line.rb CHANGED
@@ -40,16 +40,13 @@ module Serenity
40
40
  end
41
41
 
42
42
  def escape_code code
43
- code.gsub!('&apos;', "'")
44
- code.gsub!('&gt;', ">")
45
- code.gsub!('&lt;', "<")
46
- code
43
+ code.mgsub! [[/&apos;/, "'"], [/&gt;/, '>'], [/&lt/, '<'], [/&quot;/, '"']]
47
44
  end
48
45
  end
49
46
 
50
47
  class StringLine < CodeLine
51
48
  def to_buf
52
- " _buf << (" << escape_code(@text) << ").to_s;"
49
+ " _buf << (" << escape_code(@text) << ").to_s.escape_xml;"
53
50
  end
54
51
  end
55
52
  end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe String do
4
+ it 'should escape <' do
5
+ '1 < 2'.escape_xml.should == '1 &lt; 2'
6
+ end
7
+
8
+ it 'should escape >' do
9
+ '2 > 1'.escape_xml.should == '2 &gt; 1'
10
+ end
11
+
12
+ it 'should escape &' do
13
+ '1 & 2'.escape_xml.should == '1 &amp; 2'
14
+ end
15
+
16
+ it 'should escape < > &' do
17
+ '1 < 2 && 2 > 1'.escape_xml.should == '1 &lt; 2 &amp;&amp; 2 &gt; 1'
18
+ end
19
+ end
@@ -2,6 +2,10 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  module Serenity
4
4
  describe Generator do
5
+ after(:each) do
6
+ FileUtils.rm(fixture('loop_output.odt'))
7
+ end
8
+
5
9
  it 'should make context from instance variables and run the provided template' do
6
10
  class GeneratorClient
7
11
  include Generator
@@ -16,6 +16,27 @@ module Serenity
16
16
  text.inject('') { |memo, line| memo += line.strip } unless text.nil?
17
17
  end
18
18
 
19
+ def run_spec template, expected, context=@context
20
+ content = OdtEruby.new(XmlReader.new template)
21
+ result = content.evaluate context
22
+
23
+ squeeze(result).should == squeeze(expected)
24
+ end
25
+
26
+ it 'should escape single quotes properly' do
27
+ expected = template = "<text:p>It's a 'quote'</text:p>"
28
+
29
+ run_spec template, expected
30
+ end
31
+
32
+ it 'should properly escape special XML characters ("<", ">", "&")' do
33
+ template = "<text:p>{%= description %}</text:p>"
34
+ description = 'This will only hold true if length < 1 && var == true or length > 1000'
35
+ expected = "<text:p>This will only hold true if length &lt; 1 &amp;&amp; var == true or length &gt; 1000</text:p>"
36
+
37
+ run_spec template, expected, binding
38
+ end
39
+
19
40
  it 'should replace variables with values from context' do
20
41
  template = <<-EOF
21
42
  <text:p text:style-name="Text_1_body">{%= name %}</text:p>
@@ -29,42 +50,15 @@ module Serenity
29
50
  <text:p text:style-name="Text_1_body"/>
30
51
  EOF
31
52
 
32
- content = OdtEruby.new(XmlReader.new template)
33
- result = content.evaluate @context
34
-
35
- squeeze(expected).should == squeeze(result)
53
+ run_spec template, expected
36
54
  end
37
55
 
38
56
  it 'should replace multiple variables on one line' do
39
57
  template = '<text:p text:style-name="Text_1_body">{%= type %} and {%= name %}</text:p>'
40
58
  expected = '<text:p text:style-name="Text_1_body">test_type and test_name</text:p>'
41
59
 
42
- content = OdtEruby.new(XmlReader.new template)
43
- result = content.evaluate @context
44
-
45
- squeeze(expected).should == squeeze(result)
46
- end
47
- =begin
48
- should 'replace a LOOP construct with variables' do
49
- template = <<-EOF
50
- <text:p text:style-name="Text_1_body">{% for row in rows do %}</text:p>
51
- <text:p text:style-name="Text_1_body">{%= row.name %}</text:p>
52
- <text:p text:style-name="Text_1_body">{%= row.type %}</text:p>
53
- <text:p text:style-name="Text_1_body">{% end %}</text:p>
54
- EOF
55
-
56
- expected = <<-EOF
57
- <text:p text:style-name="Text_1_body">test_name_1</text:p>
58
- <text:p text:style-name="Text_1_body">test_type_1</text:p>
59
- <text:p text:style-name="Text_1_body">test_name_2</text:p>
60
- <text:p text:style-name="Text_1_body">test_type_2</text:p>
61
- EOF
62
-
63
- content = OdtEruby.new(XmlReader.new template)
64
- result = content.evaluate @context
65
- assert_equal expected, result
60
+ run_spec template, expected
66
61
  end
67
- =end
68
62
 
69
63
  it 'should remove empty tags after a control structure processing' do
70
64
  template = <<-EOF
@@ -93,10 +87,7 @@ module Serenity
93
87
  </table:table>
94
88
  EOF
95
89
 
96
- content = OdtEruby.new(XmlReader.new template)
97
- result = content.evaluate @context
98
-
99
- squeeze(expected).should == squeeze(result)
90
+ run_spec template, expected
100
91
  end
101
92
  end
102
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serenity-odt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Kramar
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-18 00:00:00 +01:00
12
+ date: 2010-02-20 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ extra_rdoc_files: []
42
42
 
43
43
  files:
44
44
  - lib/serenity/debug.rb
45
+ - lib/serenity/escape_xml.rb
45
46
  - lib/serenity/generator.rb
46
47
  - lib/serenity/line.rb
47
48
  - lib/serenity/node_type.rb
@@ -51,19 +52,7 @@ files:
51
52
  - lib/serenity.rb
52
53
  - README.md
53
54
  - Rakefile
54
- - serenity-odt.gemspec
55
55
  - LICENSE
56
- - spec/generator_spec.rb
57
- - spec/odteruby_spec.rb
58
- - spec/spec_helper.rb
59
- - spec/support/matchers/be_a_document.rb
60
- - spec/template_spec.rb
61
- - spec/xml_reader_spec.rb
62
- - fixtures/advanced.odt
63
- - fixtures/loop.odt
64
- - fixtures/loop_table.odt
65
- - fixtures/table_rows.odt
66
- - fixtures/variables.odt
67
56
  has_rdoc: true
68
57
  homepage:
69
58
  licenses: []
@@ -93,6 +82,7 @@ signing_key:
93
82
  specification_version: 3
94
83
  summary: Embedded ruby for OpenOffice Text Document (.odt) files
95
84
  test_files:
85
+ - spec/escape_xml_spec.rb
96
86
  - spec/generator_spec.rb
97
87
  - spec/odteruby_spec.rb
98
88
  - spec/spec_helper.rb
data/serenity-odt.gemspec DELETED
@@ -1,36 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- Gem::Specification.new do |s|
4
- s.name = %q{serenity-odt}
5
- s.version = "0.1.0"
6
-
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Tomas Kramar"]
9
- s.date = %q{2010-02-18}
10
- s.description = %q{ Embedded ruby for OpenOffice Text Document (.odt) files. You provide an .odt template
11
- with ruby code in a special markup and the data, and Serenity generates the document.
12
- Very similar to .erb files.
13
- }
14
- s.email = %q{kramar.tomas@gmail.com}
15
- s.files = ["lib/serenity/debug.rb", "lib/serenity/generator.rb", "lib/serenity/line.rb", "lib/serenity/node_type.rb", "lib/serenity/odteruby.rb", "lib/serenity/template.rb", "lib/serenity/xml_reader.rb", "lib/serenity.rb", "README.md", "Rakefile", "serenity-odt.gemspec", "LICENSE", "spec/generator_spec.rb", "spec/odteruby_spec.rb", "spec/spec_helper.rb", "spec/support/matchers/be_a_document.rb", "spec/template_spec.rb", "spec/xml_reader_spec.rb", "fixtures/advanced.odt", "fixtures/loop.odt", "fixtures/loop_table.odt", "fixtures/table_rows.odt", "fixtures/variables.odt"]
16
- s.require_paths = ["lib"]
17
- s.rubygems_version = %q{1.3.5}
18
- s.summary = %q{Embedded ruby for OpenOffice Text Document (.odt) files}
19
- s.test_files = ["spec/generator_spec.rb", "spec/odteruby_spec.rb", "spec/spec_helper.rb", "spec/support/matchers/be_a_document.rb", "spec/template_spec.rb", "spec/xml_reader_spec.rb", "fixtures/advanced.odt", "fixtures/loop.odt", "fixtures/loop_table.odt", "fixtures/table_rows.odt", "fixtures/variables.odt"]
20
-
21
- if s.respond_to? :specification_version then
22
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
- s.specification_version = 3
24
-
25
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
- s.add_runtime_dependency(%q<rubyzip>, [">= 0.9.1"])
27
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
28
- else
29
- s.add_dependency(%q<rubyzip>, [">= 0.9.1"])
30
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
31
- end
32
- else
33
- s.add_dependency(%q<rubyzip>, [">= 0.9.1"])
34
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
35
- end
36
- end