Class: Asciidoctor::Callouts

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/callouts.rb

Overview

Maintains a catalog of callouts and their associations.

Instance Method Summary collapse

Constructor Details

#initializeCallouts

Returns a new instance of Callouts.



5
6
7
8
9
# File 'lib/asciidoctor/callouts.rb', line 5

def initialize
  @lists = []
  @list_index = 0
  next_list
end

Instance Method Details

#callout_ids(li_ordinal) ⇒ A

Get a space-separated list of callout ids for the specified list item

Parameters:

  • li_ordinal

    the Integer ordinal (1-based) of the list item for which to retrieve the callouts

Returns:

  • (A)

    space-separated String of callout ids associated with the specified list item



61
62
63
# File 'lib/asciidoctor/callouts.rb', line 61

def callout_ids li_ordinal
  current_list.map {|it| it[:ordinal] == li_ordinal ? %(#{it[:id]} ) : '' }.join.chop
end

#current_listThe

The current list for which callouts are being collected

Returns:

  • (The)

    Array of callouts at the position of the list index pointer



68
69
70
# File 'lib/asciidoctor/callouts.rb', line 68

def current_list
  @lists[@list_index - 1]
end

#next_listvoid

This method returns an undefined value.

Advance to the next callout list in the document



75
76
77
78
79
80
81
82
83
84
# File 'lib/asciidoctor/callouts.rb', line 75

def next_list
  @list_index += 1

  if @lists.size < @list_index
    @lists << []
  end

  @co_index = 1
  nil
end

#read_next_idThe

Get the next callout index in the document

Reads the next callout index in the document and advances the pointer. This method is used during conversion to retrieve the unique id of the callout that was generated during parsing.

Returns:

  • (The)

    unique String id of the next callout in the document



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/asciidoctor/callouts.rb', line 43

def read_next_id
  id = nil
  list = current_list

  if @co_index <= list.size
    id = list[@co_index - 1][:id]
  end

  @co_index += 1
  id
end

#register(li_ordinal) ⇒ The

Register a new callout for the given list item ordinal.

Generates a unique id for this callout based on the index of the next callout list in the document and the index of this callout since the end of the last callout list.

Examples:

callouts = Asciidoctor::Callouts.new
callouts.register(1)
# => "CO1-1"
callouts.next_list
callouts.register(2)
# => "CO2-1"

Parameters:

  • li_ordinal

    the Integer ordinal (1-based) of the list item to which this callout is to be associated

Returns:

  • (The)

    unique String id of this callout



30
31
32
33
34
# File 'lib/asciidoctor/callouts.rb', line 30

def register li_ordinal
  current_list << { ordinal: li_ordinal.to_i, id: (id = generate_next_callout_id) }
  @co_index += 1
  id
end

#rewindvoid

This method returns an undefined value.

Rewind the list index pointer, intended to be used when switching from the parsing to conversion phase.



90
91
92
93
94
# File 'lib/asciidoctor/callouts.rb', line 90

def rewind
  @list_index = 1
  @co_index = 1
  nil
end