A generic parser for markdown-like languages.

public $maximumNestingLevel = 32
  • var int the maximum nesting level for language elements.
public parse( $text)

Parses the given text considering the full language.

This includes parsing block elements as well as inline elements.

  • param string $text the text to parse
  • return string parsed markup
public parseParagraph( $text)

Parses a paragraph without block elements (block elements are ignored).

  • param string $text the text to parse
  • return string parsed markup
protected $context = []
  • var array the current context the parser is in.
    TODO remove in favor of absy
protected $escapeCharacters = ['\']
  • var array these are "escapeable" characters. When using one of these prefixed with a
    backslash, the character will be outputted without the backslash and is not interpreted
    as markdown.
protected blockTypes()
  • return array a list of block element types available.
protected cleanup()

This method will be called after parse() and parseParagraph().

You can override it to do cleanup.

protected consumeParagraph( $lines, $current)

Consume lines for a paragraph

  • return array
protected detectLineType( $lines, $current)

Given a set of lines and an index of a current line it uses the registed block types to
detect the type of this line.

  • param array $lines
  • param int $current
  • return string name of the block type in lower case
protected inlineMarkers()

Returns a map of inline markers to the corresponding parser methods.

This array defines handler methods for inline markdown markers.
When a marker is found in the text, the handler method is called with the text
starting at the position of the marker.

Note that markers starting with whitespace may slow down the parser,
you may want to use [[renderText]] to deal with them.

You may override this method to define a set of markers and parsing methods.
The default implementation looks for protected methods starting with parse that
also have an @marker annotation in PHPDoc.

  • return array a map of markers to parser methods
protected parseBlock( $lines, $current)

Parses the block at current line by identifying the block type and parsing the content

  • return array Array of two elements, the first element contains the block,
    the second contains the next line index to be parsed.
protected parseBlocks( $lines)

Parse block elements by calling detectLineType() to identify them
and call consume function afterwards.

protected parseEscape( $text)

Parses escaped special characters.

  • marker \
protected parseInline( $text)

Parses inline elements of the language.

  • param string $text the inline text to parse.
  • return array
protected prepare()

This method will be called before parse() and parseParagraph().

You can override it to do some initialization work.

protected prepareMarkers( $text)

Prepare markers that are used in the text to parse

Add all markers that are present in markdown.
Check is done to avoid iterations in parseInline(), good for huge markdown files

  • param string $text
protected renderAbsy( $blocks)
protected renderParagraph( $block)

Render a paragraph block

  • return string
protected renderText( $block)

This function renders plain text sections in the markdown text.

It can be used to work on normal text sections for example to highlight keywords or
do special escaping.

private $_blockTypes = NULL
private $_depth = 0
private $_inlineMarkers = []
  • var array the set of inline markers to use in different contexts.
© 2023 Bruce Wells
Search Namespaces \ Classes