rubyword 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -20,12 +20,7 @@ module Rubyword
20
20
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
21
21
  xml.send('w:ftr', ATTRIBUTE) {
22
22
  xml.p {
23
- if footer[:text_align]
24
- xml.send('w:pPr') {
25
- xml.send('w:jc', 'w:val' => footer[:text_align])
26
- }
27
- end
28
-
23
+ Writer::Style::Paragraph.new(@section, xml, @rubyword).write(footer[:style])
29
24
  nums_type = footer[:nums_type].to_s.downcase
30
25
  if footer[:text].nil? || nums_type == 'roman' || nums_type == 'number'
31
26
  text = 'PAGE'
@@ -16,15 +16,11 @@ module Rubyword
16
16
  }
17
17
 
18
18
  def write
19
- text_align = @rubyword.header[:text_align]
19
+ header = @rubyword.header
20
20
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
21
21
  xml.send('w:hdr', ATTRIBUTE) {
22
22
  xml.p {
23
- if text_align
24
- xml.send('w:pPr') {
25
- xml.send('w:jc', 'w:val' => text_align)
26
- }
27
- end
23
+ Writer::Style::Paragraph.new(@section, xml, @rubyword).write(header[:style])
28
24
  xml.send('w:r') {
29
25
  xml.send('w:t', {'xml:space' => "preserve"}, @rubyword.header[:text])
30
26
  }
@@ -5,7 +5,6 @@ module Rubyword
5
5
  class Base
6
6
  attr_accessor :rubyword, :style, :section, :xml
7
7
  def initialize(section, xml, rubyword)
8
- @style = section.style
9
8
  @section = section
10
9
  @xml = xml
11
10
  @rubyword = rubyword
@@ -0,0 +1,47 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Style
5
+ class Paragraph < Base
6
+
7
+ ParagraphStyleList = {
8
+ text_align: 'w:jc',
9
+ spacing: 'w:spacing',
10
+ indent_left: 'w:ind',
11
+ indent_right: 'w:ind',
12
+ indent_between: 'w:ind'
13
+ }.freeze
14
+
15
+ # write paragraph style
16
+ def write(style)
17
+ return unless !style.nil? && style.is_a?(Hash)
18
+ @xml.send('w:pPr') {
19
+ style.keys.each do |style_name|
20
+ style_name = style_name.to_sym
21
+ next unless ParagraphStyleList.keys.include?(style_name)
22
+ value = style[style_name]
23
+ attribute = case style_name.to_s
24
+ when 'spacing'
25
+ {'w:after' => value}
26
+ when 'indent_left'
27
+ {'w:left' => value}
28
+ when 'indent_right'
29
+ {'w:right' => value}
30
+ when 'indent_between'
31
+ v = value.split '-'
32
+ next unless v.is_a?(Array)
33
+ { 'w:left' => v[0].to_i, 'w:right' => v[1].to_i }
34
+ when !!value == value
35
+ nil
36
+ else
37
+ {'w:val' => value}
38
+ end
39
+ doc_style = ParagraphStyleList[style_name]
40
+ @xml.send(doc_style, attribute)
41
+ end
42
+ }
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -31,7 +31,6 @@ module Rubyword
31
31
  def write
32
32
  @xml.send('w:sectPr') {
33
33
  # header or footerReference
34
-
35
34
  [@rubyword.header, @rubyword.footer].each do |target|
36
35
  next if target.nil?
37
36
  @xml.send("w:#{target[:type]}Reference", {
@@ -0,0 +1,40 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Style
5
+ class Word < Base
6
+ WordStyleList = {
7
+ font_size: 'w:sz',
8
+ color: 'w:color',
9
+ underline: 'w:u',
10
+ blod: 'w:b',
11
+ all_caps: 'w:caps',
12
+ italic: 'w:i',
13
+ bgcolor: 'w:highlight'
14
+ }.freeze
15
+
16
+ def write(style)
17
+ if !style.nil? && style.is_a?(Hash)
18
+ @xml.send('w:rPr') {
19
+ style.keys.each do |style_name|
20
+ style_name = style_name.to_sym
21
+ if WordStyleList.keys.include?(style_name)
22
+ value = style[style_name]
23
+ attribute = if !!value != value # not a bool type
24
+ {'w:val' => value}
25
+ else
26
+ nil
27
+ end
28
+ doc_style = WordStyleList[style_name]
29
+ @xml.send(doc_style, attribute)
30
+ @xml.send('w:szCs', attribute) if style_name == :font_size
31
+ end
32
+ end
33
+ }
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -21,7 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
22
  spec.add_development_dependency "rspec"
23
23
  spec.add_development_dependency "nokogiri"
24
- spec.add_development_dependency "zip"
25
- spec.add_development_dependency "open-uri"
24
+ spec.add_development_dependency "rubyzip"
26
25
  spec.add_development_dependency "fastimage"
27
26
  end
@@ -1,10 +1,25 @@
1
1
  require "spec_helper"
2
2
  module Rubyword
3
3
  describe Document do
4
- describe "#create section" do
5
- let(:document) { Rubyword::Document.new }
4
+ before(:each) do
5
+ @document = Document.new
6
+ end
7
+ describe "Initialize Document" do
6
8
  it "return a document class" do
7
-
9
+ expect(@document.class).to eq(Rubyword::Document)
10
+ end
11
+
12
+ it "return a section" do
13
+ expect(@document.section.class).to eq(Rubyword::Element::Section)
14
+ end
15
+
16
+ it "add mutiple section" do
17
+ section1 = @document.section
18
+ section2 = @document.section
19
+ section3 = @document.section
20
+ expect(section1.section_id).to eq(1)
21
+ expect(section2.section_id).to eq(2)
22
+ expect(section3.section_id).to eq(3)
8
23
  end
9
24
  end
10
25
  end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+ module Rubyword
3
+ describe Document do
4
+ before(:each) do
5
+ @document = Document.new
6
+ end
7
+ let(:page_num) { 2 }
8
+ describe "Add pagebreak" do
9
+ subject(:pagebreak) {
10
+ pagebreak = Element::PageBreak.new(@document)
11
+ pagebreak.save(page_num)
12
+ pagebreak
13
+ }
14
+
15
+ it "save page break to PageBreak Object" do
16
+ expect(pagebreak.numbers.size.to_i).to eq(1)
17
+ end
18
+
19
+ it "check the numbers pop" do
20
+ number = pagebreak.numbers.pop
21
+ expect(number).to eq(page_num)
22
+ expect(pagebreak.numbers.size.to_i).to eq(0)
23
+ end
24
+
25
+ it "Write pagebreak xml" do
26
+ build = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
27
+ xml.send('w:document') {
28
+ xml.send('w:body') {
29
+ pagebreak.write(nil, xml)
30
+ }
31
+ }
32
+ end
33
+ result =<<-EOL
34
+ <?xml version="1.0" encoding="UTF-8"?>
35
+ <w:document>
36
+ <w:body>
37
+ <w:p>
38
+ <w:r>
39
+ <w:br w:type="page"/>
40
+ </w:r>
41
+ </w:p>
42
+ <w:p>
43
+ <w:r>
44
+ <w:br w:type="page"/>
45
+ </w:r>
46
+ </w:p>
47
+ </w:body>
48
+ </w:document>
49
+ EOL
50
+ expect(remove_whitespace(build.to_xml)).to eq(remove_whitespace(result))
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+ module Rubyword
3
+ describe Document do
4
+ before(:each) do
5
+ @document = Document.new
6
+ end
7
+ let(:page_num) { 2 }
8
+ describe "Add TextBreak" do
9
+ subject(:textbreak) {
10
+ textbreak = Element::TextBreak.new(@document)
11
+ textbreak.save(page_num)
12
+ textbreak
13
+ }
14
+
15
+ it "save page break to textbreak Object" do
16
+ expect(textbreak.numbers.size.to_i).to eq(1)
17
+ end
18
+
19
+ it "check the numbers pop" do
20
+ number = textbreak.numbers.pop
21
+ expect(number).to eq(page_num)
22
+ expect(textbreak.numbers.size.to_i).to eq(0)
23
+ end
24
+
25
+ it "Write textbreak xml" do
26
+ build = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
27
+ xml.send('w:document') {
28
+ xml.send('w:body') {
29
+ textbreak.write(nil, xml)
30
+ }
31
+ }
32
+ end
33
+ result =<<-EOL
34
+ <?xml version="1.0" encoding="UTF-8"?>
35
+ <w:document>
36
+ <w:body>
37
+ <w:p/><w:p/>
38
+ </w:body>
39
+ </w:document>
40
+ EOL
41
+ expect(remove_whitespace(build.to_xml)).to eq(remove_whitespace(result))
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+ module Rubyword
3
+ describe Document do
4
+ before(:each) do
5
+ @document = Document.new
6
+ end
7
+ let(:section) { @document.section }
8
+ subject(:text_obj) {
9
+ Element::Text.new(@document, section)
10
+ }
11
+
12
+ context "Add A Simple Text" do
13
+ it 'should return nil' do
14
+ text = text_obj.save(nil)
15
+ expect(text).to eq(nil)
16
+ text = text_obj.save('simple', nil)
17
+ expect(text).to eq(nil)
18
+ end
19
+
20
+ it 'should return queue object' do
21
+ text = text_obj.save('sample text', 'text')
22
+ expect(text.class).to eq(Queue)
23
+ end
24
+
25
+ it 'should return hash' do
26
+ string = 'sample'
27
+ text = text_obj.save(string, 'text')
28
+ expect(text.pop.to_s).to eq({size: 'normal', text: string, style: nil}.to_s)
29
+ end
30
+
31
+ it 'should output xml' do
32
+ string = 'sample'
33
+ text_obj.save(string, 'text')
34
+ build = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
35
+ text_obj.write(section, xml)
36
+ end
37
+ result =<<-EOL
38
+ <?xml version="1.0" encoding="UTF-8"?>
39
+ <w:p>
40
+ <w:r>
41
+ <w:t xml:space="preserve">sample</w:t>
42
+ </w:r>
43
+ </w:p>
44
+ EOL
45
+ expect(remove_whitespace(build.to_xml)).to eq(remove_whitespace(result))
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -1,32 +1,5 @@
1
1
  require "rubyword"
2
- RSpec.configure do |config|
3
- # rspec-expectations config goes here. You can use an alternate
4
- # assertion/expectation library such as wrong or the stdlib/minitest
5
- # assertions if you prefer.
6
- config.expect_with :rspec do |expectations|
7
- # This option will default to `true` in RSpec 4. It makes the `description`
8
- # and `failure_message` of custom matchers include text for helper methods
9
- # defined using `chain`, e.g.:
10
- # be_bigger_than(2).and_smaller_than(4).description
11
- # # => "be bigger than 2 and smaller than 4"
12
- # ...rather than:
13
- # # => "be bigger than 2"
14
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
15
- end
16
2
 
17
- # rspec-mocks config goes here. You can use an alternate test double
18
- # library (such as bogus or mocha) by changing the `mock_with` option here.
19
- config.mock_with :rspec do |mocks|
20
- # Prevents you from mocking or stubbing a method that does not exist on
21
- # a real object. This is generally recommended, and will default to
22
- # `true` in RSpec 4.
23
- mocks.verify_partial_doubles = true
24
- end
25
-
26
- # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
27
- # have no way to turn it off -- the option exists only for backwards
28
- # compatibility in RSpec 3). It causes shared context metadata to be
29
- # inherited by the metadata hash of host groups and examples, rather than
30
- # triggering implicit auto-inclusion in groups with matching metadata.
31
- config.shared_context_metadata_behavior = :apply_to_host_groups
32
- end
3
+ def remove_whitespace(wordml)
4
+ wordml.gsub(/\s+/, ' ').gsub(/(?<keep>>)\s+|\s+(?<keep><)/, '\k<keep>').strip
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyword
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - young
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-21 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,21 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: zip
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: open-uri
70
+ name: rubyzip
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - ">="
@@ -114,21 +100,56 @@ email:
114
100
  - youngjy6@gmail.com
115
101
  executables:
116
102
  - console
103
+ - run-example
117
104
  - setup
118
105
  extensions: []
119
106
  extra_rdoc_files: []
120
107
  files:
121
108
  - ".gitignore"
122
109
  - ".rspec"
123
- - CHANGELOG.txt
110
+ - CHANGELOG.md
124
111
  - Gemfile
125
112
  - LICENSE
113
+ - README.cn.md
126
114
  - README.md
127
115
  - Rakefile
128
116
  - bin/console
117
+ - bin/run-example
129
118
  - bin/setup
130
119
  - doc/README.md
120
+ - doc/doc-information.md
121
+ - doc/footer.md
122
+ - doc/header.md
123
+ - doc/image.md
124
+ - doc/link.md
125
+ - doc/list.md
126
+ - doc/paragraph.md
127
+ - doc/table.md
131
128
  - doc/text.md
129
+ - doc/title.md
130
+ - doc/toc.md
131
+ - example/doc_information.rb
132
+ - example/footer.rb
133
+ - example/header.rb
134
+ - example/image.rb
135
+ - example/link.rb
136
+ - example/list.rb
137
+ - example/paragraph.rb
138
+ - example/result/.keep
139
+ - example/result/doc-information.docx
140
+ - example/result/footer.docx
141
+ - example/result/header.docx
142
+ - example/result/image.docx
143
+ - example/result/link.docx
144
+ - example/result/list.docx
145
+ - example/result/paragraph.docx
146
+ - example/result/table.docx
147
+ - example/result/test.docx
148
+ - example/result/text.docx
149
+ - example/table.rb
150
+ - example/test.rb
151
+ - example/text.rb
152
+ - example/toc.rb
132
153
  - lib/rubyword.rb
133
154
  - lib/rubyword/configuration.rb
134
155
  - lib/rubyword/document.rb
@@ -137,9 +158,12 @@ files:
137
158
  - lib/rubyword/element/link.rb
138
159
  - lib/rubyword/element/list.rb
139
160
  - lib/rubyword/element/page_break.rb
161
+ - lib/rubyword/element/paragraph.rb
140
162
  - lib/rubyword/element/section.rb
163
+ - lib/rubyword/element/table.rb
141
164
  - lib/rubyword/element/text.rb
142
165
  - lib/rubyword/element/text_break.rb
166
+ - lib/rubyword/style.rb
143
167
  - lib/rubyword/version.rb
144
168
  - lib/rubyword/writer.rb
145
169
  - lib/rubyword/writer/part/base.rb
@@ -160,10 +184,14 @@ files:
160
184
  - lib/rubyword/writer/part/toc.rb
161
185
  - lib/rubyword/writer/part/web_settings.rb
162
186
  - lib/rubyword/writer/style/base.rb
187
+ - lib/rubyword/writer/style/paragraph.rb
163
188
  - lib/rubyword/writer/style/section.rb
189
+ - lib/rubyword/writer/style/word.rb
164
190
  - rubyword.gemspec
165
- - sample/test.rb
166
191
  - spec/rubyword/document_spec.rb
192
+ - spec/rubyword/element/page_break_spec.rb
193
+ - spec/rubyword/element/text_break_spec.rb
194
+ - spec/rubyword/element/text_spec.rb
167
195
  - spec/spec_helper.rb
168
196
  - template/fontTable.xml
169
197
  - template/theme.xml
@@ -193,5 +221,7 @@ specification_version: 4
193
221
  summary: Generate MS Word for ruby
194
222
  test_files:
195
223
  - spec/rubyword/document_spec.rb
224
+ - spec/rubyword/element/page_break_spec.rb
225
+ - spec/rubyword/element/text_break_spec.rb
226
+ - spec/rubyword/element/text_spec.rb
196
227
  - spec/spec_helper.rb
197
- has_rdoc: