# Public: Methods for managing sections of AsciiDoc content in a document. # The section responds as an Array of content blocks by delegating # block-related methods to its @blocks Array. # # Examples # # section = Asciidoctor::Section.new # section.title = 'Section 1' # section.id = 'sect1' # # section.size # => 0 # # section.id # => "sect1" # # section << new_block # section.size # => 1 class Asciidoctor::Section < Asciidoctor::AbstractBlock # Public: Get/Set the Integer index of this section within the parent block attr_accessor :index # Public: Initialize an Asciidoctor::Section object. # # parent - The parent Asciidoc Object. def initialize(parent = nil, level = nil) super(parent, :section) if level.nil? && !parent.nil? @level = parent.level + 1 end @index = 0 end # Public: The name of this section, an alias of the section title alias :name :title # Public: Generate a String id for this section. # # The generated id is prefixed with value of the 'idprefix' attribute, which # is an underscore by default. # # Section id synthesis can be disabled by undefining the 'sectids' attribute. # # If the generated id is already in use in the document, a count is appended # until a unique id is found. # # Examples # # section = Section.new(parent) # section.title = "Foo" # section.generate_id # => "_foo" # # another_section = Section.new(parent) # another_section.title = "Foo" # another_section.generate_id # => "_foo_1" def generate_id if @document.attr?('sectids') base_id = @document.attr('idprefix', '_') + title.downcase.gsub(/[0-9]+;/, '_'). gsub(/\W+/, '_').tr_s('_', '_').gsub(/^_?(.*?)_?$/, '\1') gen_id = base_id cnt = 2 while @document.references[:ids].has_key? gen_id gen_id = "#{base_id}_#{cnt}" cnt += 1 end @document.references[:ids][gen_id] = title gen_id else nil end end # Public: Get the rendered String content for this Section and all its child # Blocks. def render Asciidoctor.debug { "Now rendering section for #{self}" } renderer.render('section', self) end # Public: Get the String section content by aggregating rendered section blocks. # # Examples # # section = Section.new # section << 'foo' # section << 'bar' # section << 'baz' # section.content # "
foo
bar
baz