goldendocx 0.2.2 → 0.2.3

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 (61) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/CHANGELOG.md +8 -2
  4. data/Gemfile.lock +3 -1
  5. data/README.md +10 -2
  6. data/Rakefile +14 -3
  7. data/demo/templates/generate_codes.rb +10 -0
  8. data/demo/templates/xml_to_class.rb +91 -0
  9. data/demo/texts/append_plain_text.rb +1 -1
  10. data/demo/texts/append_styled_text.rb +1 -2
  11. data/demo/texts/create_text.rb +14 -0
  12. data/lib/extensions/active_support_extensions.rb +16 -0
  13. data/lib/extensions/nokogiri_extensions.rb +41 -0
  14. data/lib/extensions/ox_extensions.rb +35 -0
  15. data/lib/extensions/xml_serialize_extensions.rb +45 -0
  16. data/lib/goldendocx/components/properties/font_property.rb +19 -0
  17. data/lib/goldendocx/components/properties/language_property.rb +18 -0
  18. data/lib/goldendocx/components/properties/run_property.rb +2 -0
  19. data/lib/goldendocx/content_types/default.rb +2 -6
  20. data/lib/goldendocx/content_types/override.rb +2 -6
  21. data/lib/goldendocx/document.rb +9 -28
  22. data/lib/goldendocx/documents/body.rb +20 -11
  23. data/lib/goldendocx/documents/document.rb +2 -4
  24. data/lib/goldendocx/documents/latent_styles.rb +12 -0
  25. data/lib/goldendocx/documents/properties/default_style_property.rb +17 -0
  26. data/lib/goldendocx/documents/properties/page_margin_property.rb +37 -0
  27. data/lib/goldendocx/documents/properties/page_size_property.rb +17 -0
  28. data/lib/goldendocx/documents/properties/paragraph_default_style_property.rb +16 -0
  29. data/lib/goldendocx/documents/properties/run_default_style_property.rb +22 -0
  30. data/lib/goldendocx/documents/properties/section_property.rb +17 -0
  31. data/lib/goldendocx/documents/properties/style_name_property.rb +16 -0
  32. data/lib/goldendocx/documents/properties.rb +12 -0
  33. data/lib/goldendocx/documents/settings.rb +23 -0
  34. data/lib/goldendocx/documents/style.rb +10 -10
  35. data/lib/goldendocx/documents/styles.rb +9 -33
  36. data/lib/goldendocx/docx.rb +55 -12
  37. data/lib/goldendocx/element.rb +33 -140
  38. data/lib/goldendocx/has_associations.rb +52 -0
  39. data/lib/goldendocx/has_attributes.rb +67 -0
  40. data/lib/goldendocx/has_children.rb +116 -0
  41. data/lib/goldendocx/{documents → models}/relationship.rb +1 -1
  42. data/lib/goldendocx/{documents → models}/relationships.rb +4 -12
  43. data/lib/goldendocx/models.rb +10 -0
  44. data/lib/goldendocx/parts/app.rb +46 -0
  45. data/lib/goldendocx/parts/content_types.rb +16 -30
  46. data/lib/goldendocx/parts/core.rb +46 -0
  47. data/lib/goldendocx/parts/documents.rb +26 -13
  48. data/lib/goldendocx/parts/properties/created_at_property.rb +17 -0
  49. data/lib/goldendocx/parts/properties/creator_property.rb +16 -0
  50. data/lib/goldendocx/parts/properties/revision_property.rb +16 -0
  51. data/lib/goldendocx/parts/properties/updated_at_property.rb +17 -0
  52. data/lib/goldendocx/parts/properties/updater_property.rb +16 -0
  53. data/lib/goldendocx/parts/properties.rb +12 -0
  54. data/lib/goldendocx/tables/row.rb +1 -1
  55. data/lib/goldendocx/version.rb +1 -1
  56. data/lib/goldendocx/xml_serializers/nokogiri.rb +30 -22
  57. data/lib/goldendocx/xml_serializers/ox.rb +10 -20
  58. data/lib/goldendocx.rb +14 -2
  59. metadata +51 -7
  60. data/lib/goldendocx/documents/element.rb +0 -23
  61. data/lib/goldendocx/documents/unparsed_style.rb +0 -17
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'nokogiri'
3
+ require 'extensions/nokogiri_extensions'
4
4
 
5
5
  module Goldendocx
6
6
  module XmlSerializers
@@ -11,19 +11,33 @@ module Goldendocx
11
11
  options.no_declaration
12
12
  end
13
13
 
14
- def parse(xml, paths = [])
15
- xml = ::Nokogiri::XML(xml)
16
- # Should with namespaces
17
- xml = xml.xpath(paths.map { |path| path.include?(':') || path == '*' ? path : ['xmlns', path].join(':') }.join('/')) unless paths.empty?
18
- xml
14
+ # TODO: Nokogiri namespace is soooo strict....
15
+ # TODO: To read nokogiri source codes and try to understand how it works
16
+ def parse(xml)
17
+ document = ::Nokogiri::XML(xml)
18
+
19
+ missing_namespaces = document.errors.filter_map do |error|
20
+ error.str1 if error.message.match?('Namespace.*not defined')
21
+ end.uniq.compact
22
+ return document if missing_namespaces.blank?
23
+
24
+ wrap_document(document, missing_namespaces)
25
+ end
26
+
27
+ def search(node, paths)
28
+ return node if paths.blank?
29
+
30
+ search_paths = paths.map { |path| path.include?(':') || path == '*' ? path : ['xmlns', path].join(':') }
31
+ namespaces = node.namespaces.merge(node.document.namespaces)
32
+ node.xpath(search_paths.join('/'), namespaces)
19
33
  end
20
34
 
21
35
  def build_xml(tag, &block)
22
- build_element(tag, &block).to_xml(indent: 0, save_with: DEFAULT_BUILD_OPTIONS).delete("\n")
36
+ CGI.unescapeHTML build_element(tag, &block).to_xml(indent: 0, save_with: DEFAULT_BUILD_OPTIONS).delete("\n")
23
37
  end
24
38
 
25
39
  def build_document_xml(tag, namespaces = [], ignore_namespaces = [], &block)
26
- build_document(tag, namespaces, ignore_namespaces, &block).to_xml(indent: 0).delete("\n")
40
+ CGI.unescapeHTML build_document(tag, namespaces, ignore_namespaces, &block).to_xml(indent: 0).delete("\n")
27
41
  end
28
42
 
29
43
  def build_document(tag, namespaces = [], ignore_namespaces = [])
@@ -41,25 +55,19 @@ module Goldendocx
41
55
 
42
56
  def build_element(tag, parent: nil)
43
57
  parent ||= ::Nokogiri::XML('<?xml version="1.0"')
44
- ::Nokogiri::XML::Node.new(tag, parent).tap do |element|
58
+ ::Nokogiri::XML::Node.new(tag.to_s, parent).tap do |element|
45
59
  yield(element) if block_given?
46
60
  end
47
61
  end
48
- end
49
- end
50
- end
51
- end
52
62
 
53
- # FIXME: Temporarily here to provider syntactic sugar
54
- module Nokogiri
55
- module XML
56
- class Node
57
- def <<(node_or_tags)
58
- # FIXME: Add this line to transform element implicitly
59
- node_or_tags = node_or_tags.public_send(:to_element, parent: self) if node_or_tags.respond_to?(:to_element)
63
+ private
60
64
 
61
- add_child(node_or_tags)
62
- self
65
+ def wrap_document(document, missing_namespaces)
66
+ missing_namespaces.each do |ns|
67
+ document.root.add_namespace ns, (Goldendocx::NAMESPACES[ns.to_sym] || "#{ns}:goldendocx")
68
+ end
69
+ ::Nokogiri::XML(document.to_xml(indent: 0).delete("\n"))
70
+ end
63
71
  end
64
72
  end
65
73
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ox'
3
+ require 'extensions/ox_extensions'
4
4
 
5
5
  module Goldendocx
6
6
  module XmlSerializers
@@ -8,13 +8,19 @@ module Goldendocx
8
8
  class << self
9
9
  def parse(xml, paths = [])
10
10
  xml = ::Ox.parse(xml)
11
- xml = xml.locate(paths.join('/')) unless paths.empty?
12
- xml
11
+ xml = ::Ox::Document.new.tap { |document| document << xml } unless xml.is_a?(::Ox::Document)
12
+ search(xml, paths)
13
+ end
14
+
15
+ def search(node, paths)
16
+ return node if paths.blank?
17
+
18
+ node.locate(paths.join('/'))
13
19
  end
14
20
 
15
21
  def build_xml(tag, &block)
16
22
  xml = build_element(tag, &block)
17
- ::Ox.dump(xml, indent: -1, with_xml: true)
23
+ ::Ox.dump(xml, indent: -1, with_xml: true, encoding: 'UTF-8')
18
24
  end
19
25
 
20
26
  def build_document_xml(tag, namespaces = [], ignore_namespaces = [], &block)
@@ -45,19 +51,3 @@ module Goldendocx
45
51
  end
46
52
  end
47
53
  end
48
-
49
- # FIXME: Temporarily here to provider syntactic sugar
50
- module Ox
51
- class Element
52
- def <<(node)
53
- # FIXME: Add this line to transform element implicitly
54
- node = node.public_send(:to_element) if node.respond_to?(:to_element)
55
-
56
- raise 'argument to << must be a String or Ox::Node.' unless node.is_a?(String) || node.is_a?(Node)
57
-
58
- @nodes = [] if !instance_variable_defined?(:@nodes) || @nodes.nil?
59
- @nodes << node
60
- self
61
- end
62
- end
63
- end
data/lib/goldendocx.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'zip'
4
+ require 'extensions/active_support_extensions'
5
+ require 'extensions/xml_serialize_extensions'
4
6
 
5
7
  require 'goldendocx/version'
6
- require 'goldendocx/docx'
7
8
 
8
9
  module Goldendocx
9
10
  autoload :Configuration, 'goldendocx/configuration'
@@ -13,6 +14,7 @@ module Goldendocx
13
14
  a14: 'http://schemas.microsoft.com/office/drawing/2010/main',
14
15
  c: 'http://schemas.openxmlformats.org/drawingml/2006/chart',
15
16
  c14: 'http://schemas.microsoft.com/office/drawing/2007/8/2/chart',
17
+ cp: 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
16
18
  cx: 'http://schemas.microsoft.com/office/drawing/2014/chartex',
17
19
  cx1: 'http://schemas.microsoft.com/office/drawing/2015/9/8/chartex',
18
20
  cx2: 'http://schemas.microsoft.com/office/drawing/2015/10/21/chartex',
@@ -22,6 +24,9 @@ module Goldendocx
22
24
  cx6: 'http://schemas.microsoft.com/office/drawing/2016/5/12/chartex',
23
25
  cx7: 'http://schemas.microsoft.com/office/drawing/2016/5/13/chartex',
24
26
  cx8: 'http://schemas.microsoft.com/office/drawing/2016/5/14/chartex',
27
+ dc: 'http://purl.org/dc/elements/1.1/',
28
+ dcmitype: 'http://purl.org/dc/dcmitype/',
29
+ dcterms: 'http://purl.org/dc/terms/',
25
30
  m: 'http://schemas.openxmlformats.org/officeDocument/2006/math',
26
31
  mc: 'http://schemas.openxmlformats.org/markup-compatibility/2006',
27
32
  mo: 'http://schemas.microsoft.com/office/mac/office/2008/main',
@@ -43,7 +48,8 @@ module Goldendocx
43
48
  wpc: 'http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas',
44
49
  wpg: 'http://schemas.microsoft.com/office/word/2010/wordprocessingGroup',
45
50
  wpi: 'http://schemas.microsoft.com/office/word/2010/wordprocessingInk',
46
- wps: 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
51
+ wps: 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape',
52
+ xsi: 'http://www.w3.org/2001/XMLSchema-instance'
47
53
  }.freeze
48
54
 
49
55
  class << self
@@ -70,3 +76,9 @@ module Goldendocx
70
76
  end
71
77
  end
72
78
  end
79
+
80
+ require 'goldendocx/units'
81
+ require 'goldendocx/element'
82
+ require 'goldendocx/document'
83
+ require 'goldendocx/has_associations'
84
+ require 'goldendocx/docx'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goldendocx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - WangYuechen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-29 00:00:00.000000000 Z
11
+ date: 2023-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: 0.8.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: timecop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.9.6
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.9.6
167
181
  description: Ruby APIs for manipulating Microsoft Word based upon OOXML standards.
168
182
  email:
169
183
  - 291943739@qq.com
@@ -190,13 +204,20 @@ files:
190
204
  - demo/tables/create_styled_table.rb
191
205
  - demo/templates/bg.png
192
206
  - demo/templates/blankDocument.docx
207
+ - demo/templates/generate_codes.rb
193
208
  - demo/templates/image_base64
194
209
  - demo/templates/styles/reportLabel
195
210
  - demo/templates/styles/reportSubTitle
196
211
  - demo/templates/styles/reportTable
197
212
  - demo/templates/styles/reportTitle
213
+ - demo/templates/xml_to_class.rb
198
214
  - demo/texts/append_plain_text.rb
199
215
  - demo/texts/append_styled_text.rb
216
+ - demo/texts/create_text.rb
217
+ - lib/extensions/active_support_extensions.rb
218
+ - lib/extensions/nokogiri_extensions.rb
219
+ - lib/extensions/ox_extensions.rb
220
+ - lib/extensions/xml_serialize_extensions.rb
200
221
  - lib/goldendocx.rb
201
222
  - lib/goldendocx/charts.rb
202
223
  - lib/goldendocx/charts/category_axis.rb
@@ -247,11 +268,13 @@ files:
247
268
  - lib/goldendocx/components/properties/color_property.rb
248
269
  - lib/goldendocx/components/properties/drawing.rb
249
270
  - lib/goldendocx/components/properties/extents_property.rb
271
+ - lib/goldendocx/components/properties/font_property.rb
250
272
  - lib/goldendocx/components/properties/graphic_data_property.rb
251
273
  - lib/goldendocx/components/properties/graphic_frame_locks_property.rb
252
274
  - lib/goldendocx/components/properties/graphic_frame_property.rb
253
275
  - lib/goldendocx/components/properties/graphic_property.rb
254
276
  - lib/goldendocx/components/properties/inline.rb
277
+ - lib/goldendocx/components/properties/language_property.rb
255
278
  - lib/goldendocx/components/properties/non_visual_property.rb
256
279
  - lib/goldendocx/components/properties/padding_property.rb
257
280
  - lib/goldendocx/components/properties/property.rb
@@ -268,14 +291,23 @@ files:
268
291
  - lib/goldendocx/documents.rb
269
292
  - lib/goldendocx/documents/body.rb
270
293
  - lib/goldendocx/documents/document.rb
271
- - lib/goldendocx/documents/element.rb
272
- - lib/goldendocx/documents/relationship.rb
273
- - lib/goldendocx/documents/relationships.rb
294
+ - lib/goldendocx/documents/latent_styles.rb
295
+ - lib/goldendocx/documents/properties.rb
296
+ - lib/goldendocx/documents/properties/default_style_property.rb
297
+ - lib/goldendocx/documents/properties/page_margin_property.rb
298
+ - lib/goldendocx/documents/properties/page_size_property.rb
299
+ - lib/goldendocx/documents/properties/paragraph_default_style_property.rb
300
+ - lib/goldendocx/documents/properties/run_default_style_property.rb
301
+ - lib/goldendocx/documents/properties/section_property.rb
302
+ - lib/goldendocx/documents/properties/style_name_property.rb
303
+ - lib/goldendocx/documents/settings.rb
274
304
  - lib/goldendocx/documents/style.rb
275
305
  - lib/goldendocx/documents/styles.rb
276
- - lib/goldendocx/documents/unparsed_style.rb
277
306
  - lib/goldendocx/docx.rb
278
307
  - lib/goldendocx/element.rb
308
+ - lib/goldendocx/has_associations.rb
309
+ - lib/goldendocx/has_attributes.rb
310
+ - lib/goldendocx/has_children.rb
279
311
  - lib/goldendocx/images.rb
280
312
  - lib/goldendocx/images/picture.rb
281
313
  - lib/goldendocx/images/properties.rb
@@ -292,10 +324,21 @@ files:
292
324
  - lib/goldendocx/images/properties/stretch_property.rb
293
325
  - lib/goldendocx/images/properties/transform_property.rb
294
326
  - lib/goldendocx/images/shape.rb
327
+ - lib/goldendocx/models.rb
328
+ - lib/goldendocx/models/relationship.rb
329
+ - lib/goldendocx/models/relationships.rb
295
330
  - lib/goldendocx/parts.rb
331
+ - lib/goldendocx/parts/app.rb
296
332
  - lib/goldendocx/parts/content_types.rb
333
+ - lib/goldendocx/parts/core.rb
297
334
  - lib/goldendocx/parts/documents.rb
298
335
  - lib/goldendocx/parts/media.rb
336
+ - lib/goldendocx/parts/properties.rb
337
+ - lib/goldendocx/parts/properties/created_at_property.rb
338
+ - lib/goldendocx/parts/properties/creator_property.rb
339
+ - lib/goldendocx/parts/properties/revision_property.rb
340
+ - lib/goldendocx/parts/properties/updated_at_property.rb
341
+ - lib/goldendocx/parts/properties/updater_property.rb
299
342
  - lib/goldendocx/tables.rb
300
343
  - lib/goldendocx/tables/cell.rb
301
344
  - lib/goldendocx/tables/header_cell.rb
@@ -324,7 +367,8 @@ files:
324
367
  homepage: https://github.com/SheldonLeo/goldendocx
325
368
  licenses:
326
369
  - Nonstandard
327
- metadata: {}
370
+ metadata:
371
+ rubygems_mfa_required: 'true'
328
372
  post_install_message:
329
373
  rdoc_options: []
330
374
  require_paths:
@@ -1,23 +0,0 @@
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
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Goldendocx
4
- module Documents
5
- class UnparsedStyle
6
- attr_reader :node
7
-
8
- def initialize(node)
9
- @node = node
10
- end
11
-
12
- def to_element(**_context)
13
- @node
14
- end
15
- end
16
- end
17
- end