Purpose
Complete reference for extending Ceradoc with custom elements, parsers, serializers, and schemas.
Extension Architecture
Ceradoc’s extension architecture allows you to:
-
Add custom block types
-
Add custom inline elements
-
Create custom serializers
-
Define custom schemas
-
Extend the parser
Element Registry
Custom Block Types
Block Class Definition
module Ceradoc
module Model
module Block
class CustomBlock < Core
attribute :custom_attr, :string
attribute :lines, :array, default: -> { [] }
end
end
end
endRequired Attributes:
-
delimiter_char(String) - Character for block delimiters -
delimiter_len(Integer) - Length of delimiter line
Custom Attributes:
Any attributes your block needs.
Block Serializer Definition
module Ceradoc
module Output
module Adoc
module Serializers
module Block
class CustomBlockSerializer < Core
def serialize_to_adoc
parts = []
parts << @model.gen_delimiter << "\n"
parts << @model.gen_title
parts << @model.gen_attributes
parts << @model.gen_lines
parts << @model.gen_delimiter << "\n"
parts.join
end
end
end
end
end
end
endRequired Methods:
-
serialize_to_adoc- Return AsciiDoc string representation
Helper Methods (inherited from Core):
-
gen_delimiter- Generate delimiter line -
gen_title- Generate title line -
gen_attributes- Generate attribute lines -
gen_lines- Generate content lines
Custom Inline Elements
Custom Serializers
Custom Schemas
Schema Class Definition
module Ceradoc
module Schema
class CustomSchema < Schema
def schema_name
:custom
end
def default_attributes
{
"doctype" => "custom",
"version" => "1.0"
}
end
def parsing_rules
{
custom_element: ->(node) { parse_custom(node) }
}
end
def serialization_rules
{
custom_element: ->(model) { serialize_custom(model) }
}
end
def validate_document(doc)
# Validation logic
end
end
end
endSchema Methods
Required:
-
schema_name- Return symbol identifying the schema
Optional:
-
default_attributes- Hash of default document attributes -
parsing_rules- Hash of parsing rule procs -
serialization_rules- Hash of serialization rule procs -
validate_document(doc)- Validate document structure -
pre_parse(input)- Transform input before parsing -
post_serialize(output)- Transform output after serialization
Parser Extensions
Parser Rule Definition
module Ceradoc
class Parser
class AsciiDoc
# Add custom rule
rule(:custom_block) do
# Parslet parsing expression
str('!!!').repeat(4) >> space?
end
end
end
endPlugin System
Creating Plugins
module CeradocPlugin
class MyCustomBlock < Ceradoc::Model::Block::Core
attribute :my_attr, :string
end
class MyCustomBlockSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
"!!!\n#{@model.my_attr}\n!!!\n"
end
end
end
# Register plugin
Ceradoc::Plugin.register do |plugin|
plugin.block :my_custom_block,
class: CeradocPlugin::MyCustomBlock,
serializer: CeradocPlugin::MyCustomBlockSerializer
endExtension Hooks
Before Parse Hook
Ceradoc.config.before_parse do |input, options|
# Transform input before parsing
input.gsub(/custom/, "replaced")
endAfter Parse Hook
Ceradoc.config.after_parse do |document, options|
# Transform document after parsing
document.header.title = document.header.title.upcase
endExtension Best Practices
1. Namespace Your Extensions
# Good
module MyProject
module CeradocExtensions
class CustomBlock < Ceradoc::Model::Block::Core
end
end
end
# Avoid
class CustomBlock < Ceradoc::Model::Block::Core
end2. Use Inheritance Properly
# Good: Inherit from appropriate base
class MyBlock < Ceradoc::Model::Block::Core
end
class MyInline < Ceradoc::Model::Inline::Base
end
# Avoid: Don't inherit from unrelated classes
class MyBlock < Ceradoc::Model::Inline::Base # Wrong!3. Register Serializers
# Always register after defining both class and serializer
class MyBlock < Ceradoc::Model::Block::Core
end
class MyBlockSerializer
def serialize_to_adoc
"..."
end
end
# Register after both are defined
Ceradoc::Output::Adoc::ElementRegistry.register(
MyBlock,
MyBlockSerializer
)Extension Distribution
Gem Structure
my-ceradoc-extension/
lib/
my-ceradoc-extension.rb
my-ceradoc-extension/
version.rb
custom_block.rb
custom_serializer.rb
spec/
spec_helper.rb
custom_block_spec.rb
my-ceradoc-extension.gemspec
README.mdEntry Point
# lib/my-ceradoc-extension.rb
require 'ceradoc'
require 'my-ceradoc-extension/version'
require 'my-ceradoc-extension/custom_block'
require 'my-ceradoc-extension/custom_serializer'
module MyCeradocExtension
class Railtie < Rails::Railtie
# Auto-register if Rails
end
end
# Register extension
Ceradoc::Output::Adoc::ElementRegistry.register(
MyCeradocExtension::CustomBlock,
MyCeradocExtension::CustomBlockSerializer
)Related Documentation
-
Custom Blocks Guide - Creating custom blocks
-
Custom Inline Guide - Creating inline elements
-
Schema System Guide - Creating schemas
-
Architecture - Extension architecture