rubyword 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -2
- data/CHANGELOG.md +44 -0
- data/README.cn.md +167 -0
- data/README.md +24 -3
- data/bin/run-example +11 -0
- data/doc/README.md +18 -1
- data/doc/doc-information.md +16 -0
- data/doc/footer.md +19 -0
- data/doc/header.md +11 -0
- data/doc/image.md +10 -0
- data/doc/link.md +10 -0
- data/doc/list.md +17 -0
- data/doc/paragraph.md +27 -0
- data/doc/table.md +21 -0
- data/doc/text.md +26 -1
- data/doc/title.md +14 -0
- data/doc/toc.md +11 -0
- data/example/doc_information.rb +13 -0
- data/example/footer.rb +5 -0
- data/example/header.rb +5 -0
- data/example/image.rb +7 -0
- data/example/link.rb +7 -0
- data/example/list.rb +11 -0
- data/example/paragraph.rb +17 -0
- data/example/result/.keep +0 -0
- data/example/result/doc-information.docx +0 -0
- data/example/result/footer.docx +0 -0
- data/example/result/header.docx +0 -0
- data/example/result/image.docx +0 -0
- data/example/result/link.docx +0 -0
- data/example/result/list.docx +0 -0
- data/example/result/paragraph.docx +0 -0
- data/example/result/table.docx +0 -0
- data/example/result/test.docx +0 -0
- data/example/result/text.docx +0 -0
- data/example/table.rb +18 -0
- data/example/test.rb +117 -0
- data/example/text.rb +9 -0
- data/example/toc.rb +24 -0
- data/lib/rubyword.rb +1 -0
- data/lib/rubyword/configuration.rb +1 -1
- data/lib/rubyword/document.rb +29 -41
- data/lib/rubyword/element/base.rb +12 -0
- data/lib/rubyword/element/link.rb +6 -5
- data/lib/rubyword/element/list.rb +4 -2
- data/lib/rubyword/element/paragraph.rb +38 -0
- data/lib/rubyword/element/section.rb +23 -10
- data/lib/rubyword/element/table.rb +60 -0
- data/lib/rubyword/element/text.rb +14 -79
- data/lib/rubyword/style.rb +4 -0
- data/lib/rubyword/version.rb +1 -1
- data/lib/rubyword/writer.rb +0 -4
- data/lib/rubyword/writer/part/document.rb +51 -44
- data/lib/rubyword/writer/part/footer.rb +1 -6
- data/lib/rubyword/writer/part/header.rb +2 -6
- data/lib/rubyword/writer/style/base.rb +0 -1
- data/lib/rubyword/writer/style/paragraph.rb +47 -0
- data/lib/rubyword/writer/style/section.rb +0 -1
- data/lib/rubyword/writer/style/word.rb +40 -0
- data/rubyword.gemspec +1 -2
- data/spec/rubyword/document_spec.rb +18 -3
- data/spec/rubyword/element/page_break_spec.rb +55 -0
- data/spec/rubyword/element/text_break_spec.rb +46 -0
- data/spec/rubyword/element/text_spec.rb +50 -0
- data/spec/spec_helper.rb +3 -30
- metadata +50 -20
- data/CHANGELOG.txt +0 -10
data/doc/table.md
ADDED
data/doc/text.md
CHANGED
@@ -1 +1,26 @@
|
|
1
|
-
#
|
1
|
+
# Text
|
2
|
+
|
3
|
+
## Style
|
4
|
+
+ font_size: Number | 16
|
5
|
+
+ color: Number | 996699
|
6
|
+
+ bgcolor: Number | 996699
|
7
|
+
+ underline: Boolean | true
|
8
|
+
+ blod: Boolean | true
|
9
|
+
+ all_caps: Boolean | false
|
10
|
+
+ italic: Boolean | false
|
11
|
+
+ text_align: 'left' || 'center' || 'right',
|
12
|
+
+ spacing: Number 200
|
13
|
+
+ indent_left: Number 100
|
14
|
+
+ indent_right: Number 200
|
15
|
+
+ indent_between: Number 200
|
16
|
+
|
17
|
+
## Example
|
18
|
+
```
|
19
|
+
Rubyword::Document::generate('hello.docx') {
|
20
|
+
section {
|
21
|
+
text "hello world"
|
22
|
+
# add some style
|
23
|
+
text "hello", { font_size: 62, color: '996699', blod: true, text_align: 'center' }
|
24
|
+
}
|
25
|
+
}
|
26
|
+
```
|
data/doc/title.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Title
|
2
|
+
|
3
|
+
## Options
|
4
|
+
+ ignore_dir: Boolean. Desc: Doc will ignore add this title to TOC
|
5
|
+
|
6
|
+
## Example
|
7
|
+
```
|
8
|
+
Rubyword::Document::generate('hello.docx') {
|
9
|
+
section {
|
10
|
+
title_1 "It's a title", ignore_dir: true
|
11
|
+
title_2 "It's a subtitle"
|
12
|
+
}
|
13
|
+
}
|
14
|
+
```
|
data/doc/toc.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'doc-information.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
information({
|
5
|
+
company: 'ruby word',
|
6
|
+
creator: 'young',
|
7
|
+
title: 'example word file',
|
8
|
+
description: 'this is a example docx',
|
9
|
+
subject: 'how to create doc info',
|
10
|
+
keywords: 'remark',
|
11
|
+
category: 'category'
|
12
|
+
})
|
13
|
+
}
|
data/example/footer.rb
ADDED
data/example/header.rb
ADDED
data/example/image.rb
ADDED
data/example/link.rb
ADDED
data/example/list.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'list.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
section {
|
5
|
+
list 'test1', 1, { font_size: 62, color: '996699', blod: true, text_align: 'center' }
|
6
|
+
list 'test1', 2
|
7
|
+
list 'test3', 2
|
8
|
+
list 'test2', 1
|
9
|
+
list 'test2', 1
|
10
|
+
}
|
11
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'paragraph.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
section {
|
5
|
+
p(text_align: 'center') {
|
6
|
+
text 'i am '
|
7
|
+
text 'a boy'
|
8
|
+
}
|
9
|
+
|
10
|
+
text 'welcome to my home..'
|
11
|
+
|
12
|
+
p {
|
13
|
+
text 'This is a '
|
14
|
+
text 'apple, yellow apple', bgcolor: 'yellow', text_align: 'center'
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
File without changes
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/example/table.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'table.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
section {
|
5
|
+
table {
|
6
|
+
tr {
|
7
|
+
th 'id'
|
8
|
+
th 'name'
|
9
|
+
th 'age'
|
10
|
+
}
|
11
|
+
tr {
|
12
|
+
th '1'
|
13
|
+
th 'young'
|
14
|
+
th '66'
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
data/example/test.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'test.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
# write the doc infomation
|
5
|
+
information({
|
6
|
+
company: 'ruby word',
|
7
|
+
creator: 'young',
|
8
|
+
title: 'example word file',
|
9
|
+
description: 'this is a example docx',
|
10
|
+
subject: 'how to create doc info',
|
11
|
+
keywords: 'remark',
|
12
|
+
category: 'category'
|
13
|
+
})
|
14
|
+
|
15
|
+
section {
|
16
|
+
text 'hello'
|
17
|
+
p(text_align: 'center') {
|
18
|
+
text 'i am '
|
19
|
+
text 'a boy'
|
20
|
+
}
|
21
|
+
text 'welcome to my home..'
|
22
|
+
|
23
|
+
p {
|
24
|
+
text 'This is a '
|
25
|
+
text 'apple, yellow apple', bgcolor: 'yellow', text_align: 'center'
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
# Generate the directory structure
|
30
|
+
title_directory font_size: 24
|
31
|
+
|
32
|
+
#insert header
|
33
|
+
add_header 'rubyword'
|
34
|
+
|
35
|
+
# insert footer with number
|
36
|
+
add_footer nil, text_align: 'center', nums_type: 'number'
|
37
|
+
# insert text
|
38
|
+
add_footer 'hello', text_align: 'center'
|
39
|
+
# # initialize section and insert something in the section
|
40
|
+
section {
|
41
|
+
# insert title
|
42
|
+
title_1 "It's a title", ignore_dir: true
|
43
|
+
# insert subtitle
|
44
|
+
title_2 "It's a subtitle"
|
45
|
+
# insert title
|
46
|
+
title_1 'Database'
|
47
|
+
# insert subtitle
|
48
|
+
title_2 'MySQL'
|
49
|
+
# insert No.3 title
|
50
|
+
title_3 'NoSQL'
|
51
|
+
# text break
|
52
|
+
text_break 3
|
53
|
+
# insert text
|
54
|
+
text 'hello word', bgcolor: 'yellow', text_align: 'center'
|
55
|
+
# page break
|
56
|
+
page_break 2
|
57
|
+
# insert text
|
58
|
+
text 'hello word', indent_between: '1440-1440'
|
59
|
+
text 'title', { font_size: 62, color: '996699', blod: true, text_align: 'center' }
|
60
|
+
|
61
|
+
# insert title
|
62
|
+
title_1 'section2 title'
|
63
|
+
title_2 'section2 title'
|
64
|
+
title_3 'section2 title'
|
65
|
+
|
66
|
+
# add a link
|
67
|
+
link 'baidu', 'http://www.baidu.com', text_align: 'center'
|
68
|
+
}
|
69
|
+
|
70
|
+
section {
|
71
|
+
# insert a text
|
72
|
+
text 'another Section', bgcolor: 'yellow', text_align: 'center'
|
73
|
+
|
74
|
+
# insert a text
|
75
|
+
text 'hello word', indent_between: '1440-1440'
|
76
|
+
text 'title', { font_size: 62, color: '996699', blod: true, text_align: 'center' }
|
77
|
+
}
|
78
|
+
|
79
|
+
section {
|
80
|
+
list 'test1', 1, { font_size: 62, color: '996699', blod: true, text_align: 'center' }
|
81
|
+
list 'test1', 2
|
82
|
+
list 'test3', 2
|
83
|
+
list 'test2', 1
|
84
|
+
list 'test2', 1
|
85
|
+
}
|
86
|
+
|
87
|
+
section {
|
88
|
+
# add a link
|
89
|
+
link 'baidu', 'http://www.baidu.com', text_align: 'center'
|
90
|
+
image 'http://www.baidu.com/img/bd_logo1.png'
|
91
|
+
}
|
92
|
+
|
93
|
+
section {
|
94
|
+
title_1 'section1 title'
|
95
|
+
title_2 'section2 title'
|
96
|
+
title_3 'section3 title'
|
97
|
+
}
|
98
|
+
|
99
|
+
section {
|
100
|
+
table {
|
101
|
+
tr {
|
102
|
+
th 'h0'
|
103
|
+
th 'h1'
|
104
|
+
th 'h2'
|
105
|
+
th 'h3'
|
106
|
+
th 'h4'
|
107
|
+
}
|
108
|
+
tr {
|
109
|
+
th 'b1'
|
110
|
+
th 'b2'
|
111
|
+
th 'b3'
|
112
|
+
th 'b4'
|
113
|
+
}
|
114
|
+
}
|
115
|
+
text 'aa'
|
116
|
+
}
|
117
|
+
}
|
data/example/text.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'text.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
section {
|
5
|
+
text 'another Section', bgcolor: 'yellow', text_align: 'center'
|
6
|
+
text 'hello word', indent_between: '1440-1440'
|
7
|
+
text 'title', { font_size: 62, color: '996699', blod: true, text_align: 'center' }
|
8
|
+
}
|
9
|
+
}
|
data/example/toc.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative '../lib/rubyword'
|
2
|
+
filename = File.join(Rubyword::TEMP_PATH, 'test.docx')
|
3
|
+
Rubyword::Document::generate(filename) {
|
4
|
+
# Generate the directory structure
|
5
|
+
title_directory font_size: 24
|
6
|
+
section {
|
7
|
+
# insert title
|
8
|
+
title_1 "It's a title", ignore_dir: true
|
9
|
+
# insert subtitle
|
10
|
+
title_2 "It's a subtitle"
|
11
|
+
# insert title
|
12
|
+
title_1 'Database'
|
13
|
+
# insert subtitle
|
14
|
+
title_2 'MySQL'
|
15
|
+
# insert No.3 title
|
16
|
+
title_3 'NoSQL'
|
17
|
+
}
|
18
|
+
|
19
|
+
section {
|
20
|
+
title_1 'section1 title'
|
21
|
+
title_2 'section2 title'
|
22
|
+
title_3 'section3 title'
|
23
|
+
}
|
24
|
+
}
|
data/lib/rubyword.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
module Rubyword
|
3
|
-
TEMP_PATH = File.join(File.expand_path('../../../', __FILE__), '
|
3
|
+
TEMP_PATH = File.join(File.expand_path('../../../', __FILE__), 'example/result')
|
4
4
|
WORD_TEMP_PATH = File.join(File.expand_path('../../../', __FILE__), 'template')
|
5
5
|
end
|
data/lib/rubyword/document.rb
CHANGED
@@ -11,6 +11,8 @@ module Rubyword
|
|
11
11
|
|
12
12
|
def self.generate(filename, options = {}, &block)
|
13
13
|
rubyword = new(options, &block)
|
14
|
+
|
15
|
+
# module Write to create word document
|
14
16
|
rubyword.save(filename)
|
15
17
|
end
|
16
18
|
|
@@ -27,54 +29,40 @@ module Rubyword
|
|
27
29
|
@section = Element::Section.new(@sections.count + 1, style, self)
|
28
30
|
self.sections << @section
|
29
31
|
@section.instance_eval(&block) if block_given?
|
32
|
+
@section
|
33
|
+
end
|
34
|
+
|
35
|
+
# initialize doc information
|
36
|
+
def information(options={})
|
37
|
+
@doc_info = options
|
30
38
|
end
|
31
39
|
|
40
|
+
# generate TOC
|
32
41
|
def title_directory(option= {})
|
33
42
|
@toc.merge!(open: true)
|
34
43
|
@toc.merge!(option)
|
35
44
|
end
|
36
45
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
@footer = {
|
58
|
-
id: 1,
|
59
|
-
text: text,
|
60
|
-
rid: self.init_rid,
|
61
|
-
text_align: options[:align] || 'center',
|
62
|
-
nums_type: options[:nums_type],
|
63
|
-
type: 'footer'
|
64
|
-
}
|
65
|
-
self.content_types << {
|
66
|
-
"/word/footer1.xml" => "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"
|
67
|
-
}
|
68
|
-
self.rels_documents << {
|
69
|
-
Id: "rId#{self.init_rid}",
|
70
|
-
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
|
71
|
-
Target: "footer1.xml"
|
72
|
-
}
|
73
|
-
self.init_rid = self.init_rid + 1
|
74
|
-
end
|
75
|
-
|
76
|
-
def information(options={})
|
77
|
-
@doc_info = options
|
46
|
+
# add header and footer
|
47
|
+
['header', 'footer'].each do |method_name|
|
48
|
+
define_method "add_#{method_name}" do |text, options={}|
|
49
|
+
instance_variable_set("@#{method_name}", {
|
50
|
+
id: 1,
|
51
|
+
text: text,
|
52
|
+
rid: self.init_rid,
|
53
|
+
type: method_name,
|
54
|
+
style: options
|
55
|
+
})
|
56
|
+
self.content_types << {
|
57
|
+
"/word/#{method_name}1.xml" => "application/vnd.openxmlformats-officedocument.wordprocessingml.#{method_name}+xml"
|
58
|
+
}
|
59
|
+
self.rels_documents << {
|
60
|
+
Id: "rId#{self.init_rid}",
|
61
|
+
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/#{method_name}",
|
62
|
+
Target: "#{method_name}1.xml"
|
63
|
+
}
|
64
|
+
self.init_rid = self.init_rid + 1
|
65
|
+
end
|
78
66
|
end
|
79
67
|
|
80
68
|
end
|