odt_handlebars 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 52ffd363fafee063e43d504c40aa30aab00058562269ee5545b1c6f193104075
4
+ data.tar.gz: e53779aa8138ab10111d54b75e24eeb631aa9915a543ea32aaa77d31904f26fe
5
+ SHA512:
6
+ metadata.gz: 138583e5182064a192d16f6573464184e801269a2fa70ec5e30b5b58d61f22efabe508a0ac96f73caa3186b132c43a3cfeeed3abc9f30ad10ec7154530e7f962
7
+ data.tar.gz: 38c1c260c1f4025aea464efe5ef1464fb6a9db4c080e9302d8a20cf8d4050106576ee914bfb8631bac71c02c84f818277321340b6411342b59d9025eb0197711
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at peter.schrammel@gmx.de. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Changelog.md ADDED
@@ -0,0 +1,2 @@
1
+ # 0.0.1
2
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in odt_handlebars.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Peter Schrammel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # OdtHandlebars
2
+
3
+ Use handlebars in an OopenOffice/LibreOffice document.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'odt_handlebars'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install odt_handlebars
20
+
21
+ ## Usage
22
+
23
+ ### Command line
24
+ ```
25
+ odt_handlebars infile.odt outfile.odt replacemants.json
26
+ ```
27
+
28
+ ### Programaticaly
29
+
30
+ ```
31
+ require 'odt_handlebars'
32
+
33
+ OdtHandlebars.replace(in_file_name,out_file_name, replacements_hash)
34
+
35
+ ```
36
+
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/odt_handlebars. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the OdtHandlebars project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/odt_handlebars/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "odt_handlebars"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json'
4
+ require 'odt_handlebars'
5
+
6
+ OdtHandlebars.replace(ARGV[0],ARGV[1],JSON.parse(File.read(ARGV[2])))
7
+
8
+
@@ -0,0 +1,10 @@
1
+ require "odt_handlebars/version"
2
+ require "odt_handlebars/odt_fill"
3
+
4
+ module OdtHandlebars
5
+ class Error < StandardError; end
6
+
7
+ def self.replace(in_file,out_file, placeholders)
8
+ OdtFill.new(in_file,out_file, placeholders).replace
9
+ end
10
+ end
@@ -0,0 +1,90 @@
1
+ # coding: utf-8
2
+
3
+ require 'strscan'
4
+
5
+ module OdtHandlebars
6
+ class OdtCleaner
7
+ def self.clean(content)
8
+ new(content).clean
9
+ end
10
+
11
+ def initialize(content)
12
+ @content=content
13
+ end
14
+
15
+ def clean
16
+ out=""
17
+ scanstart(StringScanner.new(@content),out)
18
+ end
19
+
20
+ private
21
+
22
+ def scanstart(scanner,out)
23
+ curlstart=0
24
+ until scanner.eos?
25
+ if res=scanner.scan(/<[^>]+>/)
26
+ # puts "match: #{res}"
27
+ out << res
28
+ elsif res=scanner.scan(/[^{<]+/)
29
+ # puts "match: #{res}"
30
+ curlstart = 0
31
+ out << res
32
+ elsif res=scanner.scan(/{/)
33
+ # puts "match: #{res}"
34
+ curlstart += 1
35
+ out << "{" # found one
36
+ if curlstart == 2
37
+ # puts "looking for end"
38
+ scanend(scanner,out)
39
+ curlstart=0
40
+ end
41
+ end
42
+ end
43
+ out
44
+ end
45
+
46
+ def scanend(scanner,out)
47
+ curlend=0
48
+ until curlend == 2
49
+ if res=scanner.scan(/<[^>]+>/)
50
+ # puts "ignored: #{res}"
51
+ #out << res
52
+ elsif res=scanner.scan(/[^}<]+/)
53
+ # puts "match: #{res}"
54
+ out << res.gsub(/\n/,'')
55
+ elsif res=scanner.scan(/}/)
56
+ # puts "match: #{res}"
57
+ out << "}"
58
+ curlend += 1
59
+ else
60
+ warn("failed to scan handlebars end")
61
+ # puts "else case"
62
+ end
63
+ end
64
+ # puts "found end"
65
+ end
66
+ end
67
+ end
68
+
69
+
70
+
71
+ # content=
72
+ # '
73
+ #<text:section text:style-name="Sect1" text:name="Bereich1">
74
+ # <text:p text:style-name="P12">Peter Schrammel; Langerhansstr 3; 80999 München</text:p>
75
+ # <text:p text:style-name="Standard">
76
+ # <text:span text:style-name="T8">{{</text:span>
77
+ # <text:span text:style-name="T9">a</text:span>
78
+ # <text:span text:style-name="T10">ddress.</text:span>
79
+ # <text:span text:style-name="T12">line</text:span>
80
+ # <text:span text:style-name="T13">1</text:span>
81
+ # <text:span text:style-name="T11">}}</text:span>
82
+ # </text:p>
83
+ # <text:p text:style-name="P4">{{a<text:span text:style-name="T2">d</text:span>dress.<text:span text:style-name="T2">line</text:span>2}}</text:p>
84
+ # <text:p text:style-name="P5">{{ad<text:span text:style-name="T2">d</text:span>ress.<text:span text:style-name="T2">line</text:span>3}}</text:p>
85
+ # <text:p text:style-name="P19"/>
86
+ # <text:p text:style-name="P13"/>
87
+ # <text:p text:style-name="P7"/>
88
+ # </text:section>
89
+ #
90
+ #'
@@ -0,0 +1,56 @@
1
+ # coding: utf-8
2
+ require 'nokogiri'
3
+ require 'handlebars'
4
+ require 'zip'
5
+ require_relative 'odt_cleaner'
6
+
7
+ module OdtHandlebars
8
+ class OdtFill
9
+ def initialize(infile,outfile,fill)
10
+ @infile=infile
11
+ @outfile=outfile
12
+ @fill=fill
13
+ end
14
+
15
+ def replace
16
+ unpack
17
+ end
18
+
19
+ def unpack
20
+ File.open(@outfile,"w") do |outfile|
21
+ Zip::OutputStream.write_buffer(outfile) do |outzip|
22
+ Zip::File.open(@infile) do |inzip|
23
+ inzip.entries.each do |e|
24
+ next if e.file_type_is?(:directory)
25
+ if e.name == "content.xml"
26
+ content = e.get_input_stream.read
27
+ output=replace_handlebars(content,@fill)
28
+ outzip.put_next_entry(e.name)
29
+ outzip.write output
30
+ else
31
+ outzip.put_next_entry(e.name)
32
+ outzip.write e.get_input_stream.read
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ @outfile
39
+ end
40
+
41
+ FIELD_MATCHER=Regexp.new("{{.+?}}",Regexp::MULTILINE)
42
+ def replace_handlebars(raw_content,placeholders)
43
+ content=OdtCleaner.clean(raw_content)
44
+ doc=Nokogiri.parse(content)
45
+ rows=doc.xpath("//table:table-row/*[starts-with(.,'{{/each')]")
46
+ rows.each do |row| row.parent.replace(row.to_str) end
47
+ rows=doc.xpath("//table:table-row/*[starts-with(.,'{{#each')]")
48
+ rows.each do |row| row.parent.replace(row.to_str) end
49
+ fields=doc.xpath("//text:p/*")
50
+ handlebars = Handlebars::Context.new
51
+ template = handlebars.compile(doc.to_s)
52
+ out=template.call(placeholders)
53
+ out
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module OdtHandlebars
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,46 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "odt_handlebars/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "odt_handlebars"
8
+ spec.version = OdtHandlebars::VERSION
9
+ spec.authors = ["Peter Schrammel"]
10
+ spec.email = ["peter.schrammel@gmx.de"]
11
+
12
+ spec.summary = %q{Use an OpenOffice/LibreOffice Document as a handlebar template}
13
+ spec.description = %q{Replace handleber macros in a OpenOffice/LibreOffice Writer document. Even allows to create table rows. }
14
+ spec.homepage = "https://github.com/pschrammel/odt_handlebars"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/pschrammel/odt_handlebars.git"
24
+ spec.metadata["changelog_uri"] = "https://github.com/pschrammel/odt_handlebars/blob/master/Changelog.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency 'nokogiri', "~> 1.10"
40
+ spec.add_dependency 'handlebars', "~> 0.8"
41
+ spec.add_dependency 'rubyzip', "~> 2.3"
42
+
43
+ spec.add_development_dependency 'byebug', "~> 0"
44
+ spec.add_development_dependency "bundler", "~> 1.17"
45
+ spec.add_development_dependency "rake", "~> 10.0"
46
+ end
@@ -0,0 +1,16 @@
1
+ {
2
+ "address": {
3
+ "line1": "Mr Foo Bar",
4
+ "line2": "Mainstreet 3",
5
+ "line3": "80999 München"
6
+ },
7
+ "recipient": {
8
+ "male": true,
9
+ "surname": "Bar"
10
+ },
11
+ "rows": [
12
+ {"header": "Yes, please"},
13
+ {"header": "Maybe later"}
14
+ ]
15
+
16
+ }
Binary file
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: odt_handlebars
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Schrammel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: handlebars
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubyzip
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: 'Replace handleber macros in a OpenOffice/LibreOffice Writer document.
98
+ Even allows to create table rows. '
99
+ email:
100
+ - peter.schrammel@gmx.de
101
+ executables:
102
+ - odt_handlebars
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - CODE_OF_CONDUCT.md
108
+ - Changelog.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - exe/odt_handlebars
116
+ - lib/odt_handlebars.rb
117
+ - lib/odt_handlebars/odt_cleaner.rb
118
+ - lib/odt_handlebars/odt_fill.rb
119
+ - lib/odt_handlebars/version.rb
120
+ - odt_handlebars.gemspec
121
+ - samples/placeholders.json
122
+ - samples/template.odt
123
+ homepage: https://github.com/pschrammel/odt_handlebars
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ allowed_push_host: https://rubygems.org
128
+ homepage_uri: https://github.com/pschrammel/odt_handlebars
129
+ source_code_uri: https://github.com/pschrammel/odt_handlebars.git
130
+ changelog_uri: https://github.com/pschrammel/odt_handlebars/blob/master/Changelog.md
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.7.6.2
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Use an OpenOffice/LibreOffice Document as a handlebar template
151
+ test_files: []