Purpose
This page explains how Ceradoc serializes Document Models back to AsciiDoc text. You’ll learn about the serialization layer, how it works, and how to customize it for your needs.
Overview
All Model objects in Ceradoc can be serialized to AsciiDoc through the to_adoc method. The serialization layer converts the rich object model back into AsciiDoc text, enabling round-trip document processing.
Basic Serialization
Serializing Individual Elements
Any Model object can be serialized to AsciiDoc:
# Serialize a paragraph
paragraph.to_adoc
# Serialize a section
section.to_adoc
# Serialize a complete document
document.to_adocExample: Paragraph Serialization
paragraph = Ceradoc::Model::Paragraph.new
paragraph.content = [Ceradoc::Model::TextElement.new("Hello")]
paragraph.to_adoc # => "Hello\n"Example: Section Serialization
section = Ceradoc::Model::Section.new
section.title = Ceradoc::Model::Title.new
section.title.level_int = 2
section.title.content = ["Chapter 1"]
para = Ceradoc::Model::Paragraph.new
para.content = [Ceradoc::Model::TextElement.new("Section content")]
section.blocks = [para]
section.to_adoc
# => "== Chapter 1\n\nSection content\n"Serialization Methods
Model#to_adoc
Serialize any Model object to AsciiDoc.
paragraph.to_adoc
section.to_adoc
table.to_adoc
document.to_adocReturns: (String) AsciiDoc representation
Ceradoc::Output::Adoc.serialize
Explicit serialization through the Output module.
Ceradoc::Output::Adoc.serialize(paragraph)
Ceradoc::Output::Adoc.serialize(section)Ceradoc.convert
Convert with explicit format specification.
doc = Ceradoc.parse_file("input.adoc")
# Convert to AsciiDoc (same as to_adoc)
output = Ceradoc.convert(doc, to: :adoc)Parameters:
-
document(Ceradoc::Model::Base) - Document model to convert -
to(Symbol) - Target format (:adoc,:html) -
**options(Hash) - Conversion options
Returns: String output in specified format
Serialization Architecture
Element Registry
The ElementRegistry maps Model classes to their serializers:
Ceradoc::Output::Adoc::ElementRegistry.register(
Ceradoc::Model::Block::Custom,
Ceradoc::Output::Adoc::Serializers::Block::CustomSerializer
)Serializer Classes
Each Model type has a corresponding serializer:
-
DocumentSerializer- Serializes documents -
SectionSerializer- Serializes sections -
ParagraphSerializer- Serializes paragraphs -
ListSerializer- Serializes lists -
TableSerializer- Serializes tables -
InlineSerializer- Serializes inline elements
Serialization Examples
Serializing Lists
# Unordered list
list = Ceradoc::Model::List::Unordered.new
item1 = Ceradoc::Model::ListItem.new
item1.content = [Ceradoc::Model::TextElement.new("First item")]
list.items << item1
item2 = Ceradoc::Model::ListItem.new
item2.content = [Ceradoc::Model::TextElement.new("Second item")]
list.items << item2
puts list.to_adoc
# => * First item\n* Second item\nSerializing Tables
table = Ceradoc::Model::Table.new
# Add header row
header_row = Ceradoc::Model::TableRow.new
%w[Name Age City].each do |header_text|
cell = Ceradoc::Model::TableCell.new
cell.content = [Ceradoc::Model::TextElement.new(header_text)]
header_row.columns << cell
end
table.rows << header_row
# Add data row
data_row = Ceradoc::Model::TableRow.new
%w[Alice 25 NYC].each do |cell_text|
cell = Ceradoc::Model::TableCell.new
cell.content = [Ceradoc::Model::TextElement.new(cell_text)]
data_row.columns << cell
end
table.rows << data_row
puts table.to_adoc
# => |===\n|Name |Age |City\n\n|Alice |25 |NYC\n|===\n"Serializing Blocks
# Listing block
listing = Ceradoc::Model::Block::Listing.new
listing.lines = ["def hello", " puts 'Hello, World!'", "end"]
listing.attributes = Ceradoc::Model::AttributeList.new
listing.attributes.add_positional("ruby")
puts listing.to_adoc
# => [source,ruby]\n----\ndef hello\n puts 'Hello, World!'\nend\n----\n
# Quote block
quote = Ceradoc::Model::Block::Quote.new
quote.lines = ["This is a quote.", "It spans multiple lines."]
puts quote.to_adoc
# => ____\nThis is a quote.\nIt spans multiple lines.\n____\nSerializing Inline Elements
# Create paragraph with various inline elements
paragraph = Ceradoc::Model::Paragraph.new
paragraph.content = [
Ceradoc::Model::TextElement.new("Text with "),
Ceradoc::Model::Inline::Bold.new(content: "bold"),
Ceradoc::Model::TextElement.new(", "),
Ceradoc::Model::Inline::Italic.new(content: "italic"),
Ceradoc::Model::TextElement.new(", and "),
Ceradoc::Model::Inline::Monospace.new(content: "monospace"),
Ceradoc::Model::TextElement.new(" text."),
]
puts paragraph.to_adoc
# => Text with *bold*, _italic_, and `monospace` text.\n
# Link
link = Ceradoc::Model::Inline::Link.new
link.path = "https://example.com"
link.name = "Example Site"
puts link.to_adoc
# => https://example.com[Example Site]
# Anchor
anchor = Ceradoc::Model::Inline::Anchor.new
anchor.id = "section1"
puts anchor.to_adoc
# => [[section1]]Round-Trip Serialization
Serialization is designed for round-trip processing:
original = "== My Document\n\nThis is content."
# Parse to model
doc = Ceradoc.parse(original)
# Serialize back to AsciiDoc
regenerated = doc.to_adoc
# Parse again to verify semantic equivalence
doc2 = Ceradoc.parse(regenerated)
# The models should be semantically equivalent
# (formatting may differ, but meaning is preserved)Custom Serializers
To add a serializer for a custom element:
1. Create the Serializer Class
module Ceradoc
module Output
module Adoc
module Serializers
class CustomSerializer < Core
def serialize_to_adoc
parts = []
parts << @model.gen_delimiter << "\n"
parts << @model.gen_title
parts << @model.gen_attributes
parts << @model.gen_lines
parts << @model.gen_delimiter << "\n"
parts.join
end
end
end
end
end
endRelated Documentation
-
Architecture - Serialization layer overview
-
Document Model - Model class details
-
Serialization Tutorial - Hands-on examples
-
Output Formats - Format options