openxml_docx_templater 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfe1d4c33886dbb55766f0f298cbb0ee3343c719
4
- data.tar.gz: 1e07055b775a6be7ad235a23e8456213f862edd9
3
+ metadata.gz: 6acf4751ef96a4c85d4e5a17d23fe65263ea368a
4
+ data.tar.gz: 664cff7779ba79761ff6e3e9c2c1de51cfd5057c
5
5
  SHA512:
6
- metadata.gz: a15bdf27ab3324eb543dc3e2cff30879a2a4021f0bc86c3a62c60ef9c5ee38457e55cf2781f344a0c3a1df63fe56e50f3dbc3b9b51383aa8497fd9cdc889025e
7
- data.tar.gz: b89f52d940165e8597958fd81d332d42bcf013482d2fc7167ebc24d598948b7e13c39e2154f9c545d31b1e2115d131f3a4146d7f5c24e005d6465838f15bd03e
6
+ metadata.gz: e639d4980436809ba24516da4a442e0b090318643b22ca12c3da4f60c886f4fd3b3d6e91941d586f265dc39e1813954337c8da2fcff7bf471fb8a1ebaa0269d8
7
+ data.tar.gz: 22fa05283f6009d2817a32629097f60c43f1d1c19cd916ab7ce4c60066650b1650965d5aba2ea8f42f6891d5311847da33df4a869bbf65f7a999645ea98aae58
data/.rubocop.yml ADDED
@@ -0,0 +1,40 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ DisplayCopNames: true
5
+ DisplayStyleGuide: true
6
+
7
+ AsciiComments:
8
+ Enabled: false
9
+
10
+ ClassLength:
11
+ Enabled: false
12
+
13
+ MethodLength:
14
+ Enabled: false
15
+
16
+ Documentation:
17
+ Enabled: false
18
+
19
+ Style/ClassAndModuleChildren:
20
+ Enabled: false
21
+
22
+ Style/PredicateName:
23
+ NameWhitelist:
24
+ - is_a?
25
+
26
+ Style/TrailingCommaInLiteral:
27
+ EnforcedStyleForMultiline: comma
28
+
29
+ Style/TrailingCommaInArguments:
30
+ EnforcedStyleForMultiline: comma
31
+
32
+ Layout/AlignParameters:
33
+ Enabled: false
34
+
35
+ LineLength:
36
+ Max: 120
37
+
38
+ Lint/UnifiedInteger:
39
+ Description: 'Use Integer instead of Fixnum or Bignum'
40
+ Enabled: false
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
data/circle.yml ADDED
@@ -0,0 +1,7 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.4.1
4
+
5
+ test:
6
+ override:
7
+ - bundle exec rspec
data/examples/example.rb CHANGED
@@ -1,4 +1,4 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
3
  require 'openxml_docx_templater'
4
4
 
Binary file
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "openxml_docx_templater/version"
2
4
  require "openxml_docx_templater/debug"
3
5
  require "openxml_docx_templater/node_type"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
4
  module Debug
3
5
  def debug?
@@ -1,45 +1,46 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
4
  class DocxEruby
3
5
  include Debug
4
6
 
5
7
  EMBEDDED_PATTERN = /\{%([=%]+)?(.*?)-?%\}/m
6
8
 
7
- def initialize template
9
+ def initialize(template)
8
10
  @src = convert template
9
- if debug?
10
- File.open(debug_file_path, 'w') do |f|
11
- f << @src
12
- end
11
+ return unless debug?
12
+ File.open(debug_file_path, 'w') do |f|
13
+ f << @src
13
14
  end
14
15
  end
15
16
 
16
- def evaluate context
17
+ def evaluate(context)
17
18
  eval(@src, context)
18
19
  end
19
20
 
20
21
  private
21
22
 
22
- def convert template
23
+ def convert(template)
23
24
  src = "_buf = '';"
24
25
  buffer = []
25
26
 
26
- template.each_node do |node, type|
27
+ template.each_node do |node, _type|
27
28
  buffer << process_instruction(node)
28
29
  buffer.flatten!
29
30
  end
30
31
 
31
- buffer.each { |line| src << line.to_buf }
32
- src << "\n_buf.to_s\n"
32
+ buffer.each { |line| src += line.to_buf }
33
+ src += "\n_buf.to_s\n"
33
34
  end
34
35
 
35
- def process_instruction text
36
+ def process_instruction(text)
36
37
  pos = 0
37
38
  src = []
38
39
 
39
40
  text.scan(EMBEDDED_PATTERN) do |indicator, code|
40
41
  m = Regexp.last_match
41
42
  middle = text[pos...m.begin(0)]
42
- pos = m.end(0)
43
+ pos = m.end(0)
43
44
  src << Line.text(middle) unless middle.empty?
44
45
 
45
46
  if !indicator # <% %>
@@ -51,9 +52,9 @@ module OpenxmlDocxTemplater
51
52
  end
52
53
  end
53
54
 
54
- rest = pos == 0 ? text : text[pos..-1]
55
+ rest = pos.zero? ? text : text[pos..-1]
55
56
 
56
- src << Line.text(rest) unless rest.nil? or rest.empty?
57
+ src << Line.text(rest) unless rest.nil? || rest.empty?
57
58
  src
58
59
  end
59
60
  end
@@ -1,26 +1,25 @@
1
- class String
1
+ # frozen_string_literal: true
2
2
 
3
+ class String
3
4
  CODE_LINE_PATTERN = /{(.*?)%(.+?)%(.*?)}/
4
5
  TAG_PATTERN = /<(.+?)>[\n]?/m
5
6
 
6
7
  def escape_xml
7
- mgsub!([[/&/, '&amp;'], [/</, '&lt;'], [/>/, '&gt;']])
8
+ mgsub(/&/ => '&amp;', /</ => '&lt;', />/ => '&gt;')
8
9
  end
9
10
 
10
11
  def convert_newlines
11
- gsub!("\n", '<w:br/>')
12
- self
12
+ gsub("\n", '<w:br/>')
13
13
  end
14
14
 
15
15
  def refact
16
- gsub(CODE_LINE_PATTERN) { |result| result.gsub(TAG_PATTERN, "") }
16
+ gsub(CODE_LINE_PATTERN) { |result| result.gsub(TAG_PATTERN, '') }
17
17
  end
18
18
 
19
- def mgsub!(key_value_pairs=[].freeze)
20
- regexp_fragments = key_value_pairs.collect { |k, v| k }
21
- gsub!(Regexp.union(*regexp_fragments)) do |match|
22
- key_value_pairs.detect { |k, v| k =~ match }[1]
19
+ def mgsub(key_value_pairs = {})
20
+ regexp_fragments = key_value_pairs.keys
21
+ gsub(Regexp.union(*regexp_fragments)) do |match|
22
+ key_value_pairs.detect { |k, _v| k =~ match }[1]
23
23
  end
24
- self
25
24
  end
26
25
  end
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
4
  module Generator
3
- def render_msword template_path, output_path = output_name(template_path)
5
+ def render_msword(template_path, output_path = output_name(template_path))
4
6
  template = Template.new template_path, output_path
5
7
  template.process binding
6
8
  end
7
9
 
8
10
  private
9
11
 
10
- def output_name input
12
+ def output_name(input)
11
13
  if input =~ /(.+)\.docx\Z/
12
- "#{$1}_output.docx"
14
+ "#{Regexp.last_match(1)}_output.docx"
13
15
  else
14
16
  "#{input}_output.docx"
15
17
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
4
  class Line
3
5
  attr_reader :text
4
6
 
5
- def initialize text
7
+ def initialize(text)
6
8
  @text = text
7
9
  end
8
10
 
@@ -10,59 +12,52 @@ module OpenxmlDocxTemplater
10
12
  @text
11
13
  end
12
14
 
13
- def self.text txt
15
+ def self.text(txt)
14
16
  TextLine.new txt
15
17
  end
16
18
 
17
- def self.code txt
19
+ def self.code(txt)
18
20
  CodeLine.new txt
19
21
  end
20
22
 
21
- def self.string txt
22
- StringLine.new txt
23
+ def self.string(txt)
24
+ StringLine.new(txt)
23
25
  end
24
26
 
25
- def self.literal txt
27
+ def self.literal(txt)
26
28
  LiteralLine.new txt
27
29
  end
28
-
29
30
  end
30
31
 
31
32
  class TextLine < Line
32
33
  def to_buf
33
- " _buf << '" << escape_text(@text) << "';"
34
+ " _buf << '#{escape_text(@text)}';"
34
35
  end
35
36
 
36
- def escape_text text
37
+ def escape_text(text)
37
38
  text.gsub(/['\\]/, '\\\\\&')
38
39
  end
39
40
  end
40
41
 
41
42
  class CodeLine < Line
42
43
  def to_buf
43
- escape_code(@text) << ';'
44
+ "#{escape_code(@text)};"
44
45
  end
45
46
 
46
- def escape_code code
47
- code.mgsub! [[/&apos;/, "'"], [/&gt;/, '>'], [/&lt/, '<'], [/&quot;/, '"'], [/&amp;/, '&']]
47
+ def escape_code(code)
48
+ code.mgsub(/&apos;/ => "'", /&gt;/ => '>', /&lt/ => '<', /&quot;/ => '"', /&amp;/ => '&')
48
49
  end
49
50
  end
50
51
 
51
52
  class StringLine < CodeLine
52
53
  def to_buf
53
- " _buf << (" << escape_code(@text) << ").to_s.escape_xml.convert_newlines;"
54
- end
55
-
56
- def convert_newlines text
57
- text.gsub("First line", '<w:br/>')
54
+ " _buf << (#{escape_code(@text)}).to_s.escape_xml.convert_newlines;"
58
55
  end
59
56
  end
60
57
 
61
58
  class LiteralLine < CodeLine
62
59
  def to_buf
63
- " _buf << (" << escape_code(@text) << ").to_s;"
60
+ " _buf << (#{escape_code(@text)}).to_s;"
64
61
  end
65
62
  end
66
-
67
63
  end
68
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
4
  class NodeType
3
5
  TAG = 1
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zip'
2
4
  require 'fileutils'
3
5
 
@@ -10,16 +12,16 @@ module OpenxmlDocxTemplater
10
12
  @template = output
11
13
  end
12
14
 
13
- def process context
15
+ def process(context)
14
16
  tmpfiles = []
15
17
  Zip::File.open(@template) do |zipfile|
16
18
  zipfile.entries.select { |entry| entry.name[/\.xml$/] }.each do |xml_file|
17
- content = zipfile.read(xml_file).refact.force_encoding("utf-8")
19
+ content = zipfile.read(xml_file).refact.force_encoding('utf-8')
18
20
 
19
21
  docxeruby = DocxEruby.new(XmlReader.new(content))
20
22
  out = docxeruby.evaluate(context)
21
23
 
22
- tmpfiles << (file = Tempfile.new("openxml_template"))
24
+ tmpfiles << (file = Tempfile.new('openxml_template'))
23
25
  file << out
24
26
  file.close
25
27
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
- VERSION = "0.1.3"
4
+ VERSION = '0.2.0'
3
5
  end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OpenxmlDocxTemplater
2
4
  class XmlReader
3
-
4
- def initialize src
5
+ def initialize(src)
5
6
  @src = src
6
7
  end
7
8
 
@@ -20,12 +21,9 @@ module OpenxmlDocxTemplater
20
21
  end
21
22
  end
22
23
 
23
- def node_type text
24
- if text =~ /\s*\{%[^=#].+?%\}\s*/
25
- NodeType::CONTROL
26
- else
27
- NodeType::TEMPLATE
28
- end
24
+ def node_type(text)
25
+ return NodeType::CONTROL if text.match?(/\s*\{%[^=#].+?%\}\s*/)
26
+ NodeType::TEMPLATE
29
27
  end
30
28
  end
31
29
  end
@@ -1,27 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # coding: utf-8
4
+
2
5
  lib = File.expand_path('../lib', __FILE__)
3
6
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
7
  require 'openxml_docx_templater/version'
5
8
 
6
9
  Gem::Specification.new do |spec|
7
- spec.name = "openxml_docx_templater"
10
+ spec.name = 'openxml_docx_templater'
8
11
  spec.version = OpenxmlDocxTemplater::VERSION
9
- spec.authors = ["Andrew Ageev"]
10
- spec.email = ["ageev86@gmail.com"]
12
+ spec.authors = ['Andrew Ageev']
13
+ spec.email = ['ageev86@gmail.com']
11
14
 
12
- spec.summary = %q{Simple creation .docx files by templates}
13
- spec.description = %q{Simple creation .docx files by templates}
14
- spec.homepage = "https://github.com/AgeevAndrew/openxml_docx_templater"
15
- spec.license = "MIT"
15
+ spec.summary = 'Simple creation .docx files by templates'
16
+ spec.description = 'Simple creation .docx files by templates'
17
+ spec.homepage = 'https://github.com/AgeevAndrew/openxml_docx_templater'
18
+ spec.license = 'MIT'
16
19
 
17
20
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
21
+ spec.bindir = 'exe'
19
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
23
+ spec.require_paths = ['lib']
21
24
 
22
- spec.add_development_dependency "bundler", "~> 1.10"
23
- spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake', '~> 12.1'
24
27
  spec.add_dependency 'rubyzip'
25
28
 
26
- spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency 'rspec'
27
30
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openxml_docx_templater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Ageev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-01 00:00:00.000000000 Z
11
+ date: 2017-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubyzip
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -75,13 +75,14 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
- - ".travis.yml"
78
+ - ".rubocop.yml"
79
79
  - Gemfile
80
80
  - LICENSE.txt
81
81
  - README.md
82
82
  - Rakefile
83
83
  - bin/console
84
84
  - bin/setup
85
+ - circle.yml
85
86
  - examples/example.rb
86
87
  - examples/example_template.docx
87
88
  - examples/example_template_output.docx
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  version: '0'
119
120
  requirements: []
120
121
  rubyforge_project:
121
- rubygems_version: 2.5.1
122
+ rubygems_version: 2.6.11
122
123
  signing_key:
123
124
  specification_version: 4
124
125
  summary: Simple creation .docx files by templates
data/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.3
4
- before_install: gem install bundler -v 1.10.6