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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a87a96fc100eb252c590a32da8be1ee014edd991c6274ddbc2ae2b531da23b26
4
+ data.tar.gz: 6ed1df5ce0ce3baa2dff6cf41204d04b6114c23aa4bc8d0bc188c12d11032a65
5
+ SHA512:
6
+ metadata.gz: b97d848d8df24e31e61af94a8a93590052d7e8c2916157ca2263dbfc3d941733279c8fefabf82125c9f6a44a362877d8d482ec9bedcdfd71608fb84b89a008f4
7
+ data.tar.gz: d478ecb93c71a28431b06a0a691e638262365ad2965cc17bfeb85224d358708f0cd7807b2dc3c82473fa0b7117d0904ad7459b106e2db577da73ecb9c78ee827
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Abu Nashir
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module AssetResolver
6
+ class << self
7
+ def stylesheet_path(options = {})
8
+ css_theme = options[:css_theme] || Config::DEFAULT_OPTIONS[:css_theme]
9
+ stylesheet = "#{css_theme}.css"
10
+ stylesdir = options[:stylesdir] || Config::DEFAULT_OPTIONS[:stylesdir]
11
+
12
+ if stylesdir && stylesdir != '.'
13
+ File.join(stylesdir, stylesheet)
14
+ else
15
+ stylesheet
16
+ end
17
+ end
18
+
19
+ def embedded_stylesheet(options = {})
20
+ css_theme = options[:css_theme] || Config::DEFAULT_OPTIONS[:css_theme]
21
+ stylesheet_name = "#{css_theme}.css"
22
+
23
+ themes_path = File.join(__dir__, 'assets', 'themes', stylesheet_name)
24
+ asset_path = if File.exist?(themes_path)
25
+ themes_path
26
+ else
27
+ File.join(__dir__, 'assets', stylesheet_name)
28
+ end
29
+
30
+ css_content = if File.exist?(asset_path)
31
+ File.read(asset_path)
32
+ else
33
+ default_path = File.join(__dir__, 'assets', 'coridoc.css')
34
+ File.exist?(default_path) ? File.read(default_path) : ''
35
+ end
36
+
37
+ resolve_css_imports(css_content, File.dirname(asset_path))
38
+ end
39
+
40
+ def resolve_css_imports(css_content, base_dir)
41
+ css_content.gsub(/@import\s+(?:url\()?['"]([^'"]+)['"]\)?;?/) do
42
+ import_path = ::Regexp.last_match(1)
43
+ full_path = File.join(base_dir, import_path)
44
+
45
+ if File.exist?(full_path)
46
+ imported_content = File.read(full_path)
47
+ resolve_css_imports(imported_content, File.dirname(full_path))
48
+ else
49
+ ::Regexp.last_match(0)
50
+ end
51
+ end
52
+ end
53
+
54
+ def css_link_tag(options = {})
55
+ href = stylesheet_path(options)
56
+ Builder.new { |doc| doc.link(rel: 'stylesheet', href: href) }.to_html
57
+ end
58
+
59
+ def css_style_tag(options = {})
60
+ css_content = embedded_stylesheet(options)
61
+ custom_css = options[:custom_css]
62
+
63
+ content = css_content
64
+ content += "\n\n#{custom_css}" if custom_css && !custom_css.empty?
65
+
66
+ build_text_element('style', content)
67
+ end
68
+
69
+ def custom_css_tag(custom_css)
70
+ return '' unless custom_css && !custom_css.empty?
71
+
72
+ build_text_element('style', custom_css)
73
+ end
74
+
75
+ def embed_css?(options = {})
76
+ !options.fetch(:linkcss, Config::DEFAULT_OPTIONS[:linkcss]) ||
77
+ options.fetch(:embedded, Config::DEFAULT_OPTIONS[:embedded])
78
+ end
79
+
80
+ def css_tags(options = {})
81
+ tags = []
82
+
83
+ if embed_css?(options)
84
+ tags << css_style_tag(options)
85
+ else
86
+ tags << css_link_tag(options)
87
+ tags << custom_css_tag(options[:custom_css]) if options[:custom_css]
88
+ end
89
+
90
+ tags.join("\n")
91
+ end
92
+
93
+ def embed_js?(options = {})
94
+ !options.fetch(:linkjs, Config::DEFAULT_OPTIONS[:linkjs]) ||
95
+ options.fetch(:embedded, Config::DEFAULT_OPTIONS[:embedded]) ||
96
+ !options.fetch(:linkcss, Config::DEFAULT_OPTIONS[:linkcss])
97
+ end
98
+
99
+ def javascript_path(options = {})
100
+ javascript = options[:javascript] || Config::DEFAULT_OPTIONS[:javascript]
101
+ jsdir = options[:jsdir] || Config::DEFAULT_OPTIONS[:jsdir]
102
+
103
+ if jsdir && jsdir != '.'
104
+ File.join(jsdir, javascript)
105
+ else
106
+ javascript
107
+ end
108
+ end
109
+
110
+ def embedded_javascript(options = {})
111
+ javascript_name = options[:javascript] || Config::DEFAULT_OPTIONS[:javascript]
112
+ asset_path = File.join(__dir__, 'assets', 'js', javascript_name)
113
+
114
+ File.exist?(asset_path) ? File.read(asset_path) : ''
115
+ end
116
+
117
+ def js_link_tag(options = {})
118
+ src = javascript_path(options)
119
+ Builder.new { |doc| doc.script(src: src, defer: '') }.to_html
120
+ end
121
+
122
+ def js_script_tag(options = {})
123
+ js_content = embedded_javascript(options)
124
+ return '' if js_content.empty?
125
+
126
+ build_text_element('script', js_content)
127
+ end
128
+
129
+ def js_tags(options = {})
130
+ return '' if options[:javascript] == false
131
+
132
+ tags = []
133
+
134
+ tags << if embed_js?(options)
135
+ js_script_tag(options)
136
+ else
137
+ js_link_tag(options)
138
+ end
139
+
140
+ tags.join("\n")
141
+ end
142
+
143
+ def syntax_highlighter_tags(options = {})
144
+ highlighter = options[:source_highlighter]
145
+ return '' unless highlighter
146
+
147
+ case highlighter.to_sym
148
+ when :highlightjs, :highlight_js, :'highlight.js'
149
+ highlightjs_tags(options)
150
+ else
151
+ ''
152
+ end
153
+ end
154
+
155
+ def highlightjs_tags(options = {})
156
+ theme = options[:highlightjs_theme] || Config::DEFAULT_OPTIONS[:highlightjs_theme]
157
+
158
+ Builder.new do |doc|
159
+ doc.link(rel: 'stylesheet',
160
+ href: "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/#{theme}.min.css")
161
+ doc.script(src: 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js')
162
+ doc.script('hljs.highlightAll();')
163
+ end.to_html
164
+ end
165
+
166
+ private
167
+
168
+ def build_text_element(tag_name, content)
169
+ Builder.new { |doc| doc.send(tag_name, content) }.to_html
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ # Builds HTML trees over the Leptris DOM using a
6
+ # Nokogiri::HTML::Builder-style DSL. Single source of truth for
7
+ # programmatic HTML construction in this gem (layout fallbacks,
8
+ # asset tag emitters) — no raw HTML string concatenation anywhere.
9
+ #
10
+ # Builder.new do |doc|
11
+ # doc.html(lang: 'en') do
12
+ # doc.head { doc.meta(charset: 'UTF-8') }
13
+ # doc.body { doc << body_html }
14
+ # end
15
+ # end.to_html
16
+ #
17
+ # Serialization uses leptris-ruby's HTML mode (#309): script/style
18
+ # text is emitted unescaped and void elements are unclosed.
19
+ class Builder
20
+ # Methods that are builder API, not tag names.
21
+ RESERVED = %i[text to_html to_s document parent].freeze
22
+
23
+ attr_reader :document
24
+
25
+ def initialize(document = Leptris::XML::Document.create, &block)
26
+ @document = document
27
+ @roots = []
28
+ @parents = []
29
+ instance_eval(&block) if block
30
+ end
31
+
32
+ # Tag DSL. A positional String argument becomes the element's text
33
+ # content; the block receives the builder with the new element as
34
+ # insertion parent; keyword arguments become attributes.
35
+ def method_missing(name, *args, **attrs, &block)
36
+ element = @document.create_element(name.to_s)
37
+ attrs.each { |key, value| element[key.to_s] = value.to_s }
38
+
39
+ args.each do |arg|
40
+ next unless arg.is_a?(String)
41
+
42
+ element.add_child(@document.create_text_node(arg))
43
+ end
44
+
45
+ if block
46
+ @parents.push(element)
47
+ begin
48
+ yield self
49
+ ensure
50
+ @parents.pop
51
+ end
52
+ end
53
+
54
+ if current
55
+ current.add_child(element)
56
+ else
57
+ @roots << element
58
+ end
59
+ element
60
+ end
61
+
62
+ def respond_to_missing?(name, include_private = false)
63
+ !RESERVED.include?(name) || super
64
+ end
65
+
66
+ # Raw markup insertion into the current element (parsed, never
67
+ # concatenated as text).
68
+ def <<(markup)
69
+ current&.add_child(markup)
70
+ self
71
+ end
72
+
73
+ # Text node insertion into the current element.
74
+ def text(content)
75
+ current&.add_child(@document.create_text_node(content.to_s))
76
+ self
77
+ end
78
+
79
+ # Serializes with HTML semantics: script/style text unescaped,
80
+ # void elements unclosed. Multiple top-level elements are joined
81
+ # with newlines.
82
+ def to_html
83
+ @roots.map(&:to_html).join("\n")
84
+ end
85
+ alias to_s to_html
86
+
87
+ private
88
+
89
+ def current
90
+ @parents.last
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,236 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ class Cleaner
6
+ INNER_WHITESPACE_REGEX_1 = /\n stem:\[/
7
+ NEWLINES_REGEX = /\n{3,}/
8
+ LEADING_NEWLINE_REGEX = /\A\n+/
9
+ WHITESPACE_REGEX = /[ \t\r\n]+/
10
+ TRAILING_WHITESPACE_REGEX = /[ \t\r\n]+\z/
11
+
12
+ def tidy(string)
13
+ return string.transform_values { |i| tidy(i) } if string.is_a? Hash
14
+
15
+ result = HtmlConverter.track_time 'Removing inner whitespace' do
16
+ remove_inner_whitespaces(String.new(string))
17
+ end
18
+ result = HtmlConverter.track_time 'Removing newlines' do
19
+ remove_newlines(result)
20
+ end
21
+ result = HtmlConverter.track_time 'Removing leading newlines' do
22
+ remove_leading_newlines(result)
23
+ end
24
+ result = HtmlConverter.track_time 'Cleaning tag borders' do
25
+ clean_tag_borders(result)
26
+ end
27
+ result = HtmlConverter.track_time 'Cleaning punctuation characters' do
28
+ clean_punctuation_characters(result)
29
+ end
30
+ result = remove_block_leading_newlines(result)
31
+ result = remove_section_attribute_newlines(result)
32
+ end
33
+
34
+ def remove_block_leading_newlines(string)
35
+ string.gsub("]\n****\n\n", "]\n****\n")
36
+ end
37
+
38
+ def remove_section_attribute_newlines(string)
39
+ string.gsub("]\n\n==", "]\n==")
40
+ end
41
+
42
+ def remove_newlines(string)
43
+ string.gsub(NEWLINES_REGEX, "\n\n")
44
+ end
45
+
46
+ def remove_leading_newlines(string)
47
+ string.gsub(LEADING_NEWLINE_REGEX, '')
48
+ end
49
+
50
+ # Drop the newline joining a stem macro's closing bracket to the
51
+ # following text. A linear character scan instead of a regex —
52
+ # CodeQL rb/polynomial-redos flags even nested-star and
53
+ # possessive-quantifier forms, so no quantifiers at all.
54
+ def remove_stem_macro_newlines(string)
55
+ pos = 0
56
+ while (start = string.index('stem:[', pos))
57
+ close = closing_bracket_after(string, start + 6)
58
+ break if close.nil?
59
+
60
+ next unless string[close + 1] == "\n" && string[close + 2]&.match?(/\S/)
61
+
62
+ string.slice! close + 1, 1
63
+ pos = close + 1
64
+ end
65
+ string
66
+ end
67
+
68
+ # Index of the first unescaped ']' at or after +from+.
69
+ def closing_bracket_after(string, from)
70
+ i = from
71
+ while i < string.length
72
+ char = string[i]
73
+ return i if char == ']'
74
+
75
+ i += char == '\\' ? 2 : 1
76
+ end
77
+ nil
78
+ end
79
+
80
+ def remove_inner_whitespaces(string)
81
+ unless string.nil?
82
+ string.gsub!("\n stem:[", "\nstem:[")
83
+ string.gsub!(INNER_WHITESPACE_REGEX_1, '\\1 ')
84
+ remove_stem_macro_newlines(string)
85
+ end
86
+ result = +''
87
+ string.each_line do |line|
88
+ result << preserve_border_whitespaces(line) do
89
+ line.strip.gsub(/[ \t]{2,}/, ' ')
90
+ end
91
+ end
92
+ result
93
+ end
94
+
95
+ def clean_tag_borders(string)
96
+ result = string.gsub(/\s?~{2,}.*?~{2,}\s?/) do |match|
97
+ preserve_border_whitespaces(
98
+ match,
99
+ default_border: Html.input_config.tag_border
100
+ ) do
101
+ match.strip.sub('~~ ', '~~').sub(' ~~', '~~')
102
+ end
103
+ end
104
+
105
+ clean_bracket_borders(result)
106
+ end
107
+
108
+ # Tidy brackets plus at most one adjacent whitespace per side.
109
+ # Linear scan — /\s?\[.*?\]\s?/ is modeled as polynomial-redos
110
+ # by CodeQL. Lazy .? semantics: the segment ends at the first
111
+ # ']' on the same line.
112
+ def clean_bracket_borders(result)
113
+ out = +''
114
+ i = 0
115
+ while i < result.length
116
+ start = result.index('[', i)
117
+ if start.nil?
118
+ out << result[i..]
119
+ break
120
+ end
121
+
122
+ j = start + 1
123
+ j += 1 while j < result.length && result[j] != ']' && result[j] != "\n"
124
+ unless j < result.length && result[j] == ']'
125
+ out << result[i..start]
126
+ i = start + 1
127
+ next
128
+ end
129
+
130
+ lead = start.positive? && /\s/.match?(result[start - 1]) ? start - 1 : start
131
+ trail = j + 1 < result.length && /\s/.match?(result[j + 1]) ? j + 2 : j + 1
132
+ out << result[i...lead]
133
+ match = result[lead...trail]
134
+ out << preserve_border_whitespaces(match) do
135
+ match.strip.sub('[ ', '[').sub(' ]', ']')
136
+ end
137
+ i = trail
138
+ end
139
+ out
140
+ end
141
+
142
+ def clean_punctuation_characters(string)
143
+ string.gsub(/(\*\*|~~|__)\s([.!?'"])/, '\\1\\2')
144
+ end
145
+
146
+ def preprocess_word_html(string)
147
+ clean_headings(scrub_whitespace(string.dup))
148
+ end
149
+
150
+ def scrub_whitespace(string)
151
+ string.gsub!(/&nbsp;|&#xA0;| /i, '&#xA0;')
152
+ string = Coridoc.strip_unicode(string)
153
+ # Trim the trailing space run without an end-anchored
154
+ # quantified pattern (CodeQL rb/polynomial-redos).
155
+ string[0...-1] while string.end_with?(' ')
156
+ string.gsub!("\n\n\n\n", "\n\n")
157
+ string
158
+ end
159
+
160
+ # Empty <hN ...></hN> headings collapse to a space; headings
161
+ # carrying the vertical-align: super style become <sup>body</sup>.
162
+ # Single linear pass — the gsub pair used a backreference and
163
+ # nested quantifiers that CodeQL models as polynomial-redos.
164
+ def clean_headings(string)
165
+ out = +''
166
+ i = 0
167
+ while (h = string.index('<h', i))
168
+ digit = string[h + 2]
169
+ unless digit&.match?(/[1-9]/)
170
+ out << string[i..h + 1]
171
+ i = h + 3
172
+ next
173
+ end
174
+
175
+ gt = string.index('>', h + 3)
176
+ if gt.nil?
177
+ out << string[i..]
178
+ break
179
+ end
180
+
181
+ close_tag = "</h#{digit}>"
182
+ body_start = gt + 1
183
+ body_end = string.index(close_tag, body_start)
184
+ body = body_end&.then { |end_idx| string[body_start...end_idx] }
185
+ if body_end == body_start
186
+ out << string[i...h] << ' '
187
+ i = body_start + close_tag.length
188
+ elsif !body.nil? && !body.empty? && !body.include?("\n") &&
189
+ string[h...gt].include?(' style="vertical-align: super;')
190
+ out << string[i...h] << '<sup>' << body << '</sup>'
191
+ i = body_end + close_tag.length
192
+ else
193
+ out << string[i..h + 2]
194
+ i = h + 3
195
+ end
196
+ end
197
+ out << string[i..] if i < string.length
198
+ out
199
+ end
200
+
201
+ private
202
+
203
+ def preserve_border_whitespaces(string, options = {})
204
+ return string if /\A\s*\Z/.match?(string)
205
+
206
+ default_border = options.fetch(:default_border, '')
207
+ default_border = '' if /[\[(\])]/.match?(string)
208
+ string_start = present_or_default(leading_whitespace(string), default_border)
209
+ string_end = present_or_default(trailing_whitespace(string), default_border)
210
+ result = yield
211
+ string_start + result + string_end
212
+ end
213
+
214
+ # Leading/trailing whitespace runs via a linear scan — the
215
+ # /\s*\Z/ and /\A\s*/ forms are modeled as polynomial-redos
216
+ # risks by CodeQL.
217
+ def leading_whitespace(string)
218
+ i = 0
219
+ i += 1 while i < string.length && string[i].match?(/\s/)
220
+ string[0, i]
221
+ end
222
+
223
+ def trailing_whitespace(string)
224
+ i = string.length
225
+ i -= 1 while i.positive? && string[i - 1].match?(/\s/)
226
+ string[i..]
227
+ end
228
+
229
+ def present_or_default(string, default)
230
+ return default if string.nil? || string.empty?
231
+
232
+ string
233
+ end
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module Config
6
+ DEFAULT_LANG = 'en'
7
+ DEFAULT_TITLE = 'Untitled'
8
+
9
+ DEFAULT_OPTIONS = {
10
+ theme: :classic,
11
+ modern: {
12
+ color_scheme: :glass,
13
+ primary_color: '#6366f1',
14
+ accent_color: '#8b5cf6',
15
+ max_width: '1200px',
16
+ content_width: '65ch',
17
+ sidebar_width: '280px',
18
+ theme_toggle: true,
19
+ reading_progress: true,
20
+ back_to_top: true,
21
+ toc_sticky: true,
22
+ copy_code_buttons: true,
23
+ enable_animations: true,
24
+ animation_duration: '300ms',
25
+ lazy_load_images: true
26
+ }.freeze,
27
+ html_version: :html5,
28
+ pretty_print: false,
29
+ indent: ' ',
30
+ line_wrap: 0,
31
+ escape_content: true,
32
+ preserve_whitespace: false,
33
+ convert_line_breaks: true,
34
+ preserve_comments: false,
35
+ use_semantic_elements: true,
36
+ add_css_classes: true,
37
+ add_data_attributes: false,
38
+ external_link_target: nil,
39
+ link_rel: nil,
40
+ image_loading: nil,
41
+ image_decoding: nil,
42
+ syntax_highlighter: nil,
43
+ syntax_highlighter_opts: {},
44
+ table_border: false,
45
+ table_stripes: false,
46
+ preserve_custom_attributes: true,
47
+ attribute_prefix: 'data-',
48
+ stylesheet: 'coridoc.css',
49
+ stylesdir: './css',
50
+ linkcss: false,
51
+ copycss: true,
52
+ css_theme: 'professional',
53
+ custom_css: nil,
54
+ javascript: 'coridoc.js',
55
+ jsdir: './js',
56
+ linkjs: false,
57
+ theme_toggle: true,
58
+ toc_interactive: nil,
59
+ author: nil,
60
+ description: nil,
61
+ keywords: nil,
62
+ lang: 'en',
63
+ embedded: false,
64
+ meta_tags: {},
65
+ toc: false,
66
+ toclevels: 2,
67
+ toc_title: 'Table of Contents',
68
+ toc_placement: :auto,
69
+ sectnums: false,
70
+ sectnumlevels: 3,
71
+ source_highlighter: nil,
72
+ highlightjs_theme: 'github',
73
+ pygments_style: 'default',
74
+ rouge_style: 'github'
75
+ }.freeze
76
+
77
+ class << self
78
+ def default_options
79
+ DEFAULT_OPTIONS.dup
80
+ end
81
+
82
+ def merge_options(user_options = {})
83
+ default_options.merge(user_options)
84
+ end
85
+
86
+ def validate_options(options)
87
+ valid_keys = DEFAULT_OPTIONS.keys
88
+ invalid_keys = options.keys - valid_keys
89
+
90
+ raise ArgumentError, "Invalid options: #{invalid_keys.join(', ')}" unless invalid_keys.empty?
91
+
92
+ options
93
+ end
94
+
95
+ def css_class_for(element_type, role = nil)
96
+ classes = [element_type.to_s.tr('_', '-')]
97
+ classes << role if role
98
+ classes.join(' ')
99
+ end
100
+
101
+ def data_attribute_name(name, prefix: 'data-')
102
+ "#{prefix}#{name.to_s.tr('_', '-')}"
103
+ end
104
+
105
+ def element_config(element_type, options = {})
106
+ {
107
+ tag: html_tag_for(element_type),
108
+ css_class: css_class_for(element_type, options[:role]),
109
+ attributes: options[:attributes] || {}
110
+ }
111
+ end
112
+
113
+ def html_tag_for(element_type)
114
+ TagMapping.tag_for(element_type)
115
+ end
116
+
117
+ def code_block_attributes(language, options = {})
118
+ attrs = {}
119
+ attrs[:class] = "language-#{language}" if language && !language.empty?
120
+
121
+ if options[:linenums] || options[:line_numbers]
122
+ attrs[:class] =
123
+ [attrs[:class], 'line-numbers'].compact.join(' ')
124
+ end
125
+
126
+ attrs
127
+ end
128
+
129
+ def theme_toggle?(options = {})
130
+ options.fetch(:theme_toggle, DEFAULT_OPTIONS[:theme_toggle])
131
+ end
132
+
133
+ def toc_interactive?(options = {})
134
+ toc_enabled = options.fetch(:toc, DEFAULT_OPTIONS[:toc])
135
+ toc_interactive = options[:toc_interactive]
136
+ toc_interactive.nil? ? toc_enabled : toc_interactive
137
+ end
138
+ end
139
+
140
+ ASSET_METHODS = %i[
141
+ stylesheet_path embedded_stylesheet resolve_css_imports
142
+ css_link_tag css_style_tag custom_css_tag embed_css? css_tags
143
+ javascript_path embedded_javascript
144
+ js_link_tag js_script_tag embed_js? js_tags
145
+ syntax_highlighter_tags highlightjs_tags
146
+ ].freeze
147
+
148
+ ASSET_METHODS.each do |method|
149
+ define_method(method) { |*args, **kwargs, &blk| AssetResolver.public_send(method, *args, **kwargs, &blk) }
150
+ end
151
+ module_function(*ASSET_METHODS)
152
+ end
153
+ end
154
+ end