Purpose

This tutorial teaches you how to serialize Ceradoc Document Models to AsciiDoc output. You’ll learn to convert model objects back to AsciiDoc text.

Prerequisites

Tutorial Overview

You’ll learn to:

  • Serialize basic documents

  • Serialize individual elements

  • Serialize lists, tables, and blocks

  • Serialize inline elements

  • Perform round-trip serialization

Example 1: Basic Serialization

The Code

require 'ceradoc'

input = <<~ASCIIDOC
  = Document Title

  == Section 1

  This is a paragraph.
ASCIIDOC

doc = Ceradoc.parse(input)
output = doc.to_adoc

puts "Serialized output:"
puts output

What It Does

  1. Parses an AsciiDoc document

  2. Serializes it back to AsciiDoc

  3. Prints the result

Expected Output

Serialized output:
= Document Title

== Section 1

This is a paragraph

Example 2: Serialize Individual Elements

The Code

require 'ceradoc'

# Serialize a paragraph
paragraph = Ceradoc::Model::Paragraph.new
paragraph.content = [
  Ceradoc::Model::TextElement.new("This is a paragraph with "),
  Ceradoc::Model::Inline::Bold.new(content: "bold text"),
]
puts "Paragraph:"
puts paragraph.to_adoc
puts

# Serialize a section
section = Ceradoc::Model::Section.new
section.title = Ceradoc::Model::Title.new
section.title.level_int = 2
section.title.content = ["Custom Section"]
para = Ceradoc::Model::Paragraph.new
para.content = [Ceradoc::Model::TextElement.new("Section content")]
section.blocks = [para]
puts "Section:"
puts section.to_adoc

What It Does

  1. Creates a paragraph model with inline elements

  2. Serializes the paragraph

  3. Creates a section model with content

  4. Serializes the section

Expected Output

Paragraph:
This is a paragraph with *bold text*

Section:
== Custom Section

Section content

Example 3: Serialize Lists

The Code

require 'ceradoc'

# 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 "Unordered list:"
puts list.to_adoc
puts

# Ordered list
ordered = Ceradoc::Model::List::Ordered.new
item = Ceradoc::Model::ListItem.new
item.content = [Ceradoc::Model::TextElement.new("Step one")]
ordered.items << item

item = Ceradoc::Model::ListItem.new
item.content = [Ceradoc::Model::TextElement.new("Step two")]
ordered.items << item

puts "Ordered list:"
puts ordered.to_adoc

What It Does

  1. Creates an unordered list model

  2. Serializes the unordered list

  3. Creates an ordered list model

  4. Serializes the ordered list

Expected Output

Unordered list:
* First item
* Second item

Ordered list:
. Step one
. Step two

Example 4: Serialize Tables

The Code

require 'ceradoc'

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:"
puts table.to_adoc

What It Does

  1. Creates a table model

  2. Adds a header row with cells

  3. Adds a data row with cells

  4. Serializes the table

Expected Output

Table:
|===
|Name |Age |City

|Alice |25 |NYC
|===

Example 5: Serialize Blocks

The Code

require 'ceradoc'

# 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 block:"
puts listing.to_adoc
puts

# Quote block
quote = Ceradoc::Model::Block::Quote.new
quote.lines = ["This is a quote.", "It spans multiple lines."]
puts "Quote block:"
puts quote.to_adoc

What It Does

  1. Creates a listing block with attributes

  2. Serializes the listing block

  3. Creates a quote block

  4. Serializes the quote block

Expected Output

Listing block:
[source,ruby]

def hello puts 'Hello, World!' end

Quote block:
____
This is a quote.
It spans multiple lines.
____

Example 6: Serialize Inline Elements

The Code

require 'ceradoc'

# 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 with inline elements:"
puts paragraph.to_adoc
puts

# Link
link = Ceradoc::Model::Inline::Link.new
link.path = "https://example.com"
link.name = "Example Site"
puts "Link:"
puts "#{link.to_adoc}\n"
puts

# Anchor
anchor = Ceradoc::Model::Inline::Anchor.new
anchor.id = "section1"
puts "Anchor:"
puts "#{anchor.to_adoc}\n"

What It Does

  1. Creates a paragraph with multiple inline element types

  2. Serializes each inline element type

  3. Creates and serializes a link

  4. Creates and serializes an anchor

Expected Output

Paragraph with inline elements:
Text with *bold*, _italic_, and `monospace` text.

Link:
https://example.com[Example Site]

Anchor:
[[section1]]

Example 7: Round-Trip Preservation

The Code

require 'ceradoc'

original = <<~ASCIIDOC
  = Document Title

  == Section One

  This is a paragraph with *bold* and _italic_ text.

  * List item 1
  * List item 2
ASCIIDOC

# Parse
doc1 = Ceradoc.parse(original)

# Serialize
serialized = doc1.to_adoc

# Parse again
doc2 = Ceradoc.parse(serialized)

puts "Original document title: #{doc1.header.title}"
puts "Re-parsed document title: #{doc2.header.title}"

# Both should have the same structure
puts "Original sections: #{doc1.sections.size}"
puts "Re-parsed sections: #{doc2.sections.size}"

puts "\nSerialized output:"
puts serialized

What It Does

  1. Parses an original document

  2. Serializes it to AsciiDoc

  3. Parses the serialized output again

  4. Compares the two models for semantic equivalence

Expected Output

Original document title: Document Title
Re-parsed document title: Document Title
Original sections: 1
Re-parsed sections: 1

Serialized output:
= Document Title

== Section One

This is a paragraph with *bold* and _italic_ text.

* List item 1
* List item 2

Example 8: Using Ceradoc.convert

The Code

require 'ceradoc'

doc = Ceradoc.parse("= Test\n\nContent")

# Convert to AsciiDoc (same as to_adoc)
output = Ceradoc.convert(doc, to: :adoc)
puts "Converted output:"
puts output

What It Does

  1. Parses a document

  2. Uses the convert method with explicit format

  3. Prints the result

Expected Output

Converted output:
= Test

Content

Summary

In this tutorial you learned:

  • How to serialize complete documents

  • How to serialize individual elements

  • How to serialize lists, tables, and blocks

  • How to serialize inline elements

  • How round-trip serialization preserves semantics

  • How to use the convert method

Exercises

Practice your skills:

  1. Create a document model programmatically and serialize it

  2. Write a function that adds a section to an existing document

  3. Create a custom block type and serialize it

  4. Parse a complex document, modify it, and serialize the changes

Next Steps

Source Code

The source code for these examples is available at:


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