standard-procedure-consolidate 0.1.2 → 0.1.4

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: 5647faddeab8829e8494e02b4b87acc33276e7f293fa147f3b15b1dd2a6d4c09
4
- data.tar.gz: e867ef111010caa13eef6da80f11ccaa7ee0d780f82309a8f4d976bcff058545
3
+ metadata.gz: f45c16dc8e6f37f91043673804db8076030a804ab6c08fa1139f4d86cdf54b79
4
+ data.tar.gz: 7c77802d6b578ac50b9f9c8b5bad9ff097918fab5644eb3c771b2baf98451a36
5
5
  SHA512:
6
- metadata.gz: 3f7e9ebff0ec1706b59983eb8b1f32132e58af066f3e4de56a4e05c55ea2f365a236cc513e5571930cd0173ddaebd6329a7d628cb5a137b0ac1b1b365f1d5672
7
- data.tar.gz: cab09a1aa8f2338b24397fd2eb2252c4244b4890dd965581657fcd025197948a9290896e0fa519ab782719f25b34f292e17f44de3c15f13329b1fe964550162b
6
+ metadata.gz: 2ed822b460867d75395fdb9253ef3501619781d3ab31acaca7db4daf699658f8253d4a063336bcc2321bf6dd08f0207e91600172f8ec84e4263af055ee9107a7
7
+ data.tar.gz: 37a8f85beb0b9c65ba76bf9457edcb9e141d1e87aca1f65c01b5a4e43f45f143c66695d5922eb3ffafca43625f9e3745db8fd2e3c516dd6ffb73172a8c9437ac
@@ -0,0 +1,3 @@
1
+ {
2
+
3
+ }
data/CHANGELOG.md CHANGED
@@ -1,15 +1,27 @@
1
+ ## [0.1.4] - 2023-09-11
1
2
 
2
- ## [Unreleased]
3
+ Updated which files get exclusions after crashes in production with some client files
3
4
 
4
- ## [0.1.0] - 2023-07-25
5
+ ## [0.1.3] - 2023-09-07
5
6
 
6
- - Initial release
7
+ Customer had an issue where some fields were not merging correctly
8
+
9
+ - Altered the code so it tries to perform the substitution in all .xml files that aren't obviously configuration data
10
+ - Added verbose option to show what the code was doing
11
+ - Added a list of files to `examine`
12
+ - Still no tests for the header/footer support
13
+
14
+ ## [0.1.2] - 2023-08-23
15
+
16
+ - Added untested support for headers and footers
17
+ - this work was done in a rush to deal with a customer requirement - tests to come soon
7
18
 
8
19
  ## [0.1.1] - 2023-08-02
9
20
 
10
21
  - Added `examine` to list the merge fields within a document
11
22
 
12
- ## [0.1.2] - 2023-08-23
23
+ ## [0.1.0] - 2023-07-25
13
24
 
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
25
+ - Initial release
26
+
27
+ ## [Unreleased]
data/README.md CHANGED
@@ -17,7 +17,7 @@ To list the merge fields within a document:
17
17
 
18
18
  ```ruby
19
19
  Consolidate::Docx::Merge.open "/path/to/docx" do |merge|
20
- puts merge.examine
20
+ merge.examine
21
21
  end and nil
22
22
  ```
23
23
  To perform a merge, replacing merge fields with supplied values:
@@ -31,6 +31,18 @@ end
31
31
 
32
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).
33
33
 
34
+ If your merge isn't working, you can pass `verbose: true` to `open` and it will list the internal .xml documents it finds, the fields it finds within those .xml documents and the substitutions it is trying to perform.
35
+
36
+ ## How it works
37
+
38
+ This is a bit sketchy and pieced together from the [code I found]((https://gist.github.com/ericmason/7200448)) and various bits of skimming through the published file format.
39
+
40
+ A .docx file (unlike the previous .doc file), is actually a .zip file that contains a number of .xml files. The contents of the document are stored in these .xml files, along with configuration information and stuff like fonts and styles.
41
+
42
+ When setting up a merge field, Word adds some special tags into the .xml file. There appear to be two different versions of how it does this - using `w:fldSimple` or `w:instrText` tags. Consolidate goes through each .xml file within the document (ignoring some which are configuration related) and looks for these two types of tag.
43
+
44
+ The `data` method uses the hash of `field: value` data you supply, copies the .xml files and performs a straight text substitution on any matching merge fields. Then `write_to`
45
+
34
46
  ## Development
35
47
 
36
48
  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.
@@ -0,0 +1 @@
1
+ fcf685ae67c99908b9d67a57e9bd4bfe6dc8f61c3e421e167d0c9912ab6ac32fd4ad9a273cbbee47e9157cafb311aaecb8db0995eb227aa0afe0bdcc3bc01ee3
@@ -0,0 +1 @@
1
+ da681c73463215031518b2606899503d3217ab92e1877bd8f966e5dc3ebc971ae686958b78da821feb24374f84d179e86cdaf29dbb90f0bacb0a7cbde8e66c6b
@@ -6,12 +6,14 @@ require "nokogiri"
6
6
  module Consolidate
7
7
  module Docx
8
8
  class Merge
9
- def self.open(path, force_settings: true, &block)
10
- new(path, force_settings: force_settings, &block)
9
+ def self.open(path, verbose: false, &block)
10
+ new(path, verbose: verbose, &block)
11
+ path
11
12
  end
12
13
 
13
14
  def examine
14
- extract_field_names
15
+ puts "Documents: #{extract_document_names}"
16
+ puts "Merge fields: #{extract_field_names}"
15
17
  end
16
18
 
17
19
  def data fields = {}
@@ -38,37 +40,44 @@ module Consolidate
38
40
 
39
41
  protected
40
42
 
43
+ attr_reader :verbose
41
44
  attr_reader :zip
42
45
  attr_reader :xml
43
46
  attr_reader :documents
44
47
  attr_accessor :output
45
48
 
46
- def initialize(path, force_settings: true, &block)
49
+ def initialize(path, verbose: false, &block)
47
50
  raise "No block given" unless block
51
+ @verbose = verbose
48
52
  @output = {}
49
53
  @documents = {}
50
- set_standard_settings if force_settings
51
54
  begin
52
55
  @zip = Zip::File.open(path)
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
56
+ @zip.entries.each do |entry|
57
+ next unless entry.name =~ /word\/(document|header|footer|footnotes|endnotes).?\.xml/
58
+ puts "Reading #{entry.name}" if verbose
59
+ xml = @zip.get_input_stream entry
60
+ @documents[entry.name] = Nokogiri::XML(xml) { |x| x.noent }
58
61
  end
62
+ yield self
59
63
  ensure
60
64
  @zip.close
61
65
  end
62
66
  end
63
67
 
68
+ def extract_document_names
69
+ @zip.entries.collect { |entry| entry.name }.join(", ")
70
+ end
71
+
64
72
  def extract_field_names
65
- (extract_style_one + extract_style_two).uniq
73
+ (extract_style_one + extract_style_two).uniq.join(", ")
66
74
  end
67
75
 
68
76
  def extract_style_one
69
77
  documents.collect do |name, document|
70
78
  (document / "//w:fldSimple").collect do |field|
71
79
  value = field.attributes["instr"].value.strip
80
+ puts "...found #{value} (v1) in #{name}" if verbose
72
81
  value.include?("MERGEFIELD") ? value.gsub("MERGEFIELD", "").strip : nil
73
82
  end.compact
74
83
  end.flatten
@@ -78,6 +87,7 @@ module Consolidate
78
87
  documents.collect do |name, document|
79
88
  (document / "//w:instrText").collect do |instr|
80
89
  value = instr.inner_text
90
+ puts "...found #{value} (v2) in #{name}" if verbose
81
91
  value.include?("MERGEFIELD") ? value.gsub("MERGEFIELD", "").strip : nil
82
92
  end.compact
83
93
  end.flatten
@@ -89,6 +99,7 @@ module Consolidate
89
99
  if field.attributes["instr"].value =~ /MERGEFIELD (\S+)/
90
100
  text_node = (field / ".//w:t").first
91
101
  next unless text_node
102
+ puts "...substituting v1 #{field.attributes["instr"]} with #{fields[$1]}" if verbose
92
103
  text_node.inner_html = fields[$1].to_s
93
104
  end
94
105
  end
@@ -102,17 +113,13 @@ module Consolidate
102
113
  text_node = instr.parent.next_sibling.next_sibling.xpath(".//w:t").first
103
114
  text_node ||= instr.parent.next_sibling.next_sibling.next_sibling.xpath(".//w:t").first
104
115
  next unless text_node
116
+ puts "...substituting v2 #{instr.inner_text} with #{fields[$1]}" if verbose
105
117
  text_node.inner_html = fields[$1].to_s
106
118
  end
107
119
  end
108
120
  document
109
121
  end
110
122
 
111
- def set_standard_settings
112
- output["word/settings.xml"] = %(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
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>)
114
- end
115
-
116
123
  def close
117
124
  zip.close
118
125
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Consolidate
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
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.2
4
+ version: 0.1.4
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-08-29 00:00:00.000000000 Z
11
+ date: 2023-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -47,6 +47,7 @@ extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
49
  - ".devcontainer/devcontainer.json"
50
+ - ".nova/Configuration.json"
50
51
  - ".rspec"
51
52
  - ".ruby-version"
52
53
  - ".standard.yml"
@@ -57,6 +58,8 @@ files:
57
58
  - Rakefile
58
59
  - checksums/standard-procedure-consolidate-0.1.0.gem.sha512
59
60
  - checksums/standard-procedure-consolidate-0.1.2.gem.sha512
61
+ - checksums/standard-procedure-consolidate-0.1.3.gem.sha512
62
+ - checksums/standard-procedure-consolidate-0.1.4.gem.sha512
60
63
  - exe/consolidate
61
64
  - lib/consolidate.rb
62
65
  - lib/consolidate/docx/merge.rb
@@ -71,7 +74,7 @@ metadata:
71
74
  homepage_uri: https://github.com/standard-procedure/standard-procedure-consolidate
72
75
  source_code_uri: https://github.com/standard-procedure/standard-procedure-consolidate
73
76
  changelog_uri: https://github.com/standard-procedure/standard-procedure-consolidate/blob/main/CHANGELOG.md
74
- post_install_message:
77
+ post_install_message:
75
78
  rdoc_options: []
76
79
  require_paths:
77
80
  - lib
@@ -87,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
90
  version: '0'
88
91
  requirements: []
89
92
  rubygems_version: 3.4.10
90
- signing_key:
93
+ signing_key:
91
94
  specification_version: 4
92
95
  summary: Simple ruby mailmerge for Microsoft Word .docx files.
93
96
  test_files: []