Purpose

This page explains the Transformation layer, which converts the Abstract Syntax Tree (AST) produced by the Parser into Ceradoc Model objects. Understanding this layer is important for extending Ceradoc with custom elements.

Overview

The Transformation layer is the bridge between the Parser and the Model. While the Parser produces an AST representing the syntax of AsciiDoc, the Transformer converts this into Model objects representing the semantics of the document.

Why a Separate Transformation Layer?

Separating parsing from transformation provides several benefits:

  • Clean separation - Parser rules focus on syntax, transformer on semantics

  • Testability - Each layer can be tested independently

  • Flexibility - Different parsers could produce the same models

  • Extensibility - Custom elements can be added without modifying parser

Document Resolution

When working with documents that have external references (includes, images, media), you can "freeze" the document to resolve all references.

The freeze Method

The freeze method creates a NEW document with resolved references:

doc = Ceradoc::AsciiDoc.parse(File.read("document.adoc"))

# Resolve includes only
frozen = doc.freeze(base_dir: "/path/to/docs", includes: true)

# Create self-contained document with embedded images
frozen = doc.freeze(
  base_dir: "/path/to/docs",
  includes: true,
  images: :embed,
  output_dir: "/output"
)

Resolution Strategies

For images and media references:

Strategy Description

:reference

Keep path as-is (default)

:copy

Copy file to output directory, update path

:embed

Embed as data URI (base64) for self-contained output

Resolution Infrastructure

The resolution infrastructure provides unified handling for different reference types:

  • Resolvable module - Interface for resolvable elements

  • Resolver class - Configurable resolution engine

  • IncludeResolver - Handles include directives

  • ImageResolver - Handles image references

  • MediaResolver - Handles video/audio references

Legacy Method

The expand_includes method is preserved for backward compatibility:

# Equivalent to freeze(includes: true, images: :reference, media: :reference)
expanded = doc.expand_includes("/path/to/docs")

The Transformation Process

AST Structure

The Parser produces an AST like:

{
  header: { title: "My Document", ... },
  sections: [
    { title: { level: 2, content: ["Section 1"] },
      blocks: [...] }
  ]
}

Model Transformation

The Transformer converts this AST node by node:

# AST: { title: { level: 2, content: ["Section 1"] } }
# Becomes:
Ceradoc::Model::Section.new(
  title: Ceradoc::Model::Title.new(level_int: 2, content: ["Section 1"])
)

Transformer Architecture

Main Transformer

Location: lib/ceradoc/transformer.rb

The main transformer coordinates the transformation process:

module Ceradoc
  class Transformer
    def transform(ast)
      # Transform document root
      transform_document(ast)
    end

    private

    def transform_document(ast)
      # Create Document model
      # Transform header
      # Transform sections
      # Return complete document
    end
  end
end

Transformers Router

Location: lib/ceradoc/transformer_router.rb

Routes specific AST node types to their corresponding transformers:

module Ceradoc
  class TransformerRouter
    TRANSFORMERS = {
      section: SectionTransformer,
      paragraph: ParagraphTransformer,
      list: ListTransformer,
      # ... more transformers
    }
  end
end

Individual Transformers

Each document type has a dedicated transformer:

  • SectionTransformer - Transforms section nodes

  • ParagraphTransformer - Transforms paragraphs

  • ListTransformer - Transforms all list types

  • BlockTransformer - Transforms delimited blocks

  • InlineTransformer - Transforms inline elements

Custom Transformers

To add a custom transformer for a new element:

1. Create the Transformer Class

module Ceradoc
  class Transformer
    class CustomBlockTransformer
      def transform(ast_node)
        Ceradoc::Model::Block::Custom.new(
          lines: ast_node[:lines],
          attributes: transform_attributes(ast_node[:attributes])
        )
      end

      private

      def transform_attributes(ast_attributes)
        # Convert AST attributes to AttributeList
      end
    end
  end
end

2. Register the Transformer

Ceradoc::TransformerRouter::TRANSFORMERS[:custom_block] =
  Ceradoc::Transformer::CustomBlockTransformer

3. Create a Corresponding Model Class

module Ceradoc
  module Model
    module Block
      class Custom < Core
        attribute :lines, :array, default: -> { [] }
        attribute :attributes, :AttributeList
      end
    end
  end
end

Transformer Registry

The transformer router maintains a registry of all transformers:

# Get transformer for a node type
transformer = Ceradoc::TransformerRouter.get_transformer(:section)

# Transform a node
model = transformer.transform(ast_node)

Transformation Rules

Preserving Semantics

The transformer preserves semantic meaning, not necessarily exact formatting:

  • AsciiDoc: `== Title ` (extra spaces)

  • Model: Title.new(content: ["Title"]) (normalized)

Handling Ambiguity

When AST structure is ambiguous, the transformer:

  • Infers type from context

  • Falls back to default behavior

  • Logs warnings for uncertain cases

Debugging Transformation

Inspect AST

To see what the parser produces:

require 'ceradoc/parser'

parser = Ceradoc::Parser::AsciiDoc.new
ast = parser.parse("= Title\n\nContent")

puts ast.inspect

Trace Transformation

Enable debug output to trace transformation:

Ceradoc::Transformer.debug = true
doc = Ceradoc.parse(input)

Common Transformation Issues

Missing Transformer

Error: No transformer found for node type: :custom

Solution: Register a transformer for the custom node type

Invalid AST Structure

Error: Invalid AST node structure for :section

Solution: Ensure parser produces correct AST structure

See Troubleshooting for more issues.


This site uses Just the Docs, a documentation theme for Jekyll.