rocx 0.5.8 → 0.6.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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +55 -22
  4. data/Rakefile +1 -0
  5. data/lib/rocx.rb +1 -0
  6. data/lib/rocx/attribute_builder.rb +296 -0
  7. data/lib/rocx/elements.rb +3 -2
  8. data/lib/rocx/elements/absolute_position_tab.rb +29 -0
  9. data/lib/rocx/elements/bidi_embed.rb +17 -0
  10. data/lib/rocx/elements/bidi_override.rb +17 -0
  11. data/lib/rocx/elements/break.rb +19 -5
  12. data/lib/rocx/elements/carriage_return.rb +8 -0
  13. data/lib/rocx/elements/{base_container.rb → container.rb} +21 -16
  14. data/lib/rocx/elements/content_part.rb +8 -0
  15. data/lib/rocx/elements/control.rb +10 -0
  16. data/lib/rocx/elements/deleted_text.rb +8 -0
  17. data/lib/rocx/elements/dirty.rb +6 -0
  18. data/lib/rocx/elements/element.rb +62 -0
  19. data/lib/rocx/elements/last_rendered_page_break.rb +6 -0
  20. data/lib/rocx/elements/long_day.rb +8 -0
  21. data/lib/rocx/elements/long_month.rb +8 -0
  22. data/lib/rocx/elements/long_year.rb +8 -0
  23. data/lib/rocx/elements/nonbreaking_hyphen.rb +7 -0
  24. data/lib/rocx/elements/optional_hyphen.rb +8 -0
  25. data/lib/rocx/elements/page_number.rb +7 -0
  26. data/lib/rocx/elements/paragraph.rb +2 -4
  27. data/lib/rocx/elements/ruby.rb +46 -0
  28. data/lib/rocx/elements/run.rb +2 -4
  29. data/lib/rocx/elements/short_day.rb +8 -0
  30. data/lib/rocx/elements/short_month.rb +8 -0
  31. data/lib/rocx/elements/short_year.rb +8 -0
  32. data/lib/rocx/elements/symbol.rb +6 -5
  33. data/lib/rocx/elements/tab.rb +6 -0
  34. data/lib/rocx/elements/text.rb +14 -7
  35. data/lib/rocx/properties/border.rb +0 -2
  36. data/lib/rocx/properties/color.rb +0 -2
  37. data/lib/rocx/properties/complex_property.rb +1 -0
  38. data/lib/rocx/properties/conditional_formatting.rb +0 -2
  39. data/lib/rocx/properties/east_asian_layout.rb +2 -4
  40. data/lib/rocx/properties/font.rb +0 -2
  41. data/lib/rocx/properties/frame.rb +7 -9
  42. data/lib/rocx/properties/indentation.rb +0 -2
  43. data/lib/rocx/properties/language.rb +1 -2
  44. data/lib/rocx/properties/manual_width.rb +1 -2
  45. data/lib/rocx/properties/numbering.rb +0 -2
  46. data/lib/rocx/properties/phonetic_guide_alignment.rb +13 -0
  47. data/lib/rocx/properties/phonetic_guide_base_font_size.rb +8 -0
  48. data/lib/rocx/properties/phonetic_guide_font_size.rb +8 -0
  49. data/lib/rocx/properties/phonetic_guide_font_size_raise.rb +8 -0
  50. data/lib/rocx/properties/phonetic_guide_language.rb +17 -0
  51. data/lib/rocx/properties/shading.rb +0 -2
  52. data/lib/rocx/properties/spacing.rb +0 -2
  53. data/lib/rocx/properties/underline.rb +0 -2
  54. data/lib/rocx/properties/value_property.rb +1 -1
  55. data/lib/rocx/version.rb +1 -1
  56. data/spec/elements/absolute_position_tab_spec.rb +84 -0
  57. data/spec/elements/bidi_embed_spec.rb +31 -0
  58. data/spec/elements/bidi_override_spec.rb +31 -0
  59. data/spec/elements/break_spec.rb +32 -24
  60. data/spec/elements/carriage_return_spec.rb +12 -0
  61. data/spec/elements/content_part_spec.rb +18 -0
  62. data/spec/elements/control_spec.rb +41 -0
  63. data/spec/elements/deleted_text_spec.rb +24 -0
  64. data/spec/elements/dirty_spec.rb +12 -0
  65. data/spec/elements/last_rendered_page_break_spec.rb +12 -0
  66. data/spec/elements/long_day_spec.rb +12 -0
  67. data/spec/elements/long_month_spec.rb +12 -0
  68. data/spec/elements/long_year_spec.rb +12 -0
  69. data/spec/elements/nonbreaking_hyphen_spec.rb +12 -0
  70. data/spec/elements/optional_hyphen_spec.rb +12 -0
  71. data/spec/elements/page_number_spec.rb +12 -0
  72. data/spec/elements/paragraph_spec.rb +9 -7
  73. data/spec/elements/ruby_spec.rb +66 -0
  74. data/spec/elements/run_spec.rb +2 -2
  75. data/spec/elements/short_day_spec.rb +12 -0
  76. data/spec/elements/short_month_spec.rb +12 -0
  77. data/spec/elements/short_year_spec.rb +12 -0
  78. data/spec/elements/symbol_spec.rb +20 -10
  79. data/spec/elements/tab_spec.rb +12 -0
  80. data/spec/elements/text_spec.rb +12 -11
  81. data/spec/properties/phonetic_guide_alignment_spec.rb +42 -0
  82. data/spec/properties/phonetic_guide_base_font_size_spec.rb +25 -0
  83. data/spec/properties/phonetic_guide_font_size_raise_spec.rb +25 -0
  84. data/spec/properties/phonetic_guide_font_size_spec.rb +25 -0
  85. data/spec/properties/phonetic_guide_language_spec.rb +12 -0
  86. data/spec/support/element_test_macros.rb +135 -4
  87. metadata +55 -5
  88. data/lib/rocx/elements/base_element.rb +0 -91
  89. data/lib/rocx/properties/attribute_builder.rb +0 -289
@@ -0,0 +1,17 @@
1
+ module Rocx
2
+ module Elements
3
+ class BidiEmbed < Container
4
+ tag :dir
5
+ attribute :direction, expects: :valid_direction, displays_as: :val
6
+
7
+ VALID_DIRECTIONS = %i(rtl ltr)
8
+
9
+ private
10
+
11
+ def valid_direction(value)
12
+ valid_in? value, VALID_DIRECTIONS
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Rocx
2
+ module Elements
3
+ class BidiOverride < Container
4
+ tag :bdo
5
+ attribute :direction, expects: :valid_direction, displays_as: :val
6
+
7
+ VALID_DIRECTIONS = %i(rtl ltr)
8
+
9
+ private
10
+
11
+ def valid_direction(value)
12
+ valid_in? value, VALID_DIRECTIONS
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -1,10 +1,24 @@
1
1
  module Rocx
2
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]
3
+ class Break < Element
4
+ tag :br
5
+
6
+ attribute :clear, expects: :valid_clear
7
+ attribute :type, expects: :valid_break_type
8
+
9
+ VALID_CLEARS = %i(all left none right)
10
+ VALID_BREAK_TYPES = %i(column page textWrapping)
11
+
12
+ private
13
+
14
+ def valid_clear(value)
15
+ valid_in? value, VALID_CLEARS
16
+ end
17
+
18
+ def valid_break_type(value)
19
+ valid_in? value, VALID_BREAK_TYPES
20
+ end
21
+
8
22
  end
9
23
  end
10
24
  end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class CarriageReturn < Element
4
+ tag :cr
5
+
6
+ end
7
+ end
8
+ end
@@ -1,6 +1,6 @@
1
1
  module Rocx
2
2
  module Elements
3
- class BaseContainer < BaseElement
3
+ class Container < Element
4
4
  attr_reader :children
5
5
 
6
6
  class << self
@@ -9,24 +9,28 @@ module Rocx
9
9
  @properties_tag
10
10
  end
11
11
 
12
- def value_property(name)
12
+ def value_property(name, as: nil)
13
13
  attr_reader name
14
14
 
15
+ properties[name] = (as || name).to_s
16
+
15
17
  class_eval <<-CODE, __FILE__, __LINE__ + 1
16
18
  def #{name}=(value)
17
- class_name = "#{name}".split.map(&:capitalize).join
19
+ property_key = "#{name}".to_sym
20
+ class_name = properties[property_key].split("_").map(&:capitalize).join
18
21
  prop_class = Rocx::Properties.const_get class_name
19
22
  instance_variable_set "@#{name}", prop_class.new(value)
20
23
  end
21
24
  CODE
22
-
23
- properties << name
24
25
  end
25
26
 
26
- def property(name)
27
+ def property(name, as: nil)
28
+ properties[name] = (as || name).to_s
29
+
27
30
  class_eval <<-CODE, __FILE__, __LINE__ + 1
28
31
  def #{name}
29
- class_name = "#{name}".split("_").map(&:capitalize).join
32
+ property_key = "#{name}".to_sym
33
+ class_name = properties[property_key].split("_").map(&:capitalize).join
30
34
  prop_class = Rocx::Properties.const_get class_name
31
35
 
32
36
  if instance_variable_get("@#{name}").nil?
@@ -36,18 +40,15 @@ module Rocx
36
40
  instance_variable_get "@#{name}"
37
41
  end
38
42
  CODE
39
-
40
- properties << name
41
43
  end
42
44
 
43
45
  def properties
44
- @properties ||= []
46
+ @properties ||= {}
45
47
  end
46
48
  end
47
49
 
48
- def initialize(**args)
50
+ def initialize
49
51
  @children = []
50
- super args
51
52
  end
52
53
 
53
54
  def <<(child)
@@ -55,16 +56,16 @@ module Rocx
55
56
  end
56
57
 
57
58
  def to_xml(xml)
58
- (namespace ? xml[namespace] : xml).public_send(tag_name, xml_attributes) {
59
+ xml[namespace].public_send(tag, xml_attributes) {
59
60
  property_xml(xml)
60
61
  children.each { |child| child.to_xml(xml) }
61
62
  }
62
63
  end
63
64
 
64
- protected
65
+ private
65
66
 
66
67
  def property_xml(xml)
67
- props = properties.map(&method(:send)).compact
68
+ props = properties.keys.map(&method(:send)).compact
68
69
  return if props.none?(&:render?)
69
70
  xml[namespace].public_send(properties_tag) {
70
71
  props.each { |prop| prop.to_xml(xml) }
@@ -76,7 +77,11 @@ module Rocx
76
77
  end
77
78
 
78
79
  def properties_tag
79
- self.class.properties_tag
80
+ self.class.properties_tag || default_properties_tag
81
+ end
82
+
83
+ def default_properties_tag
84
+ :"#{tag}Pr"
80
85
  end
81
86
 
82
87
  end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class ContentPart < Element
4
+ attribute :id, expects: :string, namespace: :r
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module Rocx
2
+ module Elements
3
+ class Control < Element
4
+ attribute :id, expects: :string, namespace: :r
5
+ attribute :unique_name, expects: :string, displays_as: :name
6
+ attribute :shape_id, expects: :string, displays_as: :shapeid
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class DeletedText < Text
4
+ tag :del
5
+ attribute :space, expects: :valid_space_preserve, namespace: :xml
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Rocx
2
+ module Elements
3
+ class Dirty < Element
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,62 @@
1
+ module Rocx
2
+ module Elements
3
+ class Element
4
+ include AttributeBuilder
5
+
6
+ class << self
7
+ attr_reader :property_name
8
+
9
+ def tag(*args)
10
+ @tag = args.first if args.any?
11
+ @tag
12
+ end
13
+
14
+ def name(*args)
15
+ @property_name = args.first if args.any?
16
+ @name
17
+ end
18
+
19
+ def namespace(*args)
20
+ @namespace = args.first if args.any?
21
+ @namespace
22
+ end
23
+
24
+ end
25
+
26
+ def tag
27
+ self.class.tag || default_tag
28
+ end
29
+
30
+ def name
31
+ self.class.property_name || default_name
32
+ end
33
+
34
+ def namespace
35
+ self.class.namespace || default_namespace
36
+ end
37
+
38
+ def to_xml(xml)
39
+ xml["w"].public_send(tag, xml_attributes)
40
+ end
41
+
42
+ private
43
+
44
+ def default_tag
45
+ (class_name[0, 1].downcase + class_name[1..-1]).to_sym
46
+ end
47
+
48
+ def default_name
49
+ class_name.gsub(/(.)([A-Z])/, '\1_\2').downcase
50
+ end
51
+
52
+ def default_namespace
53
+ :w
54
+ end
55
+
56
+ def class_name
57
+ self.class.to_s.split(/::/).last
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,6 @@
1
+ module Rocx
2
+ module Elements
3
+ class LastRenderedPageBreak < Element
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class LongDay < Element
4
+ tag :dayLong
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class LongMonth < Element
4
+ tag :monthLong
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class LongYear < Element
4
+ tag :yearLong
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Rocx
2
+ module Elements
3
+ class NonbreakingHyphen < Element
4
+ tag :noBreakHyphen
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class OptionalHyphen < Element
4
+ tag :softHyphen
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Rocx
2
+ module Elements
3
+ class PageNumber < Element
4
+ tag :pgNum
5
+ end
6
+ end
7
+ end
@@ -1,9 +1,7 @@
1
1
  module Rocx
2
2
  module Elements
3
- class Paragraph < BaseContainer
4
- tag_name :p
5
- namespace :w
6
- properties_tag :pPr
3
+ class Paragraph < Container
4
+ tag :p
7
5
 
8
6
  value_property :alignment
9
7
  value_property :auto_adjust_right_indent
@@ -0,0 +1,46 @@
1
+ module Rocx
2
+ module Elements
3
+ class Ruby < Container
4
+ attr_reader :base, :ruby
5
+
6
+ value_property :alignment, as: :phonetic_guide_alignment
7
+ value_property :base_font_size, as: :phonetic_guide_base_font_size
8
+ value_property :font_size, as: :phonetic_guide_font_size
9
+ value_property :font_size_raise, as: :phonetic_guide_font_size_raise
10
+ value_property :language, as: :phonetic_guide_language
11
+
12
+ def base=(other)
13
+ raise ArgumentError unless other.is_a?(Rocx::Elements::Run)
14
+ @base = other
15
+ end
16
+
17
+ def ruby=(other)
18
+ raise ArgumentError unless other.is_a?(Rocx::Elements::Run)
19
+ @ruby = other
20
+ end
21
+
22
+ def to_xml(xml)
23
+ xml[namespace].public_send(tag, xml_attributes) {
24
+ property_xml(xml)
25
+ ruby_text_xml(xml)
26
+ base_text_xml(xml)
27
+ }
28
+ end
29
+
30
+ private
31
+
32
+ def ruby_text_xml(xml)
33
+ xml["w"].rt {
34
+ ruby.to_xml(xml) if ruby
35
+ }
36
+ end
37
+
38
+ def base_text_xml(xml)
39
+ xml["w"].rubyBase {
40
+ base.to_xml(xml) if base
41
+ }
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -1,9 +1,7 @@
1
1
  module Rocx
2
2
  module Elements
3
- class Run < BaseContainer
4
- tag_name :r
5
- namespace :w
6
- properties_tag :rPr
3
+ class Run < Container
4
+ tag :r
7
5
 
8
6
  value_property :bold
9
7
  value_property :caps
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class ShortDay < Element
4
+ tag :dayShort
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class ShortMonth < Element
4
+ tag :monthShort
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Elements
3
+ class ShortYear < Element
4
+ tag :yearShort
5
+
6
+ end
7
+ end
8
+ end