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,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ class Plugin
6
+ def self.new(&)
7
+ if self == Plugin
8
+ Class.new(Plugin, &)
9
+ else
10
+ super
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ @html_tree_hooks_pre = {}
16
+ @html_tree_hooks_post = {}
17
+ end
18
+
19
+ def name
20
+ self.class.name
21
+ end
22
+
23
+ def preprocess_html_tree; end
24
+ def postprocess_coremodel_tree; end
25
+ def postprocess_output_string; end
26
+
27
+ attr_accessor :html_tree, :coremodel_tree, :output_string
28
+
29
+ def html_tree_change_tag_name_by_css(css, new_name)
30
+ html_tree.css(css).each do |e|
31
+ e.name = new_name
32
+ end
33
+ end
34
+
35
+ def html_tree_change_properties_by_css(css, properties)
36
+ html_tree.css(css).each do |e|
37
+ properties.each do |k, v|
38
+ e[k.to_s] = v
39
+ end
40
+ end
41
+ end
42
+
43
+ def html_tree_remove_by_css(css)
44
+ html_tree.css(css).each do |e|
45
+ e.parent&.remove_child(e)
46
+ end
47
+ end
48
+
49
+ def html_tree_replace_with_children_by_css(css)
50
+ html_tree.css(css).each do |e|
51
+ e.children.each { |child| e.add_previous_sibling(child) }
52
+ e.parent&.remove_child(e)
53
+ end
54
+ end
55
+
56
+ def html_tree_process_to_coremodel(tree, state = {})
57
+ Coridoc::Html::Converters.process_coridoc(tree, state)
58
+ end
59
+
60
+ def html_tree_add_hook_pre(element, &block)
61
+ @html_tree_hooks_pre[element] = block
62
+ end
63
+
64
+ def html_tree_add_hook_pre_by_css(css, &block)
65
+ html_tree.css(css).each do |e|
66
+ html_tree_add_hook_pre(e, &block)
67
+ end
68
+ end
69
+
70
+ def html_tree_add_hook_post(element, &block)
71
+ @html_tree_hooks_post[element] = block
72
+ end
73
+
74
+ def html_tree_add_hook_post_by_css(css, &block)
75
+ html_tree.css(css).each do |e|
76
+ html_tree_add_hook_post(e, &block)
77
+ end
78
+ end
79
+
80
+ def html_tree_run_hooks(node, state, &)
81
+ hook_pre = @html_tree_hooks_pre[node]
82
+ hook_post = @html_tree_hooks_post[node]
83
+
84
+ coremodel = hook_pre.call(node, state) if hook_pre
85
+ coremodel ||= yield node, state
86
+
87
+ coremodel = hook_post.call(node, coremodel, state) if hook_post
88
+
89
+ coremodel
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,212 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ class Plugin
6
+ # This plugin enhances documents from the PLATEAU project
7
+ # to extract more data.
8
+ #
9
+ # Usage:
10
+ # coridoc convert -rcoridoc/html/plugins/plateau
11
+ # --external-images -u raise --output _out/index.adoc index.html
12
+ class Plateau < Plugin
13
+ def name
14
+ 'PLATEAU'
15
+ end
16
+
17
+ def preprocess_html_tree
18
+ # Remove side menu, so we can generate TOC ourselves
19
+ html_tree_remove_by_css('.sideMenu')
20
+
21
+ # Correct non-semantic classes into semantic HTML tags
22
+ html_tree_change_tag_name_by_css('.titledata', 'h1')
23
+ html_tree_change_tag_name_by_css('.subtitledata', 'h2')
24
+ html_tree_change_tag_name_by_css('.pitemdata', 'h3')
25
+ html_tree_change_tag_name_by_css('.sitemdata', 'h4')
26
+ html_tree_change_tag_name_by_css('td[bgcolor="#D0CECE"]', 'th')
27
+ html_tree_change_tag_name_by_css('td[bgcolor="#d0cece"]', 'th')
28
+ html_tree_change_tag_name_by_css(
29
+ '.framedata, .frame_container_box',
30
+ 'aside'
31
+ )
32
+ html_tree_change_tag_name_by_css('.frame2data', 'pre')
33
+ # Assumption that all code snippets in those documents are XML...
34
+ html_tree_change_properties_by_css(
35
+ '.frame2data',
36
+ class: 'brush:xml;'
37
+ )
38
+
39
+ # Remove some CSS ids that are not important to us
40
+ html_tree_change_properties_by_css('#__nuxt', id: nil)
41
+ html_tree_change_properties_by_css('#__layout', id: nil)
42
+ html_tree_change_properties_by_css('#app', id: nil)
43
+
44
+ # Handle lists of document 02
45
+ html_tree_replace_with_children_by_css('.list_num-wrap')
46
+
47
+ # Convert table/img caption to become a caption
48
+ html_tree.css('.imagedata').each do |e|
49
+ table = e.parent.next&.children&.first
50
+ if table&.name == 'table'
51
+ e.name = 'caption'
52
+ table.prepend_child(e)
53
+ next
54
+ end
55
+
56
+ img = e.parent.previous&.children&.first
57
+ next unless img&.name == 'img' && img['src']
58
+
59
+ title = e.text.strip
60
+ img['title'] = title
61
+ e.remove
62
+ next
63
+ end
64
+
65
+ # Add hooks for H1, H2, H3, H4
66
+ html_tree_add_hook_post_by_css(
67
+ 'h1, h2, h3',
68
+ &method(:handle_headers)
69
+ )
70
+ html_tree_add_hook_post_by_css('h4', &method(:handle_headers_h4))
71
+
72
+ # Table cells aligned to center
73
+ html_tree_change_properties_by_css(
74
+ '.tableTopCenter',
75
+ align: 'center'
76
+ )
77
+
78
+ # Handle non-semantic lists and indentation
79
+ html_tree_add_hook_pre_by_css '.text2data' do |node,|
80
+ text = html_tree_process_to_coremodel(node).strip
81
+ next '' if text.empty? || text == "\u3000"
82
+
83
+ if text.start_with?(/\d+\./)
84
+ text = text.sub(/\A\d+.\s*/, '')
85
+ ".. #{text}\n"
86
+ else
87
+ text = text.gsub(/^/, '** ')
88
+ "\n\n//-PT2D\n#{text}\n//-ENDPT2D\n\n"
89
+ end
90
+ end
91
+
92
+ (3..4).each do |i|
93
+ html_tree_add_hook_pre_by_css ".text#{i}data" do |node,|
94
+ text = html_tree_process_to_coremodel(node).strip
95
+ next '' if text.empty? || text == "\u3000"
96
+
97
+ text = text.strip.gsub(/^/, "#{'*' * i} ")
98
+ "\n\n//-PT#{i}D\n#{text}\n//-ENDPT#{i}D\n\n"
99
+ end
100
+ end
101
+
102
+ (2..3).each do |i|
103
+ html_tree_add_hook_pre_by_css ".text#{i}data_point ul" do |node,|
104
+ text = html_tree_process_to_coremodel(node.children.first.children).strip
105
+
106
+ "#{'*' * i} #{text}\n"
107
+ end
108
+ end
109
+
110
+ (1..20).each do |i|
111
+ html_tree_add_hook_pre_by_css ".numtextdata_num .list_num#{i}" do |node,|
112
+ text = html_tree_process_to_coremodel(node).strip
113
+
114
+ "[start=#{i}]\n. #{text}\n"
115
+ end
116
+ end
117
+ end
118
+
119
+ IM = /[A-Z0-9]{1,3}/
120
+
121
+ def handle_headers(node, coridoc, _state)
122
+ content = coridoc.content.map(&:content).join
123
+
124
+ if %w[toc0 toc_0].any? { |i| coridoc.id&.start_with?(i) }
125
+ # Special content
126
+ case content.strip
127
+ when 'はじめに' # Introduction
128
+ coridoc.style = 'abstract' # The older version document has ".preface"
129
+ coridoc.level_int = 1
130
+ when '改定の概要' # Revision overview
131
+ coridoc.style = 'abstract' # The older version document has ".preface"
132
+ coridoc.level_int = 1
133
+ when '参考文献' # Bibliography
134
+ coridoc.style = 'bibliography'
135
+ coridoc.level_int = 1
136
+ when '改訂履歴' # Document history
137
+ coridoc.style = 'appendix'
138
+ coridoc.level_int = 1
139
+ when '0 概要' # Overview
140
+ coridoc.style = 'abstract' # I'm not sure this is correct
141
+ coridoc.level_int = 1
142
+ when '索引' # Index
143
+ coridoc.style = 'index' # I'm not sure this is correct
144
+ coridoc.level_int = 1
145
+ else
146
+ warn "Unknown section #{content.inspect}"
147
+ end
148
+ end
149
+
150
+ if node.name == 'h1' && content.start_with?('Annex')
151
+ coridoc.style = 'appendix'
152
+ coridoc.content.first.content.sub!(/\AAnnex [A-Z]/, '')
153
+ end
154
+
155
+ # Remove numbers
156
+ coridoc.content.first.content.sub!(
157
+ /\A(#{IM}\.)*#{IM}[[:space:]]/o,
158
+ ''
159
+ )
160
+
161
+ coridoc
162
+ end
163
+
164
+ def handle_headers_h4(_node, coridoc, _state)
165
+ title = Coridoc.strip_unicode(coridoc.content.first.content)
166
+ case title
167
+ when /\A\(\d+\)(.*)/
168
+ coridoc.level_int = 4
169
+ coridoc.content.first.content = ::Regexp.last_match(1).strip
170
+ coridoc
171
+ when /\A\d+\)(.*)/
172
+ coridoc.level_int = 5
173
+ coridoc.content.first.content = ::Regexp.last_match(1).strip
174
+ coridoc
175
+ when /\A#{IM}\.#{IM}\.#{IM}\.#{IM}(.*)/o
176
+ coridoc.level_int = 4
177
+ coridoc.content.first.content = ::Regexp.last_match(1).strip
178
+ else
179
+ if title.empty?
180
+ # Strip instances of faulty empty paragraphs
181
+ nil
182
+ else
183
+ ["// Unhandled h4 content\n", coridoc]
184
+ end
185
+ end
186
+ end
187
+
188
+ def postprocess_output_string
189
+ str = output_string
190
+
191
+ ### Custom indentation handling
192
+ # If there's a step up, add [none]
193
+ str = str.gsub(%r{\s+//-ENDPT2D\s+//-PT3D\s+}, "\n[none]\n")
194
+ str = str.gsub(%r{\s+//-ENDPT2D\s+//-PT4D\s+}, "\n[none]\n")
195
+ str = str.gsub(%r{\s+//-ENDPT3D\s+//-PT4D\s+}, "\n[none]\n")
196
+ # Collapse blocks of text[2,3]data
197
+ str = str.gsub(%r{\s+//-ENDPT[234]D\s+//-PT[234]D\s+}, "\n\n")
198
+ # In the beginning, add [none]
199
+ str = str.gsub(%r{\s+//-PT[234]D\s+}, "\n\n[none]\n")
200
+ # If following with another list, ensure we readd styling
201
+ str = str.gsub(%r{\s+//-ENDPT[234]D\s+\*}, "\n\n[disc]\n*")
202
+ # Otherwise, clean up
203
+ str = str.gsub(%r{\s+//-ENDPT[234]D\s+}, "\n\n")
204
+
205
+ self.output_string = str
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
211
+
212
+ Coridoc::Html.input_config.plugins << Coridoc::Html::Plugin::Plateau
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ class Postprocessor
6
+ def self.process(coridoc)
7
+ new(coridoc).process
8
+ end
9
+
10
+ def initialize(coridoc)
11
+ @tree = coridoc
12
+ end
13
+
14
+ def process
15
+ @tree
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module ReferenceMaterializers
6
+ # Render a hyperlink edge as a LinkElement with the URL as target.
7
+ # The catalog is not consulted — links are external by definition.
8
+ class Link < Coridoc::Reference::Materializer::Base
9
+ class << self
10
+ def kind
11
+ :link
12
+ end
13
+
14
+ def presentation
15
+ :any
16
+ end
17
+
18
+ def format
19
+ :html
20
+ end
21
+ end
22
+
23
+ def materialize(edge:, **)
24
+ text = display_text(edge)
25
+ Coridoc::CoreModel::LinkElement.new(
26
+ target: edge.address.to_s,
27
+ content: text,
28
+ children: [
29
+ Coridoc::CoreModel::TextElement.new(content: text)
30
+ ]
31
+ )
32
+ end
33
+
34
+ private
35
+
36
+ def display_text(edge)
37
+ return edge.label if edge.label && !edge.label.empty?
38
+
39
+ edge.address.to_s
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module ReferenceMaterializers
6
+ # Render a navigation edge (:navigation kind) as an HTML-shaped
7
+ # CoreModel::LinkElement. The href is computed by asking the
8
+ # Presentation where the target lives (so the same edge renders
9
+ # to different hrefs under different presentations). Edges whose
10
+ # target lives on another page get a page-prefixed href.
11
+ class Navigation < Coridoc::Reference::Materializer::Base
12
+ class << self
13
+ def kind
14
+ :navigation
15
+ end
16
+
17
+ def presentation
18
+ :any
19
+ end
20
+
21
+ def format
22
+ :html
23
+ end
24
+ end
25
+
26
+ def materialize(edge:, result:, node:, presentation:, pages:)
27
+ href = href_for(edge, result, node, presentation, pages)
28
+ text = display_text(edge, result)
29
+ Coridoc::CoreModel::LinkElement.new(
30
+ target: href,
31
+ content: text,
32
+ children: [
33
+ Coridoc::CoreModel::TextElement.new(content: text)
34
+ ]
35
+ )
36
+ end
37
+
38
+ private
39
+
40
+ def href_for(edge, result, node, presentation, pages)
41
+ return edge.address.to_s unless result.is_a?(Coridoc::Reference::Result::Resolved)
42
+ return edge.address.to_s unless presentation && pages
43
+
44
+ target_page = presentation.locate_page(edge, result.target, pages: pages)
45
+ return edge.address.to_s unless target_page
46
+
47
+ source_page = node && presentation.locate_page(edge, node, pages: pages)
48
+ build_href(target_page, source_page, result.target)
49
+ end
50
+
51
+ def build_href(target_page, source_page, target)
52
+ anchor = anchor_for(target)
53
+ cross_page = source_page && !source_page.equal?(target_page)
54
+ prefix = cross_page ? page_path(target_page) : ''
55
+ return "#{prefix}##{anchor}" if anchor
56
+
57
+ page_path(target_page)
58
+ end
59
+
60
+ def page_path(page)
61
+ page.id || '#'
62
+ end
63
+
64
+ def anchor_for(target)
65
+ target.id if target.is_a?(Coridoc::CoreModel::Base) && target.id
66
+ end
67
+
68
+ def display_text(edge, result)
69
+ return edge.label if edge.label && !edge.label.empty?
70
+ return result.target.title if result.is_a?(Coridoc::Reference::Result::Resolved) && result.target&.title
71
+
72
+ edge.address.to_s
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ # Materializers rendering resolved reference Edges into
6
+ # HTML-shaped CoreModel nodes. They live in the format gem (not
7
+ # coridoc core) so format knowledge stays at the spoke of the
8
+ # hub-and-spoke architecture; registration is a load-time side
9
+ # effect via the Materializer registry's global extension point.
10
+ module ReferenceMaterializers
11
+ autoload :Navigation, 'coridoc/html/reference_materializers/navigation'
12
+ autoload :Link, 'coridoc/html/reference_materializers/link'
13
+ end
14
+ end
15
+ end
16
+
17
+ Coridoc::Reference::Materializer::Registry.register_global(
18
+ Coridoc::Html::ReferenceMaterializers::Navigation
19
+ )
20
+ Coridoc::Reference::Materializer::Registry.register_global(
21
+ Coridoc::Html::ReferenceMaterializers::Link
22
+ )
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ class RenderOptions
6
+ attr_reader :layout, :lang, :toc, :toc_levels, :section_numbers,
7
+ :section_number_levels, :author, :description, :custom_css,
8
+ :dist_dir, :theme_toggle, :reading_progress, :toc_placement
9
+
10
+ def initialize(layout: :static, lang: 'en', toc: false, toc_levels: 2,
11
+ section_numbers: false, section_number_levels: 3,
12
+ author: nil, description: nil, custom_css: nil,
13
+ dist_dir: nil, theme_toggle: true, reading_progress: true,
14
+ toc_placement: :auto)
15
+ @layout = layout
16
+ @lang = lang
17
+ @toc = toc
18
+ @toc_levels = toc_levels
19
+ @section_numbers = section_numbers
20
+ @section_number_levels = section_number_levels
21
+ @author = author
22
+ @description = description
23
+ @custom_css = custom_css
24
+ @dist_dir = dist_dir
25
+ @theme_toggle = theme_toggle
26
+ @reading_progress = reading_progress
27
+ @toc_placement = toc_placement
28
+ freeze
29
+ end
30
+
31
+ def spa?
32
+ @layout == :spa
33
+ end
34
+
35
+ def static?
36
+ @layout == :static
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'liquid'
4
+ require 'leptris'
5
+
6
+ module Coridoc
7
+ module Html
8
+ class Renderer
9
+ DEFAULT_TEMPLATE_DIR = TemplateLocator::DEFAULT_TEMPLATE_DIR
10
+
11
+ include TemplateCaching
12
+
13
+ attr_reader :template_dirs, :options
14
+
15
+ def initialize(template_dirs: nil, **options)
16
+ @template_dirs = normalize_dirs(template_dirs)
17
+ @options = options
18
+ @template_cache = {}
19
+ @locator = TemplateLocator.new(
20
+ user_dirs: @template_dirs,
21
+ default_dir: DEFAULT_TEMPLATE_DIR
22
+ )
23
+ @section_numbers = {}
24
+ @layout_renderer = LayoutRenderer.new
25
+ end
26
+
27
+ def render(element)
28
+ result = Drop::DropFactory.create(element)
29
+ case result
30
+ when Drop::Base then render_drop(result)
31
+ when Array then result.map { |r| r.is_a?(Drop::Base) ? render_drop(r) : r }.join("\n")
32
+ when nil then ''
33
+ else result
34
+ end
35
+ end
36
+
37
+ def render_drop(drop)
38
+ return '' if drop.nil?
39
+ return drop.to_s unless drop.is_a?(Drop::Base)
40
+
41
+ annotate_section_number(drop)
42
+
43
+ template_type = drop.template_type
44
+ template = find_and_load_template(template_type)
45
+ return render_fallback_drop(drop) unless template
46
+
47
+ assigns = { 'element' => drop }
48
+ template.render(assigns, registers: { renderer: self, section_numbers: @section_numbers }).strip
49
+ end
50
+
51
+ def render_html5(document, **)
52
+ opts = RenderOptions.new(**)
53
+ builder = TocBuilder.from_options(opts)
54
+ @toc, @section_numbers = if document.is_a?(CoreModel::StructuralElement)
55
+ builder.build_with_numbers(document)
56
+ else
57
+ [nil, {}]
58
+ end
59
+
60
+ body_html = render(document)
61
+
62
+ if opts.spa?
63
+ render_spa_layout(document, body_html, opts)
64
+ else
65
+ render_static_layout(document, body_html, opts)
66
+ end
67
+ end
68
+
69
+ def available_templates
70
+ @locator.available_templates
71
+ end
72
+
73
+ def template_exists?(type_name)
74
+ @locator.exists?(type_name)
75
+ end
76
+
77
+ def find_template(type_name)
78
+ find_and_load_template(type_name)
79
+ end
80
+
81
+ private
82
+
83
+ def render_static_layout(document, body_html, opts)
84
+ if opts.toc && @toc
85
+ toc_html = render(@toc)
86
+ body_html = "#{toc_html}\n#{body_html}" unless toc_html.empty?
87
+ end
88
+ @layout_renderer.render_static(document, body_html, opts)
89
+ end
90
+
91
+ def render_spa_layout(document, body_html, opts)
92
+ numbered = opts.section_numbers == true
93
+ toc_data = if @toc
94
+ { entries: TocSerializer.new.serialize_entries(@toc.entries), numbered: numbered }
95
+ else
96
+ { entries: [], numbered: false }
97
+ end
98
+ @layout_renderer.render_spa(document, opts, body_html, toc_data)
99
+ end
100
+
101
+ def find_and_load_template(type_name)
102
+ cache_key = type_name.to_s
103
+ path = @locator.find(type_name)
104
+ load_template(cache: @template_cache, cache_key: cache_key, path: path)
105
+ end
106
+
107
+ def annotate_section_number(drop)
108
+ return unless drop.is_a?(SectionNumberable)
109
+ return if @section_numbers.empty?
110
+
111
+ id = drop.id
112
+ number = @section_numbers[id]
113
+ drop.section_number = number if number
114
+ end
115
+
116
+ def render_fallback_drop(drop)
117
+ type = drop.template_type
118
+ resolved = TitleText.resolve(drop.model)
119
+ text = resolved ? Escape.escape_html(resolved) : ''
120
+
121
+ Builder.new do |doc|
122
+ doc.div(class: "element element-#{type}") { doc.text text }
123
+ end.to_html
124
+ end
125
+
126
+ def normalize_dirs(dirs)
127
+ return [] if dirs.nil?
128
+
129
+ Array(dirs).map do |dir|
130
+ path = Pathname.new(dir)
131
+ path.absolute? ? path.to_s : File.expand_path(dir)
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Html
5
+ module SectionNumberable
6
+ attr_accessor :section_number
7
+ end
8
+ end
9
+ end