rocx 0.5.6

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 (111) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +48 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +34 -0
  7. data/Rakefile +9 -0
  8. data/lib/rocx.rb +8 -0
  9. data/lib/rocx/elements.rb +10 -0
  10. data/lib/rocx/elements/base_container.rb +65 -0
  11. data/lib/rocx/elements/base_element.rb +91 -0
  12. data/lib/rocx/elements/break.rb +10 -0
  13. data/lib/rocx/elements/paragraph.rb +31 -0
  14. data/lib/rocx/elements/run.rb +12 -0
  15. data/lib/rocx/elements/symbol.rb +10 -0
  16. data/lib/rocx/elements/text.rb +21 -0
  17. data/lib/rocx/package.rb +33 -0
  18. data/lib/rocx/parts.rb +8 -0
  19. data/lib/rocx/parts/base_part.rb +29 -0
  20. data/lib/rocx/parts/content_types.rb +46 -0
  21. data/lib/rocx/parts/document.rb +51 -0
  22. data/lib/rocx/parts/global_rels.rb +24 -0
  23. data/lib/rocx/parts/rels.rb +48 -0
  24. data/lib/rocx/parts/settings.rb +21 -0
  25. data/lib/rocx/parts/styles.rb +49 -0
  26. data/lib/rocx/properties.rb +12 -0
  27. data/lib/rocx/properties/alignment.rb +27 -0
  28. data/lib/rocx/properties/auto_adjust_right_indent.rb +15 -0
  29. data/lib/rocx/properties/auto_space_de.rb +15 -0
  30. data/lib/rocx/properties/auto_space_dn.rb +15 -0
  31. data/lib/rocx/properties/base_property.rb +35 -0
  32. data/lib/rocx/properties/bidi.rb +6 -0
  33. data/lib/rocx/properties/bold.rb +11 -0
  34. data/lib/rocx/properties/boolean_property.rb +15 -0
  35. data/lib/rocx/properties/conditional_formatting.rb +62 -0
  36. data/lib/rocx/properties/contextual_spacing.rb +6 -0
  37. data/lib/rocx/properties/div_id.rb +23 -0
  38. data/lib/rocx/properties/indentation.rb +31 -0
  39. data/lib/rocx/properties/italics.rb +11 -0
  40. data/lib/rocx/properties/keep_lines.rb +6 -0
  41. data/lib/rocx/properties/keep_next.rb +6 -0
  42. data/lib/rocx/properties/kinsoku.rb +6 -0
  43. data/lib/rocx/properties/mirror_indent.rb +6 -0
  44. data/lib/rocx/properties/on_off_property.rb +15 -0
  45. data/lib/rocx/properties/outline_level.rb +23 -0
  46. data/lib/rocx/properties/overflow_punctuation.rb +15 -0
  47. data/lib/rocx/properties/page_break_before.rb +6 -0
  48. data/lib/rocx/properties/snap_to_grid.rb +6 -0
  49. data/lib/rocx/properties/supress_auto_hyphens.rb +6 -0
  50. data/lib/rocx/properties/supress_line_numbers.rb +6 -0
  51. data/lib/rocx/properties/supress_overlap.rb +6 -0
  52. data/lib/rocx/properties/toggle_property.rb +15 -0
  53. data/lib/rocx/properties/widow_control.rb +6 -0
  54. data/lib/rocx/properties/word_wrap.rb +6 -0
  55. data/lib/rocx/style.rb +38 -0
  56. data/lib/rocx/version.rb +3 -0
  57. data/rocx.gemspec +30 -0
  58. data/test/data/elements/break_element.xml +4 -0
  59. data/test/data/elements/break_with_attributes_element.xml +4 -0
  60. data/test/data/elements/paragraph_element.xml +8 -0
  61. data/test/data/elements/paragraph_with_runs_element.xml +14 -0
  62. data/test/data/elements/run_element.xml +8 -0
  63. data/test/data/elements/symbol_element.xml +4 -0
  64. data/test/data/elements/text_element.xml +4 -0
  65. data/test/data/parts/content_types_part.xml +1 -0
  66. data/test/data/parts/document_with_children_part.xml +1 -0
  67. data/test/data/parts/empty_document_part.xml +1 -0
  68. data/test/data/parts/global_rels_part.xml +1 -0
  69. data/test/data/parts/rels_part.xml +1 -0
  70. data/test/data/parts/settings_part.xml +1 -0
  71. data/test/data/parts/styles_part.xml +1 -0
  72. data/test/data/parts/styles_with_custom_style_part.xml +1 -0
  73. data/test/data/styles/character_styles.xml +15 -0
  74. data/test/data/styles/paragraph_styles.xml +11 -0
  75. data/test/elements/break_test.rb +54 -0
  76. data/test/elements/paragraph_test.rb +37 -0
  77. data/test/elements/run_test.rb +18 -0
  78. data/test/elements/symbol_test.rb +23 -0
  79. data/test/elements/text_test.rb +26 -0
  80. data/test/package_test.rb +48 -0
  81. data/test/parts/content_types_test.rb +37 -0
  82. data/test/parts/document_test.rb +31 -0
  83. data/test/parts/global_rels_test.rb +13 -0
  84. data/test/parts/rels_test.rb +48 -0
  85. data/test/parts/settings_test.rb +13 -0
  86. data/test/parts/styles_test.rb +29 -0
  87. data/test/properties/auto_adjust_right_indent_test.rb +50 -0
  88. data/test/properties/auto_space_de_test.rb +50 -0
  89. data/test/properties/auto_space_dn_test.rb +50 -0
  90. data/test/properties/bidi_test.rb +50 -0
  91. data/test/properties/bold_test.rb +50 -0
  92. data/test/properties/conditional_formatting_test.rb +46 -0
  93. data/test/properties/contextual_spacing_test.rb +50 -0
  94. data/test/properties/div_id_test.rb +38 -0
  95. data/test/properties/italics_test.rb +50 -0
  96. data/test/properties/keep_lines_test.rb +50 -0
  97. data/test/properties/keep_next_test.rb +50 -0
  98. data/test/properties/kinsoku_test.rb +50 -0
  99. data/test/properties/mirror_indent_test.rb +50 -0
  100. data/test/properties/outline_level_test.rb +46 -0
  101. data/test/properties/overflow_punctuation_test.rb +50 -0
  102. data/test/properties/page_break_before_test.rb +50 -0
  103. data/test/properties/snap_to_grid_test.rb +50 -0
  104. data/test/properties/supress_auto_hyphens_test.rb +50 -0
  105. data/test/properties/supress_line_numbers_test.rb +50 -0
  106. data/test/properties/supress_overlap_test.rb +50 -0
  107. data/test/properties/widow_control_test.rb +50 -0
  108. data/test/properties/word_wrap_test.rb +50 -0
  109. data/test/style_test.rb +54 -0
  110. data/test/test_helper.rb +55 -0
  111. metadata +315 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9566d9952e4f4b81a945e27bc633c7367bcfa2c9
4
+ data.tar.gz: 23feeec4fecf2d69f371afcb15c1f42ecc5a2041
5
+ SHA512:
6
+ metadata.gz: de7d040a6ce8215a2cb59f4a5edeb40a44cc5ef60bb93780bbe4eff81573426e5cdaaa270d1d5718f498b016b88aeee0fc4107abfe39ee976a3c69ae8825fb86
7
+ data.tar.gz: 0ff4f77e0df501a0d137146aa397fc8dced38476e6b970ef32cdeccf1416be399d5a77784c66b9dddf1baf584c67fd9aa8446e2b85141e189c1ec4332dfbc77c
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ rocx-0.1.0.gem
3
+ *.gem
4
+ /coverage
5
+ TODO.md
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rocx (0.5.5)
5
+ nokogiri
6
+ open_xml_package
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ansi (1.4.3)
12
+ coderay (1.0.9)
13
+ method_source (0.8.1)
14
+ mini_portile (0.5.3)
15
+ multi_json (1.7.2)
16
+ nokogiri (1.6.1)
17
+ mini_portile (~> 0.5.0)
18
+ open_xml_package (0.0.1)
19
+ rubyzip
20
+ pry (0.9.12)
21
+ coderay (~> 1.0.5)
22
+ method_source (~> 0.8)
23
+ slop (~> 3.4)
24
+ rake (10.0.4)
25
+ rr (1.1.2)
26
+ rubyzip (1.1.3)
27
+ shoulda-context (1.1.6)
28
+ simplecov (0.7.1)
29
+ multi_json (~> 1.0)
30
+ simplecov-html (~> 0.7.1)
31
+ simplecov-html (0.7.1)
32
+ slop (3.4.4)
33
+ timecop (0.6.3)
34
+ turn (0.9.6)
35
+ ansi
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ pry
42
+ rake
43
+ rocx!
44
+ rr
45
+ shoulda-context
46
+ simplecov
47
+ timecop
48
+ turn
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Gene Doyel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # Rocx [![Code Climate](https://codeclimate.com/github/genebot/rocx.png)](https://codeclimate.com/github/genebot/rocx)
2
+
3
+ Rocx is a gem for creating .docx (Microsoft Word 2007) files. Using a simple API, you can create Word documents with:
4
+
5
+ - Paragraphs
6
+ - Titles
7
+ - Headings
8
+ - Page breaks
9
+ - Bullet points
10
+ - Tables
11
+
12
+ And, coming soon, images and more.
13
+
14
+ ## Installation
15
+
16
+ Add to your Rails application in the Gemfile:
17
+
18
+ gem 'rocx'
19
+
20
+ And then run:
21
+
22
+ bundle install
23
+
24
+ Or install it yourself:
25
+
26
+ gem install rocx
27
+
28
+ ## Contribute!
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = false
9
+ end
@@ -0,0 +1,8 @@
1
+ require "rocx/elements"
2
+ require "rocx/package"
3
+ require "rocx/parts"
4
+ require "rocx/properties"
5
+ require "rocx/style"
6
+
7
+ module Rocx
8
+ end
@@ -0,0 +1,10 @@
1
+ module Rocx
2
+ module Elements
3
+ end
4
+ end
5
+
6
+ require "rocx/elements/base_element"
7
+ require "rocx/elements/base_container"
8
+ Dir.glob("#{File.join(File.dirname(__FILE__), "elements", "*.rb")}").each do |file|
9
+ require file
10
+ end
@@ -0,0 +1,65 @@
1
+ module Rocx
2
+ module Elements
3
+ class BaseContainer < BaseElement
4
+ attr_reader :children
5
+
6
+ class << self
7
+ def properties_tag(*args)
8
+ @properties_tag = args.first if args.any?
9
+ @properties_tag
10
+ end
11
+
12
+ def property(name)
13
+ attr_reader name
14
+
15
+ define_method "#{name}=" do |value|
16
+ class_name = name.to_s.split("_").map(&:capitalize).join
17
+ prop_class = Rocx::Properties.const_get class_name
18
+ instance_variable_set "@#{name}", prop_class.new(value)
19
+ end
20
+
21
+ properties << name
22
+ end
23
+
24
+ def properties
25
+ @properties ||= []
26
+ end
27
+ end
28
+
29
+ def initialize(**args)
30
+ @children = []
31
+ super args
32
+ end
33
+
34
+ def <<(child)
35
+ children << child
36
+ end
37
+
38
+ def to_xml(xml)
39
+ (namespace ? xml[namespace] : xml).public_send(tag_name, xml_attributes) {
40
+ property_xml(xml)
41
+ children.each { |child| child.to_xml(xml) }
42
+ }
43
+ end
44
+
45
+ protected
46
+
47
+ def property_xml(xml)
48
+ props = properties.map(&method(:send)).compact
49
+ return if props.empty?
50
+ xml[namespace].public_send(properties_tag) {
51
+ props.each { |prop| prop.to_xml(xml) }
52
+ }
53
+ end
54
+
55
+ def properties
56
+ self.class.properties
57
+ end
58
+
59
+ def properties_tag
60
+ self.class.properties_tag
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,91 @@
1
+ module Rocx
2
+ module Elements
3
+ class BaseElement
4
+ class << self
5
+
6
+ def tag_name(*args)
7
+ @tag_name = args.first if args.any?
8
+ @tag_name
9
+ end
10
+
11
+ def namespace(*args)
12
+ @namespace = args.first if args.any?
13
+ @namespace
14
+ end
15
+
16
+ def attribute(name, limit_to: nil, xml_name: nil, regex: nil)
17
+ attr_reader name
18
+
19
+ if limit_to
20
+ define_method "valid_#{name}" do
21
+ limit_to
22
+ end
23
+
24
+ define_method "valid_#{name}?" do |value|
25
+ send("valid_#{name}").member? value
26
+ end
27
+
28
+ define_method "#{name}=" do |value|
29
+ valid_values = send("valid_#{name}").join(", ")
30
+ raise ArgumentError, "Invalid #{name}; must be one of #{valid_values}" unless send("valid_#{name}?", value)
31
+ instance_variable_set("@#{name}", value)
32
+ end
33
+ elsif regex
34
+ define_method "valid_#{name}?" do |value|
35
+ !!(regex =~ value)
36
+ end
37
+
38
+ define_method "#{name}=" do |value|
39
+ raise ArgumentError, "Invalid #{name}; must match #{regex.inspect}" unless send("valid_#{name}?", value)
40
+ instance_variable_set("@#{name}", value)
41
+ end
42
+ else
43
+ attr_writer name
44
+ end
45
+
46
+ attributes[name] = xml_name || name
47
+ end
48
+
49
+ def attributes
50
+ @attributes ||= {}
51
+ end
52
+ end
53
+
54
+ def initialize(**args)
55
+ args.each do |property, value|
56
+ self.send("#{property}=", value)
57
+ end
58
+ end
59
+
60
+ def to_xml(xml)
61
+ if namespace
62
+ xml[namespace].public_send tag_name, xml_attributes
63
+ else
64
+ xml.public_send tag_name, xml_attributes
65
+ end
66
+ end
67
+
68
+ protected
69
+
70
+ def xml_attributes
71
+ attributes.each_with_object({}) do |(attribute, name), props|
72
+ value = send(attribute)
73
+ props[name] = value unless value.nil?
74
+ end
75
+ end
76
+
77
+ def attributes
78
+ self.class.attributes
79
+ end
80
+
81
+ def tag_name
82
+ self.class.tag_name
83
+ end
84
+
85
+ def namespace
86
+ self.class.namespace
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,10 @@
1
+ module Rocx
2
+ module Elements
3
+ class Break < BaseElement
4
+ tag_name :br
5
+ namespace :w
6
+ attribute :break_type, xml_name: "w:type", limit_to: [nil, :column, :page, :textWrapping]
7
+ attribute :clear, xml_name: "w:clear", limit_to: [nil, :all, :left, :none, :right]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ module Rocx
2
+ module Elements
3
+ class Paragraph < BaseContainer
4
+ tag_name :p
5
+ namespace :w
6
+ properties_tag :pPr
7
+
8
+ property :alignment
9
+ property :auto_adjust_right_indent
10
+ property :auto_space_de
11
+ property :auto_space_dn
12
+ property :bidi
13
+ property :conditional_formatting
14
+ property :contextual_spacing
15
+ property :div_id
16
+ property :keep_lines
17
+ property :keep_next
18
+ property :indentation
19
+ property :mirror_indent
20
+ property :outline_level
21
+ property :overflow_punctuation
22
+ property :page_break_before
23
+ property :snap_to_grid
24
+ property :supress_auto_hyphens
25
+ property :supress_line_numbers
26
+ property :supress_overlap
27
+ property :widow_control
28
+ property :word_wrap
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module Rocx
2
+ module Elements
3
+ class Run < BaseContainer
4
+ tag_name :r
5
+ namespace :w
6
+ properties_tag :rPr
7
+ property :italics
8
+ property :bold
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module Rocx
2
+ module Elements
3
+ class Symbol < BaseElement
4
+ tag_name :sym
5
+ namespace :w
6
+ attribute :font, xml_name: "w:font"
7
+ attribute :char, xml_name: "w:char", regex: /[0-9a-f]{4}/
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ module Rocx
2
+ module Elements
3
+ class Text < BaseElement
4
+ attr_reader :text
5
+
6
+ tag_name :t
7
+ namespace :w
8
+ attribute :space, xml_name: "xml:space", limit_to: [nil, :preserve]
9
+
10
+ def initialize(text, **args)
11
+ @text = text
12
+ super args
13
+ end
14
+
15
+ def to_xml(xml)
16
+ xml[namespace].public_send tag_name, text, xml_attributes
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ require "open_xml_package"
2
+
3
+ module Rocx
4
+ class Package
5
+ attr_reader :content_types,
6
+ :document,
7
+ :global_rels,
8
+ :rels,
9
+ :settings,
10
+ :styles
11
+
12
+ def initialize
13
+ @content_types = Rocx::Parts::ContentTypes.new
14
+ @document = Rocx::Parts::Document.new
15
+ @global_rels = Rocx::Parts::GlobalRels.new
16
+ @rels = Rocx::Parts::Rels.new
17
+ @settings = Rocx::Parts::Settings.new
18
+ @styles = Rocx::Parts::Styles.new
19
+ end
20
+
21
+ def save(path)
22
+ package = OpenXmlPackage.new
23
+ package.add_part "_rels/.rels", global_rels
24
+ package.add_part "[Content_Types].xml", content_types
25
+ package.add_part "word/_rels/document.xml.rels", rels
26
+ package.add_part "word/document.xml", document
27
+ package.add_part "word/settings.xml", settings
28
+ package.add_part "word/styles.xml", styles
29
+ package.write_to path
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Parts
3
+ end
4
+ end
5
+
6
+ Dir.glob("#{File.join(File.dirname(__FILE__), "parts", "*.rb")}").each do |file|
7
+ require file
8
+ end