docstache 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/changelog.md +4 -1
- data/lib/docstache/document.rb +61 -16
- data/lib/docstache/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64b050be7b5b3314f3970e3cf195dd436c18bbb8
|
4
|
+
data.tar.gz: ca44b9c1d7f1e2881edb09cac633a759b538412d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c363eed61fd680c394a7fd47db76f2f1a134d1606bddc2645be3ca36ec09211689b6baf6318b7dcd6a0f676817d0d8e2cbee5b209560e1dc8f363f0dd495fda1
|
7
|
+
data.tar.gz: 2369c45c26d8e3613277e164d76c9ff17554aaa1f37c8b9251364db393565c69897e2889c29d0332d47a886482901edc41723657d33159c74b2a543a20de3aa0
|
data/changelog.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.3.0
|
4
|
+
* [???] Added ability to do tag interpolation in headers and footers. The header/footer used is the header or footer from the first document passed into `Document.new()`
|
5
|
+
|
3
6
|
## 0.2.1
|
4
|
-
* [25b4c2e9e3dcb19b6fe9a255fec2c2bfd92029a6] Fixed a bug where a tag with regular expression characters could not be fixed with the `fix_errors` method
|
7
|
+
* [25b4c2e9e3dcb19b6fe9a255fec2c2bfd92029a6] Fixed a bug where a tag with regular expression characters could not be fixed with the `fix_errors` method
|
5
8
|
|
6
9
|
## 0.2.0
|
7
10
|
|
data/lib/docstache/document.rb
CHANGED
@@ -4,6 +4,7 @@ module Docstache
|
|
4
4
|
raise ArgumentError if paths.empty?
|
5
5
|
@path = paths.shift
|
6
6
|
@zip_file = Zip::File.open(@path)
|
7
|
+
load_references
|
7
8
|
@document = Nokogiri::XML(unzip_read(@zip_file, "word/document.xml"))
|
8
9
|
zip_files = paths.map{|p| Zip::File.open(p)}
|
9
10
|
documents = zip_files.map{|f| Nokogiri::XML(unzip_read(f, "word/document.xml"))}
|
@@ -11,16 +12,21 @@ module Docstache
|
|
11
12
|
@document.css('w|p').last.add_next_sibling(page_break)
|
12
13
|
@document.css('w|p').last.add_next_sibling(doc.css('w|body > *:not(w|sectPr)'))
|
13
14
|
end
|
15
|
+
find_documents_to_interpolate
|
14
16
|
end
|
15
17
|
|
16
18
|
def tags
|
17
|
-
@
|
19
|
+
@documents.values.flat_map { |document|
|
20
|
+
document.text.gsub(/\s+/, '').scan(/\{\{.+?\}\}/)
|
21
|
+
}
|
18
22
|
end
|
19
23
|
|
20
24
|
def usable_tags
|
21
|
-
@
|
22
|
-
tag.text
|
23
|
-
|
25
|
+
@documents.values.flat_map { |document|
|
26
|
+
document.css('w|t').select { |tag| tag.text =~ /\{\{.+?\}\}/ }.flat_map { |tag|
|
27
|
+
tag.text.scan(/\{\{.+?\}\}/)
|
28
|
+
}
|
29
|
+
}
|
24
30
|
end
|
25
31
|
|
26
32
|
def fix_errors
|
@@ -34,19 +40,27 @@ module Docstache
|
|
34
40
|
end
|
35
41
|
|
36
42
|
def save
|
37
|
-
buffer = zip_buffer(@
|
43
|
+
buffer = zip_buffer(@documents)
|
38
44
|
File.open(@path, "w") {|f| f.write buffer.string}
|
39
45
|
end
|
40
46
|
|
41
47
|
def render_file(output, data={})
|
42
|
-
|
43
|
-
|
48
|
+
rendered_documents = Hash[
|
49
|
+
@documents.map { |(path, document)|
|
50
|
+
[path, Docstache::Renderer.new(document.dup, data).render]
|
51
|
+
}
|
52
|
+
]
|
53
|
+
buffer = zip_buffer(rendered_documents)
|
44
54
|
File.open(output, "w") {|f| f.write buffer.string}
|
45
55
|
end
|
46
56
|
|
47
57
|
def render_stream(data={})
|
48
|
-
|
49
|
-
|
58
|
+
rendered_documents = Hash[
|
59
|
+
@documents.map { |(path, document)|
|
60
|
+
[path, Docstache::Renderer.new(document.dup, data).render]
|
61
|
+
}
|
62
|
+
]
|
63
|
+
buffer = zip_buffer(rendered_documents)
|
50
64
|
buffer.rewind
|
51
65
|
return buffer.sysread
|
52
66
|
end
|
@@ -54,10 +68,16 @@ module Docstache
|
|
54
68
|
private
|
55
69
|
|
56
70
|
def problem_paragraphs
|
57
|
-
missing_tags = tags
|
58
|
-
|
59
|
-
|
71
|
+
missing_tags = tags
|
72
|
+
usable_tags.each do |usable_tag|
|
73
|
+
index = missing_tags.index(usable_tag)
|
74
|
+
missing_tags.delete_at(index) if index
|
60
75
|
end
|
76
|
+
missing_tags.flat_map { |tag|
|
77
|
+
@documents.values.inject([]) { |tags, document|
|
78
|
+
tags + document.css('w|p').select {|t| t.text =~ /#{Regexp.escape(tag)}/}
|
79
|
+
}
|
80
|
+
}
|
61
81
|
end
|
62
82
|
|
63
83
|
def flatten_paragraph(p)
|
@@ -78,16 +98,18 @@ module Docstache
|
|
78
98
|
return contents
|
79
99
|
end
|
80
100
|
|
81
|
-
def zip_buffer(
|
101
|
+
def zip_buffer(documents)
|
82
102
|
buffer = Zip::OutputStream.write_buffer do |out|
|
83
103
|
@zip_file.entries.each do |e|
|
84
|
-
unless
|
104
|
+
unless documents.keys.include?(e.name)
|
85
105
|
out.put_next_entry(e.name)
|
86
106
|
out.write(e.get_input_stream.read)
|
87
107
|
end
|
88
108
|
end
|
89
|
-
|
90
|
-
|
109
|
+
documents.each do |path, document|
|
110
|
+
out.put_next_entry(path)
|
111
|
+
out.write(document.to_xml(indent: 0).gsub("\n", ""))
|
112
|
+
end
|
91
113
|
end
|
92
114
|
return buffer
|
93
115
|
end
|
@@ -102,5 +124,28 @@ module Docstache
|
|
102
124
|
br['w:type'] = "page"
|
103
125
|
return p
|
104
126
|
end
|
127
|
+
|
128
|
+
def load_references
|
129
|
+
@references = {}
|
130
|
+
ref_xml = Nokogiri::XML(unzip_read(@zip_file, "word/_rels/document.xml.rels"))
|
131
|
+
ref_xml.css("Relationship").each do |ref|
|
132
|
+
id = ref.attributes["Id"].value
|
133
|
+
@references[id] = {
|
134
|
+
id: id,
|
135
|
+
type: ref.attributes["Type"].value.split("/")[-1].to_sym,
|
136
|
+
target: ref.attributes["Target"].value
|
137
|
+
}
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def find_documents_to_interpolate
|
142
|
+
@documents = {"word/document.xml" => @document}
|
143
|
+
@document.css("w|headerReference, w|footerReference").each do |header_ref|
|
144
|
+
if @references.has_key?(header_ref.attributes["id"].value)
|
145
|
+
ref = @references[header_ref.attributes["id"].value]
|
146
|
+
@documents["word/#{ref[:target]}"] = Nokogiri::XML(unzip_read(@zip_file, "word/#{ref[:target]}"))
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
105
150
|
end
|
106
151
|
end
|
data/lib/docstache/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docstache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Will Cosgrove
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|