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 |
|---|---|
| Keep path as-is (default) |
| Copy file to output directory, update path |
| Embed as data URI (base64) for self-contained output |
Resolution Infrastructure
The resolution infrastructure provides unified handling for different reference types:
-
Resolvablemodule - Interface for resolvable elements -
Resolverclass - Configurable resolution engine -
IncludeResolver- Handles include directives -
ImageResolver- Handles image references -
MediaResolver- Handles video/audio references
The Transformation Process
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
endTransformers 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
endIndividual 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
endTransformer 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
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.
Related Documentation
-
Architecture - Overall system architecture
-
Parsing Layer - How AST is produced
-
Document Model - Model object details
-
Custom Blocks Guide - Creating custom elements