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,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'cgi'
|
|
4
|
+
require 'json'
|
|
5
|
+
|
|
6
|
+
module Coridoc
|
|
7
|
+
module Html
|
|
8
|
+
module Escape
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def escape_html(text)
|
|
12
|
+
CGI.escapeHTML(text.to_s)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def escape_attr(value)
|
|
16
|
+
value.to_s
|
|
17
|
+
.gsub('&', '&')
|
|
18
|
+
.gsub('"', '"')
|
|
19
|
+
.gsub('<', '<')
|
|
20
|
+
.gsub('>', '>')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def safe_json(data)
|
|
24
|
+
json = data.is_a?(String) ? data : JSON.generate(data)
|
|
25
|
+
json.gsub('</script', '<\\/script')
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
# Single source of truth for FrontmatterBlock -> HTML `<meta>` and
|
|
6
|
+
# `<link>` tag mapping. (MECE: HTML-specific concerns live in the
|
|
7
|
+
# HTML gem; CoreModel has no knowledge of HTML.)
|
|
8
|
+
#
|
|
9
|
+
# The module produces a small data structure that layout templates
|
|
10
|
+
# and Builder fallbacks both consume, so we never
|
|
11
|
+
# duplicate the mapping rule across output paths (DRY).
|
|
12
|
+
#
|
|
13
|
+
# Mapping table (single source of truth — extend here only):
|
|
14
|
+
#
|
|
15
|
+
# | Frontmatter key | Output |
|
|
16
|
+
# |-----------------------|-----------------------------------------|
|
|
17
|
+
# | title | <title> (caller decides title priority) |
|
|
18
|
+
# | author | <meta name="author"> |
|
|
19
|
+
# | description, excerpt | <meta name="description"> |
|
|
20
|
+
# | date | <meta name="date"> (ISO 8601) |
|
|
21
|
+
# | subject | <meta name="subject"> |
|
|
22
|
+
# | tags, categories | <meta name="keywords"> (comma-joined) |
|
|
23
|
+
# | $schema | <link rel="schema.X" href="..."> |
|
|
24
|
+
# | (any other scalar) | <meta name="<key>" content="<value>"> |
|
|
25
|
+
module FrontmatterMeta
|
|
26
|
+
Meta = Struct.new(:name, :content, keyword_init: true)
|
|
27
|
+
LinkTag = Struct.new(:rel, :href, keyword_init: true)
|
|
28
|
+
|
|
29
|
+
DESCRIPTION_KEYS = %w[description excerpt].freeze
|
|
30
|
+
KEYWORDS_KEYS = %w[tags categories].freeze
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
# Extract a meta-tag list and link-tag list from a
|
|
34
|
+
# FrontmatterBlock. `title` is returned separately so the caller
|
|
35
|
+
# can decide precedence (e.g., document title vs. frontmatter).
|
|
36
|
+
#
|
|
37
|
+
# @param block [Coridoc::CoreModel::FrontmatterBlock, nil]
|
|
38
|
+
# @return [Hash{Symbol=>Array<Meta>,Array<LinkTag>,String,nil}]
|
|
39
|
+
# { metas:, links:, title: }
|
|
40
|
+
def extract(block)
|
|
41
|
+
return empty_result unless block.is_a?(Coridoc::CoreModel::FrontmatterBlock)
|
|
42
|
+
|
|
43
|
+
data = Coridoc::CoreModel::FrontmatterBlock::Codec.to_hash(block)
|
|
44
|
+
{
|
|
45
|
+
metas: build_metas(data),
|
|
46
|
+
links: build_links(block.schema),
|
|
47
|
+
title: scalar_value(data['title'])
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Emit meta + link tags into a Builder head context.
|
|
52
|
+
def emit_into_builder(builder_doc, block)
|
|
53
|
+
data = extract(block)
|
|
54
|
+
Array(data[:metas]).each do |meta|
|
|
55
|
+
builder_doc.meta(name: meta.name, content: meta.content)
|
|
56
|
+
end
|
|
57
|
+
Array(data[:links]).each do |link|
|
|
58
|
+
builder_doc.link(rel: link.rel, href: link.href)
|
|
59
|
+
end
|
|
60
|
+
data
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def empty_result
|
|
66
|
+
{ metas: [], links: [], title: nil }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def build_metas(data)
|
|
70
|
+
metas = []
|
|
71
|
+
consumed = []
|
|
72
|
+
|
|
73
|
+
if (v = find_first_scalar(data, DESCRIPTION_KEYS))
|
|
74
|
+
metas << Meta.new(name: 'description', content: v)
|
|
75
|
+
consumed += DESCRIPTION_KEYS
|
|
76
|
+
end
|
|
77
|
+
if (v = find_first_array_as_csv(data, KEYWORDS_KEYS))
|
|
78
|
+
metas << Meta.new(name: 'keywords', content: v)
|
|
79
|
+
consumed += KEYWORDS_KEYS
|
|
80
|
+
end
|
|
81
|
+
if (v = find_first_scalar(data, %w[date]))
|
|
82
|
+
metas << Meta.new(name: 'date', content: v)
|
|
83
|
+
consumed << 'date'
|
|
84
|
+
end
|
|
85
|
+
if (v = find_first_scalar(data, %w[author]))
|
|
86
|
+
metas << Meta.new(name: 'author', content: v)
|
|
87
|
+
consumed << 'author'
|
|
88
|
+
end
|
|
89
|
+
if (v = find_first_scalar(data, %w[subject]))
|
|
90
|
+
metas << Meta.new(name: 'subject', content: v)
|
|
91
|
+
consumed << 'subject'
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
data.each do |key, value|
|
|
95
|
+
next if consumed.include?(key) || key == 'title'
|
|
96
|
+
|
|
97
|
+
content = scalar_value(value)
|
|
98
|
+
next if content.nil? || content.empty?
|
|
99
|
+
|
|
100
|
+
metas << Meta.new(name: key, content: content)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
metas
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def build_links(schema)
|
|
107
|
+
return [] unless schema && !schema.to_s.strip.empty?
|
|
108
|
+
|
|
109
|
+
[LinkTag.new(rel: 'schema.dublin_core', href: schema.to_s)]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def find_first_scalar(data, keys)
|
|
113
|
+
keys.each do |k|
|
|
114
|
+
value = data[k]
|
|
115
|
+
next if value.nil?
|
|
116
|
+
|
|
117
|
+
str = scalar_value(value)
|
|
118
|
+
return str if str && !str.empty?
|
|
119
|
+
end
|
|
120
|
+
nil
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def find_first_array_as_csv(data, keys)
|
|
124
|
+
keys.each do |k|
|
|
125
|
+
value = data[k]
|
|
126
|
+
next if value.nil?
|
|
127
|
+
|
|
128
|
+
csv = array_to_csv(value)
|
|
129
|
+
return csv if csv && !csv.empty?
|
|
130
|
+
end
|
|
131
|
+
nil
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def scalar_value(value)
|
|
135
|
+
return nil if value.nil?
|
|
136
|
+
|
|
137
|
+
case value
|
|
138
|
+
when String then value
|
|
139
|
+
when Integer, Float, TrueClass, FalseClass then value.to_s
|
|
140
|
+
when Date, Time, DateTime then value.iso8601
|
|
141
|
+
when Symbol then value.to_s
|
|
142
|
+
when Array then array_to_csv(value)
|
|
143
|
+
when Hash then nil
|
|
144
|
+
else value.to_s
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def array_to_csv(value)
|
|
149
|
+
return nil unless value.is_a?(Array)
|
|
150
|
+
|
|
151
|
+
strings = value.map { |i| scalar_value(i) }.compact
|
|
152
|
+
strings.any? ? strings.join(', ') : nil
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
class HtmlConverter
|
|
6
|
+
def self.to_core_model(input, options = {})
|
|
7
|
+
Html.input_config.with(options) do
|
|
8
|
+
plugin_instances = prepare_plugin_instances(options)
|
|
9
|
+
|
|
10
|
+
root = track_time 'Loading input HTML document' do
|
|
11
|
+
case input
|
|
12
|
+
when String
|
|
13
|
+
next nil if input.strip.empty?
|
|
14
|
+
|
|
15
|
+
html_parse(input).root
|
|
16
|
+
when Leptris::XML::Document
|
|
17
|
+
input.root
|
|
18
|
+
when Leptris::XML::Node
|
|
19
|
+
input
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
return nil unless root
|
|
24
|
+
|
|
25
|
+
plugin_instances.each do |plugin|
|
|
26
|
+
plugin.html_tree = root
|
|
27
|
+
track_time "Preprocessing document with #{plugin.name} plugin" do
|
|
28
|
+
plugin.preprocess_html_tree
|
|
29
|
+
end
|
|
30
|
+
root = plugin.html_tree
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
coremodel = track_time 'Converting input document tree to CoreModel' do
|
|
34
|
+
Converters.process_coridoc(
|
|
35
|
+
root,
|
|
36
|
+
plugin_instances: plugin_instances
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
coremodel = track_time 'Post-process CoreModel tree' do
|
|
41
|
+
Postprocessor.process(coremodel)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
coremodel = track_time 'Normalize top-level structure' do
|
|
45
|
+
normalize_top_level(coremodel)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
plugin_instances.each do |plugin|
|
|
49
|
+
plugin.coremodel_tree = coremodel
|
|
50
|
+
track_time "Postprocessing CoreModel tree with #{plugin.name} plugin" do
|
|
51
|
+
plugin.postprocess_coremodel_tree
|
|
52
|
+
end
|
|
53
|
+
coremodel = plugin.coremodel_tree
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
options[:plugin_instances] = plugin_instances unless options.frozen?
|
|
57
|
+
|
|
58
|
+
coremodel
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# #88/#102/#103: bare body-level text keeps HTML source
|
|
63
|
+
# whitespace (trim its edges), and section ids must be
|
|
64
|
+
# non-blank and unique document-wide (no empty or duplicated
|
|
65
|
+
# [[anchor]]s in the output).
|
|
66
|
+
def self.normalize_top_level(coremodel)
|
|
67
|
+
seen_ids = {}
|
|
68
|
+
normalized = normalize_children(Array(coremodel), seen_ids)
|
|
69
|
+
coremodel.is_a?(Array) ? normalized : normalized.first
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.normalize_children(children, seen_ids)
|
|
73
|
+
children.filter_map do |child|
|
|
74
|
+
case child
|
|
75
|
+
when Coridoc::CoreModel::TextElement
|
|
76
|
+
trimmed = child.content.to_s.gsub(/\A[ \t\r\n]+/, '')
|
|
77
|
+
.gsub(/[ \t\r\n]+\z/, '')
|
|
78
|
+
next nil if trimmed.empty?
|
|
79
|
+
|
|
80
|
+
child.content = trimmed
|
|
81
|
+
when Coridoc::CoreModel::SectionElement
|
|
82
|
+
normalize_section(child, seen_ids)
|
|
83
|
+
end
|
|
84
|
+
child
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Renumbering replaces source-derived anchors (Word `_Toc…`,
|
|
89
|
+
# `anchor-16`, …) with title-generated ids (#83).
|
|
90
|
+
def self.renumber_anchors?
|
|
91
|
+
Coridoc::Html.input_config.renumber_anchors
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.normalize_section(section, seen_ids)
|
|
95
|
+
id = renumber_anchors? ? '' : section.id.to_s.strip
|
|
96
|
+
if id.empty?
|
|
97
|
+
id = Coridoc::CoreModel::IdGenerator.generate_from_title(
|
|
98
|
+
section.title
|
|
99
|
+
).to_s.sub(/\A_+/, '')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
if id.empty?
|
|
103
|
+
section.id = nil
|
|
104
|
+
else
|
|
105
|
+
original = id
|
|
106
|
+
counter = 1
|
|
107
|
+
while seen_ids.key?(id)
|
|
108
|
+
id = "#{original}_#{counter}"
|
|
109
|
+
counter += 1
|
|
110
|
+
end
|
|
111
|
+
section.id = id
|
|
112
|
+
seen_ids[id] = true
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
nested = Array(section.children).select do |c|
|
|
116
|
+
c.is_a?(Coridoc::CoreModel::SectionElement)
|
|
117
|
+
end
|
|
118
|
+
nested.each { |s| normalize_section(s, seen_ids) }
|
|
119
|
+
section
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.prepare_plugin_instances(options)
|
|
123
|
+
options[:plugin_instances] || Html.input_config.plugins.map(&:new)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Single dispatch point for the HTML parsing lane: :html4 keeps
|
|
127
|
+
# Nokogiri::HTML byte-parity; :html5 selects the WHATWG-conformant
|
|
128
|
+
# engine (implied tbody/tr, foster parenting, full implied head).
|
|
129
|
+
# A synthesized empty <head/> is stripped before conversion — an
|
|
130
|
+
# authored empty head (html4 lane, explicit markup) still produces
|
|
131
|
+
# a DocumentElement with the placeholder title.
|
|
132
|
+
def self.html_parse(input)
|
|
133
|
+
if Html.input_config.html_version == :html5
|
|
134
|
+
doc = Leptris::HTML5.parse(input)
|
|
135
|
+
head = doc.at_css('head')
|
|
136
|
+
head.parent&.remove_child(head) if head && head.children.empty?
|
|
137
|
+
doc
|
|
138
|
+
else
|
|
139
|
+
Leptris::HTML.parse(input)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
@track_time_indentation = 0
|
|
144
|
+
def self.track_time(task)
|
|
145
|
+
if Html.input_config.track_time
|
|
146
|
+
warn (' ' * @track_time_indentation) + "* #{task} is starting..."
|
|
147
|
+
@track_time_indentation += 1
|
|
148
|
+
t0 = Time.now
|
|
149
|
+
ret = yield
|
|
150
|
+
time_elapsed = Time.now - t0
|
|
151
|
+
@track_time_indentation -= 1
|
|
152
|
+
warn (' ' * @track_time_indentation) +
|
|
153
|
+
"* #{task} took #{time_elapsed.round(3)} seconds"
|
|
154
|
+
ret
|
|
155
|
+
else
|
|
156
|
+
yield
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'tmpdir'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Html
|
|
7
|
+
class InputConfig
|
|
8
|
+
def initialize
|
|
9
|
+
@unknown_tags = :pass_through
|
|
10
|
+
@input_format = :html
|
|
11
|
+
@html_version = :html4
|
|
12
|
+
@external_images = false
|
|
13
|
+
|
|
14
|
+
@destination = nil
|
|
15
|
+
@sourcedir = nil
|
|
16
|
+
|
|
17
|
+
@image_counter = 1
|
|
18
|
+
@image_counter_pattern = '%03d'
|
|
19
|
+
|
|
20
|
+
@em_delimiter = '_'
|
|
21
|
+
@strong_delimiter = '*'
|
|
22
|
+
@inline_options = {}
|
|
23
|
+
@tag_border = ' '
|
|
24
|
+
|
|
25
|
+
@split_sections = nil
|
|
26
|
+
|
|
27
|
+
# Replace source-derived anchor ids (e.g. Word _Toc… anchors)
|
|
28
|
+
# with ids generated from section titles (#83).
|
|
29
|
+
@renumber_anchors = false
|
|
30
|
+
|
|
31
|
+
@doc_width = 1000
|
|
32
|
+
|
|
33
|
+
@plugins = []
|
|
34
|
+
|
|
35
|
+
@track_time = false
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def with(options = {})
|
|
39
|
+
old_options = @inline_options
|
|
40
|
+
@inline_options = options
|
|
41
|
+
result = yield
|
|
42
|
+
@inline_options = old_options
|
|
43
|
+
result
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.declare_option(option)
|
|
47
|
+
attr_accessor option
|
|
48
|
+
|
|
49
|
+
original_reader = instance_method(option)
|
|
50
|
+
define_method(option) do
|
|
51
|
+
@inline_options[option] || original_reader.bind_call(self)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
declare_option :unknown_tags
|
|
56
|
+
declare_option :tag_border
|
|
57
|
+
declare_option :external_images
|
|
58
|
+
declare_option :destination
|
|
59
|
+
declare_option :sourcedir
|
|
60
|
+
declare_option :image_counter
|
|
61
|
+
declare_option :image_counter_pattern
|
|
62
|
+
declare_option :input_format
|
|
63
|
+
declare_option :html_version
|
|
64
|
+
declare_option :split_sections
|
|
65
|
+
declare_option :renumber_anchors
|
|
66
|
+
declare_option :doc_width
|
|
67
|
+
declare_option :plugins
|
|
68
|
+
declare_option :track_time
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'liquid'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Html
|
|
7
|
+
class LayoutRenderer
|
|
8
|
+
include TemplateCaching
|
|
9
|
+
|
|
10
|
+
LAYOUT_DIR = Pathname.new(File.join(File.dirname(__FILE__), 'templates', 'layouts'))
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@template_cache = {}
|
|
14
|
+
@dist_assets_cache = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def render_static(document, body_html, opts)
|
|
18
|
+
opts = RenderOptions.new(**opts) unless opts.is_a?(RenderOptions)
|
|
19
|
+
layout_template = load_layout('default')
|
|
20
|
+
return build_static_fallback(document, body_html, opts) unless layout_template
|
|
21
|
+
|
|
22
|
+
layout_template.render(build_static_layout_data(document, body_html, opts)).strip
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def render_spa(document, opts, body_html, toc_data)
|
|
26
|
+
opts = RenderOptions.new(**opts) unless opts.is_a?(RenderOptions)
|
|
27
|
+
dist_dir = opts.dist_dir || File.expand_path('../../../frontend/dist', __dir__)
|
|
28
|
+
assets = load_dist_assets(dist_dir)
|
|
29
|
+
|
|
30
|
+
content_data = build_spa_content_data(document, body_html, opts, toc_data)
|
|
31
|
+
safe_json = Escape.safe_json(content_data)
|
|
32
|
+
|
|
33
|
+
layout_template = load_layout('spa')
|
|
34
|
+
if layout_template
|
|
35
|
+
layout_template.render(build_spa_layout_data(document, opts, assets, safe_json)).strip
|
|
36
|
+
else
|
|
37
|
+
build_spa_fallback(document, opts, assets, safe_json)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def resolve_title(document)
|
|
44
|
+
TitleText.resolve(document&.title) || Config::DEFAULT_TITLE
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def resolve_escaped_title(document)
|
|
48
|
+
TitleText.escape(document&.title) || Config::DEFAULT_TITLE
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def resolve_lang(opts)
|
|
52
|
+
opts.lang || Config::DEFAULT_LANG
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Pull a leading FrontmatterBlock out of the document's children,
|
|
56
|
+
# if present. Returns nil when there isn't one.
|
|
57
|
+
def extract_frontmatter(document)
|
|
58
|
+
return nil unless document.is_a?(Coridoc::CoreModel::DocumentElement)
|
|
59
|
+
|
|
60
|
+
Array(document.children).find { |c| c.is_a?(Coridoc::CoreModel::FrontmatterBlock) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Filter meta_tags so caller-supplied opts (author/description)
|
|
64
|
+
# take precedence and we never emit two <meta> tags with the same
|
|
65
|
+
# name. Single source of truth for the dedup rule (DRY): both the
|
|
66
|
+
# static and SPA layout paths route through here.
|
|
67
|
+
def dedup_meta_tags(metas, opts)
|
|
68
|
+
overridden = %w[author description].each_with_object([]) do |name, acc|
|
|
69
|
+
acc << name if opts.public_send(name)
|
|
70
|
+
end
|
|
71
|
+
metas.reject { |m| overridden.include?(m.name) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def build_static_layout_data(document, body_html, opts)
|
|
75
|
+
frontmatter = extract_frontmatter(document)
|
|
76
|
+
fm_metas, fm_links = FrontmatterMeta.extract(frontmatter).values_at(:metas, :links)
|
|
77
|
+
{
|
|
78
|
+
'lang' => resolve_lang(opts),
|
|
79
|
+
'title' => resolve_escaped_title(document),
|
|
80
|
+
'author' => opts.author,
|
|
81
|
+
'description' => opts.description,
|
|
82
|
+
'generator_version' => Coridoc::VERSION.to_s,
|
|
83
|
+
'body' => body_html,
|
|
84
|
+
'custom_css' => opts.custom_css,
|
|
85
|
+
'meta_tags' => dedup_meta_tags(fm_metas, opts).map { |m| { 'name' => m.name, 'content' => m.content } },
|
|
86
|
+
'link_tags' => fm_links.map { |l| { 'rel' => l.rel, 'href' => l.href } }
|
|
87
|
+
}
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def build_static_fallback(document, body_html, opts)
|
|
91
|
+
frontmatter = extract_frontmatter(document)
|
|
92
|
+
Builder.new do |doc|
|
|
93
|
+
doc.html(lang: resolve_lang(opts)) do
|
|
94
|
+
doc.head do
|
|
95
|
+
doc.meta(charset: 'UTF-8')
|
|
96
|
+
doc.meta(name: 'viewport', content: 'width=device-width, initial-scale=1.0')
|
|
97
|
+
doc.title resolve_title(document)
|
|
98
|
+
FrontmatterMeta.emit_into_builder(doc, frontmatter)
|
|
99
|
+
end
|
|
100
|
+
doc.body { doc << body_html }
|
|
101
|
+
end
|
|
102
|
+
end.to_html
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def build_spa_content_data(document, body_html, opts, toc_data)
|
|
106
|
+
{
|
|
107
|
+
mode: 'classic',
|
|
108
|
+
contentHtml: body_html,
|
|
109
|
+
toc: toc_data,
|
|
110
|
+
meta: build_spa_meta(document, opts),
|
|
111
|
+
options: build_spa_options(opts)
|
|
112
|
+
}
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def build_spa_meta(document, _opts)
|
|
116
|
+
{
|
|
117
|
+
title: TitleText.resolve(document&.title) || Config::DEFAULT_TITLE,
|
|
118
|
+
author: _opts.author,
|
|
119
|
+
date: nil,
|
|
120
|
+
generator: "Coridoc #{Coridoc::VERSION}"
|
|
121
|
+
}
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def build_spa_options(opts)
|
|
125
|
+
{
|
|
126
|
+
toc: opts.toc ? true : false,
|
|
127
|
+
tocPlacement: (opts.toc_placement || :auto).to_s,
|
|
128
|
+
sectnums: opts.section_numbers == true,
|
|
129
|
+
themeToggle: opts.theme_toggle != false,
|
|
130
|
+
readingProgress: opts.reading_progress != false,
|
|
131
|
+
lang: resolve_lang(opts)
|
|
132
|
+
}
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def build_spa_layout_data(document, opts, assets, safe_json)
|
|
136
|
+
frontmatter = extract_frontmatter(document)
|
|
137
|
+
fm_metas, fm_links = FrontmatterMeta.extract(frontmatter).values_at(:metas, :links)
|
|
138
|
+
{
|
|
139
|
+
'lang' => resolve_lang(opts),
|
|
140
|
+
'title' => resolve_escaped_title(document),
|
|
141
|
+
'author' => opts.author,
|
|
142
|
+
'description' => opts.description,
|
|
143
|
+
'generator_version' => Coridoc::VERSION.to_s,
|
|
144
|
+
'css' => assets[:css],
|
|
145
|
+
'js' => assets[:js],
|
|
146
|
+
'data' => safe_json,
|
|
147
|
+
'meta_tags' => dedup_meta_tags(fm_metas, opts).map { |m| { 'name' => m.name, 'content' => m.content } },
|
|
148
|
+
'link_tags' => fm_links.map { |l| { 'rel' => l.rel, 'href' => l.href } }
|
|
149
|
+
}
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def build_spa_fallback(document, opts, assets, safe_json)
|
|
153
|
+
frontmatter = extract_frontmatter(document)
|
|
154
|
+
Builder.new do |doc|
|
|
155
|
+
doc.html(lang: resolve_lang(opts)) do
|
|
156
|
+
doc.head do
|
|
157
|
+
doc.meta(charset: 'UTF-8')
|
|
158
|
+
doc.meta(name: 'viewport', content: 'width=device-width, initial-scale=1.0')
|
|
159
|
+
doc.meta(name: 'generator', content: "Coridoc #{Coridoc::VERSION}")
|
|
160
|
+
doc.title resolve_title(document)
|
|
161
|
+
FrontmatterMeta.emit_into_builder(doc, frontmatter)
|
|
162
|
+
doc.style { doc.text assets[:css] } if assets[:css]
|
|
163
|
+
end
|
|
164
|
+
doc.body do
|
|
165
|
+
doc.div(id: 'coridoc-app')
|
|
166
|
+
doc.script { doc << "window.CORIDOC_DATA = #{safe_json};" }
|
|
167
|
+
doc.script { doc << assets[:js] } if assets[:js]
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end.to_html
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def load_layout(name)
|
|
174
|
+
cache_key = "layout:#{name}"
|
|
175
|
+
path = LAYOUT_DIR.join("#{name}.liquid").to_s
|
|
176
|
+
load_template(cache: @template_cache, cache_key: cache_key, path: path)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def load_dist_assets(dist_dir)
|
|
180
|
+
@dist_assets_cache[dist_dir] ||= begin
|
|
181
|
+
unless File.directory?(dist_dir)
|
|
182
|
+
raise ArgumentError,
|
|
183
|
+
"Frontend dist directory not found: #{dist_dir}. " \
|
|
184
|
+
'Build the frontend first: cd frontend && npm install && npm run build'
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
{
|
|
188
|
+
css: File.read(File.join(dist_dir, 'app.css')),
|
|
189
|
+
js: File.read(File.join(dist_dir, 'app.iife.js'))
|
|
190
|
+
}
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
module Lint
|
|
6
|
+
# HTML bindings for the shared invariant templates (#309). The
|
|
7
|
+
# parsed document is a CoreModel element array.
|
|
8
|
+
module Registrations
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def headings(document)
|
|
12
|
+
Array(document).select { |e| e.is_a?(CoreModel::SectionElement) }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def images(document)
|
|
16
|
+
Array(document).select { |e| e.is_a?(CoreModel::Image) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# HTML001 — a heading may be at most one level deeper than the
|
|
20
|
+
# previous heading.
|
|
21
|
+
HEADING_LEVELS = Coridoc::Lint::Template.bind(
|
|
22
|
+
:monotonic, id: 'HTML001', format: :html, order: :sequence,
|
|
23
|
+
roots: method(:headings).to_proc,
|
|
24
|
+
level: :level.to_proc,
|
|
25
|
+
label: :title.to_proc
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# HTML002 — images must carry alt text.
|
|
29
|
+
IMAGE_ALT = Coridoc::Lint::Template.bind(
|
|
30
|
+
:present, id: 'HTML002', format: :html,
|
|
31
|
+
subjects: method(:images).to_proc,
|
|
32
|
+
present: ->(image) { image.alt && !image.alt.strip.empty? },
|
|
33
|
+
message: ->(image) { "image without alt text: #{image.src}" }
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
RULES = [HEADING_LEVELS, IMAGE_ALT].freeze
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Html
|
|
5
|
+
# HTML lint rules: model-driven checks over the parsed CoreModel
|
|
6
|
+
# element array that Coridoc::Html.parse produces. Rules
|
|
7
|
+
# self-register into Coridoc::Lint.registry when required.
|
|
8
|
+
module Lint
|
|
9
|
+
require_relative 'lint/registrations'
|
|
10
|
+
|
|
11
|
+
RULES = Registrations::RULES
|
|
12
|
+
|
|
13
|
+
RULES.each { |rule| Coridoc::Lint.registry.register(rule) }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|