ooxml_parser 0.33.0 → 0.34.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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/docx_drawing.rb +9 -0
  3. data/lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/docx_drawing_properties.rb +6 -2
  4. data/lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/drawing_properties/ooxml_coordinates.rb +15 -9
  5. data/lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/docx_graphic.rb +2 -1
  6. data/lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/shape/shape_properties/custom_geometry/docx_custom_geometry/docx_shape_line_path/docx_shape_line_element.rb +1 -1
  7. data/lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/shape/shape_properties/docx_shape_size.rb +4 -4
  8. data/lib/ooxml_parser/common_parser/common_data/borders_properties.rb +2 -2
  9. data/lib/ooxml_parser/common_parser/common_data/color/color_helper.rb +14 -13
  10. data/lib/ooxml_parser/common_parser/common_data/color.rb +17 -13
  11. data/lib/ooxml_parser/common_parser/common_data/colors/color_alpha_channel.rb +19 -5
  12. data/lib/ooxml_parser/common_parser/common_data/colors/color_properties.rb +34 -12
  13. data/lib/ooxml_parser/common_parser/common_data/colors/image_fill.rb +2 -2
  14. data/lib/ooxml_parser/common_parser/common_data/hyperlink.rb +4 -0
  15. data/lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb +22 -1
  16. data/lib/ooxml_parser/common_parser/common_data/paragraph/paragraph_properties/numbering_properties.rb +18 -4
  17. data/lib/ooxml_parser/common_parser/common_data/paragraph/paragraph_properties.rb +4 -2
  18. data/lib/ooxml_parser/common_parser/common_data/paragraph/paragraph_run/run_properties/run_fonts.rb +26 -0
  19. data/lib/ooxml_parser/common_parser/common_data/paragraph/paragraph_run/run_properties.rb +12 -3
  20. data/lib/ooxml_parser/common_parser/common_data/table/margins/table_margins.rb +4 -4
  21. data/lib/ooxml_parser/common_parser/common_data/table/row/cell/cell_properties.rb +14 -4
  22. data/lib/ooxml_parser/common_parser/common_data/table/table_properties.rb +3 -2
  23. data/lib/ooxml_parser/common_parser/common_data/underline.rb +22 -1
  24. data/lib/ooxml_parser/common_parser/common_data/valued_child.rb +14 -0
  25. data/lib/ooxml_parser/common_parser/parser/ooxml_file.rb +10 -0
  26. data/lib/ooxml_parser/docx_parser/document_structure/default_style_helper.rb +79 -0
  27. data/lib/ooxml_parser/docx_parser/document_structure/document_background.rb +6 -1
  28. data/lib/ooxml_parser/docx_parser/document_structure/document_style.rb +9 -3
  29. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_formula/accent.rb +9 -2
  30. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_formula/bar.rb +9 -3
  31. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_formula/group_char.rb +25 -4
  32. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_formula/matrix.rb +2 -1
  33. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_paragraph_run/docx_paragraph_run_helpers.rb +21 -13
  34. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_paragraph_run/field_char.rb +22 -0
  35. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/docx_paragraph_run.rb +3 -1
  36. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/sdt/sdt_properties/form_properties.rb +1 -1
  37. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph/sdt/sdt_properties/form_text_properties.rb +2 -2
  38. data/lib/ooxml_parser/docx_parser/document_structure/docx_paragraph.rb +6 -5
  39. data/lib/ooxml_parser/docx_parser/document_structure/header_footer.rb +14 -2
  40. data/lib/ooxml_parser/docx_parser/document_structure/page_properties/columns.rb +2 -2
  41. data/lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb +10 -11
  42. data/lib/ooxml_parser/docx_parser/document_structure/page_properties/page_properties/header_footer_reference.rb +27 -0
  43. data/lib/ooxml_parser/docx_parser/document_structure/page_properties/page_properties/page_borders.rb +26 -0
  44. data/lib/ooxml_parser/docx_parser/document_structure/page_properties/page_properties.rb +20 -12
  45. data/lib/ooxml_parser/docx_parser/document_structure/styles/document_defaults/paragraph_properties_default.rb +3 -0
  46. data/lib/ooxml_parser/docx_parser/document_structure/styles.rb +6 -0
  47. data/lib/ooxml_parser/docx_parser/document_structure.rb +2 -69
  48. data/lib/ooxml_parser/pptx_parser/presentation/presentation_comments/presentation_comment.rb +1 -1
  49. data/lib/ooxml_parser/pptx_parser/presentation/slide/graphic_frame/graphic_frame/chart_reference.rb +22 -0
  50. data/lib/ooxml_parser/pptx_parser/presentation/slide/graphic_frame/graphic_frame.rb +4 -1
  51. data/lib/ooxml_parser/version.rb +1 -1
  52. data/lib/ooxml_parser/xlsx_parser/workbook/pivot_table_definition/location.rb +6 -6
  53. data/lib/ooxml_parser/xlsx_parser/workbook/style_sheet/cell_xfs/cell_style/protection.rb +2 -2
  54. data/lib/ooxml_parser/xlsx_parser/workbook/style_sheet/cell_xfs/xf.rb +5 -5
  55. data/lib/ooxml_parser/xlsx_parser/workbook/workbook_properties.rb +1 -1
  56. data/lib/ooxml_parser/xlsx_parser/workbook/worksheet/sheet_view.rb +2 -2
  57. data/lib/ooxml_parser/xlsx_parser/workbook/worksheet/table_part.rb +9 -1
  58. data/lib/ooxml_parser/xlsx_parser/workbook/worksheet/xlsx_column_properties.rb +2 -2
  59. data/lib/ooxml_parser/xlsx_parser/workbook/worksheet/xlsx_header_footer.rb +4 -4
  60. data/lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb +2 -1
  61. data/lib/ooxml_parser/xlsx_parser/workbook.rb +1 -1
  62. metadata +9 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a2c8bb2410020cc3dfcb2126b9888ef47530fbd761e6167bd917fffee6a1351
4
- data.tar.gz: 8ae388b40aabe9e72995e1bb5fa747e73253cd4464e414a00db0dcd5a87b0b40
3
+ metadata.gz: 0e89bbef1bb5d024074a4c344543e90b00a87f12699c65c43fb43b2dbcc9cd55
4
+ data.tar.gz: 34d0c7a756143e4d9eee8f5276c369f2d55e8776cba0f9c018f6ad7d54d40b2a
5
5
  SHA512:
6
- metadata.gz: 3ce96605c0637a4b2865c96088c0c10ddc898e91e7b6d41234bf8da01b598b748416567aff90cfa33b6778b7f16b7bb9397d95e5dde75e551fe524ee6d72dbdb
7
- data.tar.gz: eee336b02e4bc827de7771f46ebcf53907957af7505c7da806dad589fe79f5de56b56e89206bc0e2df1d523530a9a7b41cd7a2c22095da97e95bde953e7faaa9
6
+ metadata.gz: 124354363a41cda9a479196831b71d5e0459e3247f8b4968b46da0aebb2e22d9672263fcee5722db253b748232a5120e56fc8bda5a6b33b40d3727de1f1f8fa2
7
+ data.tar.gz: f7558e9d79eadde76d2609b270579e573490dcb2d36ddaf7be0af06468113ae480c141857bbf0fbd6cfb41df15b549c7520f51b0cb088d75c092f402d4e6aad5
@@ -8,6 +8,8 @@ require_relative 'graphic/docx_graphic'
8
8
  module OoxmlParser
9
9
  # Class for parsing `graphic` tags
10
10
  class DocxDrawing < OOXMLDocumentObject
11
+ # @return [String] id of drawing
12
+ attr_reader :id
11
13
  attr_accessor :type, :properties, :graphic
12
14
  # @return [DocProperties] doc properties
13
15
  attr_accessor :doc_properties
@@ -23,6 +25,13 @@ module OoxmlParser
23
25
  # @param [Nokogiri::XML:Node] node with NumberingProperties
24
26
  # @return [DocxDrawing] result of parsing
25
27
  def parse(node)
28
+ node.attributes.each do |key, value|
29
+ case key
30
+ when 'id'
31
+ @id = value.value.to_s
32
+ end
33
+ end
34
+
26
35
  node.xpath('*').each do |node_child|
27
36
  case node_child.name
28
37
  when 'anchor'
@@ -44,9 +44,13 @@ module OoxmlParser
44
44
  node.xpath('*').each do |content_node_child|
45
45
  case content_node_child.name
46
46
  when 'simplePos'
47
- @simple_position = OOXMLCoordinates.parse(content_node_child)
47
+ @simple_position = OOXMLCoordinates.new(parent: self).parse(content_node_child)
48
48
  when 'extent'
49
- @object_size = OOXMLCoordinates.parse(content_node_child, x_attr: 'cx', y_attr: 'cy', unit: :emu)
49
+ @object_size = OOXMLCoordinates.new(parent: self)
50
+ .parse(content_node_child,
51
+ x_attr: 'cx',
52
+ y_attr: 'cy',
53
+ unit: :emu)
50
54
  when 'positionV'
51
55
  @vertical_position = DocxDrawingPosition.new(parent: self).parse(content_node_child)
52
56
  when 'positionH'
@@ -2,10 +2,10 @@
2
2
 
3
3
  module OoxmlParser
4
4
  # Docx Coordinates
5
- class OOXMLCoordinates
5
+ class OOXMLCoordinates < OOXMLDocumentObject
6
6
  attr_accessor :x, :y
7
7
 
8
- def initialize(x_value, y_value)
8
+ def initialize(x_value = nil, y_value = nil, parent: nil)
9
9
  @x = if x_value.is_a?(OoxmlSize)
10
10
  x_value
11
11
  else
@@ -16,6 +16,7 @@ module OoxmlParser
16
16
  else
17
17
  OoxmlSize.new(y_value)
18
18
  end
19
+ super(parent: parent)
19
20
  end
20
21
 
21
22
  # @return [String] result of convert of object to string
@@ -25,22 +26,27 @@ module OoxmlParser
25
26
 
26
27
  # Compare two OOXMLCoordinates objects
27
28
  # @param other [OOXMLCoordinates] other object
28
- # @return [True, False] result of comparasion
29
+ # @return [True, False] result of comparison
29
30
  def ==(other)
30
31
  x == other.x && y == other.y
31
32
  end
32
33
 
33
34
  # Parse OOXMLCoordinates object
34
- # @param position_node [Nokogiri::XML:Element] node to parse
35
+ # @param node [Nokogiri::XML:Element] node to parse
35
36
  # @param x_attr [String] name of x attribute
36
37
  # @param y_attr [String] name of y attribute
37
38
  # @param unit [Symbol] unit in which data is stored
38
39
  # @return [OOXMLCoordinates] result of parsing
39
- def self.parse(position_node, x_attr: 'x', y_attr: 'y', unit: :dxa)
40
- return if position_node.attribute(x_attr).nil? || position_node.attribute(y_attr).nil?
41
-
42
- OOXMLCoordinates.new(OoxmlSize.new(position_node.attribute(x_attr).value.to_f, unit),
43
- OoxmlSize.new(position_node.attribute(y_attr).value.to_f, unit))
40
+ def parse(node, x_attr: 'x', y_attr: 'y', unit: :dxa)
41
+ node.attributes.each do |key, value|
42
+ case key
43
+ when x_attr
44
+ @x = OoxmlSize.new(value.value.to_f, unit)
45
+ when y_attr
46
+ @y = OoxmlSize.new(value.value.to_f, unit)
47
+ end
48
+ end
49
+ self
44
50
  end
45
51
  end
46
52
  end
@@ -23,7 +23,8 @@ module OoxmlParser
23
23
  @data = DocxPicture.new(parent: self).parse(node_child)
24
24
  when 'chart'
25
25
  @type = :chart
26
- root_object.add_to_xmls_stack("#{root_object.root_subfolder}/#{root_object.get_link_from_rels(node_child.attribute('id').value)}")
26
+ @chart_reference = ChartReference.new(parent: self).parse(node_child)
27
+ root_object.add_to_xmls_stack("#{root_object.root_subfolder}/#{root_object.get_link_from_rels(@chart_reference.id)}")
27
28
  @data = Chart.new(parent: self).parse
28
29
  root_object.xmls_stack.pop
29
30
  when 'wgp'
@@ -30,7 +30,7 @@ module OoxmlParser
30
30
  node.xpath('*').each do |node_child|
31
31
  case node_child.name
32
32
  when 'pt'
33
- @points << OOXMLCoordinates.parse(node_child)
33
+ @points << OOXMLCoordinates.new(parent: self).parse(node_child)
34
34
  end
35
35
  end
36
36
  self
@@ -37,13 +37,13 @@ module OoxmlParser
37
37
  node.xpath('*').each do |node_child|
38
38
  case node_child.name
39
39
  when 'off'
40
- @offset = OOXMLCoordinates.parse(node_child, unit: :emu)
40
+ @offset = OOXMLCoordinates.new(parent: self).parse(node_child, unit: :emu)
41
41
  when 'ext'
42
- @extent = OOXMLCoordinates.parse(node_child, x_attr: 'cx', y_attr: 'cy', unit: :emu)
42
+ @extent = OOXMLCoordinates.new(parent: self).parse(node_child, x_attr: 'cx', y_attr: 'cy', unit: :emu)
43
43
  when 'chOff'
44
- @child_offset = OOXMLCoordinates.parse(node_child, unit: :emu)
44
+ @child_offset = OOXMLCoordinates.new(parent: self).parse(node_child, unit: :emu)
45
45
  when 'chExt'
46
- @child_extent = OOXMLCoordinates.parse(node_child, x_attr: 'cx', y_attr: 'cy', unit: :emu)
46
+ @child_extent = OOXMLCoordinates.new(parent: self).parse(node_child, x_attr: 'cx', y_attr: 'cy', unit: :emu)
47
47
  end
48
48
  end
49
49
  self
@@ -50,8 +50,6 @@ module OoxmlParser
50
50
  # @param [Nokogiri::XML:Element] node with BordersProperties
51
51
  # @return [BordersProperties] value of BordersProperties
52
52
  def parse(node)
53
- return nil if node.attribute('val').value == 'nil'
54
-
55
53
  node.attributes.each do |key, value|
56
54
  case key
57
55
  when 'val'
@@ -67,6 +65,8 @@ module OoxmlParser
67
65
  @shadow = value.value
68
66
  end
69
67
  end
68
+ return nil if @val == :nil
69
+
70
70
  self
71
71
  end
72
72
  end
@@ -3,26 +3,27 @@
3
3
  module OoxmlParser
4
4
  # Helper methods for color
5
5
  module ColorHelper
6
+ # @return [String] value for auto string
7
+ AUTO_STRING_VALUE = 'auto'
8
+ # @return [Regexp] regexp for hex string with 3 digits
9
+ REGEXP_THREE_DIGITS = /(.)(.)(.)/.freeze
10
+ # @return [Regexp] regexp for hex string with 6 digits
11
+ REGEXP_SIX_DIGITS = /(..)(..)(..)/.freeze
12
+ # @return [Regexp] regexp for hex string with 8 digits
13
+ REGEXP_EIGHT_DIGITS = /(..)(..)(..)(..)/.freeze
14
+
6
15
  # Parse string in hex
7
16
  # @param [String] hex_string with or without alpha-channel
8
17
  def parse_hex_string(hex_string)
9
- return self if %w[auto null].include?(hex_string)
18
+ return self if AUTO_STRING_VALUE == hex_string
10
19
 
11
- char_array = hex_string.chars
12
- case char_array.length
20
+ case hex_string.length
13
21
  when 3
14
- @red = char_array[0].hex
15
- @green = char_array[1].hex
16
- @blue = char_array[2].hex
22
+ @red, @green, @blue = hex_string.match(REGEXP_THREE_DIGITS).captures.map(&:hex)
17
23
  when 6
18
- @red = (char_array[0] + char_array[1]).hex
19
- @green = (char_array[2] + char_array[3]).hex
20
- @blue = (char_array[4] + char_array[5]).hex
24
+ @red, @green, @blue = hex_string.match(REGEXP_SIX_DIGITS).captures.map(&:hex)
21
25
  when 8
22
- @alpha_channel = (char_array[0] + char_array[1]).hex
23
- @red = (char_array[2] + char_array[3]).hex
24
- @green = (char_array[4] + char_array[5]).hex
25
- @blue = (char_array[6] + char_array[7]).hex
26
+ @alpha_channel, @red, @green, @blue = hex_string.match(REGEXP_EIGHT_DIGITS).captures.map(&:hex)
26
27
  end
27
28
  self
28
29
  end
@@ -101,7 +101,7 @@ module OoxmlParser
101
101
 
102
102
  # Compare this object to other
103
103
  # @param other [Object] any other object
104
- # @return [True, False] result of comparision
104
+ # @return [True, False] result of comparison
105
105
  def ==(other)
106
106
  if other.is_a?(Color)
107
107
  ((@red == other.red) && (@green == other.green) && (@blue == other.blue)) ||
@@ -117,7 +117,7 @@ module OoxmlParser
117
117
  # @param [Integer] delta max delta for each of specters
118
118
  def looks_like?(color_to_check = nil, delta = 8)
119
119
  color_to_check = color_to_check.converted_color if color_to_check.is_a?(SchemeColor)
120
- color_to_check = color_to_check.pattern_fill.foreground_folor if color_to_check.is_a?(Fill)
120
+ color_to_check = color_to_check.pattern_fill.foreground_color if color_to_check.is_a?(Fill)
121
121
  color_to_check = color_to_check.color.converted_color if color_to_check.is_a?(PresentationFill)
122
122
  color_to_check = Color.parse(color_to_check) if color_to_check.is_a?(String)
123
123
  color_to_check = Color.parse(color_to_check.to_s) if color_to_check.is_a?(Symbol)
@@ -128,10 +128,9 @@ module OoxmlParser
128
128
  return false if !none? && color_to_check.none?
129
129
  return true if self == color_to_check
130
130
 
131
- red = color_to_check.red
132
- green = color_to_check.green
133
- blue = color_to_check.blue
134
- (self.red - red).abs < delta && (self.green - green).abs < delta && (self.blue - blue).abs < delta
131
+ (red - color_to_check.red).abs < delta &&
132
+ (green - color_to_check.green).abs < delta &&
133
+ (blue - color_to_check.blue).abs < delta
135
134
  end
136
135
 
137
136
  # Apply tint to color
@@ -156,7 +155,8 @@ module OoxmlParser
156
155
  # @param scheme_color_node [Nokogiri::XML:Element] node to parse
157
156
  # @return [Color] result of parsing
158
157
  def parse_scheme_color(scheme_color_node)
159
- color_scheme_color = root_object.theme.color_scheme[scheme_color_node.attribute('val').value.to_sym]
158
+ scheme_clr_object = ValuedChild.new(:symbol, parent: self).parse(scheme_color_node)
159
+ color_scheme_color = root_object.theme.color_scheme[scheme_clr_object.value]
160
160
  return unless color_scheme_color
161
161
 
162
162
  color = color_scheme_color.color
@@ -165,9 +165,11 @@ module OoxmlParser
165
165
  scheme_color_node.xpath('*').each do |scheme_color_node_child|
166
166
  case scheme_color_node_child.name
167
167
  when 'lumMod'
168
- hls.l = hls.l * (scheme_color_node_child.attribute('val').value.to_f / 100_000.0)
168
+ luminance_modulation = ValuedChild.new(:float, parent: self).parse(scheme_color_node_child)
169
+ hls.l = hls.l * (luminance_modulation.value / 100_000.0)
169
170
  when 'lumOff'
170
- hls.l = hls.l + (scheme_color_node_child.attribute('val').value.to_f / 100_000.0)
171
+ luminance_offset = ValuedChild.new(:float, parent: self).parse(scheme_color_node_child)
172
+ hls.l = hls.l + (luminance_offset.value / 100_000.0)
171
173
  end
172
174
  end
173
175
  scheme_color_node.attributes.each do |key, value|
@@ -180,7 +182,7 @@ module OoxmlParser
180
182
  @red = color.red
181
183
  @green = color.green
182
184
  @blue = color.blue
183
- @alpha_channel = ColorAlphaChannel.parse(scheme_color_node)
185
+ @alpha_channel = ColorAlphaChannel.new(parent: self).parse(scheme_color_node).value
184
186
  @scheme = scheme_name
185
187
  self
186
188
  end
@@ -200,8 +202,9 @@ module OoxmlParser
200
202
  end
201
203
  case color_model_node.name
202
204
  when 'srgbClr'
203
- color = Color.new.parse_hex_string(color_model_node.attribute('val').value)
204
- color.alpha_channel = ColorAlphaChannel.parse(color_model_node)
205
+ valued_child = ValuedChild.new(:string, parent: self).parse(color_model_node)
206
+ color = Color.new.parse_hex_string(valued_child.value)
207
+ color.alpha_channel = ColorAlphaChannel.new(parent: self).parse(color_model_node).value
205
208
  when 'schemeClr'
206
209
  color = Color.new(parent: self).parse_scheme_color(color_model_node)
207
210
  end
@@ -230,7 +233,8 @@ module OoxmlParser
230
233
  color = SchemeColor.new(parent: parent)
231
234
  return ValuedChild.new(:string, parent: parent).parse(color_node) unless root_object.theme
232
235
 
233
- color.value = root_object.theme.color_scheme[color_node.attribute('val').value.to_sym].color
236
+ scheme_clr_object = ValuedChild.new(:symbol, parent: self).parse(color_node)
237
+ color.value = root_object.theme.color_scheme[scheme_clr_object.value].color
234
238
  color.properties = ColorProperties.new(parent: color).parse(color_node)
235
239
  color.converted_color = Color.new(parent: self).parse_scheme_color(color_node)
236
240
  color.value.calculate_with_tint!(1.0 - color.properties.tint) if color.properties.tint
@@ -2,16 +2,30 @@
2
2
 
3
3
  module OoxmlParser
4
4
  # Class for working with AlphaChannel
5
- class ColorAlphaChannel
5
+ class ColorAlphaChannel < OOXMLDocumentObject
6
+ # @return [Integer] value of alpha channel
7
+ attr_reader :value
8
+
6
9
  # Parse AlphaChannel value
7
10
  # @param node [Nokogiri::XML::Element] node to parse
8
- # @return [Fixnum] value of AlphaChannel
9
- def self.parse(node)
11
+ # @return [ColorAlphaChannel] parsed object
12
+ def parse(node)
10
13
  alpha_channel_node = node.xpath('a:alpha', 'xmlns:a' => 'http://schemas.openxmlformats.org/drawingml/2006/main').first
11
14
  alpha_channel_node = node.xpath('w14:alpha', 'xmlns:w14' => 'http://schemas.microsoft.com/office/word/2010/wordml').first if alpha_channel_node.nil?
12
- return 100.0 if alpha_channel_node.nil?
13
15
 
14
- (alpha_channel_node.attribute('val').value.to_f / 1_000.0).round(0)
16
+ unless alpha_channel_node
17
+ @value = 100.0
18
+ return self
19
+ end
20
+
21
+ alpha_channel_node.attributes.each do |key, value|
22
+ case key
23
+ when 'val'
24
+ @value = (value.value.to_f / 1_000.0).round(0)
25
+ end
26
+ end
27
+
28
+ self
15
29
  end
16
30
  end
17
31
  end
@@ -3,14 +3,14 @@
3
3
  module OoxmlParser
4
4
  # Class for color transformations
5
5
  class ColorProperties < OOXMLDocumentObject
6
- # @return [Integer] alpha value of color
7
- attr_reader :alpha
8
- # @return [Float] luminance modulation value
9
- attr_reader :luminance_modulation
10
- # @return [Float] luminance offset value
11
- attr_reader :luminance_offset
12
- # @return [Float] tint value
13
- attr_reader :tint
6
+ # @return [ValuedChild] alpha value of color object
7
+ attr_reader :alpha_object
8
+ # @return [ValuedChild] luminance modulation value object
9
+ attr_reader :luminance_modulation_object
10
+ # @return [ValuedChild] luminance offset value object
11
+ attr_reader :luminance_offset_object
12
+ # @return [ValuedChild] tint value object
13
+ attr_reader :tint_object
14
14
 
15
15
  # Parse ColorProperties object
16
16
  # @param node [Nokogiri::XML:Element] node to parse
@@ -19,16 +19,38 @@ module OoxmlParser
19
19
  node.xpath('*').each do |node_child|
20
20
  case node_child.name
21
21
  when 'alpha'
22
- @alpha = (node_child.attribute('val').value.to_f / 1_000.0).round
22
+ @alpha_object = ValuedChild.new(:float, parent: self).parse(node_child)
23
23
  when 'lumMod'
24
- @luminance_modulation = node_child.attribute('val').value.to_f / 100_000.0
24
+ @luminance_modulation_object = ValuedChild.new(:float, parent: self).parse(node_child)
25
25
  when 'lumOff'
26
- @luminance_offset = node_child.attribute('val').value.to_f / 100_000.0
26
+ @luminance_offset_object = ValuedChild.new(:float, parent: self).parse(node_child)
27
27
  when 'tint'
28
- @tint = node_child.attribute('val').value.to_f / 100_000.0
28
+ @tint_object = ValuedChild.new(:float, parent: self).parse(node_child)
29
29
  end
30
30
  end
31
31
  self
32
32
  end
33
+
34
+ # @return [Integer] alpha value
35
+ def alpha
36
+ (@alpha_object.value / 1_000.0).round
37
+ end
38
+
39
+ # @return [Float] luminance modulation value
40
+ def luminance_modulation
41
+ @luminance_modulation_object.value / 100_000.0
42
+ end
43
+
44
+ # @return [Float] luminance offset value
45
+ def luminance_offset
46
+ @luminance_offset_object.value / 100_000.0
47
+ end
48
+
49
+ # @return [nil, Float] tint value
50
+ def tint
51
+ return nil unless @tint_object
52
+
53
+ @tint_object.value / 100_000.0
54
+ end
33
55
  end
34
56
  end
@@ -22,8 +22,8 @@ module OoxmlParser
22
22
  when 'blip'
23
23
  @file_reference = FileReference.new(parent: self).parse(node_child)
24
24
  when 'tile'
25
- @tile = Tile.new(OOXMLCoordinates.parse(node_child, x_attr: 'tx', y_attr: 'ty'),
26
- OOXMLCoordinates.parse(node_child, x_attr: 'sx', y_attr: 'sy'),
25
+ @tile = Tile.new(OOXMLCoordinates.new(parent: self).parse(node_child, x_attr: 'tx', y_attr: 'ty'),
26
+ OOXMLCoordinates.new(parent: self).parse(node_child, x_attr: 'sx', y_attr: 'sy'),
27
27
  parent: self).parse(node_child)
28
28
  end
29
29
  end
@@ -3,6 +3,8 @@
3
3
  module OoxmlParser
4
4
  # Class for parsing `hlinkClick`, `hyperlink` tags
5
5
  class Hyperlink < OOXMLDocumentObject
6
+ # @return [String] anchor value
7
+ attr_reader :anchor
6
8
  # @return [OOXMLDocumentObject] url of hyperlink
7
9
  attr_accessor :url
8
10
  # @return [String] tooltip value
@@ -42,6 +44,8 @@ module OoxmlParser
42
44
  def parse(node)
43
45
  node.attributes.each do |key, value|
44
46
  case key
47
+ when 'anchor'
48
+ @anchor = value.value
45
49
  when 'location'
46
50
  @url = Coordinates.new.parse_string(value.value)
47
51
  when 'id'
@@ -40,14 +40,35 @@ module OoxmlParser
40
40
  true
41
41
  end
42
42
 
43
+ # @param [String, Nokogiri::XML:Attribute] value to check
44
+ # @return [True, False] value of attribute
45
+ def boolean_attribute_value(value)
46
+ return true if value.to_s == '1'
47
+ return true if value.to_s == 'true'
48
+ return false if value.to_s == '0'
49
+ return false if value.to_s == 'false'
50
+ end
51
+
43
52
  # @return [Nokogiri::XML::Document] result of parsing xml via nokogiri
44
53
  def parse_xml(xml_path)
45
54
  xml = Nokogiri::XML(File.open(xml_path), &:strict)
46
55
  unless xml.errors.empty?
47
56
  raise NokogiriParsingException,
48
- "Nokogiri found errors in file: #{xml_path}. Errors: #{xml.errors}"
57
+ parse_error_message(xml_path, xml.errors)
49
58
  end
50
59
  xml
60
+ rescue Nokogiri::XML::SyntaxError => e
61
+ raise NokogiriParsingException,
62
+ parse_error_message(xml_path, e)
63
+ end
64
+
65
+ private
66
+
67
+ # @param [String] xml_path path to xml
68
+ # @param [String] errors errors
69
+ # @return [String] error string
70
+ def parse_error_message(xml_path, errors)
71
+ "Nokogiri found errors in file: #{xml_path}. Errors: #{errors}"
51
72
  end
52
73
  end
53
74
  end
@@ -3,7 +3,11 @@
3
3
  module OoxmlParser
4
4
  # Class for parsing `numPr` tags
5
5
  class NumberingProperties < OOXMLDocumentObject
6
- attr_accessor :size, :font, :symbol, :start_at, :type, :image, :ilvl, :numbering_properties
6
+ attr_accessor :size, :font, :symbol, :start_at, :type, :image
7
+ # @return [ValuedChild] i level
8
+ attr_reader :i_level
9
+ # @return [ValuedChild] numbering id
10
+ attr_reader :num_id
7
11
 
8
12
  def initialize(ilvl = 0, parent: nil)
9
13
  @ilvl = ilvl
@@ -12,7 +16,7 @@ module OoxmlParser
12
16
 
13
17
  # @return [AbstractNumbering] AbstractNumbering of current properties
14
18
  def abstruct_numbering
15
- root_object.numbering.properties_by_num_id(@numbering_properties)
19
+ root_object.numbering.properties_by_num_id(numbering_properties)
16
20
  end
17
21
 
18
22
  # Parse NumberingProperties
@@ -22,14 +26,24 @@ module OoxmlParser
22
26
  node.xpath('*').each do |node_child|
23
27
  case node_child.name
24
28
  when 'ilvl'
25
- @ilvl = node_child.attribute('val').value.to_i
29
+ @i_level = ValuedChild.new(:integer, parent: self).parse(node_child)
26
30
  when 'numId'
27
- @numbering_properties = node_child.attribute('val').value.to_i
31
+ @num_id = ValuedChild.new(:integer, parent: self).parse(node_child)
28
32
  end
29
33
  end
30
34
  self
31
35
  end
32
36
 
37
+ # @return [Integer] numbering properties
38
+ def numbering_properties
39
+ @num_id.value
40
+ end
41
+
42
+ # @return [Integer] i-level value
43
+ def ilvl
44
+ @i_level.value
45
+ end
46
+
33
47
  # @return [AbstractNumbering] level list of current numbering
34
48
  def numbering_level_current
35
49
  abstruct_numbering.level_list.each do |current_ilvl|
@@ -101,9 +101,11 @@ module OoxmlParser
101
101
  when 'spacing'
102
102
  @spacing = ParagraphSpacing.new(parent: self).parse(node_child)
103
103
  when 'jc'
104
- @justification = value_to_symbol(node_child.attribute('val'))
104
+ @justification_object = ValuedChild.new(:string, parent: self).parse(node_child)
105
+ @justification = value_to_symbol(@justification_object)
105
106
  when 'contextualSpacing'
106
- @contextual_spacing = option_enabled?(node_child)
107
+ contextual_spacing_object = ValuedChild.new(:boolean, parent: self).parse(node_child)
108
+ @contextual_spacing = contextual_spacing_object.value
107
109
  end
108
110
  end
109
111
  self
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OoxmlParser
4
+ # Class for parsing `w:rFonts` object
5
+ class RunFonts < OOXMLDocumentObject
6
+ # @return [String] ascii font
7
+ attr_accessor :ascii
8
+ # @return [String] ascii theme value
9
+ attr_accessor :ascii_theme
10
+
11
+ # Parse RunFonts
12
+ # @param [Nokogiri::XML:Node] node with RunFonts
13
+ # @return [RunFonts] result of parsing
14
+ def parse(node)
15
+ node.attributes.each do |key, value|
16
+ case key
17
+ when 'ascii'
18
+ @ascii = value.to_s
19
+ when 'asciiTheme'
20
+ @ascii_theme = value.to_s
21
+ end
22
+ end
23
+ self
24
+ end
25
+ end
26
+ end
@@ -2,10 +2,12 @@
2
2
 
3
3
  require_relative 'run_properties/outline'
4
4
  require_relative 'run_properties/position'
5
- require_relative 'run_properties/size'
5
+ require_relative 'run_properties/run_fonts'
6
6
  require_relative 'run_properties/run_spacing'
7
7
  require_relative 'run_properties/run_style'
8
8
  require_relative 'run_properties/shade'
9
+ require_relative 'run_properties/size'
10
+
9
11
  module OoxmlParser
10
12
  # Data about `rPr` object
11
13
  class RunProperties < OOXMLDocumentObject
@@ -49,6 +51,10 @@ module OoxmlParser
49
51
  attr_accessor :run_style
50
52
  # @return [ValuedChild] ligatures type
51
53
  attr_reader :ligatures
54
+ # @return [RunFonts] value of RunFonts
55
+ attr_reader :run_fonts
56
+ # @return [Nokogiri::XML:Element] raw node value
57
+ attr_reader :raw_node
52
58
 
53
59
  def initialize(params = {})
54
60
  @font_name = params.fetch(:font_name, '')
@@ -61,6 +67,7 @@ module OoxmlParser
61
67
  # @param node [Nokogiri::XML:Element] node to parse
62
68
  # @return [RunProperties] result of parsing
63
69
  def parse(node)
70
+ @raw_node = node
64
71
  @font_style = root_object.default_font_style.dup
65
72
  node.attributes.each do |key, value|
66
73
  case key
@@ -107,11 +114,12 @@ module OoxmlParser
107
114
  when 'u'
108
115
  @font_style.underlined = Underline.new(:single)
109
116
  when 'vertAlign'
110
- @vertical_align = node_child.attribute('val').value.to_sym
117
+ @vertical_align_object = ValuedChild.new(:symbol, parent: self).parse(node_child)
118
+ @vertical_align = @vertical_align_object.value
111
119
  when 'rFont'
112
120
  @font_name = node_child.attribute('val').value
113
121
  when 'rFonts'
114
- @font_name = node_child.attribute('ascii').value if node_child.attribute('ascii')
122
+ @run_fonts = RunFonts.new(parent: self).parse(node_child)
115
123
  when 'strike'
116
124
  @font_style.strike = option_enabled?(node_child)
117
125
  when 'hlinkClick'
@@ -142,6 +150,7 @@ module OoxmlParser
142
150
  # @return [String] name of font
143
151
  def font_name
144
152
  return @font_name unless @font_name.empty?
153
+ return @run_fonts.ascii if @run_fonts
145
154
 
146
155
  root_object.default_font_typeface
147
156
  end
@@ -39,13 +39,13 @@ module OoxmlParser
39
39
  margin_node.xpath('*').each do |cell_margin_node|
40
40
  case cell_margin_node.name
41
41
  when 'left'
42
- @left = OoxmlSize.new(cell_margin_node.attribute('w').value.to_f)
42
+ @left = OoxmlSize.new(parent: self).parse(cell_margin_node)
43
43
  when 'top'
44
- @top = OoxmlSize.new(cell_margin_node.attribute('w').value.to_f)
44
+ @top = OoxmlSize.new(parent: self).parse(cell_margin_node)
45
45
  when 'right'
46
- @right = OoxmlSize.new(cell_margin_node.attribute('w').value.to_f)
46
+ @right = OoxmlSize.new(parent: self).parse(cell_margin_node)
47
47
  when 'bottom'
48
- @bottom = OoxmlSize.new(cell_margin_node.attribute('w').value.to_f)
48
+ @bottom = OoxmlSize.new(parent: self).parse(cell_margin_node)
49
49
  end
50
50
  end
51
51
  self