coradoc-html 1.1.13 → 1.1.15

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.
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
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
+ }.freeze
88
+
89
+ def self.tag_for(element_type)
90
+ return 'div' if element_type.nil?
91
+
92
+ key = element_type.is_a?(Symbol) ? element_type : element_type.to_sym
93
+ ELEMENT_TO_TAG[key] || 'div'
94
+ end
95
+
96
+ def self.css_class_for(element_type)
97
+ return nil if element_type.nil?
98
+
99
+ key = element_type.is_a?(Symbol) ? element_type : element_type.to_sym
100
+ ELEMENT_CSS_CLASS[key]
101
+ end
102
+ end
103
+ end
104
+ end
@@ -19,11 +19,12 @@ module Coradoc
19
19
  #
20
20
  # @param options [Hash] options with :section_number_levels, :toc_levels, :section_numbers
21
21
  # @return [TocBuilder]
22
- def self.from_options(options)
23
- section_number_levels = options[:section_number_levels] || 3
24
- toc_levels = options[:toc_levels] || 2
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
25
26
  max_level = [toc_levels, section_number_levels].min
26
- new(max_level: max_level, numbered: options[:section_numbers] == true, section_number_levels: section_number_levels)
27
+ new(max_level: max_level, numbered: opts.section_numbers == true, section_number_levels: section_number_levels)
27
28
  end
28
29
 
29
30
  # Build a Toc model from a document.
@@ -31,31 +32,31 @@ module Coradoc
31
32
  # @param document [CoreModel::StructuralElement] the root document
32
33
  # @return [CoreModel::Toc] the built TOC with entries and section numbers
33
34
  def build(document)
34
- entries = []
35
- counters = [0]
36
- collect_entries(document.children, entries, 1, counters) if document.children
37
-
38
- CoreModel::Toc.new(
39
- entries: entries,
40
- min_level: 1,
41
- max_level: @max_level,
42
- numbered: @numbered
43
- )
35
+ toc, = build_with_numbers(document)
36
+ toc
44
37
  end
45
38
 
46
- # Compute a mapping of section_id => section_number_string.
47
- # Always computes numbers regardless of the +numbered+ flag,
48
- # since this is used for heading annotation in the body.
39
+ # Build TOC and section number map in a single tree walk.
49
40
  #
50
41
  # @param document [CoreModel::StructuralElement] the root document
51
- # @return [Hash{String => String}] mapping of section ID to number (e.g., "2.1")
52
- def section_number_map(document)
53
- map = {}
42
+ # @return [Array(CoreModel::Toc, Hash)] tuple of TOC model and section number map
43
+ def build_with_numbers(document)
54
44
  entries = []
55
45
  counters = [0]
56
46
  collect_entries(document.children, entries, 1, counters, always_number: true) if document.children
47
+
48
+ map = {}
57
49
  flatten_numbers(entries, map)
58
- 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]
59
60
  end
60
61
 
61
62
  private
@@ -93,6 +94,13 @@ module Coradoc
93
94
  end
94
95
  end
95
96
 
97
+ def strip_numbers(entries)
98
+ entries.each do |entry|
99
+ entry.number = nil
100
+ strip_numbers(entry.children) if entry.children
101
+ end
102
+ end
103
+
96
104
  def entry_title(section)
97
105
  TitleText.resolve(section.title)
98
106
  end
@@ -6,17 +6,16 @@ module Coradoc
6
6
  #
7
7
  # Used by the SPA layout to provide client-side navigation data.
8
8
  class TocSerializer
9
- def build_json(document, options)
9
+ def build_json(document, opts)
10
10
  return { entries: [], numbered: false } unless document.is_a?(CoreModel::StructuralElement)
11
11
 
12
- numbered = options[:section_numbers] == true
13
- builder = TocBuilder.from_options(options)
12
+ opts = RenderOptions.new(**opts) unless opts.is_a?(RenderOptions)
13
+ numbered = opts.section_numbers == true
14
+ builder = TocBuilder.from_options(opts)
14
15
  toc = builder.build(document)
15
16
  { entries: serialize_entries(toc.entries), numbered: numbered }
16
17
  end
17
18
 
18
- private
19
-
20
19
  def serialize_entries(entries)
21
20
  entries.map do |entry|
22
21
  {
@@ -10,6 +10,8 @@ module Coradoc
10
10
  # This transformer converts CoreModel to HTML strings by delegating
11
11
  # to the existing theme/renderer pipeline.
12
12
  class FromCoreModel
13
+ include Coradoc::Transform::Base
14
+
13
15
  class << self
14
16
  # Transform a CoreModel to HTML string
15
17
  #
@@ -12,6 +12,8 @@ module Coradoc
12
12
  # Nokogiri::XML::Document or Nokogiri::XML::Node objects into CoreModel
13
13
  # by delegating to the existing input converter pipeline.
14
14
  class ToCoreModel
15
+ include Coradoc::Transform::Base
16
+
15
17
  class << self
16
18
  # Transform an HTML model (Nokogiri node) to CoreModel
17
19
  #
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Coradoc
4
4
  module Html
5
- VERSION = '1.1.13'
5
+ VERSION = '1.1.15'
6
6
  end
7
7
  end
data/lib/coradoc/html.rb CHANGED
@@ -21,16 +21,15 @@ module Coradoc
21
21
  module Html
22
22
  # Autoload HTML components
23
23
  autoload :Config, 'coradoc/html/config'
24
+ autoload :AssetResolver, 'coradoc/html/asset_resolver'
25
+ autoload :TagMapping, 'coradoc/html/tag_mapping'
24
26
  autoload :Escape, 'coradoc/html/escape'
25
27
  autoload :SectionNumberable, 'coradoc/html/section_numberable'
26
28
  autoload :TemplateCaching, 'coradoc/html/template_caching'
27
29
  autoload :TitleText, 'coradoc/html/title_text'
28
30
 
29
- # Autoload Drop namespace (DropFactory loads Base + all drops)
30
- module Drop
31
- autoload :DropFactory, 'coradoc/html/drop/drop_factory'
32
- autoload :Base, 'coradoc/html/drop/base'
33
- end
31
+ # Drop layer self-registering drops loaded via parent namespace file
32
+ require 'coradoc/html/drop'
34
33
 
35
34
  # Autoload HTML output converters
36
35
  autoload :ConverterBase, 'coradoc/html/converter_base'
@@ -42,9 +41,11 @@ module Coradoc
42
41
  autoload :Theme, 'coradoc/html/theme'
43
42
  autoload :TemplateLocator, 'coradoc/html/template_locator'
44
43
  autoload :TemplateConfig, 'coradoc/html/template_config'
45
- autoload :TemplateFilters, 'coradoc/html/template_helpers'
44
+ # Side-effect: registers Liquid filters on load
45
+ require 'coradoc/html/template_helpers'
46
46
  autoload :Renderer, 'coradoc/html/renderer'
47
47
  autoload :LayoutRenderer, 'coradoc/html/layout_renderer'
48
+ autoload :RenderOptions, 'coradoc/html/render_options'
48
49
  autoload :TocSerializer, 'coradoc/html/toc_serializer'
49
50
 
50
51
  # CoreModel transformers
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coradoc-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.13
4
+ version: 1.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -117,8 +117,10 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - LICENSE.txt
119
119
  - lib/coradoc/html.rb
120
+ - lib/coradoc/html/asset_resolver.rb
120
121
  - lib/coradoc/html/config.rb
121
122
  - lib/coradoc/html/converter_base.rb
123
+ - lib/coradoc/html/drop.rb
122
124
  - lib/coradoc/html/drop/annotation_drop.rb
123
125
  - lib/coradoc/html/drop/base.rb
124
126
  - lib/coradoc/html/drop/bibliography_drop.rb
@@ -188,10 +190,12 @@ files:
188
190
  - lib/coradoc/html/input/postprocessor.rb
189
191
  - lib/coradoc/html/layout_renderer.rb
190
192
  - lib/coradoc/html/output.rb
193
+ - lib/coradoc/html/render_options.rb
191
194
  - lib/coradoc/html/renderer.rb
192
195
  - lib/coradoc/html/section_numberable.rb
193
196
  - lib/coradoc/html/spa.rb
194
197
  - lib/coradoc/html/static.rb
198
+ - lib/coradoc/html/tag_mapping.rb
195
199
  - lib/coradoc/html/template_caching.rb
196
200
  - lib/coradoc/html/template_config.rb
197
201
  - lib/coradoc/html/template_helpers.rb