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
@@ -1,10 +1,11 @@
1
1
  module Rocx
2
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}/
3
+ class Symbol < Element
4
+ tag :sym
5
+
6
+ attribute :font, expects: :string
7
+ attribute :character, expects: :hex_digit_4, displays_as: :char
8
+
8
9
  end
9
10
  end
10
11
  end
@@ -0,0 +1,6 @@
1
+ module Rocx
2
+ module Elements
3
+ class Tab < Element
4
+ end
5
+ end
6
+ end
@@ -1,19 +1,26 @@
1
1
  module Rocx
2
2
  module Elements
3
- class Text < BaseElement
3
+ class Text < Element
4
4
  attr_reader :text
5
5
 
6
- tag_name :t
7
- namespace :w
8
- attribute :space, xml_name: "xml:space", limit_to: [nil, :preserve]
6
+ tag :t
9
7
 
10
- def initialize(text, **args)
8
+ attribute :space, expects: :valid_space_preserve, namespace: :xml
9
+
10
+ VALID_SPACE_PRESERVES = [:preserve, nil]
11
+
12
+ def initialize(text=nil)
11
13
  @text = text
12
- super args
13
14
  end
14
15
 
15
16
  def to_xml(xml)
16
- xml[namespace].public_send tag_name, text, xml_attributes
17
+ xml["w"].public_send(tag, text, xml_attributes)
18
+ end
19
+
20
+ private
21
+
22
+ def valid_space_preserve(value)
23
+ valid_in? value, VALID_SPACE_PRESERVES
17
24
  end
18
25
 
19
26
  end
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Border < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  attr_reader :tag
7
5
 
8
6
  attribute :color, expects: :hex_color
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Color < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  attribute :color, expects: :hex_color, displays_as: :val
7
5
  attribute :theme_color, expects: :valid_theme_color
8
6
  attribute :theme_shade, expects: :hex_digit
@@ -1,6 +1,7 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class ComplexProperty < BaseProperty
4
+ include AttributeBuilder
4
5
 
5
6
  def to_xml(xml)
6
7
  return unless render?
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class ConditionalFormatting < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :cnfStyle
7
5
 
8
6
  attribute :even_horizontal_band, expects: :true_or_false, displays_as: :evenHBand
@@ -1,16 +1,14 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class EastAsianLayout < ComplexProperty
4
- include AttributeBuilder
5
-
6
- VALID_BRACKETS = %i(angle curly none round square)
7
-
8
4
  attribute :combine, expects: :on_or_off
9
5
  attribute :combine_brackets, expects: :valid_bracket
10
6
  attribute :id, expects: :integer
11
7
  attribute :vertical, expects: :on_or_off, displays_as: :vert
12
8
  attribute :vertical_compress, expects: :on_or_off, displays_as: :vertCompress
13
9
 
10
+ VALID_BRACKETS = %i(angle curly none round square)
11
+
14
12
  private
15
13
 
16
14
  def valid_bracket(value)
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Font < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :rFonts
7
5
 
8
6
  attribute :ascii, expects: :string
@@ -1,17 +1,8 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Frame < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :framePr
7
5
 
8
- VALID_ANCHORS = %i(margin page text)
9
- VALID_DROP_CAPS = %i(drop margin none)
10
- VALID_HEIGHT_RULES = %i(atLeast auto exact)
11
- VALID_RELATIVE_HORIZONTAL_POSITIONS = %i(center inside left outside right)
12
- VALID_RELATIVE_VERTICAL_POSITIONS = %i(bottom center inline inside outside top)
13
- VALID_WRAPS = %i(around auto none notBeside through tight)
14
-
15
6
  attribute :anchor_lock, expects: :true_or_false
16
7
  attribute :drop_cap, expects: :valid_drop_cap
17
8
  attribute :height, expects: :positive_integer, displays_as: :h
@@ -28,6 +19,13 @@ module Rocx
28
19
  attribute :width, expects: :positive_integer, displays_as: :w
29
20
  attribute :wrap, expects: :valid_wrap
30
21
 
22
+ VALID_ANCHORS = %i(margin page text)
23
+ VALID_DROP_CAPS = %i(drop margin none)
24
+ VALID_HEIGHT_RULES = %i(atLeast auto exact)
25
+ VALID_RELATIVE_HORIZONTAL_POSITIONS = %i(center inside left outside right)
26
+ VALID_RELATIVE_VERTICAL_POSITIONS = %i(bottom center inline inside outside top)
27
+ VALID_WRAPS = %i(around auto none notBeside through tight)
28
+
31
29
  private
32
30
 
33
31
  def valid_drop_cap(value)
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Indentation < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :ind
7
5
 
8
6
  attribute :end, expects: :integer
@@ -1,9 +1,8 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Language < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :lang
5
+
7
6
  attribute :bidi, expects: :valid_language
8
7
  attribute :east_asia, expects: :valid_language
9
8
  attribute :latin, expects: :valid_language, displays_as: :val
@@ -1,12 +1,11 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class ManualWidth < ComplexProperty
4
- include AttributeBuilder
4
+ tag :fitText
5
5
 
6
6
  attribute :id, expects: :integer
7
7
  attribute :width, expects: :positive_integer, displays_as: :val
8
8
 
9
- tag :fitText
10
9
  end
11
10
  end
12
11
  end
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Numbering < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :numPr
7
5
 
8
6
  attribute :level, expects: :positive_integer, displays_as: :ilvl
@@ -0,0 +1,13 @@
1
+ module Rocx
2
+ module Properties
3
+ class PhoneticGuideAlignment < ValueProperty
4
+ tag :rubyAlign
5
+ name "alignment"
6
+
7
+ def ok_values
8
+ %i(center distributeLetter distributeSpace left right rightVertical)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Properties
3
+ class PhoneticGuideBaseFontSize < PositiveIntegerProperty
4
+ tag :hpsBaseText
5
+ name "base_font_size"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Properties
3
+ class PhoneticGuideFontSize < PositiveIntegerProperty
4
+ tag :hps
5
+ name "font_size"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Rocx
2
+ module Properties
3
+ class PhoneticGuideFontSizeRaise < PositiveIntegerProperty
4
+ tag :hpsRaise
5
+ name "font_size_raise"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Rocx
2
+ module Properties
3
+ class PhoneticGuideLanguage < ValueProperty
4
+ tag :lid
5
+ name "language"
6
+
7
+ def valid?
8
+ value =~ /[a-z]{2}-[A-Z]{2}/
9
+ end
10
+
11
+ def invalid_message
12
+ "Invalid #{name}: must be a language code (RFC 4646)"
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Shading < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :shd
7
5
 
8
6
  VALID_SHADING_PATTERNS = %i(clear
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Spacing < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  VALID_LINE_RULES = %i(atLeast auto exact)
7
5
 
8
6
  attribute :after, expects: :positive_integer
@@ -1,8 +1,6 @@
1
1
  module Rocx
2
2
  module Properties
3
3
  class Underline < ComplexProperty
4
- include AttributeBuilder
5
-
6
4
  tag :u
7
5
 
8
6
  VALID_UNDERLINE_TYPES = %i(dash dashDotDotHeavy dashDotHeavy dashedHeavy dashLong dashLongHeavy dotDash dotDotDash dotted dottedHeavy double none single thick wave wavyDouble wavyHeavy words)
@@ -13,7 +13,7 @@ module Rocx
13
13
  end
14
14
 
15
15
  def invalid_message
16
- "Invalid value for #{name}; acceptable values are #{ok_values.join(", ")}"
16
+ "Invalid value for #{name}; acceptable values are #{ok_values.join(", ")} (provided: #{value.inspect})"
17
17
  end
18
18
 
19
19
  def render?
data/lib/rocx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rocx
2
- VERSION = "0.5.8"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ describe Rocx::Elements::AbsolutePositionTab do
4
+ include ElementTestMacros
5
+
6
+ it_should_use tag: :ptab, name: "absolute_position_tab"
7
+
8
+ for_attribute(:alignment) do
9
+ with_value(:center) do
10
+ it_should_assign_successfully
11
+ it_should_output "<w:ptab w:alignment=\"center\"/>"
12
+ end
13
+
14
+ with_value(:left) do
15
+ it_should_assign_successfully
16
+ it_should_output "<w:ptab w:alignment=\"left\"/>"
17
+ end
18
+
19
+ with_value(:right) do
20
+ it_should_assign_successfully
21
+ it_should_output "<w:ptab w:alignment=\"right\"/>"
22
+ end
23
+
24
+ with_value(:somethingElse) do
25
+ it_should_raise_an_exception
26
+ end
27
+ end
28
+
29
+ for_attribute(:leader) do
30
+ with_value(:dot) do
31
+ it_should_assign_successfully
32
+ it_should_output "<w:ptab w:leader=\"dot\"/>"
33
+ end
34
+
35
+ with_value(:hyphen) do
36
+ it_should_assign_successfully
37
+ it_should_output "<w:ptab w:leader=\"hyphen\"/>"
38
+ end
39
+
40
+ with_value(:middleDot) do
41
+ it_should_assign_successfully
42
+ it_should_output "<w:ptab w:leader=\"middleDot\"/>"
43
+ end
44
+
45
+ with_value(:none) do
46
+ it_should_assign_successfully
47
+ it_should_output "<w:ptab w:leader=\"none\"/>"
48
+ end
49
+
50
+ with_value(:underscore) do
51
+ it_should_assign_successfully
52
+ it_should_output "<w:ptab w:leader=\"underscore\"/>"
53
+ end
54
+
55
+ with_value(:somethingElse) do
56
+ it_should_raise_an_exception
57
+ end
58
+ end
59
+
60
+ for_attribute(:relativeTo) do
61
+ with_value(:indent) do
62
+ it_should_assign_successfully
63
+ it_should_output "<w:ptab w:relativeTo=\"indent\"/>"
64
+ end
65
+
66
+ with_value(:margin) do
67
+ it_should_assign_successfully
68
+ it_should_output "<w:ptab w:relativeTo=\"margin\"/>"
69
+ end
70
+
71
+ with_value(:somethingElse) do
72
+ it_should_raise_an_exception
73
+ end
74
+ end
75
+
76
+ with_no_attributes_set do
77
+ it_should_output "<w:ptab/>", assign: false
78
+ end
79
+
80
+ with_these_attributes_set(alignment: :center, relativeTo: :margin) do
81
+ it_should_output "<w:ptab w:alignment=\"center\" w:relativeTo=\"margin\"/>", assign: false
82
+ end
83
+
84
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Rocx::Elements::BidiEmbed do
4
+ include ElementTestMacros
5
+
6
+ it_should_use tag: :dir, name: "bidi_embed"
7
+
8
+ for_attribute(:direction) do
9
+ with_value(:rtl) do
10
+ it_should_assign_successfully
11
+ it_should_output "<w:dir w:val=\"rtl\"/>"
12
+ end
13
+
14
+ with_value(:ltr) do
15
+ it_should_assign_successfully
16
+ it_should_output "<w:dir w:val=\"ltr\"/>"
17
+ end
18
+ end
19
+
20
+ context "with children" do
21
+ before(:each) do
22
+ @instance = described_class.new
23
+ text = Rocx::Elements::Text.new("Smucker's Preserves")
24
+ run = Rocx::Elements::Run.new
25
+ run << text
26
+ instance << run
27
+ end
28
+
29
+ it_should_output "<w:dir>\n <w:r>\n <w:t>Smucker's Preserves</w:t>\n </w:r>\n </w:dir>", assign: false
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Rocx::Elements::BidiOverride do
4
+ include ElementTestMacros
5
+
6
+ it_should_use tag: :bdo, name: "bidi_override"
7
+
8
+ for_attribute(:direction) do
9
+ with_value(:rtl) do
10
+ it_should_assign_successfully
11
+ it_should_output "<w:bdo w:val=\"rtl\"/>"
12
+ end
13
+
14
+ with_value(:ltr) do
15
+ it_should_assign_successfully
16
+ it_should_output "<w:bdo w:val=\"ltr\"/>"
17
+ end
18
+ end
19
+
20
+ context "with children" do
21
+ before(:each) do
22
+ @instance = described_class.new
23
+ text = Rocx::Elements::Text.new("Smucker's Preserves")
24
+ run = Rocx::Elements::Run.new
25
+ run << text
26
+ instance << run
27
+ end
28
+
29
+ it_should_output "<w:bdo>\n <w:r>\n <w:t>Smucker's Preserves</w:t>\n </w:r>\n </w:bdo>", assign: false
30
+ end
31
+ end