rubyword 1.0.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 (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +1 -0
  4. data/CHANGELOG.txt +10 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE +22 -0
  7. data/README.md +125 -0
  8. data/Rakefile +4 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/doc/README.md +1 -0
  12. data/doc/text.md +1 -0
  13. data/lib/rubyword/configuration.rb +5 -0
  14. data/lib/rubyword/document.rb +81 -0
  15. data/lib/rubyword/element/base.rb +12 -0
  16. data/lib/rubyword/element/image.rb +45 -0
  17. data/lib/rubyword/element/link.rb +35 -0
  18. data/lib/rubyword/element/list.rb +35 -0
  19. data/lib/rubyword/element/page_break.rb +26 -0
  20. data/lib/rubyword/element/section.rb +67 -0
  21. data/lib/rubyword/element/text.rb +137 -0
  22. data/lib/rubyword/element/text_break.rb +20 -0
  23. data/lib/rubyword/version.rb +3 -0
  24. data/lib/rubyword/writer/part/base.rb +13 -0
  25. data/lib/rubyword/writer/part/content_types.rb +39 -0
  26. data/lib/rubyword/writer/part/doc_props_app.rb +20 -0
  27. data/lib/rubyword/writer/part/doc_props_core.rb +32 -0
  28. data/lib/rubyword/writer/part/doc_props_custom.rb +16 -0
  29. data/lib/rubyword/writer/part/document.rb +56 -0
  30. data/lib/rubyword/writer/part/font_table.rb +12 -0
  31. data/lib/rubyword/writer/part/footer.rb +50 -0
  32. data/lib/rubyword/writer/part/header.rb +40 -0
  33. data/lib/rubyword/writer/part/numbering.rb +51 -0
  34. data/lib/rubyword/writer/part/rels.rb +26 -0
  35. data/lib/rubyword/writer/part/rels_document.rb +31 -0
  36. data/lib/rubyword/writer/part/settings.rb +93 -0
  37. data/lib/rubyword/writer/part/styles.rb +91 -0
  38. data/lib/rubyword/writer/part/theme.rb +12 -0
  39. data/lib/rubyword/writer/part/toc.rb +89 -0
  40. data/lib/rubyword/writer/part/web_settings.rb +21 -0
  41. data/lib/rubyword/writer/style/base.rb +16 -0
  42. data/lib/rubyword/writer/style/section.rb +65 -0
  43. data/lib/rubyword/writer.rb +87 -0
  44. data/lib/rubyword.rb +9 -0
  45. data/rubyword.gemspec +27 -0
  46. data/sample/test.rb +78 -0
  47. data/spec/rubyword/document_spec.rb +11 -0
  48. data/spec/spec_helper.rb +32 -0
  49. data/template/fontTable.xml +54 -0
  50. data/template/theme.xml +284 -0
  51. metadata +197 -0
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Element
4
+ class TextBreak < Base
5
+ attr_accessor :numbers
6
+ def save(numbers)
7
+ @numbers ||= Queue.new
8
+ @numbers << numbers.to_i
9
+ end
10
+
11
+ def write(section=nil, xml=nil)
12
+ text_break = self.numbers.pop
13
+ text_break.to_i.times.each do |i|
14
+ xml.send('w:p')
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Rubyword
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Base
6
+ attr_accessor :rubyword, :section
7
+ def initialize(rubyword, section=nil)
8
+ @rubyword, @section = rubyword, section
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,39 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class ContentTypes < Base
6
+ def write
7
+ openXMLPrefix = 'application/vnd.openxmlformats-';
8
+ wordMLPrefix = "#{openXMLPrefix}officedocument.wordprocessingml";
9
+ overrides = {
10
+ '/docProps/core.xml' => "#{openXMLPrefix}package.core-properties+xml",
11
+ '/docProps/app.xml' => "#{openXMLPrefix}officedocument.extended-properties+xml",
12
+ '/docProps/custom.xml' => "#{openXMLPrefix}officedocument.custom-properties+xml",
13
+ '/word/document.xml' => "#{wordMLPrefix}.document.main+xml",
14
+ '/word/styles.xml' => "#{wordMLPrefix}.styles+xml",
15
+ '/word/numbering.xml' => "#{wordMLPrefix}.numbering+xml",
16
+ '/word/settings.xml' => "#{wordMLPrefix}.settings+xml",
17
+ '/word/theme/theme1.xml' => "#{openXMLPrefix}officedocument.theme+xml",
18
+ '/word/webSettings.xml' => "#{wordMLPrefix}.webSettings+xml",
19
+ '/word/fontTable.xml' => "#{wordMLPrefix}.fontTable+xml"
20
+ }
21
+ @rubyword.content_types.each do |target|
22
+ overrides.merge!(target)
23
+ end
24
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
25
+ xml.Types(xmlns: 'http://schemas.openxmlformats.org/package/2006/content-types') do
26
+ xml.Default(Extension: 'rels', ContentType: "application/vnd.openxmlformats-package.relationships+xml")
27
+ xml.Default(Extension: 'xml', ContentType: 'application/xml')
28
+ xml.Default(Extension: 'jpg', ContentType: 'image/jpeg')
29
+ overrides.each do |part_name, content_type|
30
+ xml.Override(PartName: part_name, ContentType: content_type)
31
+ end
32
+ end
33
+ end
34
+ builder.to_xml
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class DocPropsApp < Base
6
+ def write
7
+ infomartion = @rubyword.doc_info
8
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
9
+ xml.Properties(xmlns: 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties',
10
+ 'xmlns:vt' => 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes') do
11
+ xml.Company(@rubyword.doc_info[:company])
12
+ xml.Manager(@rubyword.doc_info[:creator])
13
+ end
14
+ end
15
+ builder.to_xml
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class DocPropsCore < Base
6
+ def write
7
+ xmlns = {
8
+ 'xmlns:cp' => 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
9
+ 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
10
+ 'xmlns:dcterms' => 'http://purl.org/dc/terms/',
11
+ 'xmlns:dcmitype' => 'http://purl.org/dc/dcmitype/',
12
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
13
+ }
14
+ infomartion = @rubyword.doc_info
15
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
16
+ xml.send('cp:coreProperties', xmlns) do
17
+ xml.send('dc:title', infomartion[:title])
18
+ xml.send('dc:subject', infomartion[:subject])
19
+ xml.send('dc:creator', infomartion[:creator])
20
+ xml.send('cp:keywords', infomartion[:keywords])
21
+ xml.send('dc:description', infomartion[:description])
22
+ xml.send('cp:lastModifiedBy', 'rubyword')
23
+ xml.send('cp:category', infomartion[:category])
24
+ xml.send('dcterms:created', {'xsi:type' => 'dcterms:W3CDTF'}, Time.now.strftime('%Y-%m-%dT%H:%M:%SZ').to_s)
25
+ end
26
+ end
27
+ builder.to_xml
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class DocPropsCustom < Base
6
+ def write
7
+ # 需要定制
8
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
9
+ xml.Properties(xmlns: 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties', 'xmlns:vt' => 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes')
10
+ end
11
+ builder.to_xml
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,56 @@
1
+ # -*- encoding : utf-8 -*
2
+ module Rubyword
3
+ module Writer
4
+ class Part::Document < Part::Base
5
+ include ::Rubyword::Writer::Part::Toc
6
+ DOCUMENT_ATTR = {
7
+ 'xmlns:ve' => "http://schemas.openxmlformats.org/markup-compatibility/2006",
8
+ 'xmlns:o' => "urn:schemas-microsoft-com:office:office",
9
+ 'xmlns:r' => "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
10
+ 'xmlns:m' => "http://schemas.openxmlformats.org/officeDocument/2006/math",
11
+ 'xmlns:v' => "urn:schemas-microsoft-com:vml",
12
+ 'xmlns:wp' => "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
13
+ 'xmlns:w10' => "urn:schemas-microsoft-com:office:word",
14
+ 'xmlns:w' => "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
15
+ 'xmlns:wne' => "http://schemas.microsoft.com/office/word/2006/wordml"
16
+ }
17
+
18
+ def write
19
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
20
+ xml.send('w:document', DOCUMENT_ATTR) {
21
+ xml.send('w:body') {
22
+ section_write(xml)
23
+ }
24
+ }
25
+ end
26
+ builder.to_xml
27
+ end
28
+
29
+ def section_write(xml)
30
+ @object_blocks = []
31
+ sections_count = @rubyword.sections.count
32
+ current_section = 0
33
+ # write TOC
34
+ toc_block = write_toc(@rubyword, xml)
35
+ @object_blocks.push(toc_block) if toc_block
36
+ @rubyword.sections.each do |section|
37
+ current_section = current_section + 1
38
+ # 遍历输出各对象 object 信息
39
+ section.objects.each{|object| @object_blocks << object.write(section, xml)}
40
+ if current_section == sections_count
41
+ @object_blocks << Style::Section.new(section, xml, @rubyword).write
42
+ else
43
+ p_block = xml.send('w:p') {
44
+ xml.send('w:pPr') {
45
+ @object_blocks << Style::Section.new(section, xml, @rubyword).write
46
+ }
47
+ }
48
+ @object_blocks << p_block
49
+ end
50
+ end
51
+ @object_blocks
52
+ end
53
+
54
+ end # end of class
55
+ end
56
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class FontTable < Base
6
+ def write
7
+ Nokogiri::XML(open(File.join(::Rubyword::WORD_TEMP_PATH, 'fontTable.xml'))).to_xml
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Footer < Base
6
+ ATTRIBUTE = {
7
+ 'xmlns:ve' => 'http://schemas.openxmlformats.org/markup-compatibility/2006',
8
+ 'xmlns:o' => 'urn:schemas-microsoft-com:office:office',
9
+ 'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
10
+ 'xmlns:m' => 'http://schemas.openxmlformats.org/officeDocument/2006/math',
11
+ 'xmlns:v' => 'urn:schemas-microsoft-com:vml',
12
+ 'xmlns:wp' => 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing',
13
+ 'xmlns:w10' => 'urn:schemas-microsoft-com:office:word',
14
+ 'xmlns:w' => 'http://schemas.openxmlformats.org/wordprocessingml/2006/main',
15
+ 'xmlns:wne' => 'http://schemas.microsoft.com/office/word/2006/wordml'
16
+ }
17
+
18
+ def write
19
+ footer = @rubyword.footer
20
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
21
+ xml.send('w:ftr', ATTRIBUTE) {
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
+
29
+ nums_type = footer[:nums_type].to_s.downcase
30
+ if footer[:text].nil? || nums_type == 'roman' || nums_type == 'number'
31
+ text = 'PAGE'
32
+ text = 'PAGE \* ROMAN' if nums_type == 'roman'
33
+ xml.send('w:r') { xml.send('w:fldChar', {'w:fldCharType' => "begin"}) }
34
+ xml.send('w:r') { xml.send('w:instrText', {'w:space' => "preserve"}, text) }
35
+ xml.send('w:r') { xml.send('w:fldChar', {'w:fldCharType' => "separate"}) }
36
+ xml.send('w:r') { xml.send('w:fldChar', {'w:fldCharType' => "end"}) }
37
+ else
38
+ text = footer[:text]
39
+ xml.send('w:r') { xml.send('w:t', {'w:space' => "preserve"}, text) }
40
+ end
41
+ }
42
+ }
43
+ end
44
+ builder.to_xml
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Header < Base
6
+ ATTRIBUTE = {
7
+ 'xmlns:ve' => 'http://schemas.openxmlformats.org/markup-compatibility/2006',
8
+ 'xmlns:o' => 'urn:schemas-microsoft-com:office:office',
9
+ 'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
10
+ 'xmlns:m' => 'http://schemas.openxmlformats.org/officeDocument/2006/math',
11
+ 'xmlns:v' => 'urn:schemas-microsoft-com:vml',
12
+ 'xmlns:wp' => 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing',
13
+ 'xmlns:w10' => 'urn:schemas-microsoft-com:office:word',
14
+ 'xmlns:w' => 'http://schemas.openxmlformats.org/wordprocessingml/2006/main',
15
+ 'xmlns:wne' => 'http://schemas.microsoft.com/office/word/2006/wordml'
16
+ }
17
+
18
+ def write
19
+ text_align = @rubyword.header[:text_align]
20
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
21
+ xml.send('w:hdr', ATTRIBUTE) {
22
+ xml.p {
23
+ if text_align
24
+ xml.send('w:pPr') {
25
+ xml.send('w:jc', 'w:val' => text_align)
26
+ }
27
+ end
28
+ xml.send('w:r') {
29
+ xml.send('w:t', {'xml:space' => "preserve"}, @rubyword.header[:text])
30
+ }
31
+ }
32
+ }
33
+ end
34
+ builder.to_xml
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,51 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Numbering < Base
6
+ def write
7
+ xmlns = {
8
+ 'xmlns:ve' => 'http://schemas.openxmlformats.org/markup-compatibility/2006',
9
+ 'xmlns:o' => 'urn:schemas-microsoft-com:office:office',
10
+ 'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
11
+ 'xmlns:m' => 'http://schemas.openxmlformats.org/officeDocument/2006/math',
12
+ 'xmlns:v' => 'urn:schemas-microsoft-com:vml',
13
+ 'xmlns:wp' => 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing',
14
+ 'xmlns:w10' => 'urn:schemas-microsoft-com:office:word',
15
+ 'xmlns:w' => 'http://schemas.openxmlformats.org/wordprocessingml/2006/main',
16
+ 'xmlns:wne' => 'http://schemas.microsoft.com/office/word/2006/wordml'
17
+ }
18
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
19
+ xml.send('w:numbering', xmlns) {
20
+ xml.send('w:abstractNum', 'w:abstractNumId' => 3) {
21
+ xml.send('w:nsid', 'w:val' => '5E15211D')
22
+ xml.send('w:multiLevelType', 'w:val' => 'multilevel')
23
+ 2.times.each do |index|
24
+ num = index + 1
25
+ left = num * 360
26
+ xml.send('w:lvl', 'w:ilvl' => index) {
27
+ xml.send('w:start', 'w:val' => 1)
28
+ xml.send('w:numFmt', 'w:val' => 'decimal')
29
+ xml.send('w:suff', 'w:val' => 'tab')
30
+ xml.send('w:lvlText', 'w:val' => "%#{num}.")
31
+ xml.send('w:pPr') {
32
+ xml.send('w:tabs') {
33
+ xml.send('w:tab', 'w:val' => 'num', 'w:pos' => 360)
34
+ }
35
+ xml.send('w:ind', 'w:left'=> left, 'w:hanging' => 360)
36
+ }
37
+ }
38
+ end
39
+ } # end of lvl style
40
+
41
+ xml.send('w:num', 'w:numId' => 3) {
42
+ xml.send('w:abstractNumId', 'w:val' => 3)
43
+ }
44
+ }
45
+ end
46
+ builder.to_xml
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Rels < Base
6
+ def write
7
+ xmlRels = {
8
+ 'docProps/core.xml' => 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
9
+ 'docProps/app.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
10
+ 'docProps/custom.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
11
+ 'word/document.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument'
12
+ }
13
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
14
+ xml.Relationships(xmlns: 'http://schemas.openxmlformats.org/package/2006/relationships') do
15
+ 3.times.each do |num|
16
+ num = num + 1
17
+ xml.Relationship(Id: "rId#{num}", Type: xmlRels.values[num], Target: xmlRels.keys[num])
18
+ end
19
+ end
20
+ end
21
+ builder.to_xml
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class RelsDocument < Base
6
+ def write
7
+ xmlRels = {
8
+ 'styles.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
9
+ 'numbering.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering',
10
+ 'settings.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings',
11
+ 'theme/theme1.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
12
+ 'webSettings.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings',
13
+ 'fontTable.xml' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
14
+ }
15
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
16
+ xml.Relationships(xmlns: 'http://schemas.openxmlformats.org/package/2006/relationships') do
17
+ xmlRels.keys.each_with_index do |rel_name, index|
18
+ id = index + 1
19
+ xml.Relationship(Id: "rId#{id}", Type: xmlRels[rel_name], Target: rel_name)
20
+ end
21
+ @rubyword.rels_documents.each do |attribute|
22
+ xml.Relationship(attribute)
23
+ end
24
+ end
25
+ end
26
+ builder.to_xml
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,93 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Settings < Base
6
+ def write
7
+ xmlns = {
8
+ 'xmlns:o' => 'urn:schemas-microsoft-com:office:office',
9
+ 'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
10
+ 'xmlns:m' => 'http://schemas.openxmlformats.org/officeDocument/2006/math',
11
+ 'xmlns:v' => 'urn:schemas-microsoft-com:vml',
12
+ 'xmlns:sl' => 'http://schemas.openxmlformats.org/schemaLibrary/2006/main',
13
+ 'xmlns:w10' => 'urn:schemas-microsoft-com:office:word',
14
+ 'xmlns:w' => 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
15
+ }
16
+
17
+ default_setting = {
18
+ 'w:zoom' => { 'w:percent' => '100'},
19
+ 'w:defaultTabStop' => {'w:val' => '708'},
20
+ 'w:hyphenationZone' => {'w:val' => '425'},
21
+ 'w:characterSpacingControl' => {'w:val' => 'doNotCompress'},
22
+ 'w:themeFontLang' => {'w:val' => 'en-US'},
23
+ 'w:decimalSymbol' => {'w:val' => '.'},
24
+ 'w:listSeparator' => {'w:val' => ';'}
25
+ }
26
+
27
+ default_math = {
28
+ 'm:mathPr' => {
29
+ 'm:mathFont' => {'m:val' => 'Cambria Math'},
30
+ 'm:brkBin' => {'m:val' => 'before'},
31
+ 'm:brkBinSub' => {'m:val' => '--'},
32
+ 'm:smallFrac' => {'m:val' => 'off'},
33
+ 'm:dispDef' => '',
34
+ 'm:lMargin' => {'m:val' => '0'},
35
+ 'm:rMargin' => {'m:val' => '0'},
36
+ 'm:defJc' => {'m:val' => 'centerGroup'},
37
+ 'm:wrapIndent' => {'m:val' => '1440'},
38
+ 'm:intLim' => {'m:val' => 'subSup'},
39
+ 'm:naryLim' => {'m:val' => 'undOvr'}
40
+ }
41
+ }
42
+
43
+ default_scheme = {
44
+ 'w:clrSchemeMapping' => {
45
+ 'w:bg1' => 'light1',
46
+ 'w:t1' => 'dark1',
47
+ 'w:bg2' => 'light2',
48
+ 'w:t2' => 'dark2',
49
+ 'w:accent1' => 'accent1',
50
+ 'w:accent2' => 'accent2',
51
+ 'w:accent3' => 'accent3',
52
+ 'w:accent4' => 'accent4',
53
+ 'w:accent5' => 'accent5',
54
+ 'w:accent6' => 'accent6',
55
+ 'w:hyperlink' => 'hyperlink',
56
+ 'w:followedHyperlink' => 'followedHyperlink'
57
+ }
58
+ }
59
+
60
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
61
+ xml.send('w:settings', xmlns) do
62
+ default_setting.each do |key, value|
63
+ xml.send(key, value)
64
+ end
65
+
66
+ # 需定制
67
+ xml.send('w:compat') {
68
+ xml.send('w:compatSetting', {
69
+ 'w:name' => 'compatibilityMode',
70
+ 'w:uri' => 'http://schemas.microsoft.com/office/word',
71
+ 'w:val' => 12
72
+ })
73
+ }
74
+
75
+ default_math.each do |k, v|
76
+ xml.send(k) {
77
+ v.each do |sk, sv|
78
+ xml.send(sk, sv)
79
+ end
80
+ }
81
+ end
82
+
83
+ default_scheme.each do |k, v|
84
+ xml.send(k, v)
85
+ end
86
+ end
87
+ end
88
+ builder.to_xml
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,91 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Styles < Base
6
+
7
+ # font default setting
8
+ DEFAULT_FONT_NAME = 'Arial'
9
+ DEFAULT_FONT_SIZE = 10
10
+ DEFAULT_FONT_COLOR = '000000'
11
+ DEFAULT_FONT_CONTENT_TYPE = 'default'
12
+
13
+ def title_style
14
+ [
15
+ {'size' => 20, 'color' => '333333', 'bold' => true},
16
+ {'size' => 16, 'color' => '333333', 'bold' => true},
17
+ {'size' => 14, 'color' => '333333', 'bold' => true},
18
+ {'size' => 12, 'color' => '333333', 'bold' => true}
19
+ ]
20
+ end
21
+
22
+ def write
23
+ attribute = {
24
+ 'xmlns:w' => 'http://schemas.openxmlformats.org/wordprocessingml/2006/main',
25
+ 'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
26
+ }
27
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
28
+ xml.send('w:styles', attribute) {
29
+ default_setting(xml)
30
+ normal_style(xml)
31
+ footer_note_style(xml)
32
+ write_font_style(xml)
33
+ }
34
+ end
35
+ builder.to_xml
36
+ end
37
+
38
+ def default_setting(xml)
39
+ xml.send('w:docDefaults') {
40
+ xml.send('w:rPrDefault') {
41
+ xml.send('w:rPr') {
42
+ xml.send('w:rFonts', {'w:ascii'=>DEFAULT_FONT_NAME, 'w:hAnsi'=>DEFAULT_FONT_NAME, 'w:eastAsia'=>DEFAULT_FONT_NAME, 'w:cs'=>DEFAULT_FONT_NAME})
43
+ xml.send('w:sz', 'w:val' => DEFAULT_FONT_SIZE * 2 )
44
+ xml.send('w:szCs', 'w:val' => DEFAULT_FONT_SIZE * 2 )
45
+ }
46
+ }
47
+ }
48
+ end
49
+
50
+ def normal_style(xml)
51
+ attribute = {
52
+ 'w:type' => 'paragraph',
53
+ 'w:default' => 1,
54
+ 'w:styleId' => 'Normal'
55
+ }
56
+ xml.send('w:style', attribute) {
57
+ xml.send('w:name', 'w:val' => 'Normal')
58
+ }
59
+ end
60
+
61
+ def footer_note_style(xml)
62
+ xml.send('w:style', {'w:type' => 'character', 'w:styleId' => 'FootnoteReference'}) {
63
+ xml.send('w:name', 'w:val' => 'Footnote Reference')
64
+ xml.send('w:semiHidden')
65
+ xml.send('w:unhideWhenUsed')
66
+ xml.send('w:rPr') {
67
+ xml.send('w:verAlign', 'w:val' => 'superscript')
68
+ }
69
+ }
70
+ end
71
+
72
+ def write_font_style(xml)
73
+ title_style.each_with_index do |style, index|
74
+ index = index + 1
75
+ xml.send('w:style', {'w:type' => 'paragraph', 'w:styleId' => "Heading#{index}"}) {
76
+ xml.send('w:link', {'w:val' => "Heading#{index}Char"})
77
+ xml.send('w:name', {'w:val' => "heading #{index}"})
78
+ xml.send('w:rPr') {
79
+ xml.send('w:color', 'w:val' => style['color'])
80
+ xml.send('w:sz', 'w:val' => style['size'] * 2)
81
+ xml.send('w:szCs', 'w:val' => style['size'] * 2)
82
+ xml.send('w:b')
83
+ }
84
+ }
85
+ end
86
+ end
87
+
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Rubyword
3
+ module Writer
4
+ module Part
5
+ class Theme < Base
6
+ def write
7
+ Nokogiri::XML(open(File.join(::Rubyword::WORD_TEMP_PATH, 'theme.xml'))).to_xml
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end