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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/lib/coridoc/html/asset_resolver.rb +174 -0
- data/lib/coridoc/html/builder.rb +94 -0
- data/lib/coridoc/html/cleaner.rb +236 -0
- data/lib/coridoc/html/config.rb +154 -0
- data/lib/coridoc/html/converter_base.rb +174 -0
- data/lib/coridoc/html/converters/a.rb +77 -0
- data/lib/coridoc/html/converters/aside.rb +20 -0
- data/lib/coridoc/html/converters/audio.rb +19 -0
- data/lib/coridoc/html/converters/base.rb +100 -0
- data/lib/coridoc/html/converters/blockquote.rb +25 -0
- data/lib/coridoc/html/converters/br.rb +17 -0
- data/lib/coridoc/html/converters/bypass.rb +82 -0
- data/lib/coridoc/html/converters/code.rb +25 -0
- data/lib/coridoc/html/converters/div.rb +23 -0
- data/lib/coridoc/html/converters/dl.rb +82 -0
- data/lib/coridoc/html/converters/drop.rb +26 -0
- data/lib/coridoc/html/converters/em.rb +23 -0
- data/lib/coridoc/html/converters/figure.rb +33 -0
- data/lib/coridoc/html/converters/h.rb +58 -0
- data/lib/coridoc/html/converters/head.rb +29 -0
- data/lib/coridoc/html/converters/hr.rb +17 -0
- data/lib/coridoc/html/converters/img.rb +105 -0
- data/lib/coridoc/html/converters/li.rb +39 -0
- data/lib/coridoc/html/converters/mark.rb +21 -0
- data/lib/coridoc/html/converters/markup.rb +93 -0
- data/lib/coridoc/html/converters/math.rb +42 -0
- data/lib/coridoc/html/converters/media_base.rb +48 -0
- data/lib/coridoc/html/converters/ol.rb +42 -0
- data/lib/coridoc/html/converters/p.rb +64 -0
- data/lib/coridoc/html/converters/pass_through.rb +15 -0
- data/lib/coridoc/html/converters/positional_formatting.rb +35 -0
- data/lib/coridoc/html/converters/pre.rb +60 -0
- data/lib/coridoc/html/converters/q.rb +25 -0
- data/lib/coridoc/html/converters/strong.rb +22 -0
- data/lib/coridoc/html/converters/sub.rb +20 -0
- data/lib/coridoc/html/converters/sup.rb +20 -0
- data/lib/coridoc/html/converters/table.rb +64 -0
- data/lib/coridoc/html/converters/td.rb +42 -0
- data/lib/coridoc/html/converters/text.rb +66 -0
- data/lib/coridoc/html/converters/tr.rb +27 -0
- data/lib/coridoc/html/converters/video.rb +27 -0
- data/lib/coridoc/html/converters.rb +104 -0
- data/lib/coridoc/html/drop/annotation_drop.rb +31 -0
- data/lib/coridoc/html/drop/base.rb +72 -0
- data/lib/coridoc/html/drop/bibliography_drop.rb +15 -0
- data/lib/coridoc/html/drop/bibliography_entry_drop.rb +24 -0
- data/lib/coridoc/html/drop/block_drop.rb +78 -0
- data/lib/coridoc/html/drop/definition_item_drop.rb +61 -0
- data/lib/coridoc/html/drop/definition_list_drop.rb +15 -0
- data/lib/coridoc/html/drop/document_drop.rb +52 -0
- data/lib/coridoc/html/drop/drop_factory.rb +64 -0
- data/lib/coridoc/html/drop/footnote_drop.rb +24 -0
- data/lib/coridoc/html/drop/image_drop.rb +35 -0
- data/lib/coridoc/html/drop/inline_element_drop.rb +43 -0
- data/lib/coridoc/html/drop/list_block_drop.rb +19 -0
- data/lib/coridoc/html/drop/list_item_drop.rb +20 -0
- data/lib/coridoc/html/drop/raw_inline_element_drop.rb +30 -0
- data/lib/coridoc/html/drop/table_cell_drop.rb +35 -0
- data/lib/coridoc/html/drop/table_drop.rb +15 -0
- data/lib/coridoc/html/drop/table_row_drop.rb +23 -0
- data/lib/coridoc/html/drop/term_drop.rb +24 -0
- data/lib/coridoc/html/drop/text_content_drop.rb +15 -0
- data/lib/coridoc/html/drop/toc_drop.rb +15 -0
- data/lib/coridoc/html/drop/toc_entry_drop.rb +32 -0
- data/lib/coridoc/html/drop.rb +40 -0
- data/lib/coridoc/html/errors.rb +11 -0
- data/lib/coridoc/html/escape.rb +29 -0
- data/lib/coridoc/html/frontmatter_meta.rb +157 -0
- data/lib/coridoc/html/html_converter.rb +161 -0
- data/lib/coridoc/html/input_config.rb +71 -0
- data/lib/coridoc/html/layout_renderer.rb +195 -0
- data/lib/coridoc/html/lint/registrations.rb +40 -0
- data/lib/coridoc/html/lint.rb +16 -0
- data/lib/coridoc/html/plugin.rb +93 -0
- data/lib/coridoc/html/plugins/plateau.rb +212 -0
- data/lib/coridoc/html/postprocessor.rb +19 -0
- data/lib/coridoc/html/reference_materializers/link.rb +44 -0
- data/lib/coridoc/html/reference_materializers/navigation.rb +77 -0
- data/lib/coridoc/html/reference_materializers.rb +22 -0
- data/lib/coridoc/html/render_options.rb +40 -0
- data/lib/coridoc/html/renderer.rb +136 -0
- data/lib/coridoc/html/section_numberable.rb +9 -0
- data/lib/coridoc/html/spa.rb +49 -0
- data/lib/coridoc/html/static.rb +57 -0
- data/lib/coridoc/html/tag_mapping.rb +106 -0
- data/lib/coridoc/html/template_caching.rb +31 -0
- data/lib/coridoc/html/template_config.rb +92 -0
- data/lib/coridoc/html/template_helpers.rb +66 -0
- data/lib/coridoc/html/template_locator.rb +120 -0
- data/lib/coridoc/html/theme.rb +7 -0
- data/lib/coridoc/html/title_text.rb +57 -0
- data/lib/coridoc/html/toc_builder.rb +114 -0
- data/lib/coridoc/html/toc_serializer.rb +32 -0
- data/lib/coridoc/html/transform/from_core_model.rb +35 -0
- data/lib/coridoc/html/transform/to_core_model.rb +39 -0
- data/lib/coridoc/html/version.rb +7 -0
- data/lib/coridoc/html.rb +246 -0
- metadata +255 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
autoload :ConverterBase, "#{__dir__}/converter_base"
|
|
6
|
+
|
|
7
|
+
# SPA HTML converter
|
|
8
|
+
#
|
|
9
|
+
# Converts CoreModel documents to SPA HTML5 with embedded Vue.js frontend.
|
|
10
|
+
# Uses the unified Liquid template pipeline.
|
|
11
|
+
class Spa < ConverterBase
|
|
12
|
+
class Configuration < ConverterBase::ConfigurationBase
|
|
13
|
+
attribute :theme_toggle, default: true
|
|
14
|
+
attribute :reading_progress, default: true
|
|
15
|
+
attribute :toc_sticky, default: true
|
|
16
|
+
attribute :toc_levels, default: 2
|
|
17
|
+
attribute :lang, default: 'en'
|
|
18
|
+
attribute :template_dirs
|
|
19
|
+
attribute :dist_dir
|
|
20
|
+
|
|
21
|
+
def validate!
|
|
22
|
+
range_check(:toc_levels, 1, 5, label: 'TOC levels')
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def convert
|
|
27
|
+
renderer = Renderer.new(template_dirs: @config.template_dirs)
|
|
28
|
+
|
|
29
|
+
renderer.render_html5(
|
|
30
|
+
@document,
|
|
31
|
+
layout: :spa,
|
|
32
|
+
lang: @config.lang,
|
|
33
|
+
toc: @config.toc_sticky,
|
|
34
|
+
toc_levels: @config.toc_levels,
|
|
35
|
+
section_numbers: false,
|
|
36
|
+
dist_dir: @config.dist_dir,
|
|
37
|
+
theme_toggle: @config.theme_toggle,
|
|
38
|
+
reading_progress: @config.reading_progress
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def configuration_class
|
|
45
|
+
Configuration
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
autoload :ConverterBase, "#{__dir__}/converter_base"
|
|
6
|
+
|
|
7
|
+
# Static HTML converter
|
|
8
|
+
#
|
|
9
|
+
# Converts CoreModel documents to static HTML5 output using
|
|
10
|
+
# the unified Liquid template pipeline.
|
|
11
|
+
class Static < ConverterBase
|
|
12
|
+
class Configuration < ConverterBase::ConfigurationBase
|
|
13
|
+
attribute :include_toc, default: false
|
|
14
|
+
attribute :toc_levels, default: 2
|
|
15
|
+
attribute :section_numbering, default: false
|
|
16
|
+
attribute :section_numbering_levels, default: 3
|
|
17
|
+
attribute :lang, default: 'en'
|
|
18
|
+
attribute :meta_tags, default: {}
|
|
19
|
+
attribute :custom_css
|
|
20
|
+
attribute :embedded, default: false
|
|
21
|
+
attribute :template_dirs
|
|
22
|
+
|
|
23
|
+
def validate!
|
|
24
|
+
range_check(:toc_levels, 1, 5, label: 'TOC levels')
|
|
25
|
+
range_check(:section_numbering_levels, 1, 6, label: 'Section numbering levels')
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def convert
|
|
30
|
+
renderer = Renderer.new(template_dirs: @config.template_dirs)
|
|
31
|
+
|
|
32
|
+
if @config.embedded
|
|
33
|
+
renderer.render(@document)
|
|
34
|
+
else
|
|
35
|
+
renderer.render_html5(
|
|
36
|
+
@document,
|
|
37
|
+
layout: :static,
|
|
38
|
+
lang: @config.lang,
|
|
39
|
+
author: @config.meta_tags[:author],
|
|
40
|
+
description: @config.meta_tags[:description],
|
|
41
|
+
custom_css: @config.custom_css,
|
|
42
|
+
toc: @config.include_toc,
|
|
43
|
+
section_numbers: @config.section_numbering,
|
|
44
|
+
section_number_levels: @config.section_numbering_levels,
|
|
45
|
+
toc_levels: @config.toc_levels
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def configuration_class
|
|
53
|
+
Configuration
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
module TagMapping
|
|
6
|
+
ELEMENT_TO_TAG = {
|
|
7
|
+
# Sections
|
|
8
|
+
section: 'section',
|
|
9
|
+
header: 'header',
|
|
10
|
+
|
|
11
|
+
# Blocks
|
|
12
|
+
paragraph: 'p',
|
|
13
|
+
example: 'div',
|
|
14
|
+
sidebar: 'aside',
|
|
15
|
+
quote: 'blockquote',
|
|
16
|
+
verse: 'div',
|
|
17
|
+
listing: 'pre',
|
|
18
|
+
literal: 'pre',
|
|
19
|
+
source: 'pre',
|
|
20
|
+
source_code: 'pre',
|
|
21
|
+
open: 'div',
|
|
22
|
+
horizontal_rule: 'hr',
|
|
23
|
+
comment: 'div',
|
|
24
|
+
pass: 'div',
|
|
25
|
+
reviewer: 'div',
|
|
26
|
+
|
|
27
|
+
# Lists
|
|
28
|
+
ordered_list: 'ol',
|
|
29
|
+
unordered_list: 'ul',
|
|
30
|
+
list_item: 'li',
|
|
31
|
+
description_list: 'dl',
|
|
32
|
+
description_term: 'dt',
|
|
33
|
+
description_detail: 'dd',
|
|
34
|
+
|
|
35
|
+
# List marker type aliases (from ListBlock#marker_type)
|
|
36
|
+
ordered: 'ol',
|
|
37
|
+
unordered: 'ul',
|
|
38
|
+
definition: 'dl',
|
|
39
|
+
|
|
40
|
+
# Tables
|
|
41
|
+
table: 'table',
|
|
42
|
+
table_row: 'tr',
|
|
43
|
+
table_cell: 'td',
|
|
44
|
+
table_header: 'th',
|
|
45
|
+
|
|
46
|
+
# Inline formatting (symbol keys from TagMapping, string keys from format_type)
|
|
47
|
+
bold: 'strong',
|
|
48
|
+
italic: 'em',
|
|
49
|
+
monospace: 'code',
|
|
50
|
+
highlight: 'mark',
|
|
51
|
+
superscript: 'sup',
|
|
52
|
+
subscript: 'sub',
|
|
53
|
+
underline: 'u',
|
|
54
|
+
strikethrough: 'del',
|
|
55
|
+
small_caps: 'span',
|
|
56
|
+
|
|
57
|
+
# Inline format type aliases (from InlineElement#resolve_format_type)
|
|
58
|
+
link: 'a',
|
|
59
|
+
xref: 'a',
|
|
60
|
+
footnote: 'sup',
|
|
61
|
+
span: 'span',
|
|
62
|
+
term: 'span',
|
|
63
|
+
quotation: 'q',
|
|
64
|
+
small: 'small',
|
|
65
|
+
stem: 'code',
|
|
66
|
+
attribute_reference: 'span',
|
|
67
|
+
|
|
68
|
+
# Links (semantic names)
|
|
69
|
+
anchor: 'a',
|
|
70
|
+
cross_reference: 'a',
|
|
71
|
+
|
|
72
|
+
# Media
|
|
73
|
+
image: 'img',
|
|
74
|
+
video: 'video',
|
|
75
|
+
audio: 'audio',
|
|
76
|
+
|
|
77
|
+
# Other
|
|
78
|
+
break: 'hr',
|
|
79
|
+
line_break: 'br',
|
|
80
|
+
admonition: 'div'
|
|
81
|
+
}.freeze
|
|
82
|
+
|
|
83
|
+
ELEMENT_CSS_CLASS = {
|
|
84
|
+
example: 'example',
|
|
85
|
+
sidebar: 'sidebar',
|
|
86
|
+
literal: 'literal',
|
|
87
|
+
stem: 'stem',
|
|
88
|
+
term: 'term'
|
|
89
|
+
}.freeze
|
|
90
|
+
|
|
91
|
+
def self.tag_for(element_type)
|
|
92
|
+
return 'div' if element_type.nil?
|
|
93
|
+
|
|
94
|
+
key = element_type.is_a?(Symbol) ? element_type : element_type.to_sym
|
|
95
|
+
ELEMENT_TO_TAG[key] || 'div'
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.css_class_for(element_type)
|
|
99
|
+
return nil if element_type.nil?
|
|
100
|
+
|
|
101
|
+
key = element_type.is_a?(Symbol) ? element_type : element_type.to_sym
|
|
102
|
+
ELEMENT_CSS_CLASS[key]
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'liquid'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Html
|
|
7
|
+
# Shared template caching logic for Renderer and LayoutRenderer.
|
|
8
|
+
#
|
|
9
|
+
# Both Renderer#find_and_load_template and LayoutRenderer#load_layout
|
|
10
|
+
# implement identical caching patterns (check cache, read file, parse
|
|
11
|
+
# Liquid, store in cache, rescue syntax errors). This module provides
|
|
12
|
+
# a single implementation.
|
|
13
|
+
module TemplateCaching
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def load_template(cache:, cache_key:, path:)
|
|
17
|
+
return cache[cache_key] if cache.key?(cache_key)
|
|
18
|
+
|
|
19
|
+
return nil unless path && File.exist?(path)
|
|
20
|
+
|
|
21
|
+
template_content = File.read(path)
|
|
22
|
+
template = Liquid::Template.parse(template_content)
|
|
23
|
+
cache[cache_key] = template
|
|
24
|
+
template
|
|
25
|
+
rescue Liquid::SyntaxError => e
|
|
26
|
+
warn "Template syntax error in #{path}: #{e.message}"
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'pathname'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Html
|
|
7
|
+
# Configuration for the Liquid template system.
|
|
8
|
+
#
|
|
9
|
+
# Delegates template lookup to TemplateLocator for consistent behavior
|
|
10
|
+
# across global configuration and per-renderer usage.
|
|
11
|
+
#
|
|
12
|
+
# @example Global configuration
|
|
13
|
+
# Coridoc::Html.configure do |config|
|
|
14
|
+
# config.template_dirs = ["/path/to/custom/templates"]
|
|
15
|
+
# end
|
|
16
|
+
class TemplateConfig
|
|
17
|
+
DEFAULT_TEMPLATE_DIR = TemplateLocator::DEFAULT_TEMPLATE_DIR
|
|
18
|
+
|
|
19
|
+
attr_accessor :template_dirs
|
|
20
|
+
|
|
21
|
+
def initialize(template_dirs: [])
|
|
22
|
+
@template_dirs = Array(template_dirs).map { |dir| Pathname.new(dir) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def locator
|
|
26
|
+
@locator ||= TemplateLocator.new(user_dirs: @template_dirs)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def all_template_dirs
|
|
30
|
+
@template_dirs + [DEFAULT_TEMPLATE_DIR]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.available_templates
|
|
34
|
+
@available_templates ||= begin
|
|
35
|
+
return [] unless DEFAULT_TEMPLATE_DIR.exist?
|
|
36
|
+
|
|
37
|
+
DEFAULT_TEMPLATE_DIR
|
|
38
|
+
.glob('*.liquid')
|
|
39
|
+
.map { |f| f.basename('.liquid').to_s.to_sym }
|
|
40
|
+
.sort
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.template_path_for(name)
|
|
45
|
+
path = DEFAULT_TEMPLATE_DIR.join("#{name}.liquid")
|
|
46
|
+
path.exist? ? path : nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def template_exists?(name)
|
|
50
|
+
locator.exists?(name.to_s)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def find_template(name)
|
|
54
|
+
locator.find(name.to_s)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def reset!
|
|
58
|
+
@template_dirs = []
|
|
59
|
+
@locator = nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def with_dirs(additional_dirs)
|
|
63
|
+
self.class.new(
|
|
64
|
+
template_dirs: @template_dirs + Array(additional_dirs).map { |d| Pathname.new(d) }
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Module-level configuration storage
|
|
70
|
+
class << self
|
|
71
|
+
def configuration
|
|
72
|
+
@configuration ||= TemplateConfig.new
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def configure
|
|
76
|
+
yield(configuration) if block_given?
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def reset_configuration!
|
|
80
|
+
@configuration = nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def available_templates
|
|
84
|
+
TemplateConfig.available_templates
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def template_path_for(name)
|
|
88
|
+
TemplateConfig.template_path_for(name)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'liquid'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Html
|
|
7
|
+
# Liquid filters for template rendering
|
|
8
|
+
module TemplateFilters
|
|
9
|
+
# Render a CoreModel element or Drop by looking up its template.
|
|
10
|
+
#
|
|
11
|
+
# Usage in templates:
|
|
12
|
+
# {{ child | render_element }}
|
|
13
|
+
# {% for item in children %}{{ item | render_element }}{% endfor %}
|
|
14
|
+
#
|
|
15
|
+
def render_element(input)
|
|
16
|
+
return '' if input.nil?
|
|
17
|
+
|
|
18
|
+
renderer = @context.registers[:renderer]
|
|
19
|
+
return '' unless renderer
|
|
20
|
+
|
|
21
|
+
case input
|
|
22
|
+
when Drop::Base
|
|
23
|
+
renderer.render_drop(input)
|
|
24
|
+
when Array
|
|
25
|
+
input.map { |i| render_element(i) }.join("\n")
|
|
26
|
+
when Hash
|
|
27
|
+
render_hash_data(input, renderer)
|
|
28
|
+
when String
|
|
29
|
+
input
|
|
30
|
+
else
|
|
31
|
+
drop = Drop::DropFactory.create(input)
|
|
32
|
+
drop.is_a?(Drop::Base) ? renderer.render_drop(drop) : drop.to_s
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def escape_html(input)
|
|
37
|
+
Escape.escape_html(input)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Escape HTML attribute values
|
|
41
|
+
def escape_attr(input)
|
|
42
|
+
Escape.escape_attr(input)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# JSON-encode with </script protection for inline JS
|
|
46
|
+
def safe_json(input)
|
|
47
|
+
Escape.safe_json(input)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def render_hash_data(data, renderer)
|
|
53
|
+
drop_type = data['drop_type']
|
|
54
|
+
return data.to_s unless drop_type
|
|
55
|
+
|
|
56
|
+
template = renderer.find_template(drop_type)
|
|
57
|
+
return data.to_s unless template
|
|
58
|
+
|
|
59
|
+
template.render(data, registers: { renderer: renderer }).strip
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Register filters with Liquid
|
|
66
|
+
Liquid::Environment.default.register_filter(Coridoc::Html::TemplateFilters)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'liquid'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
|
|
6
|
+
module Coridoc
|
|
7
|
+
module Html
|
|
8
|
+
# Locates Liquid templates from filesystem with fallback support
|
|
9
|
+
#
|
|
10
|
+
# This class implements a template lookup algorithm:
|
|
11
|
+
# 1. Check user's template directories in order (first match wins)
|
|
12
|
+
# 2. Fall back to default templates if not found
|
|
13
|
+
# 3. Return nil if no template found (caller decides fallback)
|
|
14
|
+
#
|
|
15
|
+
# @example Basic usage
|
|
16
|
+
# locator = TemplateLocator.new(
|
|
17
|
+
# user_dirs: ["/path/to/templates"],
|
|
18
|
+
# default_dir: "/default/templates"
|
|
19
|
+
# )
|
|
20
|
+
# locator.find("bibliography") # => "/path/to/templates/bibliography.liquid"
|
|
21
|
+
#
|
|
22
|
+
class TemplateLocator
|
|
23
|
+
# Default template subdirectory within each template root
|
|
24
|
+
CORE_MODEL_DIR = 'core_model'
|
|
25
|
+
|
|
26
|
+
# Canonical default template directory within the gem
|
|
27
|
+
DEFAULT_TEMPLATE_DIR = Pathname.new(File.join(File.dirname(__FILE__), 'templates', CORE_MODEL_DIR)).freeze
|
|
28
|
+
|
|
29
|
+
attr_reader :user_dirs, :default_dir
|
|
30
|
+
|
|
31
|
+
# Initialize the locator
|
|
32
|
+
#
|
|
33
|
+
# @param user_dirs [Array<String>] User template directories (checked first)
|
|
34
|
+
# @param default_dir [String] Default templates directory (fallback)
|
|
35
|
+
def initialize(user_dirs: [], default_dir: nil)
|
|
36
|
+
@user_dirs = Array(user_dirs).map { |d| Pathname.new(d) }
|
|
37
|
+
@default_dir = default_dir ? Pathname.new(default_dir) : default_template_dir
|
|
38
|
+
@cache = {}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Find a template by type name
|
|
42
|
+
#
|
|
43
|
+
# @param type_name [String] The template type (e.g., "bibliography", "section")
|
|
44
|
+
# @return [Pathname, nil] Path to template file or nil if not found
|
|
45
|
+
def find(type_name)
|
|
46
|
+
return @cache[type_name] if @cache.key?(type_name)
|
|
47
|
+
|
|
48
|
+
# First check user directories (core_model/ subdir, then root)
|
|
49
|
+
@user_dirs.each do |dir|
|
|
50
|
+
%W[#{dir}/#{CORE_MODEL_DIR}/#{type_name}.liquid #{dir}/#{type_name}.liquid].each do |template_path|
|
|
51
|
+
path = Pathname.new(template_path)
|
|
52
|
+
if path.exist?
|
|
53
|
+
@cache[type_name] = path
|
|
54
|
+
return path
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Then check default directory
|
|
60
|
+
if @default_dir
|
|
61
|
+
template_path = @default_dir / "#{type_name}.liquid"
|
|
62
|
+
if template_path.exist?
|
|
63
|
+
@cache[type_name] = template_path
|
|
64
|
+
return template_path
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
@cache[type_name] = nil
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Check if a template exists (without loading it)
|
|
73
|
+
#
|
|
74
|
+
# @param type_name [String] The template type
|
|
75
|
+
# @return [Boolean] True if template exists
|
|
76
|
+
def exists?(type_name)
|
|
77
|
+
!find(type_name).nil?
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Get all available template types
|
|
81
|
+
#
|
|
82
|
+
# @return [Array<String>] List of available template names
|
|
83
|
+
def available_templates
|
|
84
|
+
types = Set.new
|
|
85
|
+
|
|
86
|
+
# Collect from user directories (root and core_model/ subdir)
|
|
87
|
+
@user_dirs.each do |dir|
|
|
88
|
+
[dir, dir / CORE_MODEL_DIR].each do |scan_dir|
|
|
89
|
+
next unless scan_dir.exist? && scan_dir.directory?
|
|
90
|
+
|
|
91
|
+
scan_dir.glob('*.liquid') do |f|
|
|
92
|
+
types.add(f.basename('.liquid').to_s)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Collect from default directory
|
|
98
|
+
if @default_dir&.exist? && @default_dir.directory?
|
|
99
|
+
@default_dir.glob('*.liquid') do |f|
|
|
100
|
+
types.add(f.basename('.liquid').to_s)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
types.to_a.sort
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Get the default template directory path
|
|
108
|
+
#
|
|
109
|
+
# @return [Pathname] Path to default templates
|
|
110
|
+
def default_template_dir
|
|
111
|
+
DEFAULT_TEMPLATE_DIR
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Clear the template cache (useful when template directories change)
|
|
115
|
+
def clear_cache
|
|
116
|
+
@cache.clear
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
# Resolves CoreModel title attributes to plain-text strings.
|
|
6
|
+
#
|
|
7
|
+
# CoreModel titles can be String, CoreModel::Base (with .text),
|
|
8
|
+
# Array of mixed types, or nil. This utility provides a single
|
|
9
|
+
# resolution path used by TocBuilder, TocSerializer, Renderer,
|
|
10
|
+
# LayoutRenderer, and Drop classes.
|
|
11
|
+
module TitleText
|
|
12
|
+
TEXT_TYPES = [CoreModel::TextContent, CoreModel::Term].freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def resolve(title)
|
|
17
|
+
case title
|
|
18
|
+
when nil then nil
|
|
19
|
+
when String then title
|
|
20
|
+
when CoreModel::Base then resolve_model(title)
|
|
21
|
+
when Array then title.map { |t| resolve_element(t) }.join
|
|
22
|
+
else title.to_s
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def escape(title)
|
|
27
|
+
resolved = resolve(title)
|
|
28
|
+
resolved ? Escape.escape_html(resolved) : nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def resolve_model(model)
|
|
32
|
+
if text_type?(model) && model.text
|
|
33
|
+
model.text
|
|
34
|
+
elsif content_type?(model) && model.content
|
|
35
|
+
model.content.to_s
|
|
36
|
+
else
|
|
37
|
+
model.to_s
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def resolve_element(element)
|
|
42
|
+
case element
|
|
43
|
+
when CoreModel::Base then resolve_model(element)
|
|
44
|
+
else element.to_s
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def text_type?(model)
|
|
49
|
+
TEXT_TYPES.any? { |t| model.is_a?(t) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def content_type?(model)
|
|
53
|
+
model.is_a?(CoreModel::InlineElement) || model.is_a?(CoreModel::StructuralElement)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
# Builds a CoreModel::Toc from a document's StructuralElement tree.
|
|
6
|
+
#
|
|
7
|
+
# Walks the section hierarchy, assigns section numbers, and returns
|
|
8
|
+
# a Toc model with nested TocEntry children. Section numbers are
|
|
9
|
+
# derived from tree position — single source of truth for both
|
|
10
|
+
# TOC rendering and heading numbering.
|
|
11
|
+
class TocBuilder
|
|
12
|
+
def initialize(max_level: 6, numbered: false, section_number_levels: 3)
|
|
13
|
+
@max_level = max_level
|
|
14
|
+
@numbered = numbered
|
|
15
|
+
@section_number_levels = section_number_levels
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Build a TocBuilder from renderer-style options hash.
|
|
19
|
+
#
|
|
20
|
+
# @param options [Hash] options with :section_number_levels, :toc_levels, :section_numbers
|
|
21
|
+
# @return [TocBuilder]
|
|
22
|
+
def self.from_options(opts)
|
|
23
|
+
opts = RenderOptions.new(**opts) unless opts.is_a?(RenderOptions)
|
|
24
|
+
section_number_levels = opts.section_number_levels || 3
|
|
25
|
+
toc_levels = opts.toc_levels || 2
|
|
26
|
+
max_level = [toc_levels, section_number_levels].min
|
|
27
|
+
new(max_level: max_level, numbered: opts.section_numbers == true, section_number_levels: section_number_levels)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Build a Toc model from a document.
|
|
31
|
+
#
|
|
32
|
+
# @param document [CoreModel::StructuralElement] the root document
|
|
33
|
+
# @return [CoreModel::Toc] the built TOC with entries and section numbers
|
|
34
|
+
def build(document)
|
|
35
|
+
toc, = build_with_numbers(document)
|
|
36
|
+
toc
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Build TOC and section number map in a single tree walk.
|
|
40
|
+
#
|
|
41
|
+
# @param document [CoreModel::StructuralElement] the root document
|
|
42
|
+
# @return [Array(CoreModel::Toc, Hash)] tuple of TOC model and section number map
|
|
43
|
+
def build_with_numbers(document)
|
|
44
|
+
entries = []
|
|
45
|
+
counters = [0]
|
|
46
|
+
collect_entries(document.children, entries, 1, counters, always_number: true) if document.children
|
|
47
|
+
|
|
48
|
+
map = {}
|
|
49
|
+
flatten_numbers(entries, map)
|
|
50
|
+
|
|
51
|
+
strip_numbers(entries) unless @numbered
|
|
52
|
+
|
|
53
|
+
toc = CoreModel::Toc.new(
|
|
54
|
+
entries: entries,
|
|
55
|
+
min_level: 1,
|
|
56
|
+
max_level: @max_level,
|
|
57
|
+
numbered: @numbered
|
|
58
|
+
)
|
|
59
|
+
[toc, map]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def collect_entries(items, entries, level, counters, always_number: false)
|
|
65
|
+
return unless items && level <= @max_level
|
|
66
|
+
|
|
67
|
+
items.each do |item|
|
|
68
|
+
next unless item.is_a?(CoreModel::StructuralElement)
|
|
69
|
+
next unless item.section? || item.header?
|
|
70
|
+
next if item.document_title?
|
|
71
|
+
|
|
72
|
+
counters[level] = (counters[level] || 0) + 1
|
|
73
|
+
((level + 1)..@section_number_levels).each { |i| counters[i] = 0 }
|
|
74
|
+
|
|
75
|
+
use_number = always_number || @numbered
|
|
76
|
+
number = use_number && level <= @section_number_levels ? counters[1..level].join('.') : nil
|
|
77
|
+
|
|
78
|
+
children = []
|
|
79
|
+
collect_entries(item.children, children, level + 1, counters, always_number: always_number) if item.children
|
|
80
|
+
|
|
81
|
+
entries << CoreModel::TocEntry.new(
|
|
82
|
+
id: entry_id(item),
|
|
83
|
+
title: entry_title(item),
|
|
84
|
+
level: level,
|
|
85
|
+
number: number,
|
|
86
|
+
children: children
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def flatten_numbers(entries, map)
|
|
92
|
+
entries.each do |entry|
|
|
93
|
+
map[entry.id] = entry.number if entry.id && entry.number
|
|
94
|
+
flatten_numbers(entry.children, map) if entry.children
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def strip_numbers(entries)
|
|
99
|
+
entries.each do |entry|
|
|
100
|
+
entry.number = nil
|
|
101
|
+
strip_numbers(entry.children) if entry.children
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def entry_title(section)
|
|
106
|
+
TitleText.resolve(section.title)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def entry_id(section)
|
|
110
|
+
section.id
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|