class Generic
Base class for renderers implemented in Ruby.
Definitions
def initialize(flags: DEFAULT, extensions: [])
Initializes a renderer with rendering flags and extensions.
Signature
-
parameter
flagsInteger The enabled rendering flags.
-
parameter
extensionsArray(Symbol) The enabled extensions.
Implementation
def initialize(flags: DEFAULT, extensions: [])
@flags = flags
@stream = StringIO.new(+"")
@in_tight = false
@in_plain = false
@tagfilter = extensions.include?(:tagfilter)
end
attr_accessor :in_tight
Signature
-
attribute
Boolean Whether the renderer is inside a tight container.
attr_accessor :in_plain
Signature
-
attribute
Boolean Whether the renderer is emitting plain text.
def out(*args)
Writes strings, nodes, arrays of nodes, or child-node markers to the output.
Signature
-
parameter
argsArray(Object) Values to append or render.
Implementation
def out(*args)
args.each do |arg|
if arg == :children
@node.each{|child| out(child)}
elsif arg.is_a?(Array)
arg.each{|x| render(x)}
elsif arg.is_a?(Node)
render(arg)
else
@stream.write(arg)
end
end
end
def render(node)
Renders a node and returns the completed output for document nodes.
Signature
-
parameter
nodeMarkly::Node The node to render.
-
returns
String | Nil The output string when rendering a document.
Implementation
def render(node)
@node = node
if node.type == :document
document(node)
@stream.string
elsif @in_plain && node.type != :text && node.type != :softbreak
node.each{|child| render(child)}
else
send(node.type, node)
end
end
def document(_node)
Renders a document node and all of its children.
Signature
-
parameter
_nodeMarkly::Node The document node.
Implementation
def document(_node)
out(:children)
end
def code_block(_node)
Renders a code block node.
Subclasses should override this callback.
Signature
-
parameter
_nodeMarkly::Node The code block node.
Implementation
def code_block(_node)
raise NotImplementedError, "#{self.class} must implement #code_block"
end
def reference_def(_node)
Ignores reference-definition nodes, which have no direct output.
Signature
-
parameter
_nodeMarkly::Node The reference-definition node.
def cr
Writes a newline unless the output is empty or already ends with one.
Implementation
def cr
return if @stream.string.empty? || @stream.string[-1] == "\n"
out("\n")
end
def block
Renders a block surrounded by normalized newlines.
Signature
-
yields
{|| ...} The block content to render.
Implementation
def block
cr
yield
cr
end
def container(starter, ender)
Renders content between opening and closing strings.
Signature
-
parameter
starterString The opening output.
-
parameter
enderString The closing output.
-
yields
{|| ...} The container content to render.
Implementation
def container(starter, ender)
out(starter)
yield
out(ender)
end
def plain
Renders a block in plain-text mode, suppressing structural markup.
Signature
-
yields
{|| ...} The content to render as plain text.
Implementation
def plain
old_in_plain = @in_plain
@in_plain = true
yield
@in_plain = old_in_plain
end