"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/callouts.rb" (1 Jun 2019, 3137 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 module Asciidoctor
    3 # Public: Maintains a catalog of callouts and their associations.
    4 class Callouts
    5   def initialize
    6     @lists = []
    7     @list_index = 0
    8     next_list
    9   end
   10 
   11   # Public: Register a new callout for the given list item ordinal.
   12   #
   13   # Generates a unique id for this callout based on the index of the next callout
   14   # list in the document and the index of this callout since the end of the last
   15   # callout list.
   16   #
   17   # li_ordinal - the Integer ordinal (1-based) of the list item to which this
   18   #              callout is to be associated
   19   #
   20   # Examples
   21   #
   22   #  callouts = Asciidoctor::Callouts.new
   23   #  callouts.register(1)
   24   #  # => "CO1-1"
   25   #  callouts.next_list
   26   #  callouts.register(2)
   27   #  # => "CO2-1"
   28   #
   29   # Returns The unique String id of this callout
   30   def register li_ordinal
   31     current_list << { ordinal: li_ordinal.to_i, id: (id = generate_next_callout_id) }
   32     @co_index += 1
   33     id
   34   end
   35 
   36   # Public: Get the next callout index in the document
   37   #
   38   # Reads the next callout index in the document and advances the pointer.
   39   # This method is used during conversion to retrieve the unique id of the
   40   # callout that was generated during parsing.
   41   #
   42   # Returns The unique String id of the next callout in the document
   43   def read_next_id
   44     id = nil
   45     list = current_list
   46 
   47     if @co_index <= list.size
   48       id = list[@co_index - 1][:id]
   49     end
   50 
   51     @co_index += 1
   52     id
   53   end
   54 
   55   # Public: Get a space-separated list of callout ids for the specified list item
   56   #
   57   # li_ordinal - the Integer ordinal (1-based) of the list item for which to
   58   #              retrieve the callouts
   59   #
   60   # Returns A space-separated String of callout ids associated with the specified list item
   61   def callout_ids li_ordinal
   62     current_list.map {|it| it[:ordinal] == li_ordinal ? %(#{it[:id]} ) : '' }.join.chop
   63   end
   64 
   65   # Public: The current list for which callouts are being collected
   66   #
   67   # Returns The Array of callouts at the position of the list index pointer
   68   def current_list
   69     @lists[@list_index - 1]
   70   end
   71 
   72   # Public: Advance to the next callout list in the document
   73   #
   74   # Returns nothing
   75   def next_list
   76     @list_index += 1
   77 
   78     if @lists.size < @list_index
   79       @lists << []
   80     end
   81 
   82     @co_index = 1
   83     nil
   84   end
   85 
   86   # Public: Rewind the list index pointer, intended to be used when switching
   87   # from the parsing to conversion phase.
   88   #
   89   # Returns nothing
   90   def rewind
   91     @list_index = 1
   92     @co_index = 1
   93     nil
   94   end
   95 
   96   private
   97 
   98   # Internal: Generate a unique id for the callout based on the internal indexes
   99   #
  100   # Returns A unique String id for this callout
  101   def generate_next_callout_id
  102     generate_callout_id @list_index, @co_index
  103   end
  104 
  105   # Internal: Generate a unique id for the callout at the specified position
  106   #
  107   # list_index - The 1-based Integer index of the callout list within the document
  108   # co_index   - The 1-based Integer index of the callout since the end of the last callout list
  109   #
  110   # Returns A unique String id for a callout
  111   def generate_callout_id list_index, co_index
  112     %(CO#{list_index}-#{co_index})
  113   end
  114 end
  115 end