standard-procedure-consolidate 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 52bf3de85226b0aa00c8a2d5ad8caa7e0dbc7aa2e8707150d844e34a3313fcf9
4
- data.tar.gz: cf92c7c1fc1978cc7e615674d01e542d4ca5b4588da08856b6608301efd83109
3
+ metadata.gz: 5647faddeab8829e8494e02b4b87acc33276e7f293fa147f3b15b1dd2a6d4c09
4
+ data.tar.gz: e867ef111010caa13eef6da80f11ccaa7ee0d780f82309a8f4d976bcff058545
5
5
  SHA512:
6
- metadata.gz: 1a2c3dfa7bc6fe20d8f3ddc2bdfe9f1c94fe53ddd1daf75009f96af35ba20f1d7acdaf6b1f3563b9484f50fa4a0a5b53ab4c2e161008f0131fd03d39b67df76f
7
- data.tar.gz: f169d41a245b6dbe95d20eeded64c5ff684ec4822dfab36ed342374916c6e7eb2c6dee82cc14fc89bc1d141ba4c1b259629e8b6ba219952bd949db0f53384aa8
6
+ metadata.gz: 3f7e9ebff0ec1706b59983eb8b1f32132e58af066f3e4de56a4e05c55ea2f365a236cc513e5571930cd0173ddaebd6329a7d628cb5a137b0ac1b1b365f1d5672
7
+ data.tar.gz: cab09a1aa8f2338b24397fd2eb2252c4244b4890dd965581657fcd025197948a9290896e0fa519ab782719f25b34f292e17f44de3c15f13329b1fe964550162b
@@ -0,0 +1,7 @@
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
+ // README at: https://github.com/devcontainers/templates/tree/main/src/ruby
3
+ {
4
+ "name": "Ruby",
5
+ "image": "mcr.microsoft.com/devcontainers/ruby:1-3.2-bullseye",
6
+ "postCreateCommand": "bundle install"
7
+ }
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
+
1
2
  ## [Unreleased]
2
3
 
3
4
  ## [0.1.0] - 2023-07-25
4
5
 
5
6
  - Initial release
7
+
8
+ ## [0.1.1] - 2023-08-02
9
+
10
+ - Added `examine` to list the merge fields within a document
11
+
12
+ ## [0.1.2] - 2023-08-23
13
+
14
+ - Added untested support for headers and footers
15
+ - this work was done in a rush to deal with a customer requirement - tests to come soon
data/README.md CHANGED
@@ -13,25 +13,37 @@ It's pretty simple, so it probably won't work with complex Word documents, but i
13
13
 
14
14
  ## Usage
15
15
 
16
+ To list the merge fields within a document:
17
+
18
+ ```ruby
19
+ Consolidate::Docx::Merge.open "/path/to/docx" do |merge|
20
+ puts merge.examine
21
+ end and nil
22
+ ```
23
+ To perform a merge, replacing merge fields with supplied values:
24
+
16
25
  ```ruby
17
26
  Consolidate::Docx::Merge.open "/path/to/docx" do |merge|
18
27
  merge.data "Name" => "Alice Aadvark", "Company" => "TinyCo", "Job_Title" => "CEO"
19
28
  merge.write_to "/path/to/output.docx"
20
29
  end
21
-
22
30
  ```
23
31
 
24
32
  NOTE: The merge fields are case-sensitive - which is why they should be supplied as strings (using the older `{ "key" => "value" }` style ruby hash).
25
33
 
26
34
  ## Development
27
35
 
28
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+ The repo contains a .devcontainer folder - this contains instructions for a development container that has everything needed to build the project. Once the container has started, you can use `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
37
 
30
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+ `bundle exec rake install` will install the gem on your local machine (obviously not from within the devcontainer though). 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
39
 
32
40
  ## Contributing
33
41
 
34
- Bug reports and pull requests are welcome on GitHub at https://github.com/standard-procedure/standard-procedure-consolidate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/standard-procedure/standard-procedure-consolidate/blob/main/CODE_OF_CONDUCT.md).
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/standard-procedure/standard-procedure-consolidate.
43
+
44
+ When adding commit messages, please explain _why_ the change is being made.
45
+
46
+ When submitting a pull request, please ensure that there is an RSpec detailing how the feature works and please explain, in the pull request itself, the reasoning behind adding the feature.
35
47
 
36
48
  ## License
37
49
 
@@ -39,4 +51,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
39
51
 
40
52
  ## Code of Conduct
41
53
 
42
- Everyone interacting in the Standard::Procedure::Consolidate project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/standard-procedure-consolidate/blob/main/CODE_OF_CONDUCT.md).
54
+ Everyone interacting in the Standard::Procedure::Consolidate project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/standard-procedure/standard-procedure-consolidate/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1 @@
1
+ 055a4ccf5326b9f737e0222cecfdbaee34554fc42dcfaf017807e643fdc1ceb06b35329fe6a8ca32805c7ca3adf0c7df39b6bc32766208d69820243cde0a3648
@@ -10,23 +10,27 @@ module Consolidate
10
10
  new(path, force_settings: force_settings, &block)
11
11
  end
12
12
 
13
+ def examine
14
+ extract_field_names
15
+ end
16
+
13
17
  def data fields = {}
14
18
  fields = fields.transform_keys(&:to_s)
15
19
 
16
- xml = @zip.read("word/document.xml")
17
- doc = Nokogiri::XML(xml) { |x| x.noent }
18
-
19
- doc = substitute_style_one_with xml, doc, fields
20
- doc = substitute_style_two_with xml, doc, fields
20
+ @documents.each do |name, document|
21
+ result = document.dup
22
+ result = substitute_style_one_with result, fields
23
+ result = substitute_style_two_with result, fields
21
24
 
22
- @output["word/document.xml"] = doc.serialize save_with: 0
25
+ @output[name] = result.serialize save_with: 0
26
+ end
23
27
  end
24
28
 
25
29
  def write_to path
26
30
  Zip::File.open(path, Zip::File::CREATE) do |out|
27
- @zip.each do |entry|
31
+ zip.each do |entry|
28
32
  out.get_output_stream(entry.name) do |o|
29
- o.write(@output[entry.name] || @zip.read(entry.name))
33
+ o.write(output[entry.name] || zip.read(entry.name))
30
34
  end
31
35
  end
32
36
  end
@@ -34,33 +38,66 @@ module Consolidate
34
38
 
35
39
  protected
36
40
 
41
+ attr_reader :zip
42
+ attr_reader :xml
43
+ attr_reader :documents
44
+ attr_accessor :output
45
+
37
46
  def initialize(path, force_settings: true, &block)
38
47
  raise "No block given" unless block
39
48
  @output = {}
49
+ @documents = {}
40
50
  set_standard_settings if force_settings
41
51
  begin
42
52
  @zip = Zip::File.open(path)
43
- yield self
53
+ ["word/document.xml", "word/header1.xml", "word/footer1.xml"].each do |document|
54
+ next unless @zip.find_entry(document)
55
+ xml = @zip.read document
56
+ @documents[document] = Nokogiri::XML(xml) { |x| x.noent }
57
+ yield self
58
+ end
44
59
  ensure
45
60
  @zip.close
46
61
  end
47
62
  end
48
63
 
49
- def substitute_style_one_with xml, doc, fields
64
+ def extract_field_names
65
+ (extract_style_one + extract_style_two).uniq
66
+ end
67
+
68
+ def extract_style_one
69
+ documents.collect do |name, document|
70
+ (document / "//w:fldSimple").collect do |field|
71
+ value = field.attributes["instr"].value.strip
72
+ value.include?("MERGEFIELD") ? value.gsub("MERGEFIELD", "").strip : nil
73
+ end.compact
74
+ end.flatten
75
+ end
76
+
77
+ def extract_style_two
78
+ documents.collect do |name, document|
79
+ (document / "//w:instrText").collect do |instr|
80
+ value = instr.inner_text
81
+ value.include?("MERGEFIELD") ? value.gsub("MERGEFIELD", "").strip : nil
82
+ end.compact
83
+ end.flatten
84
+ end
85
+
86
+ def substitute_style_one_with document, fields
50
87
  # Word's first way of doing things
51
- (doc / "//w:fldSimple").each do |field|
88
+ (document / "//w:fldSimple").each do |field|
52
89
  if field.attributes["instr"].value =~ /MERGEFIELD (\S+)/
53
90
  text_node = (field / ".//w:t").first
54
91
  next unless text_node
55
92
  text_node.inner_html = fields[$1].to_s
56
93
  end
57
94
  end
58
- doc
95
+ document
59
96
  end
60
97
 
61
- def substitute_style_two_with xml, doc, fields
98
+ def substitute_style_two_with document, fields
62
99
  # Word's second way of doing things
63
- (doc / "//w:instrText").each do |instr|
100
+ (document / "//w:instrText").each do |instr|
64
101
  if instr.inner_text =~ /MERGEFIELD (\S+)/
65
102
  text_node = instr.parent.next_sibling.next_sibling.xpath(".//w:t").first
66
103
  text_node ||= instr.parent.next_sibling.next_sibling.next_sibling.xpath(".//w:t").first
@@ -68,16 +105,16 @@ module Consolidate
68
105
  text_node.inner_html = fields[$1].to_s
69
106
  end
70
107
  end
71
- doc
108
+ document
72
109
  end
73
110
 
74
111
  def set_standard_settings
75
- @output["word/settings.xml"] = %(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
112
+ output["word/settings.xml"] = %(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
76
113
  <w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main"><w:zoom w:percent="100"/></w:settings>)
77
114
  end
78
115
 
79
116
  def close
80
- @zip.close
117
+ zip.close
81
118
  end
82
119
  end
83
120
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consolidate
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard-procedure-consolidate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rahoul Baruah
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-25 00:00:00.000000000 Z
11
+ date: 2023-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -46,7 +46,9 @@ executables:
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - ".devcontainer/devcontainer.json"
49
50
  - ".rspec"
51
+ - ".ruby-version"
50
52
  - ".standard.yml"
51
53
  - CHANGELOG.md
52
54
  - CODE_OF_CONDUCT.md
@@ -54,6 +56,7 @@ files:
54
56
  - README.md
55
57
  - Rakefile
56
58
  - checksums/standard-procedure-consolidate-0.1.0.gem.sha512
59
+ - checksums/standard-procedure-consolidate-0.1.2.gem.sha512
57
60
  - exe/consolidate
58
61
  - lib/consolidate.rb
59
62
  - lib/consolidate/docx/merge.rb
@@ -68,7 +71,7 @@ metadata:
68
71
  homepage_uri: https://github.com/standard-procedure/standard-procedure-consolidate
69
72
  source_code_uri: https://github.com/standard-procedure/standard-procedure-consolidate
70
73
  changelog_uri: https://github.com/standard-procedure/standard-procedure-consolidate/blob/main/CHANGELOG.md
71
- post_install_message:
74
+ post_install_message:
72
75
  rdoc_options: []
73
76
  require_paths:
74
77
  - lib
@@ -83,8 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
86
  - !ruby/object:Gem::Version
84
87
  version: '0'
85
88
  requirements: []
86
- rubygems_version: 3.4.14
87
- signing_key:
89
+ rubygems_version: 3.4.10
90
+ signing_key:
88
91
  specification_version: 4
89
92
  summary: Simple ruby mailmerge for Microsoft Word .docx files.
90
93
  test_files: []