"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/list.rb" (1 Jun 2019, 3637 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: Methods for managing AsciiDoc lists (ordered, unordered and description lists)
    4 class List < AbstractBlock
    5 
    6   # Public: Create alias for blocks
    7   alias items blocks
    8   # Public: Get the items in this list as an Array
    9   alias content blocks
   10   # Public: Create alias to check if this list has blocks
   11   alias items? blocks?
   12 
   13   def initialize parent, context, opts = {}
   14     super
   15   end
   16 
   17   # Check whether this list is an outline list (unordered or ordered).
   18   #
   19   # Return true if this list is an outline list. Otherwise, return false.
   20   def outline?
   21     @context == :ulist || @context == :olist
   22   end
   23 
   24   def convert
   25     if @context == :colist
   26       result = super
   27       @document.callouts.next_list
   28       result
   29     else
   30       super
   31     end
   32   end
   33 
   34   # Deprecated: Use {List#convert} instead.
   35   alias render convert
   36 
   37   def to_s
   38     %(#<#{self.class}@#{object_id} {context: #{@context.inspect}, style: #{@style.inspect}, items: #{items.size}}>)
   39   end
   40 
   41 end
   42 
   43 # Public: Methods for managing items for AsciiDoc olists, ulist, and dlists.
   44 #
   45 # In a description list (dlist), each item is a tuple that consists of a 2-item Array of ListItem terms and a ListItem
   46 # description (i.e., [[term, term, ...], desc]. If a description is not set, then the second entry in the tuple is nil.
   47 class ListItem < AbstractBlock
   48 
   49   # A contextual alias for the list parent node; counterpart to the items alias on List
   50   alias list parent
   51 
   52   # Public: Get/Set the String used to mark this list item
   53   attr_accessor :marker
   54 
   55   # Public: Initialize an Asciidoctor::ListItem object.
   56   #
   57   # parent - The parent list block for this list item
   58   # text - the String text (default nil)
   59   def initialize parent, text = nil
   60     super parent, :list_item
   61     @text = text
   62     @level = parent.level
   63     @subs = NORMAL_SUBS.drop 0
   64   end
   65 
   66   # Public: A convenience method that checks whether the text of this list item
   67   # is not blank (i.e., not nil or empty string).
   68   def text?
   69     @text.nil_or_empty? ? false : true
   70   end
   71 
   72   # Public: Get the String text of this ListItem with substitutions applied.
   73   #
   74   # By default, normal substitutions are applied to the text. The substitutions
   75   # can be modified by altering the subs property of this object.
   76   #
   77   # Returns the converted String text for this ListItem
   78   def text
   79     # NOTE @text can be nil if dd node only has block content
   80     @text && (apply_subs @text, @subs)
   81   end
   82 
   83   # Public: Set the String text.
   84   #
   85   # Returns the new String text assigned to this ListItem
   86   def text= val
   87     @text = val
   88   end
   89 
   90   # Check whether this list item has simple content (no nested blocks aside from a single outline list).
   91   # Primarily relevant for outline lists.
   92   #
   93   # Return true if the list item contains no blocks or it contains a single outline list. Otherwise, return false.
   94   def simple?
   95     @blocks.empty? || (@blocks.size == 1 && List === (blk = @blocks[0]) && blk.outline?)
   96   end
   97 
   98   # Check whether this list item has compound content (nested blocks aside from a single outline list).
   99   # Primarily relevant for outline lists.
  100   #
  101   # Return true if the list item contains blocks other than a single outline list. Otherwise, return false.
  102   def compound?
  103     !simple?
  104   end
  105 
  106   # Internal: Fold the adjacent paragraph block into the list item text
  107   #
  108   # Returns nothing
  109   def fold_first
  110     @text = @text.nil_or_empty? ? @blocks.shift.source : %(#{@text}#{LF}#{@blocks.shift.source})
  111     nil
  112   end
  113 
  114   def to_s
  115     %(#<#{self.class}@#{object_id} {list_context: #{parent.context.inspect}, text: #{@text.inspect}, blocks: #{(@blocks || []).size}}>)
  116   end
  117 end
  118 end