Parser
in package
A generic parser for markdown-like languages.
Tags
Table of Contents
Properties
- $maximumNestingLevel : int
- $context : array<string|int, mixed>
- $escapeCharacters : array<string|int, mixed>
- $_blockTypes : mixed
- $_depth : mixed
- $_inlineMarkers : array<string|int, mixed>
Methods
- parse() : string
- Parses the given text considering the full language.
- parseParagraph() : string
- Parses a paragraph without block elements (block elements are ignored).
- blockTypes() : array<string|int, mixed>
- cleanup() : mixed
- This method will be called after `parse()` and `parseParagraph()`.
- consumeParagraph() : array<string|int, mixed>
- Consume lines for a paragraph
- detectLineType() : string
- 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.
- inlineMarkers() : array<string|int, mixed>
- Returns a map of inline markers to the corresponding parser methods.
- parseBlock() : array<string|int, mixed>
- Parses the block at current line by identifying the block type and parsing the content
- parseBlocks() : mixed
- Parse block elements by calling `detectLineType()` to identify them and call consume function afterwards.
- parseEscape() : mixed
- Parses escaped special characters.
- parseInline() : array<string|int, mixed>
- Parses inline elements of the language.
- prepare() : mixed
- This method will be called before `parse()` and `parseParagraph()`.
- prepareMarkers() : mixed
- Prepare markers that are used in the text to parse
- renderAbsy() : mixed
- renderParagraph() : string
- Render a paragraph block
- renderText() : mixed
- This function renders plain text sections in the markdown text.
Properties
$maximumNestingLevel
public
int
$maximumNestingLevel
= 32
the maximum nesting level for language elements.
$context
protected
array<string|int, mixed>
$context
= []
the current context the parser is in. TODO remove in favor of absy
$escapeCharacters
protected
array<string|int, mixed>
$escapeCharacters
= ['\\']
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.
$_blockTypes
private
mixed
$_blockTypes
$_depth
private
mixed
$_depth
= 0
$_inlineMarkers
private
array<string|int, mixed>
$_inlineMarkers
= []
the set of inline markers to use in different contexts.
Methods
parse()
Parses the given text considering the full language.
public
parse(string $text) : string
This includes parsing block elements as well as inline elements.
Parameters
- $text : string
-
the text to parse
Return values
string —parsed markup
parseParagraph()
Parses a paragraph without block elements (block elements are ignored).
public
parseParagraph(string $text) : string
Parameters
- $text : string
-
the text to parse
Return values
string —parsed markup
blockTypes()
protected
blockTypes() : array<string|int, mixed>
Return values
array<string|int, mixed> —a list of block element types available.
cleanup()
This method will be called after `parse()` and `parseParagraph()`.
protected
cleanup() : mixed
You can override it to do cleanup.
consumeParagraph()
Consume lines for a paragraph
protected
consumeParagraph(mixed $lines, mixed $current) : array<string|int, mixed>
Parameters
- $lines : mixed
- $current : mixed
Return values
array<string|int, mixed>detectLineType()
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.
protected
detectLineType(array<string|int, mixed> $lines, int $current) : string
Parameters
- $lines : array<string|int, mixed>
- $current : int
Return values
string —name of the block type in lower case
inlineMarkers()
Returns a map of inline markers to the corresponding parser methods.
protected
inlineMarkers() : array<string|int, mixed>
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 values
array<string|int, mixed> —a map of markers to parser methods
parseBlock()
Parses the block at current line by identifying the block type and parsing the content
protected
parseBlock(mixed $lines, mixed $current) : array<string|int, mixed>
Parameters
- $lines : mixed
- $current : mixed
Return values
array<string|int, mixed> —Array of two elements, the first element contains the block, the second contains the next line index to be parsed.
parseBlocks()
Parse block elements by calling `detectLineType()` to identify them and call consume function afterwards.
protected
parseBlocks(mixed $lines) : mixed
Parameters
- $lines : mixed
parseEscape()
Parses escaped special characters.
protected
parseEscape(mixed $text) : mixed
Parameters
- $text : mixed
Tags
parseInline()
Parses inline elements of the language.
protected
parseInline(string $text) : array<string|int, mixed>
Parameters
- $text : string
-
the inline text to parse.
Return values
array<string|int, mixed>prepare()
This method will be called before `parse()` and `parseParagraph()`.
protected
prepare() : mixed
You can override it to do some initialization work.
prepareMarkers()
Prepare markers that are used in the text to parse
protected
prepareMarkers(string $text) : mixed
Add all markers that are present in markdown. Check is done to avoid iterations in parseInline(), good for huge markdown files
Parameters
- $text : string
renderAbsy()
protected
renderAbsy(mixed $blocks) : mixed
Parameters
- $blocks : mixed
renderParagraph()
Render a paragraph block
protected
renderParagraph(mixed $block) : string
Parameters
- $block : mixed
Return values
stringrenderText()
This function renders plain text sections in the markdown text.
protected
renderText(mixed $block) : mixed
It can be used to work on normal text sections for example to highlight keywords or do special escaping.
Parameters
- $block : mixed