Purpose
This tutorial teaches you about round-trip processing with Ceradoc - parsing a document, serializing it, and parsing again while preserving semantic meaning.
Prerequisites
-
Completed Basic Parsing Tutorial
-
Completed Serialization Tutorial
-
Understanding of semantic vs. syntactic preservation
Tutorial Overview
You’ll learn to:
-
Understand what round-tripping means
-
Preserve semantic meaning through parse/serialize cycles
-
Handle common round-trip challenges
-
Verify semantic equivalence
What is Round-tripping?
Round-tripping is the process of:
-
Parse - Convert AsciiDoc text to a Document Model
-
Serialize - Convert the Document Model back to AsciiDoc text
-
Re-parse - Parse the serialized output again
The goal is that the semantic meaning remains the same, even if the exact formatting differs.
Example 1: Basic Round-trip
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)
# Compare
puts "Original title: #{doc1.header.title}"
puts "Re-parsed title: #{doc2.header.title}"
puts "\nOriginal sections: #{doc1.sections.size}"
puts "Re-parsed sections: #{doc2.sections.size}"
puts "\nSerialized output:"
puts serializedExample 2: Preserving Document Attributes
The Code
require 'ceradoc'
original = <<~ASCIIDOC
= Document Title
:author: John Doe
:version: 1.0
Content here.
ASCIIDOC
doc1 = Ceradoc.parse(original)
serialized = doc1.to_adoc
doc2 = Ceradoc.parse(serialized)
puts "Original attributes:"
doc1.document_attributes.data.each { |k, v| puts " #{k}: #{v}" }
puts "\nRe-parsed attributes:"
doc2.document_attributes.data.each { |k, v| puts " #{k}: #{v}" }
puts "\nAttributes match: #{doc1.document_attributes.data == doc2.document_attributes.data}"Example 3: Preserving Nested Structure
The Code
require 'ceradoc'
original = <<~ASCIIDOC
= Main
== Section 1
=== Subsection 1.1
Content in 1.1
=== Subsection 1.2
Content in 1.2
== Section 2
Content in section 2.
ASCIIDOC
doc1 = Ceradoc.parse(original)
serialized = doc1.to_adoc
doc2 = Ceradoc.parse(serialized)
def count_sections(doc)
count = 0
doc.sections.each do |s|
next unless s.is_a?(Ceradoc::Model::Section)
count += 1
count += count_sections(s)
end
count
end
puts "Original section count: #{count_sections(doc1)}"
puts "Re-parsed section count: #{count_sections(doc2)}"Example 4: Preserving Block Types
The Code
require 'ceradoc'
original = <<~ASCIIDOC
= Blocks
[source,ruby]
----
def hello
puts "world"
end
----
____
Quote block.
____
NOTE: Admonition block.
ASCIIDOC
doc1 = Ceradoc.parse(original)
serialized = doc1.to_adoc
doc2 = Ceradoc.parse(serialized)
def block_types(doc)
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
return [] unless section
section.contents.map { |b| b.class.name }
end
puts "Original block types: #{block_types(doc1).join(', ')}"
puts "Re-parsed block types: #{block_types(doc2).join(', ')}"Round-trip Considerations
Verifying Semantic Equivalence
To verify round-trip success:
def equivalent_models?(doc1, doc2)
# Compare structure
return false if doc1.header.title != doc2.header.title
return false if doc1.sections.size != doc2.sections.size
# Compare attributes
return false if doc1.document_attributes.data != doc2.document_attributes.data
true
rescue
false
end
original = "= Title\n\nContent"
doc1 = Ceradoc.parse(original)
serialized = doc1.to_adoc
doc2 = Ceradoc.parse(serialized)
puts "Equivalent: #{equivalent_models?(doc1, doc2)}"Summary
In this tutorial you learned:
-
What round-tripping means
-
How semantic meaning is preserved
-
What may change during round-trip
-
How to verify semantic equivalence
Exercises
Practice your skills:
-
Round-trip a complex document and inspect the differences
-
Write a function that compares two document models for equivalence
-
Test round-tripping with tables and nested lists
-
Identify features that don’t round-trip successfully
Next Steps
-
HTML Conversion Tutorial - Converting other formats
-
Architecture - Understanding the transformation layer
-
Troubleshooting - Common issues