DecodeSourceDecodeCommentExample

class Example

Represents a code example with an optional title.

Should contain nested text lines representing the example code.

Definitions

def self.parse(directive, text, lines, tags, level = 0)

Parse an example directive from text.

Signature

parameter directive String

The directive name.

parameter text String?

The optional title text.

parameter lines Array(String)

The remaining lines.

parameter tags Tags

The tags parser.

parameter level Integer

The indentation level.

Implementation

def self.parse(directive, text, lines, tags, level = 0)
	node = self.new(directive, text)
	
	tags.parse(lines, level + 1) do |child|
		node.add(child)
	end
	
	return node
end

def initialize(directive, title = nil)

Initialize a new example tag.

Signature

parameter directive String

The directive name.

parameter title String?

The optional title for the example.

Implementation

def initialize(directive, title = nil)
	super(directive)
	
	# @type ivar @title: String?
	@title = title&.strip unless title&.empty?
end

attr :title

Signature

attribute String?

The title of the example.

def code

Get the example code as a single string with leading indentation removed.

Signature

returns String?

The example code joined with newlines, or nil if no code.

Implementation

def code
	lines = text
	return unless lines
	
	# Get the indentation from the first line
	if indentation = lines.first[/\A\s+/]
		# Remove the base indentation from all lines
		lines = lines.map{|line| line.sub(indentation, "")}
	end
	
	return lines.join("\n")
end