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.
Class Method Summary collapse
-
.included(base) ⇒ Object
Hook called when module is included in a class.
Instance Method Summary collapse
-
#default_anchor ⇒ String?
Generate the default anchor based on the element’s id.
-
#gen_anchor(inline: false) ⇒ String
Generate the anchor string for serialization.
-
#initialize(*args) ⇒ Object
Override initialize to set default anchor.
Class Method Details
.included(base) ⇒ Object
Hook called when module is included in a class
Automatically adds the :anchor attribute to the including class.
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_anchor ⇒ String?
Generate the default anchor based on the element’s 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
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 |