Purpose
This tutorial teaches you how to work with the Ceradoc Document Model. You’ll learn to navigate, query, and manipulate document structure programmatically.
Prerequisites
-
Completed Basic Parsing Tutorial or equivalent
-
Understanding of Ruby objects and methods
-
Familiarity with AsciiDoc structure
Tutorial Overview
You’ll learn to:
-
Access document structure
-
Work with paragraphs and inline elements
-
Navigate nested sections
-
Manipulate lists and tables
-
Work with document attributes
Example 1: Accessing Document Structure
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Main Title
== Section 1
Paragraph in section 1.
=== Subsection 1.1
Content in subsection.
== Section 2
Paragraph in section 2.
ASCIIDOC
doc = Ceradoc.parse(input)
puts "Document title: #{doc.header.title}"
puts "Top-level sections: #{doc.sections.map { |s| s.title&.content&.first || '(unnamed)' }.join(', ')}"
# Navigate to nested sections
section1 = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if section1
subsections = section1.sections.select { |s| s.is_a?(Ceradoc::Model::Section) }
puts "Section 1 subsections: #{subsections.map { |s| s.title.content.first }.join(', ')}"
endExample 2: Working with Paragraphs
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Paragraph Examples
First paragraph.
Second paragraph with *bold* text.
Third paragraph with a https://example.com[link].
ASCIIDOC
doc = Ceradoc.parse(input)
# Find a section with paragraphs
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if !section
# Try document-level paragraphs
paragraphs = doc.sections.select { |s| s.is_a?(Ceradoc::Model::Paragraph) }
else
paragraphs = section.paragraphs
end
paragraphs.each_with_index do |para, i|
puts "Paragraph #{i + 1}:"
para.content.each do |element|
case element
when Ceradoc::Model::TextElement
puts " Text: #{element.text}"
when Ceradoc::Model::Inline::Bold
puts " Bold: #{element.content}"
when Ceradoc::Model::Inline::Link
puts " Link: #{element.path} [#{element.name}]"
end
end
endExample 3: Working with Lists
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= List Examples
* Item 1
** Item 1.1
** Item 1.2
* Item 2
. Step 1
. Step 2
. Step 3
ASCIIDOC
doc = Ceradoc.parse(input)
# Find a section with lists
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if !section
# Try document-level lists
lists = doc.sections.select { |s| s.is_a?(Ceradoc::Model::List::Core) }
else
lists = section.blocks.select { |b| b.is_a?(Ceradoc::Model::List::Core) }
end
# Find unordered list
unordered_list = lists.find { |l| l.is_a?(Ceradoc::Model::List::Unordered) }
if unordered_list
puts "Unordered list has #{unordered_list.items.size} top-level items"
unordered_list.items.each do |item|
text = item.content.first&.text || "(empty)"
puts " Item: #{text}"
# Check for nested lists
if item.nested_list
puts " Has nested list with #{item.nested_list.items.size} items"
end
end
end
# Find ordered list
ordered_list = lists.find { |l| l.is_a?(Ceradoc::Model::List::Ordered) }
if ordered_list
puts "\nOrdered list has #{ordered_list.items.size} items"
endExample 4: Working with Tables
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Table Examples
|===
| Header 1 | Header 2 | Header 3
| Cell 1.1 | Cell 1.2 | Cell 1.3
| Cell 2.1 | Cell 2.2 | Cell 2.3
|===
ASCIIDOC
doc = Ceradoc.parse(input)
# Find a section with tables
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if !section
# Try document-level tables
table = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Table) }
else
table = section.blocks.find { |b| b.is_a?(Ceradoc::Model::Table) }
end
if table
puts "Table has #{table.rows.size} rows"
table.rows.each_with_index do |row, i|
cells_text = row.columns.map { |cell| cell.content.first&.text || '' }.join(" | ")
puts " Row #{i + 1}: #{cells_text}"
end
endExample 5: Working with Inline Elements
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Inline Elements
This paragraph has *bold*, _italic_, and `monospace` text.
It also has a https://example.com[link to example] and <<anchor,cross-reference>>.
[[anchor]]
This section has an anchor.
ASCIIDOC
doc = Ceradoc.parse(input)
# Find a paragraph with inline elements
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if !section
# Try document-level paragraphs
paragraph = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Paragraph) }
else
paragraph = section.paragraphs.first
end
if paragraph
puts "Inline element types in first paragraph:"
element_types = paragraph.content.map { |e| e.class.name }.uniq
element_types.each do |type|
puts " - #{type}"
end
endExample 6: Working with Attributes
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Document Title
:author: John Doe
:version: 1.0
== Section
Content here.
ASCIIDOC
doc = Ceradoc.parse(input)
puts "Document attributes:"
doc.document_attributes.data.each do |key, value|
puts " #{key}: #{value}"
endExample 7: Working with Blocks
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Block Examples
[source,ruby]
----
def example
puts "Hello"
end
----
____
This is a quote block.
It can span multiple lines.
____
NOTE: This is a note admonition.
ASCIIDOC
doc = Ceradoc.parse(input)
# Find section with blocks
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if section
puts "Block types in document:"
section.contents.each do |block|
puts " - #{block.class.name}"
if block.respond_to?(:lines)
puts " Lines: #{block.lines.size}"
end
if block.respond_to?(:content)
puts " Content: #{block.content}"
end
end
endSummary
In this tutorial you learned:
-
How to access document structure (header, sections)
-
How to work with paragraphs and content elements
-
How to navigate nested sections
-
How to manipulate lists and detect nesting
-
How to work with tables
-
How to identify inline element types
-
How to access document attributes
-
How to work with different block types
Exercises
Practice your skills:
-
Write a function that recursively prints the entire section hierarchy
-
Create a script that extracts all links from a document
-
Count the total number of list items in a document (including nested)
-
Write a function that finds all admonitions in a document
Next Steps
-
Serialization Tutorial - Learn to output AsciiDoc
-
Document Model - Model class details
-
Model Classes Reference - Complete catalog