Module: Coradoc::AsciiDoc::Model::Anchorable

Included in:
Audio, Bibliography, Block::Core, Header, Image::Core, List::Core, List::DefinitionItem, List::Item, Paragraph, Section, Table, TableCell, Title, Video
Defined in:
lib/coradoc/asciidoc/model/anchorable.rb

Overview

Mixin for elements that can have anchors (IDs and references).

The Anchorable module provides functionality for elements that can be referenced from other parts of the document. It automatically creates an Inline::Anchor based on the element’s id attribute.

Examples:

Including Anchorable in a class

class MyElement < Coradoc::AsciiDoc::Model::Base
  include Coradoc::AsciiDoc::Model::Anchorable
  attribute :id, :string
end

Using the generated anchor

element = MyElement.new(id: "section1")
element.anchor.to_adoc # => "[[section1]]"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Hook called when module is included in a class

Automatically adds the :anchor attribute to the including class.

Parameters:

  • base (Class)

    The class including this module



28
29
30
31
32
# File 'lib/coradoc/asciidoc/model/anchorable.rb', line 28

def self.included(base)
  base.class_eval do
    attribute :anchor, :string
  end
end

Instance Method Details

#default_anchorString?

Generate the default anchor based on the element’s id

Returns:

  • (String, nil)

    The anchor string, or nil if no id



43
44
45
# File 'lib/coradoc/asciidoc/model/anchorable.rb', line 43

def default_anchor
  id.nil? ? nil : "[[#{id}]]"
end

#gen_anchor(inline: false) ⇒ String

Generate the anchor string for serialization

Parameters:

  • inline (Boolean) (defaults to: false)

    If true, don’t add trailing newline

Returns:

  • (String)

    The serialized anchor string



51
52
53
54
55
56
57
58
59
60
# File 'lib/coradoc/asciidoc/model/anchorable.rb', line 51

def gen_anchor(inline: false)
  return '' if anchor.nil?

  anchor_str = anchor.to_adoc
  if anchor_str.empty?
    ''
  else
    "#{anchor_str}#{inline ? '' : "\n"}"
  end
end

#initialize(*args) ⇒ Object

Override initialize to set default anchor



35
36
37
38
# File 'lib/coradoc/asciidoc/model/anchorable.rb', line 35

def initialize(*args)
  super
  @anchor ||= default_anchor
end