# 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.name = 'DESCRIPTION' # section.anchor = 'DESCRIPTION' # # section.size # => 0 # # section.section_id # => "_description" # # section << new_block # section.size # => 1 class Asciidoctor::Section # Public: Get/Set the Integer section level. attr_accessor :level # Public: Set the String section name. attr_writer :name # Public: Get/Set the String section caption. attr_accessor :caption # Public: Get/Set the String section anchor name. attr_accessor :anchor alias :id :anchor # Public: Get the Hash of attributes for this block attr_accessor :attributes # Public: Get the Array of section blocks. attr_reader :blocks # Public: Initialize an Asciidoctor::Section object. # # parent - The parent Asciidoc Object. def initialize(parent) @parent = parent @attributes = {} @blocks = [] end # Public: Get the String section name with intrinsics converted # # Examples # # section.name = "git-web{litdd}browse(1) Manual Page" # section.name # => "git-web--browse(1) Manual Page" # # Returns the String section name def name @name && @name.gsub(/(^|[^\\])\{(\w[\w\-]+\w)\}/) { $1 + Asciidoctor::INTRINSICS[$2] }. gsub( /`([^`]+)`/, '\1' ) end # Public: Get the String section id prefixed with value of idprefix attribute, otherwise an underscore # # Section ID synthesis can be disabled by undefining the sectids attribute. # # Examples # # section = Section.new(parent) # section.name = "Foo" # section.section_id # => "_foo" def section_id if self.document.attributes.has_key? 'sectids' self.document.attributes.fetch('idprefix', '_') + "#{name && name.downcase.gsub(/\W+/,'_').gsub(/_+$/, '')}".tr_s('_', '_') else nil end end # Public: Get the Asciidoctor::Document instance to which this Block belongs def document @parent.is_a?(Asciidoctor::Document) ? @parent : @parent.document end def attr(name, default = nil) default.nil? ? @attributes.fetch(name.to_s, self.document.attr(name)) : @attributes.fetch(name.to_s, self.document.attr(name, default)) end def attr?(name) @attributes.has_key?(name.to_s) || self.document.attr?(name) end def update_attributes(attributes) @attributes.update(attributes) end # Public: Get the Asciidoctor::Renderer instance being used for the ancestor # Asciidoctor::Document instance. def renderer Asciidoctor.debug "Section#renderer: Looking for my renderer up in #{@parent}" @parent.renderer 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