Approach
To add a custom inline element:
-
Create an inline class inheriting from
Ceradoc::Model::Inline::Base -
Define the element’s attributes
-
Create a serializer class
-
Register the serializer in the ElementRegistry
Step-by-Step
Step 1: Create the Inline Class
Define your custom inline element by inheriting from Inline::Base:
module Ceradoc
module Model
module Inline
class Highlight < Base
attribute :text, :string
attribute :color, :string, default: -> { "yellow" }
end
end
end
endKey attributes:
-
textorcontent- The text content -
Custom attributes for your element’s specific properties
Step 2: Create the Serializer
Implement the serializer for your custom inline element:
module Ceradoc
module Output
module Adoc
module Serializers
module Inline
class HighlightSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
# Generate AsciiDoc representation
"##{@model.text}#"
end
end
end
end
end
end
endKey considerations:
-
Choose AsciiDoc syntax that won’t conflict with existing markup
-
Return a string from
serialize_to_adoc -
Handle all attributes your element defines
Complete Example
Creating a Highlight Inline Element
Here’s a complete example of a custom highlight element:
require 'ceradoc'
# 1. Define the inline class
module Ceradoc
module Model
module Inline
class Highlight < Base
attribute :text, :string
attribute :marker, :string, default: -> { "#" }
end
end
end
end
# 2. Define the serializer
module Ceradoc
module Output
module Adoc
module Serializers
module Inline
class HighlightSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
"#{@model.marker}#{@model.text}#{@model.marker}"
end
end
end
end
end
end
end
# 3. Register the serializer
Ceradoc::Output::Adoc::ElementRegistry.register(
Ceradoc::Model::Inline::Highlight,
Ceradoc::Output::Adoc::Serializers::Inline::HighlightSerializer
)
# 4. Use the custom inline element
highlight = Ceradoc::Model::Inline::Highlight.new
highlight.text = "important text"
highlight.marker = "#"
puts highlight.to_adocCreating Different Inline Types
Simple Text Wrapper
For elements that wrap text with delimiters:
class Underline < Ceradoc::Model::Inline::Base
attribute :text, :string
attribute :delimiter, :string, default: -> { "_" }
end
class UnderlineSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
"#{@model.delimiter}#{@model.text}#{@model.delimiter}"
end
endAttribute-Based Inline
For elements with attributes:
class StyledText < Ceradoc::Model::Inline::Base
attribute :text, :string
attribute :style, :string
end
class StyledTextSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
"[#{@model.style}]##{@model.text}#"
end
endLink-like Inline
For elements similar to links:
class Reference < Ceradoc::Model::Inline::Base
attribute :target, :string
attribute :text, :string
end
class ReferenceSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
if @model.text
"#{@model.target}[#{@model.text}]"
else
@model.target
end
end
endUsing Custom Inline Elements
Common Patterns
Conditional Delimiters
Choose delimiters based on attributes:
class FlexibleHighlight < Ceradoc::Model::Inline::Base
attribute :text, :string
attribute :level, :integer, default: -> { 1 }
end
class FlexibleHighlightSerializer
def initialize(model)
@model = model
end
def delimiter
case @model.level
when 1 then "#"
when 2 then "##"
else "###"
end
end
def serialize_to_adoc
"#{delimiter}#{@model.text}#{delimiter}"
end
endEscaped Content
Handle special characters properly:
class CodeInline < Ceradoc::Model::Inline::Base
attribute :text, :string
def escaped_text
@text.gsub('\\', '\\\\\\\\')
end
end
class CodeInlineSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
"`#{@model.escaped_text}`"
end
endCaveats
Parser Integration
Like custom blocks, custom inline elements require parser extensions to be recognized during parsing. The class alone handles serialization.
Complete Paragraph Example
require 'ceradoc'
# Define custom inline element
module Ceradoc
module Model
module Inline
class Mark < Base
attribute :text, :string
attribute :color, :string, default: -> { "yellow" }
end
end
end
end
# Define serializer
module Ceradoc
module Output
module Adoc
module Serializers
module Inline
class MarkSerializer
def initialize(model)
@model = model
end
def serialize_to_adoc
"##{@model.text}#"
end
end
end
end
end
end
end
# Register
Ceradoc::Output::Adoc::ElementRegistry.register(
Ceradoc::Model::Inline::Mark,
Ceradoc::Output::Adoc::Serializers::Inline::MarkSerializer
)
# Use in paragraph
paragraph = Ceradoc::Model::Paragraph.new
paragraph.content = [
Ceradoc::Model::TextElement.new("This is "),
Ceradoc::Model::Inline::Mark.new(text: "marked"),
Ceradoc::Model::TextElement.new(" text with "),
Ceradoc::Model::Inline::Bold.new(content: "bold"),
Ceradoc::Model::TextElement.new(" formatting.")
]
puts paragraph.to_adocTroubleshooting
Problem: My inline element isn’t being serialized.
Solution: Verify the serializer is registered correctly and implements serialize_to_adoc.
Problem: Delimiters conflict with existing markup.
Solution: Choose unique delimiter characters or sequences. Consider using double characters like ##.
Problem: Special characters aren’t rendering correctly.
Solution: Properly escape backslashes and special characters in your serializer.
Summary
In this guide you learned:
-
How to create a custom inline class
-
How to implement an inline serializer
-
How to register the serializer
-
Common inline element patterns
-
Using custom inline in paragraphs
-
Caveats and troubleshooting
Related Documentation
-
Custom Blocks Guide - Create block types
-
Document Model - Inline element architecture
-
Extension Points - Extension API
-
examples/custom_inline.rb - Example code