Module: Coradoc::Html

Defined in:
lib/coradoc/html/input.rb,
lib/coradoc/html.rb,
lib/coradoc/html/spa.rb,
lib/coradoc/html/base.rb,
lib/coradoc/html/theme.rb,
lib/coradoc/html/config.rb,
lib/coradoc/html/entity.rb,
lib/coradoc/html/static.rb,
lib/coradoc/html/version.rb,
lib/coradoc/html/renderer.rb,
lib/coradoc/html/theme/base.rb,
lib/coradoc/html/converter_base.rb,
lib/coradoc/html/theme/registry.rb,
lib/coradoc/html/converters/base.rb,
lib/coradoc/html/converters/bold.rb,
lib/coradoc/html/converters/link.rb,
lib/coradoc/html/converters/open.rb,
lib/coradoc/html/converters/span.rb,
lib/coradoc/html/converters/term.rb,
lib/coradoc/html/element_mapping.rb,
lib/coradoc/html/template_config.rb,
lib/coradoc/html/converters/audio.rb,
lib/coradoc/html/converters/break.rb,
lib/coradoc/html/converters/quote.rb,
lib/coradoc/html/converters/table.rb,
lib/coradoc/html/converters/verse.rb,
lib/coradoc/html/converters/video.rb,
lib/coradoc/html/template_helpers.rb,
lib/coradoc/html/template_locator.rb,
lib/coradoc/html/converters/italic.rb,
lib/coradoc/html/converters/source.rb,
lib/coradoc/html/converters/example.rb,
lib/coradoc/html/converters/include.rb,
lib/coradoc/html/converters/listing.rb,
lib/coradoc/html/converters/literal.rb,
lib/coradoc/html/converters/ordered.rb,
lib/coradoc/html/converters/section.rb,
lib/coradoc/html/converters/sidebar.rb,
lib/coradoc/html/converters/document.rb,
lib/coradoc/html/converters/attribute.rb,
lib/coradoc/html/converters/highlight.rb,
lib/coradoc/html/converters/list_item.rb,
lib/coradoc/html/converters/monospace.rb,
lib/coradoc/html/converters/paragraph.rb,
lib/coradoc/html/converters/subscript.rb,
lib/coradoc/html/converters/table_row.rb,
lib/coradoc/html/converters/underline.rb,
lib/coradoc/html/converters/unordered.rb,
lib/coradoc/html/converters/admonition.rb,
lib/coradoc/html/converters/line_break.rb,
lib/coradoc/html/converters/table_cell.rb,
lib/coradoc/html/theme/modern_renderer.rb,
lib/coradoc/html/converters/block_image.rb,
lib/coradoc/html/converters/source_code.rb,
lib/coradoc/html/converters/superscript.rb,
lib/coradoc/html/theme/classic_renderer.rb,
lib/coradoc/html/converters/bibliography.rb,
lib/coradoc/html/converters/comment_line.rb,
lib/coradoc/html/converters/inline_image.rb,
lib/coradoc/html/converters/text_element.rb,
lib/coradoc/html/transform/to_core_model.rb,
lib/coradoc/html/converters/comment_block.rb,
lib/coradoc/html/converters/reviewer_note.rb,
lib/coradoc/html/converters/strikethrough.rb,
lib/coradoc/html/transform/from_core_model.rb,
lib/coradoc/html/converters/cross_reference.rb,
lib/coradoc/html/theme/modern/css_generator.rb,
lib/coradoc/html/converters/reviewer_comment.rb,
lib/coradoc/html/converters/bibliography_entry.rb,
lib/coradoc/html/converters/attribute_reference.rb,
lib/coradoc/html/theme/modern/javascript_generator.rb,
lib/coradoc/html/converters/template_html_converter.rb,
lib/coradoc/html/theme/modern/vue_template_generator.rb,
lib/coradoc/html/theme/modern/tailwind_config_builder.rb,
lib/coradoc/html/theme/modern/components/ui_components.rb,
lib/coradoc/html/theme/modern/serializers/document_serializer.rb

Overview

Backward compatibility alias Some legacy code references Coradoc::Html::Input instead of Coradoc::Input::Html

Defined Under Namespace

Modules: Base, Config, Converters, ElementMapping, Entity, TemplateFilters, Theme, Transform Classes: ConverterBase, Renderer, Spa, Static, TemplateConfig, TemplateLocator

Constant Summary collapse

Input =
Coradoc::Input::Html
VERSION =
'1.1.7'
TemplateRenderer =

Backwards compatibility alias

Renderer

Class Method Summary collapse

Class Method Details

.available_templatesArray<Symbol>

List all available default templates

Returns:

  • (Array<Symbol>)

    List of template names



138
139
140
# File 'lib/coradoc/html/template_config.rb', line 138

def available_templates
  TemplateConfig.available_templates
end

.configurationTemplateConfig

Get the global configuration

Returns:



111
112
113
# File 'lib/coradoc/html/template_config.rb', line 111

def configuration
  @configuration ||= TemplateConfig.new
end

.configure {|TemplateConfig| ... } ⇒ void

This method returns an undefined value.

Configure the template system

Examples:

Coradoc::Html.configure do |config|
  config.template_dirs = ["/path/to/templates"]
end

Yields:



124
125
126
# File 'lib/coradoc/html/template_config.rb', line 124

def configure
  yield(configuration) if block_given?
end

.from_core_model(core_document) ⇒ Object

Transform CoreModel to HTML-ready structure

Parameters:

  • core_document (Coradoc::CoreModel::Base)

    CoreModel document

Returns:

  • (Object)

    HTML-ready structure



246
247
248
# File 'lib/coradoc/html.rb', line 246

def self.from_core_model(core_document)
  Transform::FromCoreModel.transform(core_document)
end

.from_file(filename, **options) ⇒ Object

Parse HTML file



144
145
146
147
# File 'lib/coradoc/html.rb', line 144

def self.from_file(filename, **options)
  content = File.read(filename)
  parse(content, **options)
end

.handles_model?(model) ⇒ Boolean

Check if this format can transform the given model to CoreModel

HTML produces CoreModel directly, so it only handles already-CoreModel objects.

Parameters:

  • model (Object)

    The model to check

Returns:

  • (Boolean)

    true if the model is already CoreModel



234
235
236
# File 'lib/coradoc/html.rb', line 234

def self.handles_model?(model)
  model.is_a?(Coradoc::CoreModel::Base)
end

.parse(html, options = {}) ⇒ Object

Parse HTML content and return CoreModel elements (may be an Array)



105
106
107
108
# File 'lib/coradoc/html.rb', line 105

def self.parse(html, options = {})
  # Input::Html is autoloaded via Coradoc::Input
  ::Coradoc::Html::Input.to_coradoc(html, options)
end

.parse_to_core(html, options = {}) ⇒ Coradoc::CoreModel::StructuralElement

Parse HTML content directly into a CoreModel document

Unlike #parse which returns an Array of CoreModel elements, this wraps the result into a top-level StructuralElement document suitable for use with Coradoc.serialize and other CoreModel pipelines.

Parameters:

  • html (String)

    HTML content

  • options (Hash) (defaults to: {})

    Parse options

Returns:

  • (Coradoc::CoreModel::StructuralElement)

    CoreModel document



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/coradoc/html.rb', line 119

def self.parse_to_core(html, options = {})
  elements = parse(html, options)

  return elements if elements.is_a?(Coradoc::CoreModel::Base)

  # Extract document title from the first heading element
  title = nil
  children = elements
  if elements.is_a?(Array) && !elements.empty?
    first = elements.first
    if first.is_a?(Coradoc::CoreModel::StructuralElement) &&
       first.section? && first.level == 1
      title = first.title
      children = first.children + elements[1..]
    end
  end

  Coradoc::CoreModel::StructuralElement.new(
    element_type: 'document',
    title: title,
    children: Array(children)
  )
end

.reset_configuration!void

This method returns an undefined value.

Reset configuration to defaults



131
132
133
# File 'lib/coradoc/html/template_config.rb', line 131

def reset_configuration!
  @configuration = nil
end

.serialize(document, options = {}) ⇒ String

Serialize CoreModel document to HTML

Uses the theme system to render HTML. Default theme is :classic.

Parameters:

  • document (Coradoc::CoreModel::Base)

    CoreModel document to serialize

  • options (Hash) (defaults to: {})

    Output options

Returns:

  • (String)

    HTML output



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/coradoc/html.rb', line 156

def self.serialize(document, options = {})
  # Validate input is CoreModel
  validate_core_model!(document)

  # Trigger theme autoloads to ensure renderers are registered
  Theme::ClassicRenderer if options[:theme].nil? || options[:theme] == :classic
  Theme::ModernRenderer if options[:theme] == :modern

  # Use theme registry to find and use the appropriate renderer
  theme = options[:theme] || :classic
  renderer_class = Theme::Registry.find(theme)
  renderer = renderer_class.new(document, options)
  renderer.render_html5
end

.serialize_as(document, format, options = {}) ⇒ String

Serialize CoreModel document to HTML with specified format

Parameters:

  • document (Coradoc::CoreModel::Base)

    CoreModel document to serialize

  • format (Symbol)

    Output format (:static, :spa, :classic)

  • options (Hash) (defaults to: {})

    Converter options

Returns:

  • (String)

    HTML output



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/coradoc/html.rb', line 205

def self.serialize_as(document, format, options = {})
  # Validate input is CoreModel
  validate_core_model!(document)

  case format.to_sym
  when :static, :html_static, :classic
    serialize_static(document, options)
  when :spa, :html_spa, :modern
    serialize_spa(document, options)
  else
    raise ArgumentError, "Unknown output format: #{format}. " \
      'Valid formats: :static, :spa'
  end
end

.serialize_spa(document, config = {}) ⇒ String

Serialize CoreModel document to SPA HTML

Uses the modern theme renderer for Vue.js + Tailwind output.

Parameters:

  • document (Coradoc::CoreModel::Base)

    CoreModel document to serialize

  • config (Hash, Spa::Configuration) (defaults to: {})

    SPA converter configuration

Returns:

  • (String)

    HTML output



192
193
194
195
196
197
# File 'lib/coradoc/html.rb', line 192

def self.serialize_spa(document, config = {})
  # Validate input is CoreModel
  validate_core_model!(document)

  Spa.convert(document, config)
end

.serialize_static(document, config = {}) ⇒ String

Serialize CoreModel document to static HTML

Uses the classic theme renderer for traditional HTML output.

Parameters:

  • document (Coradoc::CoreModel::Base)

    CoreModel document to serialize

  • config (Hash, Static::Configuration) (defaults to: {})

    Static converter configuration

Returns:

  • (String)

    HTML output



178
179
180
181
182
183
# File 'lib/coradoc/html.rb', line 178

def self.serialize_static(document, config = {})
  # Validate input is CoreModel
  validate_core_model!(document)

  Static.convert(document, config)
end

.template_path_for(name) ⇒ Pathname?

Get the path to a default template

Parameters:

  • name (Symbol, String)

    Template name

Returns:

  • (Pathname, nil)

    Path to template or nil



146
147
148
# File 'lib/coradoc/html/template_config.rb', line 146

def template_path_for(name)
  TemplateConfig.template_path_for(name)
end

.to_core(document) ⇒ Object



238
239
240
# File 'lib/coradoc/html.rb', line 238

def self.to_core(document)
  to_core_model(document)
end

.to_core_model(document) ⇒ Coradoc::CoreModel::Base

Transform HTML model to CoreModel

Parameters:

  • document (Object)

    HTML input model

Returns:

  • (Coradoc::CoreModel::Base)

    CoreModel document



224
225
226
# File 'lib/coradoc/html.rb', line 224

def self.to_core_model(document)
  Transform::ToCoreModel.transform(document)
end

.validate_core_model!(document) ⇒ Object

Validate that input is a CoreModel type

Parameters:

  • document (Object)

    Document to validate

Returns:

  • (Object)

    The validated document

Raises:

  • (ArgumentError)

    If document is not a CoreModel type



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/coradoc/html.rb', line 91

def self.validate_core_model!(document)
  return document if document.nil?

  unless document.is_a?(Coradoc::CoreModel::Base)
    raise ArgumentError,
          'coradoc-html only accepts CoreModel types. ' \
          "Got: #{document.class}. " \
          'Transform your document to CoreModel before passing to HTML conversion.'
  end

  document
end