A block of markdown lines.

A MDBlock can contains lines and/or sub-blocks.

Introduced properties

fun add_line(line: MDLine)

markdown :: MDBlock :: add_line

Add a line to this block.
fun count_blocks: Int

markdown :: MDBlock :: count_blocks

Count sub-blocks.
fun count_lines: Int

markdown :: MDBlock :: count_lines

Count block lines.
fun first_block: nullable MDBlock

markdown :: MDBlock :: first_block

First sub-block if any.
fun first_block=(first_block: nullable MDBlock)

markdown :: MDBlock :: first_block=

First sub-block if any.
fun first_line: nullable MDLine

markdown :: MDBlock :: first_line

First line if any.
fun first_line=(first_line: nullable MDLine)

markdown :: MDBlock :: first_line=

First line if any.
fun has_blocks: Bool

markdown :: MDBlock :: has_blocks

Does this block contain subblocks?
fun has_lines: Bool

markdown :: MDBlock :: has_lines

Does this block contain lines?
fun kind: Block

markdown :: MDBlock :: kind

Kind of block.
fun kind=(kind: Block)

markdown :: MDBlock :: kind=

Kind of block.
fun last_block: nullable MDBlock

markdown :: MDBlock :: last_block

Last sub-block if any.
fun last_block=(last_block: nullable MDBlock)

markdown :: MDBlock :: last_block=

Last sub-block if any.
fun last_line: nullable MDLine

markdown :: MDBlock :: last_line

Last line if any.
fun last_line=(last_line: nullable MDLine)

markdown :: MDBlock :: last_line=

Last line if any.
fun location: MDLocation

markdown :: MDBlock :: location

Position of self in the input.
protected fun location=(location: MDLocation)

markdown :: MDBlock :: location=

Position of self in the input.
fun next: nullable MDBlock

markdown :: MDBlock :: next

Next block if any.
fun next=(next: nullable MDBlock)

markdown :: MDBlock :: next=

Next block if any.
fun prev: nullable MDBlock

markdown :: MDBlock :: prev

Previous block if any.
fun prev=(prev: nullable MDBlock)

markdown :: MDBlock :: prev=

Previous block if any.
fun remove_line(line: MDLine)

markdown :: MDBlock :: remove_line

Remove line from this block.
fun remove_list_indent(v: MarkdownProcessor)

markdown :: MDBlock :: remove_list_indent

Remove list markers and up to 4 leading spaces.
fun remove_surrounding_empty_lines: Bool

markdown :: MDBlock :: remove_surrounding_empty_lines

Remove leading and trailing empty lines.
fun split(line: MDLine): MDBlock

markdown :: MDBlock :: split

Split self creating a new sub-block having line has last_line.
fun text: String

markdown :: MDBlock :: text

Collect block line text.

Redefined properties

redef type SELF: MDBlock

markdown $ MDBlock :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun add_line(line: MDLine)

markdown :: MDBlock :: add_line

Add a line to this block.
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun count_blocks: Int

markdown :: MDBlock :: count_blocks

Count sub-blocks.
fun count_lines: Int

markdown :: MDBlock :: count_lines

Count block lines.
fun first_block: nullable MDBlock

markdown :: MDBlock :: first_block

First sub-block if any.
fun first_block=(first_block: nullable MDBlock)

markdown :: MDBlock :: first_block=

First sub-block if any.
fun first_line: nullable MDLine

markdown :: MDBlock :: first_line

First line if any.
fun first_line=(first_line: nullable MDLine)

markdown :: MDBlock :: first_line=

First line if any.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun has_blocks: Bool

markdown :: MDBlock :: has_blocks

Does this block contain subblocks?
fun has_lines: Bool

markdown :: MDBlock :: has_lines

Does this block contain lines?
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun kind: Block

markdown :: MDBlock :: kind

Kind of block.
fun kind=(kind: Block)

markdown :: MDBlock :: kind=

Kind of block.
fun last_block: nullable MDBlock

markdown :: MDBlock :: last_block

Last sub-block if any.
fun last_block=(last_block: nullable MDBlock)

markdown :: MDBlock :: last_block=

Last sub-block if any.
fun last_line: nullable MDLine

markdown :: MDBlock :: last_line

Last line if any.
fun last_line=(last_line: nullable MDLine)

markdown :: MDBlock :: last_line=

Last line if any.
fun location: MDLocation

markdown :: MDBlock :: location

Position of self in the input.
protected fun location=(location: MDLocation)

markdown :: MDBlock :: location=

Position of self in the input.
fun next: nullable MDBlock

markdown :: MDBlock :: next

Next block if any.
fun next=(next: nullable MDBlock)

markdown :: MDBlock :: next=

Next block if any.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun prev: nullable MDBlock

markdown :: MDBlock :: prev

Previous block if any.
fun prev=(prev: nullable MDBlock)

markdown :: MDBlock :: prev=

Previous block if any.
fun remove_line(line: MDLine)

markdown :: MDBlock :: remove_line

Remove line from this block.
fun remove_list_indent(v: MarkdownProcessor)

markdown :: MDBlock :: remove_list_indent

Remove list markers and up to 4 leading spaces.
fun remove_surrounding_empty_lines: Bool

markdown :: MDBlock :: remove_surrounding_empty_lines

Remove leading and trailing empty lines.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun split(line: MDLine): MDBlock

markdown :: MDBlock :: split

Split self creating a new sub-block having line has last_line.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun text: String

markdown :: MDBlock :: text

Collect block line text.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram markdown::MDBlock MDBlock core::Object Object markdown::MDBlock->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

markdown $ MDBlock
# A block of markdown lines.
# A `MDBlock` can contains lines and/or sub-blocks.
class MDBlock

	# Position of `self` in the input.
	var location: MDLocation

	# Kind of block.
	# See `Block`.
	var kind: Block = new BlockNone(self) is writable

	# First line if any.
	var first_line: nullable MDLine = null is writable

	# Last line if any.
	var last_line: nullable MDLine = null is writable

	# First sub-block if any.
	var first_block: nullable MDBlock = null is writable

	# Last sub-block if any.
	var last_block: nullable MDBlock = null is writable

	# Previous block if any.
	var prev: nullable MDBlock = null is writable

	# Next block if any.
	var next: nullable MDBlock = null is writable

	# Does this block contain subblocks?
	fun has_blocks: Bool do return first_block != null

	# Count sub-blocks.
	fun count_blocks: Int do
		var count = 0
		var block = first_block
		while block != null do
			count += 1
			block = block.next
		end
		return count
	end

	# Does this block contain lines?
	fun has_lines: Bool do return first_line != null

	# Count block lines.
	fun count_lines: Int do
		var count = 0
		var line = first_line
		while line != null do
			count += 1
			line = line.next
		end
		return count
	end

	# Split `self` creating a new sub-block having `line` has `last_line`.
	fun split(line: MDLine): MDBlock do
		# location for new block
		var new_loc = new MDLocation(
			first_line.as(not null).location.line_start,
			first_line.as(not null).location.column_start,
			line.location.line_end,
			line.location.column_end)
		# create block
		var block = new MDBlock(new_loc)
		block.first_line = first_line
		block.last_line = line
		first_line = line.next
		line.next = null
		if first_line == null then
			last_line = null
		else
			first_line.as(not null).prev = null
			# update current block loc
			location.line_start = first_line.as(not null).location.line_start
			location.column_start = first_line.as(not null).location.column_start
		end
		if first_block == null then
			first_block = block
			last_block = block
		else
			last_block.as(not null).next = block
			last_block = block
		end
		return block
	end

	# Add a `line` to this block.
	fun add_line(line: MDLine) do
		if last_line == null then
			first_line = line
			last_line = line
		else
			last_line.as(not null).next_empty = line.is_empty
			line.prev_empty = last_line.as(not null).is_empty
			line.prev = last_line
			last_line.as(not null).next = line
			last_line = line
		end
	end

	# Remove `line` from this block.
	fun remove_line(line: MDLine) do
		if line.prev == null then
			first_line = line.next
		else
			line.prev.as(not null).next = line.next
		end
		if line.next == null then
			last_line = line.prev
		else
			line.next.as(not null).prev = line.prev
		end
		line.prev = null
		line.next = null
	end

	# Remove leading empty lines.
	fun remove_leading_empty_lines: Bool do
		var was_empty = false
		var line = first_line
		while line != null and line.is_empty do
			remove_line line
			line = first_line
			was_empty = true
		end
		return was_empty
	end

	# Remove trailing empty lines.
	fun remove_trailing_empty_lines: Bool do
		var was_empty = false
		var line = last_line
		while line != null and line.is_empty do
			remove_line line
			line = last_line
			was_empty = true
		end
		return was_empty
	end

	# Remove leading and trailing empty lines.
	fun remove_surrounding_empty_lines: Bool do
		var was_empty = false
		if remove_leading_empty_lines then was_empty = true
		if remove_trailing_empty_lines then was_empty = true
		return was_empty
	end

	# Remove list markers and up to 4 leading spaces.
	# Used to clean nested lists.
	fun remove_list_indent(v: MarkdownProcessor) do
		var line = first_line
		while line != null do
			if not line.is_empty then
				var kind = v.line_kind(line)
				if kind isa LineList then
					line.value = kind.extract_value(line)
				else
					line.value = line.value.substring_from(line.leading.min(4))
				end
				line.leading = line.process_leading
			end
			line = line.next
		end
	end

	# Collect block line text.
	fun text: String do
		var text = new FlatBuffer
		var line = first_line
		while line != null do
			if not line.is_empty then
				text.append line.text
			end
			text.append "\n"
			line = line.next
		end
		var block = first_block
		while block != null do
			text.append block.text
			text.append "\n"
			block = block.next
		end
		return text.write_to_string
	end
end
lib/markdown/markdown.nit:944,1--1132,3