Purpose

This page explains how Ceradoc parses AsciiDoc documents into a structured document model. You’ll learn about the parser layer, what it produces, and how to work with parsed documents.

Overview

Ceradoc parses AsciiDoc documents into a rich object model using the Parslet parsing expression grammar library. The parser converts text into an Abstract Syntax Tree (AST), which is then transformed into Model objects.

Parsing Methods

Ceradoc.parse(input, **options)

Parse an AsciiDoc string into a document model.

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

Parameters:

  • input (String) - AsciiDoc string to parse

  • **options (Hash) - Optional parsing parameters

Returns: Ceradoc::Model::Document

Ceradoc.parse_file(filename, **options)

Parse an AsciiDoc file into a document model.

doc = Ceradoc.parse_file("document.adoc")

Parameters:

  • filename (String) - Path to AsciiDoc file

  • **options (Hash) - Optional parsing parameters

Returns: Ceradoc::Model::Document

The Parsing Process

Stage 1: Lexical Analysis

The AsciiDoc text is tokenized into meaningful elements:

  • Headings (== Title)

  • Delimiters (----, __)

  • List markers (*, .)

  • Inline formatting (bold, italic)

  • Attributes (:name: value)

Stage 2: AST Generation

Parslet constructs an Abstract Syntax Tree representing the syntactic structure of the document. The AST captures:

  • Hierarchical heading structure

  • Block boundaries and types

  • List nesting

  • Inline element boundaries

Stage 3: Transformation

The AST is transformed into Model objects by the Transformer layer. This is where syntax becomes semantics:

  • AST heading node → Ceradoc::Model::Title

  • AST paragraph node → Ceradoc::Model::Paragraph

  • AST list node → Ceradoc::Model::List::Unordered

Working with Parsed Documents

Accessing the Header

doc = Ceradoc.parse("= Document Title\n\nContent")

doc.header.title              # => "Document Title"
doc.header.author             # => Array of Author objects
doc.header.revision           # => Revision object

Accessing Sections

doc = Ceradoc.parse("== Section 1\n\nContent\n\n== Section 2\n\nContent")

# All top-level sections
doc.sections.each do |section|
  puts section.title.content
end

# First section
section = doc.sections.first
puts section.title.level_int  # => 2
puts section.paragraphs       # => Array of paragraphs

# Nested sections
section.sections.each do |subsection|
  puts subsection.title.content
end

Accessing Document Attributes

doc = Ceradoc.parse(":author: John Doe\n\n== Title\n\nContent")

# All attributes
doc.document_attributes.data.each do |key, value|
  puts "#{key}: #{value}"
end

# Specific attribute
doc.document_attributes.data["author"]  # => "John Doe"

Accessing Blocks

doc = Ceradoc.parse("== Section\n\nParagraph 1.\n\nParagraph 2.")

section = doc.sections.first

# Paragraphs
section.paragraphs.each do |para|
  puts para.content
end

# All blocks (paragraphs, lists, tables, etc.)
section.blocks.each do |block|
  puts block.class.name
end

Accessing Inline Elements

doc = Ceradoc.parse("= Title\n\nThis has *bold* and _italic_ text.")

paragraph = doc.sections.first.paragraphs.first

paragraph.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::Italic
    puts "Italic: #{element.content}"
  end
end

What Gets Parsed

Ceradoc parses the following AsciiDoc elements:

  • Structure: Document title, sections, headers

  • Blocks: Paragraphs, delimited blocks, lists, tables

  • Inline: Bold, italic, monospace, links, anchors

  • Attributes: Document attributes, block attributes

  • Metadata: Author, revision info

Parser Limitations

The parser focuses on semantic AsciiDoc elements. Some advanced features may not be fully supported:

  • Complex include directives

  • All macro types

  • Conditional processing

See Troubleshooting for known limitations.


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