rubyword 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -2
  3. data/CHANGELOG.md +44 -0
  4. data/README.cn.md +167 -0
  5. data/README.md +24 -3
  6. data/bin/run-example +11 -0
  7. data/doc/README.md +18 -1
  8. data/doc/doc-information.md +16 -0
  9. data/doc/footer.md +19 -0
  10. data/doc/header.md +11 -0
  11. data/doc/image.md +10 -0
  12. data/doc/link.md +10 -0
  13. data/doc/list.md +17 -0
  14. data/doc/paragraph.md +27 -0
  15. data/doc/table.md +21 -0
  16. data/doc/text.md +26 -1
  17. data/doc/title.md +14 -0
  18. data/doc/toc.md +11 -0
  19. data/example/doc_information.rb +13 -0
  20. data/example/footer.rb +5 -0
  21. data/example/header.rb +5 -0
  22. data/example/image.rb +7 -0
  23. data/example/link.rb +7 -0
  24. data/example/list.rb +11 -0
  25. data/example/paragraph.rb +17 -0
  26. data/example/result/.keep +0 -0
  27. data/example/result/doc-information.docx +0 -0
  28. data/example/result/footer.docx +0 -0
  29. data/example/result/header.docx +0 -0
  30. data/example/result/image.docx +0 -0
  31. data/example/result/link.docx +0 -0
  32. data/example/result/list.docx +0 -0
  33. data/example/result/paragraph.docx +0 -0
  34. data/example/result/table.docx +0 -0
  35. data/example/result/test.docx +0 -0
  36. data/example/result/text.docx +0 -0
  37. data/example/table.rb +18 -0
  38. data/example/test.rb +117 -0
  39. data/example/text.rb +9 -0
  40. data/example/toc.rb +24 -0
  41. data/lib/rubyword.rb +1 -0
  42. data/lib/rubyword/configuration.rb +1 -1
  43. data/lib/rubyword/document.rb +29 -41
  44. data/lib/rubyword/element/base.rb +12 -0
  45. data/lib/rubyword/element/link.rb +6 -5
  46. data/lib/rubyword/element/list.rb +4 -2
  47. data/lib/rubyword/element/paragraph.rb +38 -0
  48. data/lib/rubyword/element/section.rb +23 -10
  49. data/lib/rubyword/element/table.rb +60 -0
  50. data/lib/rubyword/element/text.rb +14 -79
  51. data/lib/rubyword/style.rb +4 -0
  52. data/lib/rubyword/version.rb +1 -1
  53. data/lib/rubyword/writer.rb +0 -4
  54. data/lib/rubyword/writer/part/document.rb +51 -44
  55. data/lib/rubyword/writer/part/footer.rb +1 -6
  56. data/lib/rubyword/writer/part/header.rb +2 -6
  57. data/lib/rubyword/writer/style/base.rb +0 -1
  58. data/lib/rubyword/writer/style/paragraph.rb +47 -0
  59. data/lib/rubyword/writer/style/section.rb +0 -1
  60. data/lib/rubyword/writer/style/word.rb +40 -0
  61. data/rubyword.gemspec +1 -2
  62. data/spec/rubyword/document_spec.rb +18 -3
  63. data/spec/rubyword/element/page_break_spec.rb +55 -0
  64. data/spec/rubyword/element/text_break_spec.rb +46 -0
  65. data/spec/rubyword/element/text_spec.rb +50 -0
  66. data/spec/spec_helper.rb +3 -30
  67. metadata +50 -20
  68. data/CHANGELOG.txt +0 -10
@@ -0,0 +1,21 @@
1
+ # Table
2
+
3
+ ## Example
4
+ ```
5
+ Rubyword::Document::generate('hello.docx') {
6
+ section {
7
+ table {
8
+ tr {
9
+ th 'id'
10
+ th 'name'
11
+ th 'age'
12
+ }
13
+ tr {
14
+ th '1'
15
+ th 'young'
16
+ th '66'
17
+ }
18
+ }
19
+ }
20
+ }
21
+ ```
@@ -1 +1,26 @@
1
- # keep
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
+ ```
@@ -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
+ ```
@@ -0,0 +1,11 @@
1
+ # Toc
2
+
3
+ ## Options
4
+ + font_size: Number
5
+
6
+ ## Example
7
+ ```
8
+ Rubyword::Document::generate('hello.docx') {
9
+ title_directory font_size: 24
10
+ }
11
+ ```
@@ -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
+ }
@@ -0,0 +1,5 @@
1
+ require_relative '../lib/rubyword'
2
+ filename = File.join(Rubyword::TEMP_PATH, 'footer.docx')
3
+ Rubyword::Document::generate(filename) {
4
+ add_footer 'hello', text_align: 'center'
5
+ }
@@ -0,0 +1,5 @@
1
+ require_relative '../lib/rubyword'
2
+ filename = File.join(Rubyword::TEMP_PATH, 'header.docx')
3
+ Rubyword::Document::generate(filename) {
4
+ add_header 'rubyword'
5
+ }
@@ -0,0 +1,7 @@
1
+ require_relative '../lib/rubyword'
2
+ filename = File.join(Rubyword::TEMP_PATH, 'image.docx')
3
+ Rubyword::Document::generate(filename) {
4
+ section {
5
+ image 'http://www.baidu.com/img/bd_logo1.png'
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ require_relative '../lib/rubyword'
2
+ filename = File.join(Rubyword::TEMP_PATH, 'link.docx')
3
+ Rubyword::Document::generate(filename) {
4
+ section {
5
+ link 'baidu', 'http://www.baidu.com', text_align: 'center'
6
+ }
7
+ }
@@ -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
@@ -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
+ }
@@ -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
+ }
@@ -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
+ }
@@ -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
+ }
@@ -6,4 +6,5 @@ require "fastimage"
6
6
  require_relative "rubyword/version"
7
7
  require_relative "rubyword/configuration"
8
8
  require_relative "rubyword/writer"
9
+ require_relative "rubyword/style"
9
10
  require_relative "rubyword/document"
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Rubyword
3
- TEMP_PATH = File.join(File.expand_path('../../../', __FILE__), 'temp')
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
@@ -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
- def add_header(text, options={})
38
- @header = {
39
- id: 1,
40
- text: text,
41
- rid: self.init_rid,
42
- text_align: options[:align] || 'center',
43
- type: 'header'
44
- }
45
- self.content_types << {
46
- "/word/header1.xml" => "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"
47
- }
48
- self.rels_documents << {
49
- Id: "rId#{self.init_rid}",
50
- Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
51
- Target: "header1.xml"
52
- }
53
- self.init_rid = self.init_rid + 1
54
- end
55
-
56
- def add_footer(text=nil, options={})
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