Approach
To add a custom block type:
-
Create a block class inheriting from
Ceradoc::Model::Block::Core -
Define delimiter characters and length
-
Create a serializer class
-
Register the serializer in the ElementRegistry
Step-by-Step
Step 1: Create the Block Class
Define your custom block by inheriting from Block::Core:
module Ceradoc
module Model
module Block
class Callout < Core
attribute :delimiter_char, :string, default: -> { "!" }
attribute :delimiter_len, :integer, default: -> { 4 }
attribute :callout_id, :string
attribute :title, :string
end
end
end
endKey attributes:
-
delimiter_char- The character used for block delimiters -
delimiter_len- Length of the delimiter line -
Custom attributes for your block’s specific data
Step 2: Create the Serializer
Implement the serializer for your custom block:
module Ceradoc
module Output
module Adoc
module Serializers
module Block
class CalloutSerializer < Core
def serialize_to_adoc
parts = []
# Generate delimiter line
parts << @model.gen_delimiter << "\n"
# Generate title if present
parts << ".#{@model.title}\n" if @model.title
# Generate attributes
parts << @model.gen_attributes
# Generate content
parts << @model.gen_lines
# Generate closing delimiter
parts << @model.gen_delimiter << "\n"
parts.join
end
end
end
end
end
end
endKey methods:
-
gen_delimiter- Generates delimiter line fromdelimiter_charanddelimiter_len -
gen_title- Generates title line (e.g.,.Title) -
gen_attributes- Generates attribute lines -
gen_lines- Generates block content lines
Complete Example
Creating a Note Block
Here’s a complete example of a custom note block:
require 'ceradoc'
# 1. Define the block class
module Ceradoc
module Model
module Block
class NoteBlock < Core
attribute :delimiter_char, :string, default: -> { "!" }
attribute :delimiter_len, :integer, default: -> { 4 }
attribute :note_type, :string, default: -> { "note" }
attribute :title, :string
end
end
end
end
# 2. Define the serializer
module Ceradoc
module Output
module Adoc
module Serializers
module Block
class NoteBlockSerializer < Core
def serialize_to_adoc
parts = []
# Opening delimiter
parts << @model.gen_delimiter << "\n"
# Title if present
if @model.title
parts << ".#{@model.title}\n"
end
# Note type as attribute
parts << "[#{@model.note_type}]\n"
# Content
parts << @model.lines.join("\n")
# Closing delimiter
parts << "\n" << @model.gen_delimiter << "\n"
parts.join
end
end
end
end
end
end
end
# 3. Register the serializer
Ceradoc::Output::Adoc::ElementRegistry.register(
Ceradoc::Model::Block::NoteBlock,
Ceradoc::Output::Adoc::Serializers::Block::NoteBlockSerializer
)
# 4. Use the custom block
note = Ceradoc::Model::Block::NoteBlock.new
note.lines = ["This is an important note.", "Pay attention to it."]
note.title = "Important"
note.note_type = "warning"
puts note.to_adocCreating Different Block Types
Single-Line Delimiter Block
For blocks with single-character delimiters (like quotes):
class SimpleBlock < Ceradoc::Model::Block::Core
attribute :delimiter_char, :string, default: -> { '"' }
attribute :delimiter_len, :integer, default: -> { 1 }
endMulti-Line Delimiter Block
For blocks with multi-character delimiters (like code blocks):
class CodeBlock < Ceradoc::Model::Block::Core
attribute :delimiter_char, :string, default: -> { "-" }
attribute :delimiter_len, :integer, default: -> { 4 }
endCustom Attribute Block
For blocks with specific attributes:
class AttributedBlock < Ceradoc::Model::Block::Core
attribute :delimiter_char, :string, default: -> { "|" }
attribute :delimiter_len, :integer, default: -> { 4 }
attribute :custom_attr, :string
def gen_attributes
return "\n" unless custom_attr
"[custom=#{custom_attr}]\n"
end
endCommon Patterns
Caveats
Parser Integration
Creating a custom block class doesn’t automatically make it parseable. To enable parsing:
-
Extend the parser to recognize your delimiter pattern
-
Add transformer rules to create your block from the AST
-
Register the transformer
Serialization Order
The ElementRegistry processes serializers in LIFO order. If your custom block isn’t being serialized:
-
Ensure registration happens after loading all standard serializers
-
Check for conflicts with existing block types
-
Verify your serializer’s
serialize_to_adocmethod returns a string
Troubleshooting
Problem: My block isn’t being recognized during parsing.
Solution: Custom blocks require parser extensions. The block class alone only handles serialization.
Problem: Serialization produces empty output.
Solution: Verify your serializer’s serialize_to_adoc method returns a string. Check that all parts are being joined.
Problem: Attributes aren’t being serialized.
Solution: Ensure gen_attributes is called in your serializer and returns the correct format.
Summary
In this guide you learned:
-
How to create a custom block class
-
How to implement a serializer
-
How to register the serializer
-
Common block type patterns
-
Caveats and troubleshooting
Related Documentation
-
Custom Inline Guide - Create inline elements
-
Serialization Layer - Serializer architecture
-
Extension Points - Extension API
-
examples/custom_blocks.rb - Example code