class Asciidoctor::Block

Public: Methods for managing blocks of Asciidoc content in a section.

Examples

block = Asciidoctor::Block.new(parent, :paragraph, :source => '_This_ is a <test>')
block.content
=> "<em>This</em> is a &lt;test&gt;"

Constants

DEFAULT_CONTENT_MODEL

Attributes

lines[RW]

Public: Get/Set the original Array content for this block, if applicable

Public Class Methods

new(parent, context, opts = {}) click to toggle source

Public: Initialize an Asciidoctor::Block object.

parent - The parent AbstractBlock with a compound content model to which this Block will be appended. context - The Symbol context name for the type of content (e.g., :paragraph). opts - a Hash of options to customize block initialization: (default: {})

* :content_model indicates whether blocks can be nested in this Block (:compound), otherwise
    how the lines should be processed (:simple, :verbatim, :raw, :empty). (default: :simple)
* :attributes a Hash of attributes (key/value pairs) to assign to this Block. (default: {})
* :source a String or Array of raw source for this Block. (default: nil)
Calls superclass method Asciidoctor::AbstractBlock.new
# File lib/asciidoctor/block.rb, line 42
def initialize parent, context, opts = {}
  super
  @content_model = opts[:content_model] || DEFAULT_CONTENT_MODEL[context]
  if opts.has_key? :subs
    # FIXME this is a bit funky
    # we have to be defensive to avoid lock_in_subs wiping out the override
    if !(subs = opts[:subs]) || (subs.is_a? ::Array)
      @subs = subs || []
      @default_subs = @subs.dup
      @attributes.delete('subs')
    else
      @attributes['subs'] = %Q(#{subs})
    end
  end
  if !(raw_source = opts[:source])
    @lines = []
  elsif raw_source.is_a? ::String
    @lines = Helpers.normalize_lines_from_string raw_source
  else
    @lines = raw_source.dup
  end
end

Public Instance Methods

content() click to toggle source

Public: Get the converted result of the child blocks by converting the children appropriate to content model that this block supports.

Examples

doc = Asciidoctor::Document.new
block = Asciidoctor::Block.new(doc, :paragraph,
    :source => '_This_ is what happens when you <meet> a stranger in the <alps>!')
block.content
=> "<em>This</em> is what happens when you &lt;meet&gt; a stranger in the &lt;alps&gt;!"
Calls superclass method Asciidoctor::AbstractBlock#content
# File lib/asciidoctor/block.rb, line 75
def content
  case @content_model
  when :compound
    super
  when :simple
    apply_subs(@lines * EOL, @subs)
  when :verbatim, :raw
    #((apply_subs @lines.join(EOL), @subs).sub StripLineWiseRx, '\1')

    # QUESTION could we use strip here instead of popping empty lines?
    # maybe apply_subs can know how to strip whitespace?
    result = apply_subs @lines, @subs
    if result.size < 2
      result[0]
    else
      result.shift while (first = result[0]) && first.rstrip.empty?
      result.pop while (last = result[-1]) && last.rstrip.empty?
      result * EOL
    end
  else
    warn %Q(Unknown content model '#{@content_model}' for block: #{to_s}) unless @content_model == :empty
    nil
  end
end
source() click to toggle source

Public: Returns the preprocessed source of this block

Returns the a String containing the lines joined together or nil if there are no lines

# File lib/asciidoctor/block.rb, line 104
def source
  @lines * EOL
end
to_s() click to toggle source
# File lib/asciidoctor/block.rb, line 108
def to_s
  content_summary = @content_model == :compound ? %Q(blocks: #{@blocks.size}) : %Q(lines: #{@lines.size})
  %Q(#<#{self.class}@#{object_id} {context: #{@context.inspect}, content_model: #{@content_model.inspect}, style: #{@style.inspect}, #{content_summary}}>)
end