docx-cloner 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +21 -14
- data/docx-examples/test.rb +16 -3
- data/features/dsl.feature +59 -0
- data/features/read.feature +0 -1
- data/features/replace.feature +0 -1
- data/features/steps_define/steps.rb +30 -6
- data/lib/docx/cloner.rb +3 -3
- data/lib/docx/cloner/version.rb +1 -1
- data/lib/docx/dsl.rb +42 -0
- data/spec/cloner_spec.rb +5 -5
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OTc0OTQ0ZjIzMTFkNDAzZGU2ODUwZmM3MWM2ZDNlMjQyYTZlNTkwNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NDM0MjBmNTg4ZWUzZGVjNWNkNGI4OWYwNjIxODQ1MmY5Y2VmYWRkZg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OTY5ODY1Y2Y4ZDYwZDA0NzY5Zjg0NmQ5YzU2Zjk0Mjg3MTA5OGYzNmI3ZDVj
|
10
|
+
ZGUzM2Q0YzM1YzkxMDllMzg3MTFhYzRhZGVjYzU4Y2UzMmE4OWEyNDk2ZjIz
|
11
|
+
MjI5N2JiOTBhMWIxMDZkZWM5YWFhZWMyYWM5ZDNkZDM4ODE3ZGE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDQxYmQ0MTY5MWZhMDY3YjAzZGE3OWM5ODdlZDc0YWE2ZmE3ZTg2NzBkNTBh
|
14
|
+
ZmY2YzA2MzVkMWVjZjk3MTA0NTViY2E5MzZmNDE4YmFkMzIzZTcyZDUzNzQ0
|
15
|
+
NWU2OTA1MTljNDJjMDE1NWRiZjAxYWQ0N2U4MTYwYzAxNmYwMTQ=
|
data/README.md
CHANGED
@@ -17,22 +17,29 @@ Or install it yourself as:
|
|
17
17
|
$ gem install docx-cloner
|
18
18
|
|
19
19
|
## Usage
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
sourc_file = 'source.docx'
|
24
|
-
dest_file = 'dest.docx'
|
25
|
-
docx = Docx::Cloner::DocxTool.new sourc_file
|
26
|
-
|
27
|
-
docx.set_single_tag '{Name}', '周大福'
|
20
|
+
* docx_cloner
|
21
|
+
* set_text
|
22
|
+
* set_row_tr
|
28
23
|
|
29
|
-
|
30
|
-
table_data = [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
31
|
-
docx.set_row_tags table_title, table_data, 'tr'
|
32
|
-
|
33
|
-
docx.save dest_file
|
34
|
-
docx.release
|
24
|
+
it's so easy to use it with DSL:
|
35
25
|
|
26
|
+
#encoding: utf-8
|
27
|
+
require 'docx/dsl'
|
28
|
+
|
29
|
+
class C
|
30
|
+
include Docx::DSL
|
31
|
+
|
32
|
+
def my_method
|
33
|
+
#template is 'source.docx' and save to 'dest.docx'
|
34
|
+
docx_cloner 'source.docx', 'dest.docx' do
|
35
|
+
#replace the text of '{Name}' in 'source.docx'
|
36
|
+
set_text '{Name}', '周大福'
|
37
|
+
#multi lines replace
|
38
|
+
set_row_tr ["{名称1}", "{00.01}"], [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
36
43
|
|
37
44
|
## Contributing
|
38
45
|
|
data/docx-examples/test.rb
CHANGED
@@ -1,15 +1,28 @@
|
|
1
1
|
#encoding: utf-8
|
2
|
-
require 'docx/
|
2
|
+
require 'docx/dsl'
|
3
3
|
|
4
|
+
<<-ABC
|
4
5
|
sourc_file = 'source.docx'
|
5
6
|
dest_file = 'dest.docx'
|
6
7
|
docx = Docx::Cloner::DocxTool.new sourc_file
|
7
8
|
|
8
|
-
docx.
|
9
|
+
docx.set_text_tag '{Name}', '周大福'
|
9
10
|
|
10
11
|
table_title = ["{名称1}", "{00.01}"]
|
11
12
|
table_data = [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
12
13
|
docx.set_row_tags table_title, table_data, 'tr'
|
13
14
|
|
14
15
|
docx.save dest_file
|
15
|
-
docx.release
|
16
|
+
docx.release
|
17
|
+
|
18
|
+
ABC
|
19
|
+
##################
|
20
|
+
|
21
|
+
extend Docx::DSL
|
22
|
+
docx_cloner 'source.docx', 'dest.docx' do
|
23
|
+
set_text '{Name}', '周大福'
|
24
|
+
set_row 'tr' do |t|
|
25
|
+
t[:tags] = ["{名称1}", "{00.01}"]
|
26
|
+
t[:data] = [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#language: zh-CN
|
2
|
+
|
3
|
+
功能: 支持DSL用法
|
4
|
+
允许在ruby代码中使用类似的DSL语法来指定docx标签替换操作:
|
5
|
+
"""
|
6
|
+
extend 'Docx::DSL'
|
7
|
+
docx_cloner 'source.docx', 'dest.docx' do
|
8
|
+
set_text '{Name}', '周大福'
|
9
|
+
set_row 'tr' do |t|
|
10
|
+
t[:tags] = ["{名称1}", "{00.01}"]
|
11
|
+
t[:data] = [["自行车1", "125.00"], ["大卡车1", "256500.00"]]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
"""
|
15
|
+
其中:
|
16
|
+
1、需要用include或extend包含 'Docx::DSL'
|
17
|
+
2、docx_cloner,是指定过程容器,'source_docx'是模板文件,'dest.docx'是要保存的目标文件。
|
18
|
+
3、set_text,可以将独立的文字标签替换,如'{Name}'替换为'周大福'
|
19
|
+
4、set_row,可以按照一行内的标签模板,根据指定数据集生成多行文字,t[:tags]为标签数组,t[:data]为被替换的二维数组
|
20
|
+
|
21
|
+
背景: 被替换的源文件
|
22
|
+
假如"docx-examples"示例文件夹中存在一个"source.docx"的文件
|
23
|
+
而且"docx-examples/dest.docx"这个目标文件已经被清除
|
24
|
+
而且要求使用DSL语法
|
25
|
+
|
26
|
+
|
27
|
+
场景大纲: 1、使用DSL,简单替换单个文字标签
|
28
|
+
这是最简单的情形,例如将标签{name},替换为真正的姓名。
|
29
|
+
|
30
|
+
假如程序使用DSL语法将目标文件中的"<tagname>"替换为"<value>"
|
31
|
+
那么应该生成目标文件
|
32
|
+
而且被目标文件中应该包含"<value>"这个标签词
|
33
|
+
|
34
|
+
例子: 替换单个标签的几种情况
|
35
|
+
|
36
|
+
| tagname | value |
|
37
|
+
| {name} | 周大福 |
|
38
|
+
| {Name} | 周大福 |
|
39
|
+
| {NAME} | 周大福 |
|
40
|
+
| {{名字}} | 周大福 |
|
41
|
+
| $名字$ | 周大福 |
|
42
|
+
|
43
|
+
@wip
|
44
|
+
场景: 2、使用DSL,替换表格行数据
|
45
|
+
按行数据替换表格内容是常见的应用
|
46
|
+
|
47
|
+
假如有这样一组数据:
|
48
|
+
| {名称1} | {00.01} |
|
49
|
+
| 自行车 | 256.00 |
|
50
|
+
| 小汽车 | 125600.00 |
|
51
|
+
| 大卡车 | 256000.00 |
|
52
|
+
| 电视机 | 6999.00 |
|
53
|
+
| 洗衣机 | 3488.00 |
|
54
|
+
|
55
|
+
当程序使用DSL,将表中第1行作为标签名,第2行以后作为行数据替换
|
56
|
+
那么应该生成目标文件
|
57
|
+
而且被目标文件中应该包含被替换的第2行以后的数据
|
58
|
+
|
59
|
+
|
data/features/read.feature
CHANGED
data/features/replace.feature
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
lib = File.expand_path('../../../lib', __FILE__)
|
3
|
+
puts lib
|
3
4
|
require "#{lib}/docx/cloner"
|
4
5
|
#require 'fileutils'
|
6
|
+
require "#{lib}/docx/dsl"
|
7
|
+
|
5
8
|
|
6
9
|
假如(/^"(.*?)"示例文件夹中存在一个"(.*?)"的文件$/) do |folder, file|
|
7
10
|
@source_filename = File.expand_path "#{folder}/#{file}"
|
@@ -11,7 +14,7 @@ end
|
|
11
14
|
|
12
15
|
那么(/^程序应该能读到"(.*?)"这个标签词$/) do |tag_name|
|
13
16
|
docx = Docx::Cloner::DocxTool.new @source_filename
|
14
|
-
result = docx.
|
17
|
+
result = docx.include_text_tag? tag_name
|
15
18
|
docx.release
|
16
19
|
result.should be_true
|
17
20
|
end
|
@@ -25,7 +28,7 @@ end
|
|
25
28
|
|
26
29
|
假如(/^程序将目标文件中的"(.*?)"替换为"(.*?)"$/) do |tag, value|
|
27
30
|
docx = Docx::Cloner::DocxTool.new @source_filename
|
28
|
-
result = docx.
|
31
|
+
result = docx.set_text_tag tag, value
|
29
32
|
docx.save @dest_filename
|
30
33
|
docx.release
|
31
34
|
result.should be_true
|
@@ -37,7 +40,7 @@ end
|
|
37
40
|
|
38
41
|
而且(/^被目标文件中应该包含"(.*?)"这个标签词$/) do |value|
|
39
42
|
docx = Docx::Cloner::DocxTool.new @dest_filename
|
40
|
-
result = docx.
|
43
|
+
result = docx.include_text_tag? value
|
41
44
|
docx.release
|
42
45
|
result.should be_true
|
43
46
|
end
|
@@ -50,7 +53,7 @@ end
|
|
50
53
|
result = true
|
51
54
|
docx = Docx::Cloner::DocxTool.new @source_filename
|
52
55
|
@data.each do |row|
|
53
|
-
result &= docx.
|
56
|
+
result &= docx.set_text_tag row[0], row[1]
|
54
57
|
end
|
55
58
|
docx.save @dest_filename
|
56
59
|
docx.release
|
@@ -61,7 +64,7 @@ end
|
|
61
64
|
result = true
|
62
65
|
docx = Docx::Cloner::DocxTool.new @dest_filename
|
63
66
|
@data.each do |row|
|
64
|
-
result &= docx.
|
67
|
+
result &= docx.include_text_tag? row[1]
|
65
68
|
end
|
66
69
|
docx.release
|
67
70
|
result.should be_true
|
@@ -84,9 +87,30 @@ end
|
|
84
87
|
docx = Docx::Cloner::DocxTool.new @dest_filename
|
85
88
|
@data[1..-1].each do |row|
|
86
89
|
row.each do |value|
|
87
|
-
result &= docx.
|
90
|
+
result &= docx.include_text_tag? value
|
88
91
|
end
|
89
92
|
end
|
90
93
|
docx.release
|
91
94
|
result.should be_true
|
95
|
+
end
|
96
|
+
|
97
|
+
假如(/^要求使用DSL语法$/) do
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
假如(/^程序使用DSL语法将目标文件中的"(.*?)"替换为"(.*?)"$/) do |tag, value|
|
102
|
+
#定义一个DSL的执行环境
|
103
|
+
extend Docx::DSL
|
104
|
+
docx_cloner @source_filename, @dest_filename do
|
105
|
+
set_text tag, value
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
当(/^程序使用DSL,将表中第1行作为标签名,第2行以后作为行数据替换$/) do
|
110
|
+
#定义一个DSL的执行环境
|
111
|
+
extend Docx::DSL
|
112
|
+
docx_cloner @source_filename, @dest_filename do
|
113
|
+
#set_row @data.first, @data[1..-1], 'tr'
|
114
|
+
set_row_tr @data.first, @data[1..-1]
|
115
|
+
end
|
92
116
|
end
|
data/lib/docx/cloner.rb
CHANGED
@@ -40,7 +40,7 @@ module Docx
|
|
40
40
|
end
|
41
41
|
|
42
42
|
|
43
|
-
def
|
43
|
+
def include_text_tag?(tag)
|
44
44
|
@global_paragraph.each do |p|
|
45
45
|
if p[:text_content].include? tag
|
46
46
|
return true
|
@@ -49,7 +49,7 @@ module Docx
|
|
49
49
|
return false
|
50
50
|
end
|
51
51
|
|
52
|
-
def
|
52
|
+
def read_text_tag_xml(tag)
|
53
53
|
@global_paragraph.each do |p|
|
54
54
|
if p[:text_content].include? tag
|
55
55
|
from = p[:text_content].index tag
|
@@ -75,7 +75,7 @@ module Docx
|
|
75
75
|
end
|
76
76
|
|
77
77
|
#替换单个标签为指定值
|
78
|
-
def
|
78
|
+
def set_text_tag tag, value
|
79
79
|
replace_tag tag, value
|
80
80
|
end
|
81
81
|
|
data/lib/docx/cloner/version.rb
CHANGED
data/lib/docx/dsl.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require 'docx/cloner'
|
3
|
+
|
4
|
+
module Docx
|
5
|
+
module DSL
|
6
|
+
lambda{
|
7
|
+
docx = nil
|
8
|
+
define_method :set_text do |tag, value|
|
9
|
+
#puts "#set_text: #{tag}, #{value}"
|
10
|
+
docx.set_text_tag tag, value
|
11
|
+
end
|
12
|
+
|
13
|
+
define_method :set_row do |tags, data, type|
|
14
|
+
#puts "#set_row_#{type}"
|
15
|
+
#puts "tags: #{tags}"
|
16
|
+
#puts "data: #{data}"
|
17
|
+
docx.set_row_tags tags, data, type
|
18
|
+
end
|
19
|
+
|
20
|
+
define_method :docx_cloner do |source, dest, &b|
|
21
|
+
docx = Docx::Cloner::DocxTool.new source
|
22
|
+
#return unless block_given?
|
23
|
+
#puts 'start dsl'
|
24
|
+
b.call self
|
25
|
+
#puts 'end dsl'
|
26
|
+
|
27
|
+
docx.save dest
|
28
|
+
docx.release
|
29
|
+
end
|
30
|
+
}.call
|
31
|
+
|
32
|
+
def method_missing name, *args
|
33
|
+
puts "name: #{name}"
|
34
|
+
puts "args: #{args}"
|
35
|
+
/set_row_(.+)/.match name do
|
36
|
+
return set_row *(args << $1) if $1
|
37
|
+
end
|
38
|
+
super
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/spec/cloner_spec.rb
CHANGED
@@ -10,18 +10,18 @@ module Docx
|
|
10
10
|
end
|
11
11
|
context "#include_single_tag?" do
|
12
12
|
it "读取'$名字$'标签" do
|
13
|
-
result = @docx.
|
13
|
+
result = @docx.include_text_tag? "$名字$"
|
14
14
|
result.should be_true
|
15
15
|
end
|
16
16
|
it "读取'{Name}'标签" do
|
17
|
-
result = @docx.
|
17
|
+
result = @docx.include_text_tag? "{Name}"
|
18
18
|
result.should be_true
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
context "#read_single_tags_xml" do
|
23
23
|
it "读取'{name}'的xml标签'<w:r>',应该是" do
|
24
|
-
result = @docx.
|
24
|
+
result = @docx.read_text_tag_xml "{name}"
|
25
25
|
result.should == <<-HERE
|
26
26
|
<w:r w:rsidR="000F595B">
|
27
27
|
<w:rPr>
|
@@ -65,11 +65,11 @@ module Docx
|
|
65
65
|
context "#set_single_tag" do
|
66
66
|
it "设置单个标签{Name}" do
|
67
67
|
value = '周大福'
|
68
|
-
@source_docx.
|
68
|
+
@source_docx.set_text_tag '{Name}', value
|
69
69
|
@source_docx.save @dest_file
|
70
70
|
|
71
71
|
dest = DocxTool.new @dest_file
|
72
|
-
result = dest.
|
72
|
+
result = dest.include_text_tag? value
|
73
73
|
dest.release
|
74
74
|
result.should be_true
|
75
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx-cloner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- homeway
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -57,11 +57,13 @@ files:
|
|
57
57
|
- docx-examples/source.docx
|
58
58
|
- docx-examples/test.rb
|
59
59
|
- docx-examples/wp.xml
|
60
|
+
- features/dsl.feature
|
60
61
|
- features/read.feature
|
61
62
|
- features/replace.feature
|
62
63
|
- features/steps_define/steps.rb
|
63
64
|
- lib/docx/cloner.rb
|
64
65
|
- lib/docx/cloner/version.rb
|
66
|
+
- lib/docx/dsl.rb
|
65
67
|
- spec/cloner_spec.rb
|
66
68
|
- spec/spec_helper.rb
|
67
69
|
homepage: ''
|
@@ -89,6 +91,7 @@ signing_key:
|
|
89
91
|
specification_version: 4
|
90
92
|
summary: generate docx file with tags
|
91
93
|
test_files:
|
94
|
+
- features/dsl.feature
|
92
95
|
- features/read.feature
|
93
96
|
- features/replace.feature
|
94
97
|
- features/steps_define/steps.rb
|