coradoc 2.0.1 → 2.0.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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +55 -167
  3. data/coradoc-adoc/lib/coradoc/asciidoc/model/base.rb +4 -3
  4. data/coradoc-adoc/lib/coradoc/asciidoc/model/document.rb +1 -1
  5. data/coradoc-adoc/lib/coradoc/asciidoc/model/include.rb +1 -1
  6. data/coradoc-adoc/lib/coradoc/asciidoc/model/resolver.rb +2 -2
  7. data/coradoc-adoc/lib/coradoc/asciidoc/model/serialization/asciidoc_transform.rb +3 -3
  8. data/coradoc-adoc/lib/coradoc/asciidoc/model/table_row.rb +1 -1
  9. data/coradoc-adoc/lib/coradoc/asciidoc/model/text_element.rb +4 -8
  10. data/coradoc-adoc/lib/coradoc/asciidoc/parse_error.rb +6 -6
  11. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/adoc_serializer.rb +5 -10
  12. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/formatter.rb +4 -3
  13. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/serializers/base.rb +8 -20
  14. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/serializers/block/core.rb +1 -1
  15. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/serializers/document.rb +3 -6
  16. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/serializers/inline/strikethrough.rb +1 -1
  17. data/coradoc-adoc/lib/coradoc/asciidoc/serializer/serializers/list/item.rb +5 -9
  18. data/coradoc-adoc/lib/coradoc/asciidoc/transform/from_core_model.rb +26 -34
  19. data/coradoc-adoc/lib/coradoc/asciidoc/transform/from_core_model_registrations.rb +18 -18
  20. data/coradoc-adoc/lib/coradoc/asciidoc/transform/to_core_model.rb +96 -123
  21. data/coradoc-adoc/lib/coradoc/asciidoc/transform/to_core_model_registrations.rb +10 -6
  22. data/coradoc-adoc/lib/coradoc/asciidoc/transformer/header_rules.rb +5 -5
  23. data/coradoc-adoc/lib/coradoc/asciidoc/transformer/list_rules.rb +2 -2
  24. data/coradoc-adoc/lib/coradoc/asciidoc/transformer/structural_rules.rb +1 -1
  25. data/coradoc-adoc/lib/coradoc/asciidoc/transformer.rb +5 -5
  26. data/coradoc-adoc/lib/coradoc/asciidoc.rb +1 -1
  27. data/coradoc-adoc/lib/coradoc/util/asciidoc.rb +4 -3
  28. data/coradoc-adoc/spec/coradoc/asciidoc/transform/from_core_model_spec.rb +4 -2
  29. data/coradoc-docx/lib/coradoc/docx/transform/rules/run_rule.rb +4 -1
  30. data/coradoc-docx/lib/coradoc/docx/transform/rules/simple_field_rule.rb +5 -1
  31. data/coradoc-docx/lib/coradoc/docx/transform/rules/table_rule.rb +0 -1
  32. data/coradoc-docx/lib/coradoc/docx.rb +6 -4
  33. data/coradoc-docx/spec/coradoc/docx/transform/from_core_model_spec.rb +5 -2
  34. data/coradoc-docx/spec/coradoc/docx/transform/rules/rule_unit_spec.rb +27 -7
  35. data/coradoc-docx/spec/coradoc/docx/transform/to_core_model_spec.rb +6 -2
  36. data/coradoc-html/lib/coradoc/html/converters/base.rb +4 -1
  37. data/coradoc-html/lib/coradoc/html.rb +1 -1
  38. data/coradoc-markdown/lib/coradoc/markdown/transform/from_core_model.rb +2 -2
  39. data/coradoc-markdown/lib/coradoc/markdown.rb +1 -1
  40. data/lib/coradoc/configurable.rb +6 -2
  41. data/lib/coradoc/coradoc.rb +18 -16
  42. data/lib/coradoc/core_model/base.rb +3 -3
  43. data/lib/coradoc/core_model/list_item.rb +3 -3
  44. data/lib/coradoc/core_model/toc_generator.rb +1 -1
  45. data/lib/coradoc/document_manipulator.rb +9 -13
  46. data/lib/coradoc/format_module.rb +16 -4
  47. data/lib/coradoc/input.rb +1 -1
  48. data/lib/coradoc/output.rb +1 -1
  49. data/lib/coradoc/query.rb +38 -186
  50. data/lib/coradoc/registry.rb +5 -7
  51. data/lib/coradoc/serializer/registry.rb +3 -5
  52. data/lib/coradoc/validation.rb +40 -21
  53. data/lib/coradoc/version.rb +1 -1
  54. metadata +1 -1
@@ -85,19 +85,12 @@ module Coradoc
85
85
  when Proc
86
86
  child.call
87
87
  else
88
- # Handle objects that respond to to_adoc (like test doubles)
89
- if child.respond_to?(:to_adoc)
90
- # For non-Coradoc objects, call to_adoc without arguments
91
- # Test doubles and external objects typically don't accept context
92
- child.to_adoc
93
- else
94
- # This is a programming error - we received an unexpected type
95
- raise ArgumentError,
96
- "Cannot serialize child of type #{child.class.name}. " \
97
- 'Expected String, Coradoc::AsciiDoc::Model::Base, ' \
98
- 'Lutaml::Model::Serializable, or Proc. ' \
99
- "Got: #{child.inspect[0..100]}"
100
- end
88
+ # This is a programming error - we received an unexpected type
89
+ raise ArgumentError,
90
+ "Cannot serialize child of type #{child.class.name}. " \
91
+ 'Expected String, Coradoc::AsciiDoc::Model::Base, ' \
92
+ 'Lutaml::Model::Serializable, or Proc. ' \
93
+ "Got: #{child.inspect[0..100]}"
101
94
  end
102
95
  end
103
96
 
@@ -115,14 +108,10 @@ module Coradoc
115
108
  when nil
116
109
  ''
117
110
  else
118
- # Try AdocSerializer first for Lutaml models
111
+ # Try AdocSerializer for Lutaml models
119
112
  if content.is_a?(Lutaml::Model::Serializable) || content.is_a?(Coradoc::AsciiDoc::Model::Base)
120
113
  AdocSerializer.serialize(content)
121
- # Fall back to to_adoc if available
122
- elsif content.respond_to?(:to_adoc)
123
- content.to_adoc
124
114
  else
125
- # This is a programming error - we received an unexpected type
126
115
  raise ArgumentError,
127
116
  "Cannot serialize content of type #{content.class.name}. " \
128
117
  'Expected String, nil, Array, or serializable object. ' \
@@ -163,8 +152,7 @@ module Coradoc
163
152
  # @param model [Coradoc::AsciiDoc::Model::Base] Model with attributes
164
153
  # @return [String] Formatted block attributes
165
154
  def format_block_attributes(model)
166
- return '' unless model.respond_to?(:attributes) &&
167
- model.attributes
155
+ return '' unless model.is_a?(Lutaml::Model::Serializable) && model.attributes
168
156
 
169
157
  Formatter.block_attributes(model.attributes)
170
158
  end
@@ -52,7 +52,7 @@ module Coradoc
52
52
  def gen_anchor
53
53
  return '' unless @model.anchor
54
54
 
55
- anchor_str = if @model.anchor.respond_to?(:to_adoc)
55
+ anchor_str = if @model.anchor.is_a?(Coradoc::AsciiDoc::Model::Base)
56
56
  @model.anchor.to_adoc
57
57
  else
58
58
  @model.anchor.to_s
@@ -25,23 +25,20 @@ module Coradoc
25
25
  parts << "\n" if @model.sections && !@model.sections.empty? && !@model.header
26
26
 
27
27
  # Only serialize header if it has a non-empty title
28
- if @model.header.respond_to?(:title) && !@model.header.title.to_s.empty?
28
+ if @model.header&.title && !@model.header.title.to_s.empty?
29
29
  parts << serialize_child(@model.header,
30
30
  @context)
31
31
  end
32
32
 
33
33
  # Only serialize document_attributes if it has data
34
- if @model.document_attributes.respond_to?(:data) &&
35
- @model.document_attributes.data && !@model.document_attributes.data.empty?
36
- parts << serialize_child(@model.document_attributes, @context)
37
- end
34
+ parts << serialize_child(@model.document_attributes, @context) if @model.document_attributes&.data && !@model.document_attributes.data.empty?
38
35
 
39
36
  # Serialize sections with last_element tracking
40
37
  parts << serialize_sections_with_last_element if @model.sections && !@model.sections.empty?
41
38
  result = parts.join
42
39
 
43
40
  # Clean up leading newlines when we have header
44
- if @model.header.respond_to?(:title) && !@model.header.title.to_s.empty?
41
+ if @model.header&.title && !@model.header.title.to_s.empty?
45
42
  result = result.sub(LEADING_NEWLINES_REGEX,
46
43
  '')
47
44
  end
@@ -12,7 +12,7 @@ module Coradoc
12
12
 
13
13
  return '' if content.empty?
14
14
 
15
- if model.respond_to?(:unconstrained) && model.unconstrained
15
+ if model.unconstrained
16
16
  "[.line-through]#[.line-through]##{content}[.line-through]#[.line-through]#"
17
17
  else
18
18
  "[.line-through]##{content}#"
@@ -31,14 +31,10 @@ module Coradoc
31
31
  when Coradoc::AsciiDoc::Model::Base, Lutaml::Model::Serializable
32
32
  subitem.to_adoc
33
33
  else
34
- if subitem.respond_to?(:to_adoc)
35
- subitem.to_adoc
36
- else
37
- raise ArgumentError,
38
- "Cannot serialize list item content of type #{subitem.class.name}. " \
39
- 'Expected String, Coradoc::AsciiDoc::Model::Base, or ' \
40
- "Lutaml::Model::Serializable. Got: #{subitem.inspect[0..100]}"
41
- end
34
+ raise ArgumentError,
35
+ "Cannot serialize list item content of type #{subitem.class.name}. " \
36
+ 'Expected String, Coradoc::AsciiDoc::Model::Base, or ' \
37
+ "Lutaml::Model::Serializable. Got: #{subitem.inspect[0..100]}"
42
38
  end
43
39
 
44
40
  inline = inline?(subitem)
@@ -90,7 +86,7 @@ module Coradoc
90
86
  attach = model.attached.map do |elem|
91
87
  "+\n#{elem.to_adoc}"
92
88
  end.join
93
- nest = model.nested.nil? || (model.nested.respond_to?(:empty?) && model.nested.empty?) ? '' : model.nested.to_adoc
89
+ nest = model.nested.nil? || (model.nested.is_a?(Array) && model.nested.empty?) ? '' : model.nested.to_adoc
94
90
  out = " #{_anchor}#{out}#{model.line_break}"
95
91
  out + attach + nest
96
92
  end
@@ -65,8 +65,6 @@ module Coradoc
65
65
  end
66
66
  end
67
67
 
68
- private
69
-
70
68
  def transform_structural_element(element)
71
69
  case element.element_type
72
70
  when 'document'
@@ -173,29 +171,6 @@ module Coradoc
173
171
  end
174
172
  end
175
173
 
176
- def safe_content_to_string(content)
177
- case content
178
- when String
179
- content
180
- when Array
181
- content.map { |item| safe_content_to_string(item) }.join
182
- when Lutaml::Model::Serializable
183
- if content.respond_to?(:to_adoc)
184
- content.to_adoc
185
- elsif content.respond_to?(:text)
186
- content.text.to_s
187
- elsif content.respond_to?(:content)
188
- safe_content_to_string(content.content)
189
- else
190
- ''
191
- end
192
- when nil
193
- ''
194
- else
195
- content.respond_to?(:to_str) ? content.to_s : ''
196
- end
197
- end
198
-
199
174
  def transform_table(table)
200
175
  rows = Array(table.rows).map do |row|
201
176
  columns = Array(row.cells).map do |cell|
@@ -373,6 +348,29 @@ module Coradoc
373
348
  )
374
349
  end
375
350
 
351
+ private
352
+
353
+ def safe_content_to_string(content)
354
+ case content
355
+ when String
356
+ content
357
+ when Array
358
+ content.map { |item| safe_content_to_string(item) }.join
359
+ when Lutaml::Model::Serializable
360
+ if content.is_a?(Coradoc::AsciiDoc::Model::Base)
361
+ content.to_adoc
362
+ elsif content.class.attributes.key?(:text)
363
+ content.text.to_s
364
+ else
365
+ ''
366
+ end
367
+ when nil
368
+ ''
369
+ else
370
+ content.is_a?(String) ? content : ''
371
+ end
372
+ end
373
+
376
374
  def create_title(text, level)
377
375
  return nil if text.nil?
378
376
 
@@ -391,12 +389,10 @@ module Coradoc
391
389
  when Coradoc::AsciiDoc::Model::Base
392
390
  content
393
391
  when Lutaml::Model::Serializable
394
- text = if content.respond_to?(:to_adoc)
392
+ text = if content.is_a?(Coradoc::AsciiDoc::Model::Base)
395
393
  content.to_adoc
396
- elsif content.respond_to?(:text)
394
+ elsif content.class.attributes.key?(:text)
397
395
  content.text.to_s
398
- elsif content.respond_to?(:content)
399
- content.content.to_s
400
396
  else
401
397
  ''
402
398
  end
@@ -404,11 +400,7 @@ module Coradoc
404
400
  when String
405
401
  Coradoc::AsciiDoc::Model::TextElement.new(content: content)
406
402
  else
407
- text = if content.respond_to?(:to_str)
408
- content.to_s
409
- else
410
- ''
411
- end
403
+ text = content.is_a?(String) ? content : ''
412
404
  Coradoc::AsciiDoc::Model::TextElement.new(content: text)
413
405
  end
414
406
  end
@@ -19,101 +19,101 @@ module Coradoc
19
19
  def register_structural_transformers!
20
20
  Registry.register(
21
21
  Coradoc::CoreModel::StructuralElement,
22
- ->(model) { FromCoreModel.send(:transform_structural_element, model) }
22
+ ->(model) { FromCoreModel.transform_structural_element(model) }
23
23
  )
24
24
  end
25
25
 
26
26
  def register_block_transformers!
27
27
  Registry.register_with_priority(
28
28
  Coradoc::CoreModel::AnnotationBlock,
29
- ->(model) { FromCoreModel.send(:transform_annotation, model) },
29
+ ->(model) { FromCoreModel.transform_annotation(model) },
30
30
  priority: 10
31
31
  )
32
32
 
33
33
  Registry.register(
34
34
  Coradoc::CoreModel::Block,
35
- ->(model) { FromCoreModel.send(:transform_block, model) }
35
+ ->(model) { FromCoreModel.transform_block(model) }
36
36
  )
37
37
  end
38
38
 
39
39
  def register_list_transformers!
40
40
  Registry.register(
41
41
  Coradoc::CoreModel::ListBlock,
42
- ->(model) { FromCoreModel.send(:transform_list, model) }
42
+ ->(model) { FromCoreModel.transform_list(model) }
43
43
  )
44
44
 
45
45
  Registry.register(
46
46
  Coradoc::CoreModel::ListItem,
47
- ->(model) { FromCoreModel.send(:transform_list_item, model) }
47
+ ->(model) { FromCoreModel.transform_list_item(model) }
48
48
  )
49
49
 
50
50
  Registry.register(
51
51
  Coradoc::CoreModel::DefinitionList,
52
- ->(model) { FromCoreModel.send(:transform_definition_list, model) }
52
+ ->(model) { FromCoreModel.transform_definition_list(model) }
53
53
  )
54
54
 
55
55
  Registry.register(
56
56
  Coradoc::CoreModel::DefinitionItem,
57
- ->(model) { FromCoreModel.send(:transform_definition_item, model) }
57
+ ->(model) { FromCoreModel.transform_definition_item(model) }
58
58
  )
59
59
  end
60
60
 
61
61
  def register_inline_transformers!
62
62
  Registry.register(
63
63
  Coradoc::CoreModel::InlineElement,
64
- ->(model) { FromCoreModel.send(:transform_inline, model) }
64
+ ->(model) { FromCoreModel.transform_inline(model) }
65
65
  )
66
66
  end
67
67
 
68
68
  def register_other_transformers!
69
69
  Registry.register(
70
70
  Coradoc::CoreModel::Table,
71
- ->(model) { FromCoreModel.send(:transform_table, model) }
71
+ ->(model) { FromCoreModel.transform_table(model) }
72
72
  )
73
73
 
74
74
  Registry.register(
75
75
  Coradoc::CoreModel::Term,
76
- ->(model) { FromCoreModel.send(:transform_term, model) }
76
+ ->(model) { FromCoreModel.transform_term(model) }
77
77
  )
78
78
 
79
79
  Registry.register(
80
80
  Coradoc::CoreModel::Image,
81
- ->(model) { FromCoreModel.send(:transform_image, model) }
81
+ ->(model) { FromCoreModel.transform_image(model) }
82
82
  )
83
83
 
84
84
  Registry.register(
85
85
  Coradoc::CoreModel::Footnote,
86
- ->(model) { FromCoreModel.send(:transform_footnote, model) }
86
+ ->(model) { FromCoreModel.transform_footnote(model) }
87
87
  )
88
88
 
89
89
  Registry.register(
90
90
  Coradoc::CoreModel::FootnoteReference,
91
- ->(model) { FromCoreModel.send(:transform_footnote_reference, model) }
91
+ ->(model) { FromCoreModel.transform_footnote_reference(model) }
92
92
  )
93
93
 
94
94
  Registry.register(
95
95
  Coradoc::CoreModel::Abbreviation,
96
- ->(model) { FromCoreModel.send(:transform_abbreviation, model) }
96
+ ->(model) { FromCoreModel.transform_abbreviation(model) }
97
97
  )
98
98
 
99
99
  Registry.register(
100
100
  Coradoc::CoreModel::Toc,
101
- ->(model) { FromCoreModel.send(:transform_toc, model) }
101
+ ->(model) { FromCoreModel.transform_toc(model) }
102
102
  )
103
103
 
104
104
  Registry.register(
105
105
  Coradoc::CoreModel::TocEntry,
106
- ->(model) { FromCoreModel.send(:transform_toc_entry, model) }
106
+ ->(model) { FromCoreModel.transform_toc_entry(model) }
107
107
  )
108
108
 
109
109
  Registry.register(
110
110
  Coradoc::CoreModel::Bibliography,
111
- ->(model) { FromCoreModel.send(:transform_bibliography, model) }
111
+ ->(model) { FromCoreModel.transform_bibliography(model) }
112
112
  )
113
113
 
114
114
  Registry.register(
115
115
  Coradoc::CoreModel::BibliographyEntry,
116
- ->(model) { FromCoreModel.send(:transform_bibliography_entry, model) }
116
+ ->(model) { FromCoreModel.transform_bibliography_entry(model) }
117
117
  )
118
118
  end
119
119
  end