Supported Formats
AsciiDOC (:adoc)
Default output format. Serializes Document Model to AsciiDoc text.
output = Ceradoc.convert(doc, to: :adoc)
# or
output = doc.to_adocFormat Options:
-
indent(Integer) - Output indentation level (default:0) -
line_width(Integer, nil) - Maximum line width,nilfor no limit -
sort_attributes(Boolean) - Sort attributes alphabetically -
preserve(Boolean) - Preserve original formatting when possible
Examples:
# Basic output
output = doc.to_adoc
# With formatting
output = doc.to_adoc(indent: 2, line_width: 80)
# With sorted attributes
output = doc.to_adoc(sort_attributes: true)Output Characteristics:
-
Preserves semantic meaning
-
Normalizes whitespace
-
Reorders attributes alphabetically (if
sort_attributes: true) -
Uses standard AsciiDoc syntax
HTML (:html)
Convert Document Model to HTML (experimental).
output = Ceradoc.convert(doc, to: :html)Format Options:
-
pretty(Boolean) - Pretty-print HTML with indentation -
include_styles(Boolean) - Include default CSS styles -
header_footer(Boolean) - Include HTML header and footer
Examples:
# Basic HTML
html = Ceradoc.convert(doc, to: :html)
# Pretty-printed HTML
html = Ceradoc.convert(doc, to: :html, pretty: true)
# With styles and structure
html = Ceradoc.convert(doc, to: :html,
include_styles: true,
header_footer: true)HTML Mapping:
| AsciiDoc Element | HTML Element | |-----------------|--------------| | Document | <html><body>…</body></html> | | Section (level 1) | <h1> | | Section (level 2) | <h2> | | Paragraph | <p> | | Bold | <strong> | | Italic | <em> | | Monospace | <code> | | Link | <a> | | Unordered List | <ul><li> | | Ordered List | <ol><li> | | Table | <table> |
DOCX (:docx)
Parse Microsoft Word DOCX files to CoreModel (parse only, no serialization).
Requires the ceradoc-docx and uniword gems.
require 'ceradoc/docx'
# Parse DOCX to CoreModel
core = Ceradoc.parse("input.docx", format: :docx)
# Convert DOCX to AsciiDoc
adoc = Ceradoc.convert("input.docx", from: :docx, to: :asciidoc)
# Convert DOCX to Markdown
md = Ceradoc.convert("input.docx", from: :docx, to: :markdown)Supported input types:
-
File path (String)
-
IO / StringIO
-
Uniword::Wordprocessingml::DocumentRoot(pre-parsed OOXML)
DOCX Element Mapping:
| OOXML Element | CoreModel Target | |---------------|------------------| | Heading styles | StructuralElement (section) | | Numbered paragraphs | ListBlock + ListItem | | Quote style | Block (quote) | | Code style | Block (source/listing) | | Plain paragraphs | Block (paragraph) | | Bold runs | InlineElement (bold) | | Italic runs | InlineElement (italic) | | Hyperlinks | InlineElement (link) | | Tables | Table | | Drawings | Image | | Math (OMML) | Block/InlineElement (stem) | | Footnotes | FootnoteReference |
Limitations:
-
Parse only — DOCX serialization is not yet implemented
-
Print-layout properties (page size, margins, frame styles) are discarded
-
Headers/footers text content is not yet extracted
Conversion API
Ceradoc.convert
Primary conversion method.
signature: convert(document, to:, **options) -> StringParameters:
-
document(Model::Base, String) - Document or file path to convert -
to(Symbol) - Target format (:adoc,:html,:markdown,:asciidoc) -
format_options(Hash) - Format-specific options
Returns: String in target format
Raises: Ceradoc::SerializationError if conversion fails
Format-Specific Options
AsciiDoc Options
# Indentation
output = doc.to_adoc(indent: 2)
# Result: Lines are indented by 2 spaces per level
# Line width
output = doc.to_adoc(line_width: 80)
# Result: Lines are wrapped to 80 characters where possible
# Attribute sorting
output = doc.to_adoc(sort_attributes: true)
# Result: [a=1 b=2] instead of [b=2 a=1]
# Preserve formatting
output = doc.to_adoc(preserve: true)
# Result: Attempts to preserve original formattingHTML Options
# Pretty printing
html = Ceradoc.convert(doc, to: :html, pretty: true)
# Result: Indented HTML with newlines
# Include styles
html = Ceradoc.convert(doc, to: :html, include_styles: true)
# Result: HTML includes embedded CSS
# Header and footer
html = Ceradoc.convert(doc, to: :html, header_footer: true)
# Result: Complete HTML document structure
# Custom CSS
html = Ceradoc.convert(doc, to: :html, custom_css: "body { font: sans-serif; }")
# Result: HTML with custom stylesOutput Streaming
For large documents, stream output to avoid memory issues:
# Stream to file
File.open("output.adoc", "w") do |file|
file.write(doc.to_adoc)
end
# Stream line by line (if supported)
doc.to_adoc.each_line do |line|
puts line
endCustom Output Formats
Create custom output formats by implementing a converter:
module Ceradoc
module Output
class Markdown
def self.convert(document)
# Custom conversion logic
"# #{document.header.title}\n\n"
end
end
end
end
# Register the converter
Ceradoc::Output.register(:markdown, Ceradoc::Output::Markdown)
# Use it
output = Ceradoc.convert(doc, to: :markdown)Output Quality
Output Comparison
Given this input:
= Document Title
== Section One
Paragraph with *bold* text.
* Item 1
* Item 2Format Selection Guide
| Use Case | Recommended Format | |----------|-------------------| | Store for later editing | AsciiDoc (:adoc) | | Display in web browser | HTML (:html) | | Version control | AsciiDoc (:adoc) | | Email/Markdown systems | AsciiDoc (:adoc) + external converter | | Print/PDF | AsciiDoc (:adoc) + AsciiDoctor PDF | | Convert Word documents | DOCX (:docx) → AsciiDoc or Markdown |
Related Documentation
-
Serialization Layer - Serialization architecture
-
Custom Blocks Guide - Custom serialization
-
Serialization Tutorial - Serialization examples