Task
Use Ceradoc’s schema system to handle specialized document types like ISO standards, technical specifications, or other structured documents.
Approach
The schema system allows you to:
-
Define custom parsing rules for specific document types
-
Specify custom serialization formats
-
Enforce document structure constraints
-
Handle type-specific attributes and metadata
Understanding Schemas
A schema in Ceradoc defines:
-
Parsing rules - How to parse specific document types
-
Serialization rules - How to serialize for specific output formats
-
Structure constraints - Required/optional elements
-
Attribute definitions - Document metadata
Using Built-in Schemas
Creating Custom Schemas
Step 1: Define the Schema Class
module Ceradoc
module Schema
class CustomSchema < Schema
def schema_name
:custom
end
def rules
# Define parsing and serialization rules
end
end
end
endStep 2: Define Parsing Rules
module Ceradoc
module Schema
class CustomSchema < Schema
def parsing_rules
{
front_matter: ->(node) { parse_front_matter(node) },
metadata: ->(node) { parse_metadata(node) }
}
end
private
def parse_front_matter(node)
# Custom front matter parsing logic
end
def parse_metadata(node)
# Custom metadata parsing logic
end
end
end
endStep 3: Define Serialization Rules
module Ceradoc
module Schema
class CustomSchema < Schema
def serialization_rules
{
front_matter: ->(doc) { serialize_front_matter(doc) },
metadata: ->(doc) { serialize_metadata(doc) }
}
end
private
def serialize_front_matter(doc)
# Custom front matter serialization logic
end
def serialize_metadata(doc)
# Custom metadata serialization logic
end
end
end
endSchema Attributes
Schemas can define custom document attributes:
module Ceradoc
module Schema
class TechSpecSchema < Schema
def default_attributes
{
"doctype" => "technical-specification",
"status" => "draft",
"version" => "1.0"
}
end
end
end
endThese attributes are automatically added to documents parsed with this schema.
Schema Constraints
Enforce document structure constraints:
module Ceradoc
module Schema
class StrictSchema < Schema
def required_sections
%w[introduction scope definitions references]
end
def validate_document(doc)
missing = required_sections - doc.sections.map { |s| s.title.content.first.to_s.downcase }
unless missing.empty?
raise "Missing required sections: #{missing.join(', ')}"
end
end
end
end
endComplete Example: Technical Report Schema
require 'ceradoc'
module Ceradoc
module Schema
class TechReportSchema < Schema
def schema_name
:tech_report
end
def default_attributes
{
"doctype" => "technical-report",
"org" => "Acme Corporation",
"confidentiality" => "public"
}
end
def required_sections
%w[abstract introduction methodology results conclusions]
end
def validate_document(doc)
missing = required_sections - doc.sections.map do |s|
s.title&.content&.first&.to_s&.downcase || ""
end
unless missing.empty?
warn "Warning: Missing recommended sections: #{missing.join(', ')}"
end
end
def pre_parse(input)
# Add custom front matter if missing
unless input.include?(':doctype:')
input = ":doctype: technical-report\n\n#{input}"
end
input
end
def post_serialize(output)
# Add custom footer
output + "\n\n----\nGenerated by Acme Tech Report Generator\n"
end
end
end
end
# Register the schema
Ceradoc::Schema.register(:tech_report, Ceradoc::Schema::TechReportSchema)
# Use the schema
doc = Ceradoc.parse_file("report.adoc", schema: :tech_report)
# Validate
doc.schema.validate_document(doc)
# Serialize with schema post-processing
output = doc.schema.post_serialize(doc.to_adoc)
puts outputSchema Use Cases
Standards Documents (ISO, IEC, IEEE)
doc = Ceradoc.parse_file("iso-12345.adoc", schema: :iso)-
Handles ISO front matter
-
Processes ISO metadata
-
Enforces ISO structure
Schema Architecture
Schema Base Class
module Ceradoc
module Schema
class Schema
attr_reader :name, :options
def initialize(options = {})
@options = options
end
def schema_name
raise NotImplementedError
end
def default_attributes
{}
end
def parsing_rules
{}
end
def serialization_rules
{}
end
def validate_document(doc)
# Override in subclass
end
end
end
endSchema File Location
Troubleshooting
Problem: Schema isn’t being applied.
Solution: Verify the schema is registered and the name matches exactly. Check Ceradoc::Schema.registry.keys.
Problem: Document validation fails.
Solution: Ensure all required sections are present with correct titles. Check case sensitivity in title matching.
Problem: Custom attributes aren’t showing up.
Solution: Verify default_attributes returns a hash. Check that attributes don’t conflict with document-level attributes.
Summary
In this guide you learned:
-
What schemas are and when to use them
-
How to use built-in schemas
-
How to create custom schemas
-
How to define parsing and serialization rules
-
How to validate documents against schemas
-
Common schema use cases
Related Documentation
-
Architecture - Schema layer in architecture
-
Extension Points - Extending Ceradoc
-
Configuration Guide - Configuration options
-
lib/ceradoc/schema.rb - Schema implementation