standard-procedure-consolidate 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +9 -1
- data/lib/consolidate/docx/merge.rb +44 -16
- data/lib/consolidate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f240fafdaf4fea72b63ba38b70c0e49736bb46a3f1f793ed79030f0f184d7760
|
4
|
+
data.tar.gz: '08930023d6b6865933a65a1cbefcd026d3a33b56323f702b5b46c9eedb76a210'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b205661ee1cd85599254559477ef73809086a9c232036c34a090b706437a01f46a947def4dfe3a90c12b50a0a589dcac671c1b5c5f1d9cea69bfadf160de5b02
|
7
|
+
data.tar.gz: 917b84c6116e50936b1de0a31605c345d8ec4ac48c466bd97afec9f937ebe81b67f0b56f9c5a75fda735d71f496382ef714d7b5e9012878b2f1af4223833a8d0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -13,12 +13,20 @@ 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).
|
@@ -10,23 +10,25 @@ 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
|
-
doc = substitute_style_one_with xml, doc, fields
|
20
|
-
doc = substitute_style_two_with xml, doc, fields
|
20
|
+
result = doc.dup
|
21
|
+
result = substitute_style_one_with result, fields
|
22
|
+
result = substitute_style_two_with result, fields
|
21
23
|
|
22
|
-
@output["word/document.xml"] =
|
24
|
+
@output["word/document.xml"] = result.serialize save_with: 0
|
23
25
|
end
|
24
26
|
|
25
27
|
def write_to path
|
26
28
|
Zip::File.open(path, Zip::File::CREATE) do |out|
|
27
|
-
|
29
|
+
zip.each do |entry|
|
28
30
|
out.get_output_stream(entry.name) do |o|
|
29
|
-
o.write(
|
31
|
+
o.write(output[entry.name] || zip.read(entry.name))
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
@@ -34,33 +36,59 @@ module Consolidate
|
|
34
36
|
|
35
37
|
protected
|
36
38
|
|
39
|
+
attr_reader :zip
|
40
|
+
attr_reader :xml
|
41
|
+
attr_reader :doc
|
42
|
+
attr_accessor :output
|
43
|
+
|
37
44
|
def initialize(path, force_settings: true, &block)
|
38
45
|
raise "No block given" unless block
|
39
46
|
@output = {}
|
40
47
|
set_standard_settings if force_settings
|
41
48
|
begin
|
42
49
|
@zip = Zip::File.open(path)
|
50
|
+
@xml = @zip.read("word/document.xml")
|
51
|
+
@doc = Nokogiri::XML(xml) { |x| x.noent }
|
52
|
+
|
43
53
|
yield self
|
44
54
|
ensure
|
45
55
|
@zip.close
|
46
56
|
end
|
47
57
|
end
|
48
58
|
|
49
|
-
def
|
59
|
+
def extract_field_names
|
60
|
+
(extract_style_one + extract_style_two).uniq
|
61
|
+
end
|
62
|
+
|
63
|
+
def extract_style_one
|
64
|
+
(doc / "//w:fldSimple").collect do |field|
|
65
|
+
value = field.attributes["instr"].value.strip
|
66
|
+
value.include?("MERGEFIELD") ? value.gsub("MERGEFIELD", "").strip : nil
|
67
|
+
end.compact
|
68
|
+
end
|
69
|
+
|
70
|
+
def extract_style_two
|
71
|
+
(doc / "//w:instrText").collect do |instr|
|
72
|
+
value = instr.inner_text
|
73
|
+
value.include?("MERGEFIELD") ? value.gsub("MERGEFIELD", "").strip : nil
|
74
|
+
end.compact
|
75
|
+
end
|
76
|
+
|
77
|
+
def substitute_style_one_with document, fields
|
50
78
|
# Word's first way of doing things
|
51
|
-
(
|
79
|
+
(document / "//w:fldSimple").each do |field|
|
52
80
|
if field.attributes["instr"].value =~ /MERGEFIELD (\S+)/
|
53
81
|
text_node = (field / ".//w:t").first
|
54
82
|
next unless text_node
|
55
83
|
text_node.inner_html = fields[$1].to_s
|
56
84
|
end
|
57
85
|
end
|
58
|
-
|
86
|
+
document
|
59
87
|
end
|
60
88
|
|
61
|
-
def substitute_style_two_with
|
89
|
+
def substitute_style_two_with document, fields
|
62
90
|
# Word's second way of doing things
|
63
|
-
(
|
91
|
+
(document / "//w:instrText").each do |instr|
|
64
92
|
if instr.inner_text =~ /MERGEFIELD (\S+)/
|
65
93
|
text_node = instr.parent.next_sibling.next_sibling.xpath(".//w:t").first
|
66
94
|
text_node ||= instr.parent.next_sibling.next_sibling.next_sibling.xpath(".//w:t").first
|
@@ -68,16 +96,16 @@ module Consolidate
|
|
68
96
|
text_node.inner_html = fields[$1].to_s
|
69
97
|
end
|
70
98
|
end
|
71
|
-
|
99
|
+
document
|
72
100
|
end
|
73
101
|
|
74
102
|
def set_standard_settings
|
75
|
-
|
103
|
+
output["word/settings.xml"] = %(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
76
104
|
<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
105
|
end
|
78
106
|
|
79
107
|
def close
|
80
|
-
|
108
|
+
zip.close
|
81
109
|
end
|
82
110
|
end
|
83
111
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rahoul Baruah
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|