Class: Coradoc::Html::ConverterBase Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/html/converter_base.rb

Overview

This class is abstract.

Subclass and implement #convert to create a custom converter

Abstract base class for HTML output converters

This class defines the interface that all HTML output converters must implement. It provides common functionality for document validation, configuration building, and HTML output generation.

Examples:

Creating a custom converter

class MyConverter < Coradoc::Html::ConverterBase
  def convert
    # Generate HTML from document
  end
end

Direct Known Subclasses

Spa, Static

Defined Under Namespace

Classes: UnsupportedDocumentError, ValidationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, config = {}) ⇒ ConverterBase

Initialize a new converter instance

Parameters:

  • document (Coradoc::CoreModel::StructuralElement)

    The document to convert

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

    Converter configuration

Raises:



33
34
35
36
# File 'lib/coradoc/html/converter_base.rb', line 33

def initialize(document, config = {})
  @document = validate_input(document)
  @config = build_config(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



26
27
28
# File 'lib/coradoc/html/converter_base.rb', line 26

def config
  @config
end

#documentObject (readonly)

Returns the value of attribute document.



26
27
28
# File 'lib/coradoc/html/converter_base.rb', line 26

def document
  @document
end

Class Method Details

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

Class method to convert a document

Parameters:

  • document (Coradoc::CoreModel::StructuralElement)

    The document to convert

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

    Converter configuration

Returns:

  • (String)

    HTML output



69
70
71
# File 'lib/coradoc/html/converter_base.rb', line 69

def self.convert(document, config = {})
  new(document, config).convert
end

.to_file(document, output_path, config = {}) ⇒ String

Class method to convert and write to file

Parameters:

  • document (Coradoc::CoreModel::StructuralElement)

    The document to convert

  • output_path (String)

    Path to write the output file

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

    Converter configuration

Returns:

  • (String)

    The output path



79
80
81
# File 'lib/coradoc/html/converter_base.rb', line 79

def self.to_file(document, output_path, config = {})
  new(document, config).to_file(output_path)
end

Instance Method Details

#convertString

This method is abstract.

Subclasses must implement this method

Convert the document to HTML

Returns:

  • (String)

    HTML output

Raises:

  • (NotImplementedError)

    if not implemented by subclass



43
44
45
46
# File 'lib/coradoc/html/converter_base.rb', line 43

def convert
  raise NotImplementedError,
        "#{self.class.name} must implement #convert method"
end

#converter_nameSymbol

Get the converter name

Returns:

  • (Symbol)

    Converter name (e.g., :static, :spa)



86
87
88
89
90
91
92
# File 'lib/coradoc/html/converter_base.rb', line 86

def converter_name
  @converter_name ||= self.class.name.split('::').last
                          .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
                          .gsub(/([a-z\d])([A-Z])/, '\1_\2')
                          .downcase
                          .to_sym
end

#to_file(output_path) ⇒ String

Convert and write to file

Parameters:

  • output_path (String)

    Path to write the output file

Returns:

  • (String)

    The output path



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/coradoc/html/converter_base.rb', line 52

def to_file(output_path)
  html = convert

  # Ensure parent directory exists
  output_dir = File.dirname(output_path)
  FileUtils.mkdir_p(output_dir) unless output_dir == '.'

  File.write(output_path, html)

  output_path
end