Purpose
This tutorial teaches you how to parse AsciiDoc documents with Ceradoc. You’ll learn to parse strings, files, and access document structure.
Prerequisites
-
Ruby installed on your machine
-
Ceradoc gem installed (see Getting Started)
-
Basic familiarity with AsciiDoc syntax
Tutorial Overview
You’ll learn to:
-
Parse a simple AsciiDoc string
-
Parse an AsciiDoc file
-
Access document header information
-
Navigate sections
-
Work with paragraphs and blocks
-
Parse lists and inline formatting
Example 1: Parse a Simple Document
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Hello World
This is a paragraph.
== Section 1
This is content in section 1.
ASCIIDOC
doc = Ceradoc.parse(input)
puts "Document title: #{doc.header.title}"
puts "Number of sections: #{doc.sections.size}"
# Find the first actual Section
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if section
puts "First section title: #{section.title.content}"
endExample 2: Parse from File
The Code
require 'ceradoc'
# Create a temporary file
temp_file = "/tmp/ceradoc_example.adoc"
File.write(temp_file, <<~ASCIIDOC)
= Document Title
Author Name <author@example.com>
== Introduction
This is the introduction paragraph.
* List item 1
* List item 2
* List item 3
ASCIIDOC
doc = Ceradoc.parse_file(temp_file)
puts "Document title: #{doc.header.title}"
puts "Author: #{doc.header.author&.first&.name}"
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
if section
paragraphs = section.contents.select { |c| c.is_a?(Ceradoc::Model::Paragraph) }
lists = section.contents.select { |c| c.is_a?(Ceradoc::Model::List::Core) }
puts "Number of paragraphs: #{paragraphs.size}"
puts "Number of lists: #{lists.size}"
end
# Cleanup
File.delete(temp_file) if File.exist?(temp_file)Example 3: Parse with Inline Formatting
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Formatting Examples
This has *bold text* and _italic text_.
This has `monospace text` and a https://example.com[link].
ASCIIDOC
doc = Ceradoc.parse(input)
# Find a paragraph (may be at document level)
paragraph = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Paragraph) }
if !paragraph
# Try nested in first section
section = doc.sections.find { |s| s.is_a?(Ceradoc::Model::Section) }
paragraph = section&.paragraphs&.first
end
if paragraph
puts "Paragraph has #{paragraph.content.size} content elements"
paragraph.content.each do |element|
puts " - #{element.class.name}"
end
endExample 4: Parse Lists
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Lists
* Unordered item 1
** Nested item
* Unordered item 2
. Ordered item 1
. Ordered item 2
. Ordered item 3
[horizontal]
Term 1:: Definition 1
Term 2:: Definition 2
ASCIIDOC
doc = Ceradoc.parse(input)
# Find all list objects at document level
lists = doc.sections.select { |s| s.is_a?(Ceradoc::Model::List::Core) }
puts "Number of lists: #{lists.size}"
lists.each_with_index do |list, i|
puts " List #{i + 1}: #{list.class.name} with #{list.items.size} items"
endExample 5: Parse Blocks
The Code
require 'ceradoc'
input = <<~ASCIIDOC
= Blocks
[source,ruby]
----
def hello
puts "Hello, World!"
end
----
____
Quoted text block.
____
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 parse AsciiDoc strings with
Ceradoc.parse -
How to parse files with
Ceradoc.parse_file -
How to access document headers
-
How to navigate sections
-
How to work with paragraphs and content elements
-
How to parse lists, blocks, and inline formatting
Exercises
Practice your skills:
-
Parse a document with nested sections and print the section hierarchy
-
Create a document with a table and access the table cells
-
Parse a document with cross-references (
text) and access the link targets -
Write a script that counts all paragraphs in a document
Next Steps
-
Document Modeling Tutorial - Learn to manipulate the model
-
Parsing Layer - Deep dive into the parser
-
API Reference - Complete parsing API