This reference documents all Ruby methods available in Ceradoc’s public API.
Main Module Methods
Ceradoc.parse
Parse a document string into a model.
Ceradoc.parse(input, format: :asciidoc, **options) -> CoreModel::Base input |
|
format |
|
schema |
|
strict |
|
attributes |
|
Ceradoc::CoreModel::StructuralElement (document)
# Parse AsciiDoc
doc = Ceradoc.parse("= Title\n\nContent", format: :asciidoc)
# Parse Markdown
doc = Ceradoc.parse("# Title\n\nContent", format: :markdown)
# Parse HTML
doc = Ceradoc.parse("<h1>Title</h1><p>Content</p>", format: :html)
# With strict mode
begin
doc = Ceradoc.parse(input, format: :asciidoc, strict: true)
rescue Ceradoc::ParseError => e
puts "Error at line #{e.line}: #{e.message}"
endCeradoc.convert
Convert a document between formats.
Ceradoc.convert(input, from:, to:, **options) -> String input |
|
from |
|
to |
|
theme |
|
validate |
|
String - Converted document
# Markdown to HTML
html = Ceradoc.convert("# Title", from: :markdown, to: :html)
# AsciiDoc to Markdown
md = Ceradoc.convert("= Title\n\nContent", from: :asciidoc, to: :markdown)
# HTML to AsciiDoc
adoc = Ceradoc.convert("<h1>Title</h1>", from: :html, to: :asciidoc)
# With theme
html = Ceradoc.convert(adoc, from: :asciidoc, to: :html, theme: :modern)Ceradoc.serialize
Serialize a CoreModel to a format string.
Ceradoc.serialize(model, to:, **options) -> String model |
|
to |
|
indent |
|
line_width |
|
String - Serialized document
# Parse then serialize
doc = Ceradoc.parse(input, format: :asciidoc)
html = Ceradoc.serialize(doc, to: :html)
# With formatting options
adoc = Ceradoc.serialize(doc, to: :asciidoc, indent: 2, line_width: 80)Format Module Methods
Ceradoc::AsciiDoc.parse
Parse AsciiDoc to AsciiDoc model.
Ceradoc::AsciiDoc.parse(input, **options) -> AsciiDoc::Model::Documentrequire 'ceradoc/asciidoc'
doc = Ceradoc::AsciiDoc.parse("= Title\n\nParagraph")
doc.header.title # => "Title"Ceradoc::Markdown.parse
Parse Markdown to Markdown model.
Ceradoc::Markdown.parse(input, **options) -> Markdown::Model::Documentrequire 'ceradoc/markdown'
doc = Ceradoc::Markdown.parse("# Title\n\nParagraph")
doc.headings.first.content # => "Title"CoreModel Methods
StructuralElement
# Create a document
doc = Ceradoc::CoreModel::StructuralElement.new(
element_type: "document",
title: "My Document",
id: "my-doc",
children: []
)
# Create a section
section = Ceradoc::CoreModel::StructuralElement.new(
element_type: "section",
level: 1,
title: "Introduction",
children: []
)
# Access methods
doc.element_type # => "document"
doc.title # => "My Document"
doc.id # => "my-doc"
doc.children # => []Block
# Paragraph
para = Ceradoc::CoreModel::Block.new(
element_type: "paragraph",
content: "Text content"
)
# Code block
code = Ceradoc::CoreModel::Block.new(
element_type: "block",
delimiter_type: "----",
content: "def hello; end",
language: "ruby"
)
# Quote block
quote = Ceradoc::CoreModel::Block.new(
element_type: "block",
delimiter_type: "____",
content: "Quote text"
)ListBlock and ListItem
# Unordered list
list = Ceradoc::CoreModel::ListBlock.new(
marker_type: "unordered",
items: [
Ceradoc::CoreModel::ListItem.new(content: "Item 1", marker: "*"),
Ceradoc::CoreModel::ListItem.new(content: "Item 2", marker: "*")
]
)
# Ordered list
olist = Ceradoc::CoreModel::ListBlock.new(
marker_type: "ordered",
items: [
Ceradoc::CoreModel::ListItem.new(content: "First", marker: "."),
Ceradoc::CoreModel::ListItem.new(content: "Second", marker: ".")
]
)Table, TableRow, TableCell
# Create a table
table = Ceradoc::CoreModel::Table.new(
frame: "all",
grid: "all",
rows: [
Ceradoc::CoreModel::TableRow.new(
header: true,
cells: [
Ceradoc::CoreModel::TableCell.new(content: "Name", header: true),
Ceradoc::CoreModel::TableCell.new(content: "Value", header: true)
]
),
Ceradoc::CoreModel::TableRow.new(
cells: [
Ceradoc::CoreModel::TableCell.new(content: "Item"),
Ceradoc::CoreModel::TableCell.new(content: "100")
]
)
]
)
# Cell with formatting
cell = Ceradoc::CoreModel::TableCell.new(
content: "Centered",
alignment: "center",
colspan: 2
)InlineElement
# Bold
bold = Ceradoc::CoreModel::InlineElement.new(
format_type: "bold",
content: "Important text"
)
# Link
link = Ceradoc::CoreModel::InlineElement.new(
format_type: "link",
content: "Click here",
target: "https://example.com"
)
# Italic
italic = Ceradoc::CoreModel::InlineElement.new(
format_type: "italic",
content: "Emphasized text"
)Document Manipulator
DocumentManipulator
require 'ceradoc/document_manipulator'
# Create manipulator
manipulator = Ceradoc::DocumentManipulator.new(doc)
# Chain operations
result = manipulator
.transform_text(&:upcase)
.transform_headings(&:upcase)
.add_toc(levels: 3, position: :top)
.add_metadata("processed_at" => Time.now.iso8601)
.remove_elements(:comment_line)
.to_html
# Available methods
manipulator.transform_text(&:method) # Transform all text
manipulator.transform_headings(&:method) # Transform headings
manipulator.add_toc(options) # Add table of contents
manipulator.add_metadata(hash) # Add metadata
manipulator.remove_elements(type) # Remove element types
manipulator.to_html # Output as HTML
manipulator.to_markdown # Output as Markdown
manipulator.to_asciidoc # Output as AsciiDocQuery API
Query Methods
# Parse document
doc = Ceradoc.parse(input, format: :asciidoc)
# Find by element type
sections = doc.query('section')
paragraphs = doc.query('paragraph')
tables = doc.query('table')
# Find by ID
intro = doc.query('#intro').first
# Find by class/role
examples = doc.query('.example')
# Complex selectors
results = doc.query('section > paragraph:first-child')
# Query within element
section = sections.first
paras = doc.query_within(section, 'paragraph')
# Chain queries
doc.query('section')
.filter { |s| s.level == 1 }
.map { |s| s.title }Streaming API
Streaming Methods
require 'ceradoc/streaming'
# Stream parse large file
Ceradoc::Streaming.parse_large_file("large.adoc", format: :asciidoc,
chunk_size: 100) do |chunk|
chunk.each { |element| process_element(element) }
end
# Transform in chunks
results = Ceradoc::Streaming.transform_in_chunks(elements, chunk_size: 50) do |chunk|
chunk.map { |el| transform_element(el) }
end
# Incremental serialization
File.open("output.html", "w") do |file|
Ceradoc::Streaming.serialize_incremental(document, format: :html) do |chunk|
file.write(chunk)
end
end
# Memory-limited processing
progress = Ceradoc::Streaming.process_with_memory_limit(
"input.adoc", "output.html",
format: :asciidoc, output_format: :html,
max_memory: 50 * 1024 * 1024 # 50MB
)
puts progress.to_s # "100 processed (100.0%) at 10.0/sec"Validation API
Validation Methods
require 'ceradoc/validation'
# Define a schema
schema = Ceradoc::Validation::Schema.define do
required :title, type: String, min_length: 1
required :sections, type: Array, min_count: 1
optional :author, type: String
rule :check_references do |doc|
refs = doc.query('xref')
missing = refs.reject { |r| doc.resolve_reference(r) }
missing.map { |r| "Unresolved reference: #{r.target}" }
end
end
# Validate document
result = schema.validate(document)
if result.valid?
puts "Document is valid"
else
result.errors.each { |e| puts "#{e.path}: #{e.message}" }
endLazy Evaluation API
Lazy Methods
require 'ceradoc/lazy'
# Wrap document for lazy iteration
wrapper = Ceradoc::Lazy.wrap(document)
wrapper.each_section do |section|
process_section(section) # Processed on-demand
end
# Lazy transformation pipeline
result = Ceradoc::Lazy.transform(sections) do |p|
p.map { |s| transform_section(s) }
.select { |s| s.visible? }
.take(10)
end.to_a # Only evaluates when to_a is called
# Process in batches
wrapper.each_batch(10) do |batch|
batch.each { |section| process(section) }
end
# Lazy reference resolution
resolver = Ceradoc::Lazy.resolver(document, loader: ->(ref, _) {
load_include_file(ref)
})
content = resolver.resolve("include::chapter1.adoc[]")Error Handling
Error Classes
# Error hierarchy
Ceradoc::Error # Base error class
├── Ceradoc::ParseError # Parsing errors
├── Ceradoc::ValidationError # Validation errors
├── Ceradoc::TransformationError # Transform errors
├── Ceradoc::UnsupportedFormatError # Unknown format
└── Ceradoc::ConfigurationError # Config errors
# Handling errors
begin
doc = Ceradoc.parse(input, format: :asciidoc, strict: true)
rescue Ceradoc::ParseError => e
puts "Parse error at line #{e.line}, column #{e.column}"
puts e.message
rescue Ceradoc::ValidationError => e
puts "Validation failed: #{e.message}"
rescue Ceradoc::UnsupportedFormatError => e
puts "Unknown format: #{e.format}"
endConfiguration
Global Configuration
# Configure globally
Ceradoc.configure do |config|
config.default_format = :asciidoc
config.strict = false
config.warn_on_undefined = true
config.parser_options = { preserve_whitespace: true }
end
# Access configuration
Ceradoc.config.default_format # => :asciidoc
# Debug mode
Ceradoc.debug = true