goldendocx 0.2.2

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 (152) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.rubocop.yml +47 -0
  4. data/CHANGELOG.md +43 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +101 -0
  7. data/README.md +76 -0
  8. data/Rakefile +10 -0
  9. data/coverage/.last_run.json +5 -0
  10. data/demo/charts/create_bar_chart.rb +30 -0
  11. data/demo/charts/create_column_chart.rb +29 -0
  12. data/demo/charts/create_doughnut_chart.rb +28 -0
  13. data/demo/charts/create_line_chart.rb +41 -0
  14. data/demo/images/append_image.rb +21 -0
  15. data/demo/images/append_styled_image.rb +14 -0
  16. data/demo/tables/create_embed_image_table.rb +42 -0
  17. data/demo/tables/create_plain_table.rb +22 -0
  18. data/demo/tables/create_styled_table.rb +36 -0
  19. data/demo/templates/bg.png +0 -0
  20. data/demo/templates/blankDocument.docx +0 -0
  21. data/demo/templates/image_base64 +1 -0
  22. data/demo/templates/styles/reportLabel +19 -0
  23. data/demo/templates/styles/reportSubTitle +20 -0
  24. data/demo/templates/styles/reportTable +27 -0
  25. data/demo/templates/styles/reportTitle +19 -0
  26. data/demo/texts/append_plain_text.rb +13 -0
  27. data/demo/texts/append_styled_text.rb +23 -0
  28. data/lib/goldendocx/charts/category_axis.rb +20 -0
  29. data/lib/goldendocx/charts/properties/auto_title_deleted_property.rb +16 -0
  30. data/lib/goldendocx/charts/properties/axis_property.rb +16 -0
  31. data/lib/goldendocx/charts/properties/bar_chart_property.rb +28 -0
  32. data/lib/goldendocx/charts/properties/chart_property.rb +16 -0
  33. data/lib/goldendocx/charts/properties/column_chart_property.rb +28 -0
  34. data/lib/goldendocx/charts/properties/cross_axis_property.rb +16 -0
  35. data/lib/goldendocx/charts/properties/data_lables_property.rb +16 -0
  36. data/lib/goldendocx/charts/properties/direction_property.rb +16 -0
  37. data/lib/goldendocx/charts/properties/doughnut_chart_property.rb +17 -0
  38. data/lib/goldendocx/charts/properties/grouping_property.rb +16 -0
  39. data/lib/goldendocx/charts/properties/hole_property.rb +16 -0
  40. data/lib/goldendocx/charts/properties/index_property.rb +16 -0
  41. data/lib/goldendocx/charts/properties/label_hidden_property.rb +16 -0
  42. data/lib/goldendocx/charts/properties/layout_property.rb +14 -0
  43. data/lib/goldendocx/charts/properties/line_chart_property.rb +24 -0
  44. data/lib/goldendocx/charts/properties/marker_property.rb +16 -0
  45. data/lib/goldendocx/charts/properties/number_list_property.rb +17 -0
  46. data/lib/goldendocx/charts/properties/order_property.rb +16 -0
  47. data/lib/goldendocx/charts/properties/plot_area_property.rb +23 -0
  48. data/lib/goldendocx/charts/properties/point_count_property.rb +16 -0
  49. data/lib/goldendocx/charts/properties/point_property.rb +18 -0
  50. data/lib/goldendocx/charts/properties/rounded_corner_property.rb +16 -0
  51. data/lib/goldendocx/charts/properties/series_category_property.rb +16 -0
  52. data/lib/goldendocx/charts/properties/series_text_property.rb +16 -0
  53. data/lib/goldendocx/charts/properties/series_value_property.rb +16 -0
  54. data/lib/goldendocx/charts/properties/smooth_property.rb +14 -0
  55. data/lib/goldendocx/charts/properties/string_list_property.rb +17 -0
  56. data/lib/goldendocx/charts/properties/symbol_property.rb +16 -0
  57. data/lib/goldendocx/charts/properties/text_value_property.rb +16 -0
  58. data/lib/goldendocx/charts/properties.rb +12 -0
  59. data/lib/goldendocx/charts/reference.rb +16 -0
  60. data/lib/goldendocx/charts/series.rb +57 -0
  61. data/lib/goldendocx/charts/value_axis.rb +20 -0
  62. data/lib/goldendocx/charts.rb +26 -0
  63. data/lib/goldendocx/components/bar_chart.rb +17 -0
  64. data/lib/goldendocx/components/chart.rb +55 -0
  65. data/lib/goldendocx/components/column_chart.rb +17 -0
  66. data/lib/goldendocx/components/doughnut_chart.rb +17 -0
  67. data/lib/goldendocx/components/image.rb +77 -0
  68. data/lib/goldendocx/components/line_chart.rb +17 -0
  69. data/lib/goldendocx/components/paragraph.rb +23 -0
  70. data/lib/goldendocx/components/properties/align_property.rb +16 -0
  71. data/lib/goldendocx/components/properties/bold_property.rb +16 -0
  72. data/lib/goldendocx/components/properties/color_property.rb +16 -0
  73. data/lib/goldendocx/components/properties/drawing.rb +16 -0
  74. data/lib/goldendocx/components/properties/extents_property.rb +17 -0
  75. data/lib/goldendocx/components/properties/graphic_data_property.rb +17 -0
  76. data/lib/goldendocx/components/properties/graphic_frame_locks_property.rb +18 -0
  77. data/lib/goldendocx/components/properties/graphic_frame_property.rb +16 -0
  78. data/lib/goldendocx/components/properties/graphic_property.rb +16 -0
  79. data/lib/goldendocx/components/properties/inline.rb +20 -0
  80. data/lib/goldendocx/components/properties/non_visual_property.rb +17 -0
  81. data/lib/goldendocx/components/properties/padding_property.rb +19 -0
  82. data/lib/goldendocx/components/properties/property.rb +17 -0
  83. data/lib/goldendocx/components/properties/run.rb +20 -0
  84. data/lib/goldendocx/components/properties/run_property.rb +17 -0
  85. data/lib/goldendocx/components/properties/style_property.rb +16 -0
  86. data/lib/goldendocx/components/properties/textual.rb +16 -0
  87. data/lib/goldendocx/components/properties.rb +12 -0
  88. data/lib/goldendocx/components/table.rb +44 -0
  89. data/lib/goldendocx/components/text.rb +35 -0
  90. data/lib/goldendocx/components.rb +16 -0
  91. data/lib/goldendocx/configuration.rb +18 -0
  92. data/lib/goldendocx/content_types/default.rb +22 -0
  93. data/lib/goldendocx/content_types/override.rb +22 -0
  94. data/lib/goldendocx/document.rb +51 -0
  95. data/lib/goldendocx/documents/body.rb +90 -0
  96. data/lib/goldendocx/documents/document.rb +29 -0
  97. data/lib/goldendocx/documents/element.rb +23 -0
  98. data/lib/goldendocx/documents/relationship.rb +17 -0
  99. data/lib/goldendocx/documents/relationships.rb +39 -0
  100. data/lib/goldendocx/documents/style.rb +21 -0
  101. data/lib/goldendocx/documents/styles.rb +74 -0
  102. data/lib/goldendocx/documents/unparsed_style.rb +17 -0
  103. data/lib/goldendocx/documents.rb +10 -0
  104. data/lib/goldendocx/docx.rb +108 -0
  105. data/lib/goldendocx/element.rb +192 -0
  106. data/lib/goldendocx/images/picture.rb +41 -0
  107. data/lib/goldendocx/images/properties/blip_property.rb +16 -0
  108. data/lib/goldendocx/images/properties/drawing_property.rb +14 -0
  109. data/lib/goldendocx/images/properties/extents_property.rb +17 -0
  110. data/lib/goldendocx/images/properties/fill_rectangle_property.rb +14 -0
  111. data/lib/goldendocx/images/properties/image_data_property.rb +17 -0
  112. data/lib/goldendocx/images/properties/non_visual_drawing_property.rb +17 -0
  113. data/lib/goldendocx/images/properties/non_visual_picture_property.rb +17 -0
  114. data/lib/goldendocx/images/properties/picture_fill_property.rb +17 -0
  115. data/lib/goldendocx/images/properties/picture_shape_property.rb +16 -0
  116. data/lib/goldendocx/images/properties/shape_property.rb +30 -0
  117. data/lib/goldendocx/images/properties/stretch_property.rb +17 -0
  118. data/lib/goldendocx/images/properties/transform_property.rb +16 -0
  119. data/lib/goldendocx/images/properties.rb +12 -0
  120. data/lib/goldendocx/images/shape.rb +32 -0
  121. data/lib/goldendocx/images.rb +12 -0
  122. data/lib/goldendocx/parts/content_types.rb +63 -0
  123. data/lib/goldendocx/parts/documents.rb +103 -0
  124. data/lib/goldendocx/parts/media.rb +43 -0
  125. data/lib/goldendocx/parts.rb +10 -0
  126. data/lib/goldendocx/tables/cell.rb +33 -0
  127. data/lib/goldendocx/tables/header_cell.rb +16 -0
  128. data/lib/goldendocx/tables/header_row.rb +14 -0
  129. data/lib/goldendocx/tables/image_cell.rb +23 -0
  130. data/lib/goldendocx/tables/properties/cell_property.rb +19 -0
  131. data/lib/goldendocx/tables/properties/cell_width_property.rb +17 -0
  132. data/lib/goldendocx/tables/properties/grid_column_property.rb +16 -0
  133. data/lib/goldendocx/tables/properties/grid_property.rb +16 -0
  134. data/lib/goldendocx/tables/properties/grid_span_property.rb +16 -0
  135. data/lib/goldendocx/tables/properties/header_row_property.rb +14 -0
  136. data/lib/goldendocx/tables/properties/property.rb +17 -0
  137. data/lib/goldendocx/tables/properties/row_height_property.rb +17 -0
  138. data/lib/goldendocx/tables/properties/row_property.rb +16 -0
  139. data/lib/goldendocx/tables/properties/shading_property.rb +18 -0
  140. data/lib/goldendocx/tables/properties/style_property.rb +16 -0
  141. data/lib/goldendocx/tables/properties/vertical_align_property.rb +16 -0
  142. data/lib/goldendocx/tables/properties/width_property.rb +17 -0
  143. data/lib/goldendocx/tables/properties.rb +12 -0
  144. data/lib/goldendocx/tables/row.rb +28 -0
  145. data/lib/goldendocx/tables.rb +19 -0
  146. data/lib/goldendocx/units.rb +11 -0
  147. data/lib/goldendocx/version.rb +5 -0
  148. data/lib/goldendocx/xml_serializers/nokogiri.rb +66 -0
  149. data/lib/goldendocx/xml_serializers/ox.rb +63 -0
  150. data/lib/goldendocx.rb +72 -0
  151. data/sig/goldendocx.rbs +4 -0
  152. metadata +347 -0
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class BoldProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :b
11
+
12
+ attribute :enabled, alias_name: :val, namespace: :w
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class ColorProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :color
11
+
12
+ attribute :hex, alias_name: :val, namespace: :w
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class Drawing
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :drawing
11
+
12
+ embeds_one :inline, class_name: 'Goldendocx::Components::Properties::Inline', auto_build: true
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class ExtentsProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :wp
10
+ tag :extent
11
+
12
+ attribute :width, alias_name: :cx, default: Goldendocx::Components::DEFAULT_WIDTH
13
+ attribute :height, alias_name: :cy, default: Goldendocx::Components::DEFAULT_HEIGHT
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class GraphicDataProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :a
10
+ tag :graphicData
11
+
12
+ embeds_one :picture, class_name: 'Goldendocx::Images::Picture', auto_build: false
13
+ embeds_one :chart, class_name: 'Goldendocx::Charts::Properties::Reference', auto_build: false
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class GraphicFrameLocksProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :a
10
+ tag :graphicFrameLocks
11
+
12
+ attribute :lock_position, alias_name: :noMove, default: true
13
+ attribute :lock_size, alias_name: :noResize, default: true
14
+ attribute :lock_aspect, alias_name: :noChangeAspect, default: true
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class GraphicFrameProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :wp
10
+ tag :cNvGraphicFramePr
11
+
12
+ embeds_one :locker, class_name: 'Goldendocx::Components::Properties::GraphicFrameLocksProperty', auto_build: true
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class GraphicProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :a
10
+ tag :graphic
11
+
12
+ embeds_one :data, class_name: 'Goldendocx::Components::Properties::GraphicDataProperty', auto_build: true
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class Inline
7
+ include Goldendocx::Element
8
+
9
+ namespace :wp
10
+ tag :inline
11
+
12
+ embeds_one :extents, class_name: 'Goldendocx::Components::Properties::ExtentsProperty', auto_build: true
13
+ embeds_one :padding, class_name: 'Goldendocx::Components::Properties::PaddingProperty', auto_build: true
14
+ embeds_one :non_visual_property, class_name: 'Goldendocx::Components::Properties::NonVisualProperty', auto_build: true
15
+ embeds_one :graphic_frame, class_name: 'Goldendocx::Components::Properties::GraphicFrameProperty', auto_build: true
16
+ embeds_one :graphic, class_name: 'Goldendocx::Components::Properties::GraphicProperty', auto_build: true
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class NonVisualProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :wp
10
+ tag :docPr
11
+
12
+ attribute :relationship_id, alias_name: :id
13
+ attribute :name
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class PaddingProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :wp
10
+ tag :effectExtent
11
+
12
+ attribute :left, alias_name: :l, default: 0
13
+ attribute :right, alias_name: :r, default: 0
14
+ attribute :top, alias_name: :t, default: 0
15
+ attribute :bottom, alias_name: :b, default: 0
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class Property
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :pPr
11
+
12
+ embeds_one :style, class_name: 'Goldendocx::Components::Properties::StyleProperty', auto_build: true
13
+ embeds_one :align, class_name: 'Goldendocx::Components::Properties::AlignProperty', auto_build: true
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class Run
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :r
11
+
12
+ embeds_one :property, class_name: 'Goldendocx::Components::Properties::RunProperty', auto_build: false
13
+
14
+ embeds_one :text, class_name: 'Goldendocx::Components::Properties::Textual', auto_build: false
15
+ embeds_one :shape, class_name: 'Goldendocx::Images::Shape', auto_build: false
16
+ embeds_one :drawing, class_name: 'Goldendocx::Components::Properties::Drawing', auto_build: false
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class RunProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :rPr
11
+
12
+ embeds_one :color, class_name: 'Goldendocx::Components::Properties::ColorProperty', auto_build: false
13
+ embeds_one :bold, class_name: 'Goldendocx::Components::Properties::BoldProperty', auto_build: false
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class StyleProperty
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :pStyle
11
+
12
+ attribute :style_id, alias_name: :val, namespace: :w
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ class Textual
7
+ include Goldendocx::Element
8
+
9
+ namespace :w
10
+ tag :t
11
+
12
+ embeds_one :value, class_name: 'String'
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ module Properties
6
+ end
7
+ end
8
+ end
9
+
10
+ Dir.glob(File.join(File.dirname(__FILE__), 'properties', '*.rb').to_s).sort.each do |file|
11
+ require file
12
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: Only support create new table for now.
4
+ # TODO: To parse a table from exist file for replacement someday
5
+ require 'goldendocx/tables'
6
+
7
+ module Goldendocx
8
+ module Components
9
+ class Table
10
+ include Goldendocx::Element
11
+
12
+ attr_reader :headers
13
+
14
+ namespace :w
15
+ tag :tbl
16
+
17
+ embeds_one :property, class_name: 'Goldendocx::Tables::Properties::Property', auto_build: true
18
+ embeds_one :grid_property, class_name: 'Goldendocx::Tables::Properties::GridProperty', auto_build: true
19
+
20
+ embeds_one :header, class_name: 'Goldendocx::Tables::HeaderRow', auto_build: true
21
+ embeds_many :rows, class_name: 'Goldendocx::Tables::Row'
22
+
23
+ def width=(width)
24
+ property.table_width.width = width if width
25
+ end
26
+
27
+ def style=(style)
28
+ property.table_style.style_id = style if style
29
+ end
30
+
31
+ def add_header(title, width: nil)
32
+ header.build_cell(content: title, width: width)
33
+ grid_property.build_grid_column(width: width) if width
34
+ header
35
+ end
36
+
37
+ def add_row(cells)
38
+ row = build_row
39
+ cells.each { |data| row.add_cell data }
40
+ row
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ class Text
6
+ include Goldendocx::Element
7
+
8
+ namespace :w
9
+ tag :p
10
+
11
+ embeds_one :property, class_name: 'Goldendocx::Components::Properties::Property', auto_build: true
12
+ embeds_one :run, class_name: 'Goldendocx::Components::Properties::Run', auto_build: true
13
+
14
+ def align=(align)
15
+ property.align.align = align if align
16
+ end
17
+
18
+ def style=(style)
19
+ property.style.style_id = style if style
20
+ end
21
+
22
+ def text=(text)
23
+ run.build_text.value = text if text
24
+ end
25
+
26
+ def color=(color_hex)
27
+ (run.property || run.build_property).build_color(hex: color_hex) if color_hex
28
+ end
29
+
30
+ def bold=(bold)
31
+ (run.property || run.build_property).build_bold(enabled: bold) if bold
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Components
5
+ # 1 : 0.618
6
+ DEFAULT_WIDTH = Goldendocx::Units::EMU_PER_CENTIMETER * 15
7
+ DEFAULT_HEIGHT = Goldendocx::Units::EMU_PER_CENTIMETER * 9.27
8
+ end
9
+ end
10
+
11
+ require 'goldendocx/components/properties'
12
+ require 'goldendocx/components/chart'
13
+
14
+ Dir.glob(File.join(File.dirname(__FILE__), 'components', '*.rb').to_s).sort.each do |file|
15
+ require file
16
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ class Configuration
5
+ DEFAULT = {
6
+ xml_serializer: :ox
7
+ }.freeze
8
+
9
+ attr_accessor :xml_serializer
10
+
11
+ def initialize
12
+ DEFAULT.each do |key, value|
13
+ var_name = :"@#{key}"
14
+ instance_variable_set var_name, value
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module ContentTypes
5
+ class Default
6
+ include Goldendocx::Element
7
+
8
+ tag :Default
9
+ attribute :extension, alias_name: :Extension, readonly: true
10
+ attribute :content_type, alias_name: :ContentType, readonly: true
11
+
12
+ def initialize(extension, content_type)
13
+ @extension = extension
14
+ @content_type = content_type
15
+ end
16
+
17
+ def ==(other)
18
+ extension == other.extension && content_type == other.content_type
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module ContentTypes
5
+ class Override
6
+ include Goldendocx::Element
7
+
8
+ tag :Override
9
+ attribute :part_name, alias_name: :PartName, readonly: true
10
+ attribute :content_type, alias_name: :ContentType, readonly: true
11
+
12
+ def initialize(part_name, content_type)
13
+ @part_name = part_name
14
+ @content_type = content_type
15
+ end
16
+
17
+ def ==(other)
18
+ part_name == other.part_name && content_type == other.content_type
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Just to mark a element as a root document node
4
+ module Goldendocx
5
+ module Document
6
+ def self.included(base)
7
+ base.include(Goldendocx::Element)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def concern_namespaces(*namespaces)
13
+ namespaces.each do |namespace|
14
+ concerned_namespaces << namespace unless concerned_namespaces.include?(namespace)
15
+ end
16
+ end
17
+
18
+ def concerned_namespaces
19
+ @concerned_namespaces ||= []
20
+ end
21
+
22
+ def ignore_namespaces(*namespaces)
23
+ namespaces.each do |namespace|
24
+ ignorable_namespaces << namespace.to_sym unless ignorable_namespaces.include?(namespace)
25
+ end
26
+ end
27
+
28
+ def ignorable_namespaces
29
+ @ignorable_namespaces ||= []
30
+ end
31
+ end
32
+
33
+ def concerned_namespaces
34
+ self.class.concerning_ancestors.flat_map(&:concerned_namespaces)
35
+ end
36
+
37
+ def ignorable_namespaces
38
+ self.class.concerning_ancestors.flat_map(&:ignorable_namespaces)
39
+ end
40
+
41
+ def to_document_xml
42
+ Goldendocx.xml_serializer.build_document_xml(root_tag, concerned_namespaces, ignorable_namespaces) do |xml|
43
+ attributes.each { |name, value| xml[name] = value }
44
+
45
+ yield(xml) if block_given?
46
+
47
+ children.each { |child| xml << child }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Documents
5
+ class Body
6
+ include Goldendocx::Element
7
+
8
+ XML_PATH = 'word/document.xml'
9
+
10
+ attr_reader :components, :properties, :charts
11
+
12
+ namespace :w
13
+ tag :body
14
+
15
+ embeds_many :texts, class_name: 'Goldendocx::Components::Text'
16
+ embeds_many :images, class_name: 'Goldendocx::Components::Image'
17
+ embeds_many :tables, class_name: 'Goldendocx::Components::Table'
18
+
19
+ def initialize
20
+ @components = []
21
+ @charts = []
22
+ end
23
+
24
+ def read_from(docx_file)
25
+ Goldendocx.xml_serializer.parse(docx_file.read(XML_PATH), %w[w:document w:body *]).map do |node|
26
+ element = Goldendocx::Documents::Element.new(node)
27
+ @components << element if element.component?
28
+ @properties = element if element.properties?
29
+ end
30
+ end
31
+
32
+ # FIXME: Override for children not in correctly order
33
+ def to_element(**_context)
34
+ Goldendocx.xml_serializer.build_element(root_tag) do |xml|
35
+ components.each { |component| xml << component }
36
+ xml << properties if properties
37
+
38
+ yield(xml) if block_given?
39
+ end
40
+ end
41
+
42
+ def create_text(text, options = {})
43
+ text = build_text(text: text, **options.slice(:align, :color, :bold))
44
+
45
+ components << text
46
+
47
+ text
48
+ end
49
+
50
+ def create_table(options = {})
51
+ table = build_table
52
+ table.width = options[:width] if options[:width]
53
+
54
+ components << table
55
+
56
+ table
57
+ end
58
+
59
+ def create_image(relationship_id, options)
60
+ image = build_image(relationship_id: relationship_id, width: options[:width], height: options[:height])
61
+
62
+ components << image
63
+
64
+ image
65
+ end
66
+
67
+ def create_embed_image(relationship_id, options)
68
+ Goldendocx::Components::Image.new(relationship_id: relationship_id, width: options[:width], height: options[:height])
69
+ end
70
+
71
+ def ensure_chart_type(chart_type)
72
+ chart_class = "::Goldendocx::Components::#{chart_type.capitalize}Chart"
73
+ raise Goldendocx::Charts::InvalidChartType unless Kernel.const_defined?(chart_class)
74
+
75
+ Kernel.const_get(chart_class)
76
+ end
77
+
78
+ def create_chart(chart_type, chart_id, relationship_id, options)
79
+ chart_class = ensure_chart_type(chart_type)
80
+
81
+ chart = chart_class.new(chart_id, relationship_id, options)
82
+
83
+ charts << chart
84
+ components << chart
85
+
86
+ chart
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Documents
5
+ class Document
6
+ include Goldendocx::Document
7
+
8
+ XML_PATH = 'word/document.xml'
9
+
10
+ namespace :w
11
+ tag :document
12
+ concern_namespaces :wpc, :o, :mo, :mv, :r, :m, :v, :wp14, :wp, :w10,
13
+ :w, :w14, :w15, :w16cid, :w16se, :wpg, :wpi, :wne, :wps, :mc,
14
+ :a, :a14, :pic, :c
15
+ ignore_namespaces :w14, :w15, :w16se, :w16cid, :wp14
16
+
17
+ embeds_one :body, class_name: 'Goldendocx::Documents::Body', auto_build: true
18
+
19
+ def read_from(docx_file)
20
+ body.read_from(docx_file)
21
+ end
22
+
23
+ def write_to(zos)
24
+ zos.put_next_entry XML_PATH
25
+ zos.write to_document_xml
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldendocx
4
+ module Documents
5
+ class Element
6
+ def initialize(node)
7
+ @node = node
8
+ end
9
+
10
+ def component?
11
+ %w[w:p w:tbl].include?(@node.name)
12
+ end
13
+
14
+ def properties?
15
+ @node.name == 'w:sectPr'
16
+ end
17
+
18
+ def to_element(**_context)
19
+ @node
20
+ end
21
+ end
22
+ end
23
+ end