class Example
Represents a code example with an optional title.
@example Title@example
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
directiveString The directive name.
-
parameter
textString? The optional title text.
-
parameter
linesArray(String) The remaining lines.
-
parameter
tagsTags The tags parser.
-
parameter
levelInteger 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
directiveString The directive name.
-
parameter
titleString? 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