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 +4 -4
- data/.devcontainer/devcontainer.json +7 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +10 -0
- data/README.md +17 -5
- data/checksums/standard-procedure-consolidate-0.1.2.gem.sha512 +1 -0
- data/lib/consolidate/docx/merge.rb +54 -17
- data/lib/consolidate/version.rb +1 -1
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5647faddeab8829e8494e02b4b87acc33276e7f293fa147f3b15b1dd2a6d4c09
|
4
|
+
data.tar.gz: e867ef111010caa13eef6da80f11ccaa7ee0d780f82309a8f4d976bcff058545
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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.
|
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/
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
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
|
-
|
31
|
+
zip.each do |entry|
|
28
32
|
out.get_output_stream(entry.name) do |o|
|
29
|
-
o.write(
|
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
|
-
|
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
|
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
|
-
(
|
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
|
-
|
95
|
+
document
|
59
96
|
end
|
60
97
|
|
61
|
-
def substitute_style_two_with
|
98
|
+
def substitute_style_two_with document, fields
|
62
99
|
# Word's second way of doing things
|
63
|
-
(
|
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
|
-
|
108
|
+
document
|
72
109
|
end
|
73
110
|
|
74
111
|
def set_standard_settings
|
75
|
-
|
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
|
-
|
117
|
+
zip.close
|
81
118
|
end
|
82
119
|
end
|
83
120
|
end
|
data/lib/consolidate/version.rb
CHANGED
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.
|
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-
|
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.
|
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: []
|