Markdown AST representation

Introduced classes

class MdASTPrinter

markdown2 :: MdASTPrinter

Print the AST content
abstract class MdBlock

markdown2 :: MdBlock

An abstract markdown block
class MdBlockQuote

markdown2 :: MdBlockQuote

A block quote
class MdCode

markdown2 :: MdCode

An inline code string
abstract class MdCodeBlock

markdown2 :: MdCodeBlock

A block of code (indented or fenced)
abstract class MdDelimited

markdown2 :: MdDelimited

A node that users delimiters in the Markdown form
class MdDocument

markdown2 :: MdDocument

A Markdown document
class MdEmphasis

markdown2 :: MdEmphasis

An emphasis
class MdFencedCodeBlock

markdown2 :: MdFencedCodeBlock

A code block that starts with a fence
class MdHardLineBreak

markdown2 :: MdHardLineBreak

A hardline break (\\n or \n)
class MdHeading

markdown2 :: MdHeading

A heading
class MdHtmlBlock

markdown2 :: MdHtmlBlock

An html block
class MdHtmlInline

markdown2 :: MdHtmlInline

An inlined html string
class MdImage

markdown2 :: MdImage

An image
class MdIndentedCodeBlock

markdown2 :: MdIndentedCodeBlock

A block code that starts with an indent
abstract class MdLineBreak

markdown2 :: MdLineBreak

A line break (soft or hard)
abstract class MdLinkOrImage

markdown2 :: MdLinkOrImage

A link or image
abstract class MdListBlock

markdown2 :: MdListBlock

An ordered or unordered list block
class MdListItem

markdown2 :: MdListItem

An ordered or unordered list item block
class MdLocation

markdown2 :: MdLocation

MdNode location in the Markdown input
abstract class MdNode

markdown2 :: MdNode

An abstract node
class MdOrderedList

markdown2 :: MdOrderedList

An ordered list block
class MdParagraph

markdown2 :: MdParagraph

A paragraph block
class MdSoftLineBreak

markdown2 :: MdSoftLineBreak

A soft line breack (\r or \n)
class MdStrongEmphasis

markdown2 :: MdStrongEmphasis

A strong emphasis token
class MdText

markdown2 :: MdText

A raw text token
class MdUnorderedList

markdown2 :: MdUnorderedList

An unordered list
interface MdVisitor

markdown2 :: MdVisitor

A visitor for Markdown AST

All class definitions

class MdASTPrinter

markdown2 $ MdASTPrinter

Print the AST content
abstract class MdBlock

markdown2 $ MdBlock

An abstract markdown block
class MdBlockQuote

markdown2 $ MdBlockQuote

A block quote
class MdCode

markdown2 $ MdCode

An inline code string
abstract class MdCodeBlock

markdown2 $ MdCodeBlock

A block of code (indented or fenced)
abstract class MdDelimited

markdown2 $ MdDelimited

A node that users delimiters in the Markdown form
class MdDocument

markdown2 $ MdDocument

A Markdown document
class MdEmphasis

markdown2 $ MdEmphasis

An emphasis
class MdFencedCodeBlock

markdown2 $ MdFencedCodeBlock

A code block that starts with a fence
class MdHardLineBreak

markdown2 $ MdHardLineBreak

A hardline break (\\n or \n)
class MdHeading

markdown2 $ MdHeading

A heading
class MdHtmlBlock

markdown2 $ MdHtmlBlock

An html block
class MdHtmlInline

markdown2 $ MdHtmlInline

An inlined html string
class MdImage

markdown2 $ MdImage

An image
class MdIndentedCodeBlock

markdown2 $ MdIndentedCodeBlock

A block code that starts with an indent
abstract class MdLineBreak

markdown2 $ MdLineBreak

A line break (soft or hard)
abstract class MdLinkOrImage

markdown2 $ MdLinkOrImage

A link or image
abstract class MdListBlock

markdown2 $ MdListBlock

An ordered or unordered list block
class MdListItem

markdown2 $ MdListItem

An ordered or unordered list item block
class MdLocation

markdown2 $ MdLocation

MdNode location in the Markdown input
abstract class MdNode

markdown2 $ MdNode

An abstract node
class MdOrderedList

markdown2 $ MdOrderedList

An ordered list block
class MdParagraph

markdown2 $ MdParagraph

A paragraph block
class MdSoftLineBreak

markdown2 $ MdSoftLineBreak

A soft line breack (\r or \n)
class MdStrongEmphasis

markdown2 $ MdStrongEmphasis

A strong emphasis token
class MdText

markdown2 $ MdText

A raw text token
class MdUnorderedList

markdown2 $ MdUnorderedList

An unordered list
interface MdVisitor

markdown2 $ MdVisitor

A visitor for Markdown AST
package_diagram markdown2::markdown_ast markdown_ast core core markdown2::markdown_ast->core markdown2::markdown_inline_parsing markdown_inline_parsing markdown2::markdown_inline_parsing->markdown2::markdown_ast markdown2::markdown_rendering markdown_rendering markdown2::markdown_rendering->markdown2::markdown_ast markdown2::markdown_block_parsing markdown_block_parsing markdown2::markdown_block_parsing->markdown2::markdown_inline_parsing markdown2::markdown_block_parsing... ... markdown2::markdown_block_parsing...->markdown2::markdown_block_parsing markdown2::markdown_html_rendering markdown_html_rendering markdown2::markdown_html_rendering->markdown2::markdown_rendering markdown2::markdown_latex_rendering markdown_latex_rendering markdown2::markdown_latex_rendering->markdown2::markdown_rendering markdown2::markdown_man_rendering markdown_man_rendering markdown2::markdown_man_rendering->markdown2::markdown_rendering markdown2::markdown_md_rendering markdown_md_rendering markdown2::markdown_md_rendering->markdown2::markdown_rendering markdown2::markdown_html_rendering... ... markdown2::markdown_html_rendering...->markdown2::markdown_html_rendering markdown2::markdown_latex_rendering... ... markdown2::markdown_latex_rendering...->markdown2::markdown_latex_rendering markdown2::markdown_man_rendering... ... markdown2::markdown_man_rendering...->markdown2::markdown_man_rendering markdown2::markdown_md_rendering... ... markdown2::markdown_md_rendering...->markdown2::markdown_md_rendering

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.

Children

module markdown_inline_parsing

markdown2 :: markdown_inline_parsing

Parser for inline markdown
module markdown_rendering

markdown2 :: markdown_rendering

Markdown document rendering

Descendants

module a_star-m

a_star-m

module markdown_block_parsing

markdown2 :: markdown_block_parsing

Markdown blocks parsing
module markdown_github

markdown2 :: markdown_github

Markdown Github mode
module markdown_html_rendering

markdown2 :: markdown_html_rendering

HTML rendering of Markdown documents
module markdown_latex_rendering

markdown2 :: markdown_latex_rendering

LaTeX rendering of Markdown documents
module markdown_man_rendering

markdown2 :: markdown_man_rendering

Manpages rendering of Markdown documents
module markdown_md_rendering

markdown2 :: markdown_md_rendering

Markdown rendering of Markdown documents
module nitmd

markdown2 :: nitmd

A Markdown parser for Nit.
module test_markdown

markdown2 :: test_markdown

Test suites for module markdown
# Markdown AST representation
module markdown_ast

# An abstract node
abstract class MdNode

	# Node location in original markdown
	var location: MdLocation

	# Node parent
	var parent: nullable MdNode = null is writable

	# First child
	var first_child: nullable MdNode = null is writable

	# Last child
	var last_child: nullable MdNode = null is writable

	# Previous node
	var prev: nullable MdNode = null is writable

	# Next node
	var next: nullable MdNode = null is writable

	# Children nodes of `self`
	fun children: Array[MdNode] do
		var nodes = new Array[MdNode]

		var node = first_child
		while node != null do
			nodes.add node
			node = node.next
		end

		return nodes
	end

	# Append a child to `self`
	fun append_child(child: MdNode) do
		child.unlink
		child.parent = self
		if last_child != null then
			last_child.as(not null).next = child
			child.prev = last_child
			last_child = child
		else
			first_child = child
			last_child = child
		end
	end

	# Prepend a child to `self`
	fun prepend_child(child: MdNode) do
		child.unlink
		child.parent = self
		if first_child != null then
			first_child.as(not null).prev = child
			child.next = first_child
			first_child = child
		else
			first_child = child
			last_child = child
		end
	end

	# Unlink `self` from its `parent`
	fun unlink do
		if prev != null then
			prev.as(not null).next = next
		else if parent != null then
			parent.as(not null).first_child = next
		end
		if next != null then
			next.as(not null).prev = prev
		else if parent != null then
			parent.as(not null).last_child = prev
		end
		parent = null
		next = null
		prev = null
	end

	# Insert `sibling` after `self`.
	fun insert_after(sibling: MdNode) do
		sibling.unlink
		sibling.next = next
		if sibling.next != null then
			sibling.next.as(not null).prev = sibling
		end
		sibling.prev = self
		next = sibling
		sibling.parent = parent
		if sibling.next == null then
			sibling.parent.as(not null).last_child = sibling
		end
	end

	# Insert `sibling` before `self`.
	fun insert_before(sibling: MdNode) do
		sibling.unlink
		sibling.prev = prev
		if sibling.prev != null then
			sibling.prev.as(not null).next = sibling
		end
		sibling.next = self
		prev = sibling
		sibling.parent = parent
		if sibling.prev == null then
			sibling.parent.as(not null).first_child = sibling
		end
	end

	# Visit all children or `self`
	fun visit_all(v: MdVisitor) do
		var node = first_child
		while node != null do
			var next = node.next
			v.visit(node)
			node = next
		end
	end

	redef fun to_s do return "{super}\{{to_s_attrs}\}"

	# Returns `self` attributes as a String
	#
	# Mainly used for debug purposes.
	fun to_s_attrs: String do return "loc: {location}"

	# Print `self` AST
	fun debug do
		var v = new MdASTPrinter
		v.enter_visit(self)
	end
end

# A visitor for Markdown AST
interface MdVisitor

	# Start visiting `node`
	fun enter_visit(node: MdNode) do visit(node)

	# Visit `node`
	#
	# Method to define in specific visitor.
	# It should not be called directly but used by `enter_visit`.
	protected fun visit(node: MdNode) is abstract
end

# Print the AST content
class MdASTPrinter
	super MdVisitor

	# Current indent level
	var indent = 0

	# Visit `self` to print the AST content
	fun print_ast(node: MdNode) do
		print "{"  " * indent}{node}"
		indent += 1
		node.visit_all(self)
		indent -= 1
	end

	redef fun visit(node) do print_ast(node)
end

# Blocks

# An abstract markdown block
abstract class MdBlock
	super MdNode

	redef fun parent do return super

	# Can this block contain other blocks?
	var is_container = false

	# Can this block contain `block`?
	fun can_contain(block: MdBlock): Bool do return false

	# Parents of blocks can only be blocks
	redef fun parent=(node) do
		assert parent == null or parent isa MdBlock else
			print "Parent of block must also be block."
		end
		super(node)
	end
end

# A Markdown document
class MdDocument
	super MdBlock

	redef var is_container = true

	redef fun can_contain(block) do return true
end

# A block quote
class MdBlockQuote
	super MdBlock

	redef var is_container = true

	redef fun can_contain(block) do return true
end

# A block of code (indented or fenced)
abstract class MdCodeBlock
	super MdBlock

	# Literal content
	var literal: nullable String = null is writable

	# Fence info / meta
	var info: nullable String = null is writable

	redef fun to_s_attrs do return "{super}, info={info or else "null"}, literal={literal or else "null"}"
end

# A block code that starts with an indent
class MdIndentedCodeBlock
	super MdCodeBlock

	# Does this block use tabs instead of spaces?
	var use_tabs: Bool

	redef fun to_s_attrs do return "{super}, use_tabs={use_tabs}"
end

# A code block that starts with a fence
class MdFencedCodeBlock
	super MdCodeBlock

	# Fence character
	var fence_char: Char

	# Fence length
	var fence_length: Int

	# Fence indentation level
	var fence_indent: Int

	redef fun to_s_attrs do return "{super}, fence_char={fence_char}, fence_length={fence_length}, fence_indent={fence_indent}"
end

# A heading
class MdHeading
	super MdBlock

	# Heading level (from 1 to 6)
	var level: Int

	# Is this heading in the setext format in the original source?
	var is_setext = false is optional

	# Does this heading has an atx trailing in the original source?
	var has_atx_trailing = false is optional

	redef fun to_s_attrs do return "{super}, level={level}"
end

# An html block
class MdHtmlBlock
	super MdBlock

	# Literal content
	var literal: nullable String = null is writable

	redef fun to_s_attrs do return "{super}, literal={literal or else "null"}"
end

# An ordered or unordered list block
abstract class MdListBlock
	super MdBlock

	# Does this list contains line breaks?
	var is_tight: Bool = false is writable

	redef var is_container = true

	redef fun can_contain(block) do return block isa MdListItem

	redef fun to_s_attrs do return "{super}, is_tight={is_tight}"
end

# An ordered or unordered list item block
class MdListItem
	super MdBlock

	redef var is_container = true

	redef fun can_contain(block) do return true
end

# An ordered list block
class MdOrderedList
	super MdListBlock

	# List starting number
	var start_number: Int

	# List number delimiter
	var delimiter: Char

	redef fun to_s_attrs do return "{super}, start_number={start_number}, delimiter={delimiter}"
end

# An unordered list
class MdUnorderedList
	super MdListBlock

	# List bullet marker
	var bullet_marker: Char

	redef fun to_s_attrs do return "{super}, bullet_marker={bullet_marker}"
end

# A paragraph block
class MdParagraph
	super MdBlock

	# Is this paragraph in a list?
	fun is_in_list: Bool do
		var parent = self.parent
		return parent != null and parent.parent isa MdListBlock
	end

	# Is this paragraph in a tight list?
	fun is_in_tight_list: Bool do
		var parent = self.parent
		if parent == null then return false
		var gramps = parent.parent
		return gramps isa MdListBlock and gramps.is_tight
	end
end

# A ruler
class MdThematicBreak
	super MdBlock

	# Thematic break pattern used in markdown source
	var original_pattern: String
end

# Inline nodes

# A line break (soft or hard)
abstract class MdLineBreak
	super MdNode
end

# A hardline break (`\\n` or `  \n`)
class MdHardLineBreak
	super MdLineBreak

	# Does this line break used a backslash in the original source?
	var has_backslash: Bool
end

# A soft line breack (`\r` or `\n`)
class MdSoftLineBreak
	super MdLineBreak
end

# An inline code string
class MdCode
	super MdNode

	# Emphasis delimiter
	var delimiter: String

	# Literal content
	var literal: String is writable

	redef fun to_s_attrs do return "{super}, literal={literal}"
end

# A node that users delimiters in the Markdown form
#
# For example the emphasis: `*bold*`.
abstract class MdDelimited
	super MdNode

	# Emphasis delimiter
	var delimiter: String

	# Opening delimiter
	fun opening_delimiter: String do return delimiter

	# Closing delimiter
	fun closing_delimiter: String do return delimiter

	redef fun to_s_attrs do return "{super}, delimiter={delimiter}"
end

# An emphasis
class MdEmphasis
	super MdDelimited
end

# A strong emphasis token
class MdStrongEmphasis
	super MdDelimited
end

# An inlined html string
class MdHtmlInline
	super MdNode

	# Literal content
	var literal: String is writable

	redef fun to_s_attrs do return "{super}, literal={literal}"
end

# A link or image
abstract class MdLinkOrImage
	super MdNode

	# Link destination
	var destination: String is writable

	# Link title
	var title: nullable String is writable

	# Is the `destination` between pointy brackets `<dest>`
	var has_brackets = false is writable

	redef fun to_s_attrs do return "{super}, destination={destination}, title={title or else "null"}"
end

# An image
class MdImage
	super MdLinkOrImage
end

# A link
class MdLink
	super MdLinkOrImage

	# Is this link an autolink?
	var is_autolink = false is optional, writable
end

# A raw text token
class MdText
	super MdNode

	# Literal content
	var literal: String is writable

	redef fun to_s_attrs do return "{super}, literal={literal}"
end

# MdNode location in the Markdown input
class MdLocation

	# Starting line number (starting from 1).
	var line_start: Int is writable

	# Starting column number (starting from 1).
	var column_start: Int is writable

	# Stopping line number (starting from 1).
	var line_end: Int is writable

	# Stopping column number (starting from 1).
	var column_end: Int is writable

	redef fun to_s do return "{line_start},{column_start}--{line_end},{column_end}"
end
lib/markdown2/markdown_ast.nit:15,1--487,3