doct 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "nokogiri"
7
+ gem "rubyzip"
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "bundler", "~> 1.1.0"
13
+ gem "jeweler", "~> 1.8.3"
14
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Anton Argirov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = doct
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to doct
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Anton Argirov. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "doct"
18
+ gem.homepage = "http://redmine.academ.org"
19
+ gem.license = "MIT"
20
+ gem.summary = 'Simple Office Document Templating'
21
+ gem.description = %Q(Provides an ability to pre-populate an office document through a simple templating mechanism that can easily be managed by an end user.)
22
+ gem.email = "anton.argirov@gmail.com"
23
+ gem.authors = ["Anton Argirov"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "render_parent #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.3
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ require 'doct/template'
@@ -0,0 +1,13 @@
1
+ module Doct::Formats
2
+ class Base
3
+ def initialize(path)
4
+ @template_path = path
5
+ end
6
+ def render(params)
7
+ raise NotImplementedError
8
+ end
9
+ def placeholders
10
+ raise NotImplementedError
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ require 'doct/formats/open_format'
2
+
3
+ module Doct::Formats
4
+ class Docx < OpenFormat
5
+
6
+ def content_entry?(entry)
7
+ return true if entry == 'word/document.xml'
8
+ entry =~ /word\/(header|footer)\d*.xml/
9
+ end
10
+
11
+ def entry_render(entry, content, params)
12
+ xml = Nokogiri::XML(content)
13
+ orig_xml = xml.dup
14
+ current_xml = xml
15
+ sectpr_node = xml.xpath("w:document/w:body/w:sectPr").first
16
+ if entry == 'word/document.xml'
17
+ for page in params
18
+ nodes = current_xml.xpath("w:document/w:body/*[name()!='w:sectPr']")
19
+ nodes.xpath("//w:sdt//w:t").each do |node|
20
+ node.content = page[node.content.to_sym].to_s
21
+ end
22
+ sectpr_node.before(nodes)
23
+ if page != params.last
24
+ sectpr_node.before("<w:p><w:r><w:br w:type='page'/></w:r></w:p>")
25
+ end
26
+ current_xml = orig_xml.dup
27
+ end
28
+ elsif not params.empty?
29
+ xml.xpath("//w:sdt//w:t").each do |node|
30
+ node.content = params.first[node.content.to_sym]
31
+ end
32
+ end
33
+ xml.to_s
34
+ end
35
+
36
+ def entry_placeholders(entry, content)
37
+ xml = Nokogiri::XML(content)
38
+ xml.xpath("//w:sdt//w:t").collect {|node| node.content.to_s}
39
+ end
40
+
41
+ private
42
+
43
+ Doct::Template::register_format(self, 'docx')
44
+ end
45
+ end
@@ -0,0 +1,43 @@
1
+ require 'doct/formats/open_format'
2
+
3
+ module Doct::Formats
4
+ class Odt < OpenFormat
5
+ def content_entries
6
+ %w(content.xml styles.xml)
7
+ end
8
+
9
+ def entry_render(entry, content, params)
10
+ xml = Nokogiri::XML(content)
11
+ orig_xml = xml.dup
12
+ current_xml = xml
13
+ text_node = xml.xpath("//office:text").first
14
+ if entry == 'content.xml'
15
+ for page in params
16
+ nodes = current_xml.xpath("//office:text/*")
17
+ nodes.xpath("//text:database-display").each do |node|
18
+ param_name = node.attributes["column-name"].to_s
19
+ node.replace page[param_name.to_sym].to_s
20
+ end
21
+ text_node.add_child(nodes)
22
+ if page != params.last
23
+ text_node.add_child("<text:soft-page-break>")
24
+ end
25
+ current_xml = orig_xml.dup
26
+ end
27
+ elsif not params.empty?
28
+ xml.xpath("//text:database-display").each do |node|
29
+ param_name = node.attributes["column-name"].to_s
30
+ node.replace params.first[param_name.to_sym].to_s
31
+ end
32
+ end
33
+ xml.to_s
34
+ end
35
+
36
+ def entry_placeholders(entry, content)
37
+ xml = Nokogiri::XML(content)
38
+ xml.xpath("//text:database-display").collect { |node| node.attributes["column-name"].to_s }
39
+ end
40
+
41
+ Doct::Template::register_format(self, 'odt')
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ require 'doct/formats/base'
2
+ require 'zip/zip'
3
+ require 'nokogiri'
4
+ require 'fileutils'
5
+
6
+ module Doct::Formats
7
+ class OpenFormat < Base
8
+
9
+ def render(params)
10
+ Zip::ZipOutputStream.write_buffer do |output|
11
+ each_entry do |entry, file|
12
+ file = entry_render(entry, file, params) if _content_entry?(entry)
13
+ output.put_next_entry entry
14
+ output.write file
15
+ end
16
+ end.string
17
+ end
18
+
19
+ def placeholders
20
+ placeholders = []
21
+ each_entry do |entry, file|
22
+ placeholders += entry_placeholders(entry, file) if _content_entry?(entry)
23
+ end
24
+ placeholders
25
+ end
26
+
27
+ def content_entries
28
+ nil
29
+ end
30
+
31
+ def content_entry?(entry)
32
+ false
33
+ end
34
+
35
+ private
36
+
37
+ def _content_entry?(entry)
38
+ content_entries && content_entries.include?(entry) || content_entry?(entry)
39
+ end
40
+
41
+ def each_entry
42
+ Zip::ZipInputStream.open(@template_path) do |input|
43
+ while (entry = input.get_next_entry) do
44
+ yield entry.name, input.read
45
+ end
46
+ end
47
+ end
48
+
49
+ def entry_placeholders(entry, xml)
50
+ []
51
+ end
52
+
53
+ def entry_render(entry, xml, template)
54
+ xml
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,26 @@
1
+ module Doct
2
+ class Template < Array
3
+ attr_reader :format
4
+ FORMATS = {}
5
+
6
+ def self.register_format(format, *extensions)
7
+ extensions.each {|ext| FORMATS[ext] = format}
8
+ end
9
+ def initialize(filename, format = nil)
10
+ format ||= File.extname(filename)[1..-1].to_s.downcase
11
+ raise "Unrecognized format '#{format}'" unless FORMATS[format]
12
+ @format = FORMATS[format].new(filename)
13
+ end
14
+ def placeholders
15
+ @format.placeholders.map(&:to_sym)
16
+ end
17
+ def save_as filename
18
+ open(filename, "wb") { |io| io << render }
19
+ end
20
+ def render
21
+ @format.render self
22
+ end
23
+ end
24
+ end
25
+
26
+ Dir["#{File.dirname(__FILE__)}/formats/*.rb"].each {|file| require file}
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anton Argirov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rubyzip
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.1.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ description: Provides an ability to pre-populate an office document through a simple
79
+ templating mechanism that can easily be managed by an end user.
80
+ email: anton.argirov@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files:
84
+ - LICENSE.txt
85
+ - README.rdoc
86
+ files:
87
+ - .document
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ - Rakefile
92
+ - VERSION
93
+ - examples/output.docx
94
+ - examples/output.odt
95
+ - examples/template.docx
96
+ - examples/template.odt
97
+ - lib/doct.rb
98
+ - lib/doct/formats/base.rb
99
+ - lib/doct/formats/docx.rb
100
+ - lib/doct/formats/odt.rb
101
+ - lib/doct/formats/open_format.rb
102
+ - lib/doct/template.rb
103
+ homepage: http://redmine.academ.org
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 616003599
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.24
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Simple Office Document Templating
131
+ test_files: []