markdown :: Block :: defaultinit
# Representation of a markdown block in the AST.
# Each `Block` is linked to a `MDBlock` that contains mardown code.
abstract class Block
# The markdown block `self` is related to.
var block: MDBlock
# Output `self` using `v.decorator`.
fun emit(v: MarkdownProcessor) do v.emit_in(self)
# Emit the containts of `self`, lines or blocks.
fun emit_in(v: MarkdownProcessor) do
block.remove_surrounding_empty_lines
if block.has_lines then
emit_lines(v)
else
emit_blocks(v)
end
end
# Emit lines contained in `block`.
fun emit_lines(v: MarkdownProcessor) do
var tpl = v.push_buffer
var line = block.first_line
while line != null do
if not line.is_empty then
v.add line.value.substring(line.leading, line.value.length - line.trailing)
if line.trailing >= 2 then v.decorator.add_line_break(v)
end
if line.next != null then
v.addn
end
line = line.next
end
v.pop_buffer
v.emit_text(tpl)
end
# Emit sub-blocks contained in `block`.
fun emit_blocks(v: MarkdownProcessor) do
var block = self.block.first_block
while block != null do
v.push_loc(block.location)
block.kind.emit(v)
v.pop_loc
block = block.next
end
end
# The raw content of the block as a multi-line string.
fun raw_content: String do
var infence = self isa BlockFence
var text = new FlatBuffer
var line = self.block.first_line
while line != null do
if not line.is_empty then
var str = line.value
if not infence and str.has_prefix(" ") then
text.append str.substring(4, str.length - line.trailing)
else
text.append str
end
end
text.append "\n"
line = line.next
end
return text.write_to_string
end
end
lib/markdown/markdown.nit:1134,1--1202,3