coridoc-html 1.1.23

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 (100) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/lib/coridoc/html/asset_resolver.rb +174 -0
  4. data/lib/coridoc/html/builder.rb +94 -0
  5. data/lib/coridoc/html/cleaner.rb +236 -0
  6. data/lib/coridoc/html/config.rb +154 -0
  7. data/lib/coridoc/html/converter_base.rb +174 -0
  8. data/lib/coridoc/html/converters/a.rb +77 -0
  9. data/lib/coridoc/html/converters/aside.rb +20 -0
  10. data/lib/coridoc/html/converters/audio.rb +19 -0
  11. data/lib/coridoc/html/converters/base.rb +100 -0
  12. data/lib/coridoc/html/converters/blockquote.rb +25 -0
  13. data/lib/coridoc/html/converters/br.rb +17 -0
  14. data/lib/coridoc/html/converters/bypass.rb +82 -0
  15. data/lib/coridoc/html/converters/code.rb +25 -0
  16. data/lib/coridoc/html/converters/div.rb +23 -0
  17. data/lib/coridoc/html/converters/dl.rb +82 -0
  18. data/lib/coridoc/html/converters/drop.rb +26 -0
  19. data/lib/coridoc/html/converters/em.rb +23 -0
  20. data/lib/coridoc/html/converters/figure.rb +33 -0
  21. data/lib/coridoc/html/converters/h.rb +58 -0
  22. data/lib/coridoc/html/converters/head.rb +29 -0
  23. data/lib/coridoc/html/converters/hr.rb +17 -0
  24. data/lib/coridoc/html/converters/img.rb +105 -0
  25. data/lib/coridoc/html/converters/li.rb +39 -0
  26. data/lib/coridoc/html/converters/mark.rb +21 -0
  27. data/lib/coridoc/html/converters/markup.rb +93 -0
  28. data/lib/coridoc/html/converters/math.rb +42 -0
  29. data/lib/coridoc/html/converters/media_base.rb +48 -0
  30. data/lib/coridoc/html/converters/ol.rb +42 -0
  31. data/lib/coridoc/html/converters/p.rb +64 -0
  32. data/lib/coridoc/html/converters/pass_through.rb +15 -0
  33. data/lib/coridoc/html/converters/positional_formatting.rb +35 -0
  34. data/lib/coridoc/html/converters/pre.rb +60 -0
  35. data/lib/coridoc/html/converters/q.rb +25 -0
  36. data/lib/coridoc/html/converters/strong.rb +22 -0
  37. data/lib/coridoc/html/converters/sub.rb +20 -0
  38. data/lib/coridoc/html/converters/sup.rb +20 -0
  39. data/lib/coridoc/html/converters/table.rb +64 -0
  40. data/lib/coridoc/html/converters/td.rb +42 -0
  41. data/lib/coridoc/html/converters/text.rb +66 -0
  42. data/lib/coridoc/html/converters/tr.rb +27 -0
  43. data/lib/coridoc/html/converters/video.rb +27 -0
  44. data/lib/coridoc/html/converters.rb +104 -0
  45. data/lib/coridoc/html/drop/annotation_drop.rb +31 -0
  46. data/lib/coridoc/html/drop/base.rb +72 -0
  47. data/lib/coridoc/html/drop/bibliography_drop.rb +15 -0
  48. data/lib/coridoc/html/drop/bibliography_entry_drop.rb +24 -0
  49. data/lib/coridoc/html/drop/block_drop.rb +78 -0
  50. data/lib/coridoc/html/drop/definition_item_drop.rb +61 -0
  51. data/lib/coridoc/html/drop/definition_list_drop.rb +15 -0
  52. data/lib/coridoc/html/drop/document_drop.rb +52 -0
  53. data/lib/coridoc/html/drop/drop_factory.rb +64 -0
  54. data/lib/coridoc/html/drop/footnote_drop.rb +24 -0
  55. data/lib/coridoc/html/drop/image_drop.rb +35 -0
  56. data/lib/coridoc/html/drop/inline_element_drop.rb +43 -0
  57. data/lib/coridoc/html/drop/list_block_drop.rb +19 -0
  58. data/lib/coridoc/html/drop/list_item_drop.rb +20 -0
  59. data/lib/coridoc/html/drop/raw_inline_element_drop.rb +30 -0
  60. data/lib/coridoc/html/drop/table_cell_drop.rb +35 -0
  61. data/lib/coridoc/html/drop/table_drop.rb +15 -0
  62. data/lib/coridoc/html/drop/table_row_drop.rb +23 -0
  63. data/lib/coridoc/html/drop/term_drop.rb +24 -0
  64. data/lib/coridoc/html/drop/text_content_drop.rb +15 -0
  65. data/lib/coridoc/html/drop/toc_drop.rb +15 -0
  66. data/lib/coridoc/html/drop/toc_entry_drop.rb +32 -0
  67. data/lib/coridoc/html/drop.rb +40 -0
  68. data/lib/coridoc/html/errors.rb +11 -0
  69. data/lib/coridoc/html/escape.rb +29 -0
  70. data/lib/coridoc/html/frontmatter_meta.rb +157 -0
  71. data/lib/coridoc/html/html_converter.rb +161 -0
  72. data/lib/coridoc/html/input_config.rb +71 -0
  73. data/lib/coridoc/html/layout_renderer.rb +195 -0
  74. data/lib/coridoc/html/lint/registrations.rb +40 -0
  75. data/lib/coridoc/html/lint.rb +16 -0
  76. data/lib/coridoc/html/plugin.rb +93 -0
  77. data/lib/coridoc/html/plugins/plateau.rb +212 -0
  78. data/lib/coridoc/html/postprocessor.rb +19 -0
  79. data/lib/coridoc/html/reference_materializers/link.rb +44 -0
  80. data/lib/coridoc/html/reference_materializers/navigation.rb +77 -0
  81. data/lib/coridoc/html/reference_materializers.rb +22 -0
  82. data/lib/coridoc/html/render_options.rb +40 -0
  83. data/lib/coridoc/html/renderer.rb +136 -0
  84. data/lib/coridoc/html/section_numberable.rb +9 -0
  85. data/lib/coridoc/html/spa.rb +49 -0
  86. data/lib/coridoc/html/static.rb +57 -0
  87. data/lib/coridoc/html/tag_mapping.rb +106 -0
  88. data/lib/coridoc/html/template_caching.rb +31 -0
  89. data/lib/coridoc/html/template_config.rb +92 -0
  90. data/lib/coridoc/html/template_helpers.rb +66 -0
  91. data/lib/coridoc/html/template_locator.rb +120 -0
  92. data/lib/coridoc/html/theme.rb +7 -0
  93. data/lib/coridoc/html/title_text.rb +57 -0
  94. data/lib/coridoc/html/toc_builder.rb +114 -0
  95. data/lib/coridoc/html/toc_serializer.rb +32 -0
  96. data/lib/coridoc/html/transform/from_core_model.rb +35 -0
  97. data/lib/coridoc/html/transform/to_core_model.rb +39 -0
  98. data/lib/coridoc/html/version.rb +7 -0
  99. data/lib/coridoc/html.rb +246 -0
  100. metadata +255 -0
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Em < Markup
7
+ INSTANCE = new
8
+
9
+ def coridoc_format_type
10
+ 'italic'
11
+ end
12
+
13
+ def markup_ancestor_tag_names
14
+ %w[em i cite]
15
+ end
16
+ end
17
+
18
+ register :em, Em::INSTANCE
19
+ register :i, Em::INSTANCE
20
+ register :cite, Em::INSTANCE
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Figure < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, state = {})
10
+ id = node['id']
11
+ title_content = extract_title(node)
12
+ content = treat_children_coridoc(node, state)
13
+
14
+ # Use CoreModel::ExampleBlock for example/figure
15
+ Coridoc::CoreModel::ExampleBlock.new(
16
+ title: extract_text_from_content(title_content),
17
+ children: content,
18
+ id: id
19
+ )
20
+ end
21
+
22
+ def extract_title(node)
23
+ title = node.at('./figcaption')
24
+ return '' if title.nil?
25
+
26
+ treat_children_coridoc(title, {})
27
+ end
28
+ end
29
+
30
+ register :figure, Figure::INSTANCE
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class H < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, state = {})
10
+ id = node['id']
11
+ internal_anchor = treat_children_anchors(node, state)
12
+
13
+ if id.to_s.empty? && internal_anchor.size.positive?
14
+ first_model = internal_anchor.first
15
+ id = first_model.target if first_model.is_a?(Coridoc::CoreModel::InlineElement) && first_model.target
16
+ end
17
+
18
+ level_int = node.name[/\d/].to_i
19
+ content = treat_children_no_anchors(node, state)
20
+
21
+ Coridoc::CoreModel::SectionElement.new(
22
+ title: extract_title_text(content),
23
+ level: level_int,
24
+ id: id,
25
+ children: []
26
+ )
27
+ end
28
+
29
+ def treat_children_no_anchors(node, state)
30
+ node.children.reject { |a| a.name == 'a' }
31
+ .map do |child|
32
+ treat_coridoc(child, state)
33
+ end.flatten.compact
34
+ end
35
+
36
+ def treat_children_anchors(node, state)
37
+ node.children.select { |a| a.name == 'a' }
38
+ .map do |child|
39
+ treat_coridoc(child, state)
40
+ end.flatten.compact
41
+ end
42
+
43
+ private
44
+
45
+ def extract_title_text(content)
46
+ Coridoc::CoreModel::InlineContent.text_of(content).strip
47
+ end
48
+ end
49
+
50
+ register :h1, H::INSTANCE
51
+ register :h2, H::INSTANCE
52
+ register :h3, H::INSTANCE
53
+ register :h4, H::INSTANCE
54
+ register :h5, H::INSTANCE
55
+ register :h6, H::INSTANCE
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Head < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, _state = {})
10
+ title = extract_title(node)
11
+ # Use DocumentElement for document header
12
+ Coridoc::CoreModel::DocumentElement.new(
13
+ title: title,
14
+ level: 0
15
+ )
16
+ end
17
+
18
+ def extract_title(node)
19
+ title = node.at('./title')
20
+ return '(???)' if title.nil?
21
+
22
+ title.text
23
+ end
24
+ end
25
+
26
+ register :head, Head::INSTANCE
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Hr < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(_node, _state = {})
10
+ Coridoc::CoreModel::HorizontalRuleBlock.new
11
+ end
12
+ end
13
+
14
+ register :hr, Hr::INSTANCE
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'pathname'
5
+ require 'tempfile'
6
+ require 'base64'
7
+ require 'marcel'
8
+
9
+ module Coridoc
10
+ module Html
11
+ module Converters
12
+ class Img < Base
13
+ INSTANCE = new
14
+
15
+ def image_number
16
+ format(
17
+ Html.input_config.image_counter_pattern,
18
+ Html.input_config.image_counter
19
+ )
20
+ end
21
+
22
+ def image_number_increment
23
+ Html.input_config.image_counter += 1
24
+ end
25
+
26
+ def datauri2file(src)
27
+ return unless src
28
+
29
+ %r{^data:image/(?:[^;]+);base64,(?<imgdata>.+)$} =~ src
30
+
31
+ dest_dir = Pathname.new(Html.input_config.destination).dirname
32
+ images_dir = dest_dir.join('images')
33
+ FileUtils.mkdir_p(images_dir)
34
+
35
+ ext, image_src_path, tempfile = determine_image_src_path(
36
+ src,
37
+ imgdata
38
+ )
39
+ image_dest_path = images_dir + "#{image_number}.#{ext}"
40
+
41
+ if File.exist?(image_src_path)
42
+ FileUtils.cp(image_src_path, image_dest_path)
43
+ else
44
+ Kernel.warn "Image #{image_src_path} does not exist"
45
+ end
46
+
47
+ image_number_increment
48
+
49
+ image_dest_path.relative_path_from(dest_dir)
50
+ ensure
51
+ tempfile&.close!
52
+ end
53
+
54
+ def determine_image_src_path(src, imgdata)
55
+ return copy_temp_file(imgdata) if imgdata
56
+
57
+ ext = File.extname(src).strip.downcase[1..]
58
+ [ext, Pathname.new(Html.input_config.sourcedir).join(src)]
59
+ end
60
+
61
+ def copy_temp_file(imgdata)
62
+ f = Tempfile.open(['radoc', '.jpg'])
63
+ f.binmode
64
+ f.write(Base64.strict_decode64(imgdata))
65
+ f.rewind
66
+ ext = Marcel::MimeType.for(f).sub(%r{^[^/]+/}, '')
67
+ ext = 'svg' if ext == 'svg+xml'
68
+ [ext, f.path, f]
69
+ end
70
+
71
+ def to_coridoc(node, _state = {})
72
+ id = node['id']
73
+ alt = node['alt']
74
+ src = node['src']
75
+ width = node['width']
76
+ height = node['height']
77
+
78
+ # Convert width/height to integers if they are numeric strings
79
+ width = width.to_i if width&.match?(/\A\d+\z/)
80
+ height = height.to_i if height&.match?(/\A\d+\z/)
81
+
82
+ title = extract_title(node)
83
+
84
+ src = datauri2file(src) if Html.input_config.external_images
85
+
86
+ # Use CoreModel::Image
87
+ return unless src
88
+
89
+ # HTML title -> image block title (#109): serialized as the
90
+ # `.title` caption line, not stuffed into the alt attributes.
91
+ Coridoc::CoreModel::Image.new(
92
+ src: src,
93
+ alt: alt,
94
+ title: title,
95
+ width: width&.to_s,
96
+ height: height&.to_s,
97
+ id: id
98
+ )
99
+ end
100
+ end
101
+
102
+ register :img, Img::INSTANCE
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Li < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, state = {})
10
+ id = node['id']
11
+
12
+ # Check if all children are <p> tags
13
+ p_children = node.children.select { |child| child.name == 'p' }
14
+ non_empty_children = node.children.reject { |c| c.text? && c.text.strip.empty? }
15
+
16
+ content = if p_children.any? && p_children.size == non_empty_children.size && p_children.size == 1
17
+ # Single <p> tag - extract its content directly as inline content
18
+ treat_children_coridoc(p_children.first, state)
19
+ else
20
+ treat_children_coridoc(node, state)
21
+ end
22
+
23
+ # Nested lists live in the canonical +nested_list+ slot
24
+ # (single); remaining children carry inline content.
25
+ nested = Array(content).find { |c| c.is_a?(Coridoc::CoreModel::ListBlock) }
26
+ children = nested ? Array(content).reject { |c| c.equal?(nested) } : content
27
+
28
+ Coridoc::CoreModel::ListItem.new(
29
+ children: children,
30
+ nested_list: nested,
31
+ id: id
32
+ )
33
+ end
34
+ end
35
+
36
+ register :li, Li::INSTANCE
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Mark < Markup
7
+ INSTANCE = new
8
+
9
+ def coridoc_format_type
10
+ 'highlight'
11
+ end
12
+
13
+ def markup_ancestor_tag_names
14
+ %w[mark]
15
+ end
16
+ end
17
+
18
+ register :mark, Mark::INSTANCE
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Markup < Base
7
+ def to_coridoc(node, state = {})
8
+ u_before = unconstrained_before?(node)
9
+ u_after = unconstrained_after?(node)
10
+
11
+ leading_ws, trailing_ws =
12
+ extract_leading_trailing_whitespace(node)
13
+
14
+ # Wrap whitespace in InlineElement so it can be processed
15
+ leading_whitespace = if leading_ws
16
+ Coridoc::CoreModel::TextElement.new(
17
+ content: leading_ws
18
+ )
19
+ end
20
+ trailing_whitespace = if trailing_ws
21
+ Coridoc::CoreModel::TextElement.new(
22
+ content: trailing_ws
23
+ )
24
+ end
25
+
26
+ content = treat_children_coridoc(node, state)
27
+
28
+ if node_has_ancestor?(node, markup_ancestor_tag_names)
29
+ content
30
+ elsif node.children.empty?
31
+ # Return InlineElement wrapper for whitespace
32
+ if leading_ws
33
+ Coridoc::CoreModel::TextElement.new(
34
+ content: leading_ws
35
+ )
36
+ end
37
+ else
38
+ u = (u_before && leading_whitespace.nil?) ||
39
+ (u_after && trailing_whitespace.nil?)
40
+
41
+ # Separate text strings from InlineElements in content array
42
+ text_content, nested = extract_text_and_elements(content)
43
+
44
+ # Create CoreModel::InlineElement with the appropriate format type
45
+ inline_element = Coridoc::CoreModel::InlineElement.format_type_class(coridoc_format_type).new(
46
+ content: text_content,
47
+ nested_elements: nested.empty? ? nil : nested,
48
+ metadata: { unconstrained: u }
49
+ )
50
+ result = [leading_whitespace, inline_element, trailing_whitespace].compact
51
+ result.length == 1 ? result.first : result
52
+ end
53
+ end
54
+
55
+ # Extract text content and InlineElements from mixed content array
56
+ def extract_text_and_elements(content)
57
+ return [content, []] unless content.is_a?(Array)
58
+
59
+ text_parts = []
60
+ elements = []
61
+
62
+ content.each do |item|
63
+ case item
64
+ when String
65
+ text_parts << item
66
+ when Coridoc::CoreModel::InlineElement
67
+ elements << item
68
+ when Coridoc::CoreModel::Base
69
+ # For other block types, convert to text
70
+ text_parts << extract_text_from_model(item)
71
+ else
72
+ text_parts << item.to_s
73
+ end
74
+ end
75
+
76
+ [text_parts.join, elements]
77
+ end
78
+
79
+ # Extract text from a CoreModel object via the shared
80
+ # CoreModel::InlineContent helper. Kept as a thin wrapper so
81
+ # callers in Markup can pass single elements without wrapping.
82
+ def extract_text_from_model(model)
83
+ Coridoc::CoreModel::InlineContent.text_of(model)
84
+ end
85
+
86
+ # Subclasses should override this to return the format type
87
+ def coridoc_format_type
88
+ 'text'
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'plurimath'
4
+
5
+ module Coridoc
6
+ module Html
7
+ module Converters
8
+ class Math < Base
9
+ INSTANCE = new
10
+
11
+ def to_coridoc(node, _state = {})
12
+ # Leptris#to_s returns an inspect string; serialize the node
13
+ stem = node.to_xml.tr("\n", ' ')
14
+ stem = Plurimath::Math.parse(stem, :mathml).to_asciimath
15
+
16
+ unless stem.nil?
17
+ # stem:[...] is an inline macro: backslashes must be
18
+ # escaped FIRST, then the macro-terminating brackets —
19
+ # otherwise an input backslash would swallow our escapes
20
+ # (CodeQL rb/incomplete-sanitization).
21
+ stem = stem.gsub('\\', '\\\\')
22
+ stem = stem.gsub('[', '\\[')
23
+ stem = stem.gsub(']', '\\]')
24
+ loop do
25
+ new_stem = stem.gsub(/\(\(([^)]{1,100})\)\)/, '(\\1)')
26
+ break if new_stem == stem
27
+
28
+ stem = new_stem
29
+ end
30
+ end
31
+
32
+ Coridoc::CoreModel::StemElement.new(
33
+ content: stem,
34
+ stem_type: 'mathml'
35
+ )
36
+ end
37
+ end
38
+
39
+ register :math, Math::INSTANCE
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class MediaBase < Base
7
+ def to_coridoc(node, _state = {})
8
+ src = node['src']
9
+ id = node['id']
10
+ title = extract_title(node)
11
+
12
+ Coridoc::CoreModel::Block.new(
13
+ block_semantic_type: semantic_type,
14
+ content: src,
15
+ title: title,
16
+ id: id,
17
+ element_attributes: build_attributes(node)
18
+ )
19
+ end
20
+
21
+ def extract_title(node)
22
+ track = node.at('./track') || node.at('.//source')
23
+ return '' if track.nil?
24
+
25
+ track['label'] || track['srclang'] || ''
26
+ end
27
+
28
+ private
29
+
30
+ def semantic_type
31
+ raise NotImplementedError
32
+ end
33
+
34
+ def base_attributes(node)
35
+ {
36
+ autoplay: node['autoplay'],
37
+ loop: node['loop'],
38
+ controls: node['controls']
39
+ }.compact
40
+ end
41
+
42
+ def build_attributes(node)
43
+ base_attributes(node)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class Ol < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, state = {})
10
+ id = node['id']
11
+ items = treat_children_coridoc(node, state)
12
+
13
+ marker_type = get_list_type(node, state)
14
+
15
+ Coridoc::CoreModel::ListBlock.new(
16
+ marker_type: marker_type,
17
+ items: items,
18
+ id: id,
19
+ start: node['start']&.to_i
20
+ )
21
+ end
22
+
23
+ def get_list_type(node, _state)
24
+ case node.name
25
+ when 'ol'
26
+ 'ordered'
27
+ when 'ul'
28
+ 'unordered'
29
+ when 'dir'
30
+ 'unordered'
31
+ else
32
+ 'unordered'
33
+ end
34
+ end
35
+ end
36
+
37
+ register :ol, Ol::INSTANCE
38
+ register :ul, Ol::INSTANCE
39
+ register :dir, Ol::INSTANCE
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class P < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, state = {})
10
+ id = node['id']
11
+ content = treat_children_coridoc(node, state)
12
+
13
+ content = strip_fullwidth_spaces(content)
14
+
15
+ Coridoc::CoreModel::ParagraphBlock.new(
16
+ children: content,
17
+ id: id
18
+ )
19
+ end
20
+
21
+ private
22
+
23
+ def strip_fullwidth_spaces(content)
24
+ return content unless content.is_a?(Array)
25
+
26
+ content = strip_fullwidth_per_element(content)
27
+ content = Coridoc::CoreModel::InlineContent.strip_edges(content)
28
+ reject_empty_elements(content)
29
+ end
30
+
31
+ # Strip CJK fullwidth spaces from the leading/trailing edge of
32
+ # every InlineElement's content. Returns a new array; inputs
33
+ # are not mutated.
34
+ def strip_fullwidth_per_element(content)
35
+ content.map do |item|
36
+ next item unless item.is_a?(Coridoc::CoreModel::InlineElement)
37
+ next item unless item.content.is_a?(String)
38
+
39
+ item.with_content(item.content.gsub(/\A +| +\z/, ''))
40
+ end
41
+ end
42
+
43
+ def reject_empty_elements(content)
44
+ content.reject do |item|
45
+ if item.is_a?(Coridoc::CoreModel::InlineElement)
46
+ item.content.to_s.empty? && !has_nested_content?(item)
47
+ elsif item.is_a?(String)
48
+ item.empty?
49
+ else
50
+ false
51
+ end
52
+ end
53
+ end
54
+
55
+ def has_nested_content?(item)
56
+ item.is_a?(Coridoc::CoreModel::InlineElement) &&
57
+ item.nested_elements && !item.nested_elements.empty?
58
+ end
59
+ end
60
+
61
+ register :p, P::INSTANCE
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ class PassThrough < Base
7
+ INSTANCE = new
8
+
9
+ def to_coridoc(node, _state = {})
10
+ node.is_a?(Leptris::XML::Node) ? node.to_xml(no_decl: true) : node.to_s
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Converters
6
+ # Shared logic for superscript/subscript converters.
7
+ #
8
+ # Subclasses must implement `element_class` returning the
9
+ # CoreModel class (e.g., SuperscriptElement, SubscriptElement).
10
+ module PositionalFormatting
11
+ def to_coridoc(node, state = {})
12
+ leading_whitespace, trailing_whitespace = extract_leading_trailing_whitespace(node)
13
+
14
+ content = treat_children_coridoc(node, state)
15
+
16
+ return nil if content_empty?(content)
17
+
18
+ e = element_class.new(content: content)
19
+ result = [leading_whitespace, e, trailing_whitespace].compact
20
+ result.length == 1 ? result.first : result
21
+ end
22
+
23
+ private
24
+
25
+ def content_empty?(content)
26
+ return true if content.nil?
27
+ return content.strip.empty? if content.is_a?(String)
28
+ return content.empty? if content.is_a?(Array)
29
+
30
+ false
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end