Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

Markdown Extra - A text-to-HTML conversion tool for web writers

Author: Michel Fortin <michel.fortin@michelf.com>
Copyright: 2004-2019 Michel Fortin <https://michelf.com/projects/php-markdown/>
Copyright: (Original Markdown) 2004-2006 John Gruber <https://daringfireball.net/projects/markdown/>
File Size: 1893 lines (54 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 4 classes

MarkdownExtra:: (5 methods):
  __construct()
  setup()
  teardown()
  doExtraAttributes()
  stripLinkDefinitions()

attr:: (3 methods):
  _stripLinkDefinitions_callback()
  hashHTMLBlocks()
  _hashHTMLBlocks_inMarkdown()

name:: (35 methods):
  _hashHTMLBlocks_inHTML()
  hashClean()
  doAnchors()
  _doAnchors_reference_callback()
  _doAnchors_inline_callback()
  doImages()
  _doImages_reference_callback()
  _doImages_inline_callback()
  doHeaders()
  _doHeaders_callback_setext()
  _doHeaders_callback_atx()
  doTables()
  _doTable_leadingPipe_callback()
  _doTable_makeAlignAttr()
  _doTable_callback()
  doDefLists()
  _doDefLists_callback()
  processDefListItems()
  _processDefListItems_callback_dt()
  _processDefListItems_callback_dd()
  doFencedCodeBlocks()
  _doFencedCodeBlocks_callback()
  _doFencedCodeBlocks_newlines()
  formParagraphs()
  stripFootnotes()
  _stripFootnotes_callback()
  doFootnotes()
  appendFootnotes()
  _doFootnotes()
  _appendFootnotes_callback()
  parseFootnotePlaceholders()
  stripAbbreviations()
  _stripAbbreviations_callback()
  doAbbreviations()
  _doAbbreviations_callback()

name:: (35 methods):
  _hashHTMLBlocks_inHTML()
  hashClean()
  doAnchors()
  _doAnchors_reference_callback()
  _doAnchors_inline_callback()
  doImages()
  _doImages_reference_callback()
  _doImages_inline_callback()
  doHeaders()
  _doHeaders_callback_setext()
  _doHeaders_callback_atx()
  doTables()
  _doTable_leadingPipe_callback()
  _doTable_makeAlignAttr()
  _doTable_callback()
  doDefLists()
  _doDefLists_callback()
  processDefListItems()
  _processDefListItems_callback_dt()
  _processDefListItems_callback_dd()
  doFencedCodeBlocks()
  _doFencedCodeBlocks_callback()
  _doFencedCodeBlocks_newlines()
  formParagraphs()
  stripFootnotes()
  _stripFootnotes_callback()
  doFootnotes()
  appendFootnotes()
  _doFootnotes()
  _appendFootnotes_callback()
  parseFootnotePlaceholders()
  stripAbbreviations()
  _stripAbbreviations_callback()
  doAbbreviations()
  _doAbbreviations_callback()


Class: MarkdownExtra  - X-Ref

Markdown Extra Parser Class

__construct()   X-Ref
Constructor function. Initialize the parser object.

return: void

setup()   X-Ref
Setting up Extra-specific variables.


teardown()   X-Ref
Clearing Extra-specific variables.


doExtraAttributes($tag_name, $attr, $defaultIdValue = null, $classes = array()   X-Ref
Parse attributes caught by the $this->id_class_attr_catch_re expression
and return the HTML-formatted list of attributes.

Currently supported attributes are .class and #id.

In addition, this method also supports supplying a default Id value,
which will be used to populate the id attribute in case it was not
overridden.
return: string
param: string $tag_name
param: string $attr
param: mixed  $defaultIdValue
param: array  $classes

stripLinkDefinitions($text)   X-Ref
Strips link definitions from text, stores the URLs and titles in
hash references.

return: string
param: string $text

Class: attr  - X-Ref

_stripLinkDefinitions_callback($matches)   X-Ref
Strip link definition callback

return: string
param: array $matches

hashHTMLBlocks($text)   X-Ref
Hashify HTML Blocks and "clean tags".

We only want to do this for block-level HTML tags, such as headers,
lists, and tables. That's because we still want to wrap <p>s around
"paragraphs" that are wrapped in non-block-level tags, such as anchors,
phrase emphasis, and spans. The list of tags we're looking for is
hard-coded.

This works by calling _HashHTMLBlocks_InMarkdown, which then calls
_HashHTMLBlocks_InHTML when it encounter block tags. When the markdown="1"
attribute is found within a tag, _HashHTMLBlocks_InHTML calls back
_HashHTMLBlocks_InMarkdown to handle the Markdown syntax within the tag.
These two functions are calling each other. It's recursive!
return: string
param: string $text

_hashHTMLBlocks_inMarkdown($text, $indent = 0,$enclosing_tag_re = '', $span = false)   X-Ref
Parse markdown text, calling _HashHTMLBlocks_InHTML for block tags.

*   $indent is the number of space to be ignored when checking for code
blocks. This is important because if we don't take the indent into
account, something like this (which looks right) won't work as expected:

<div>
<div markdown="1">
Hello World.  <-- Is this a Markdown code block or text?
</div>  <-- Is this a Markdown code block or a real tag?
<div>

If you don't like this, just don't indent the tag on which
you apply the markdown="1" attribute.

*   If $enclosing_tag_re is not empty, stops at the first unmatched closing
tag with that name. Nested tags supported.

*   If $span is true, text inside must treated as span. So any double
newline will be replaced by a single newline so that it does not create
paragraphs.

Returns an array of that form: ( processed text , remaining text )

return: array
param: string  $text
param: integer $indent
param: string  $enclosing_tag_re
param: boolean $span

Class: name  - X-Ref

_hashHTMLBlocks_inHTML($text, $hash_method, $md_attr)   X-Ref
Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags.

*   Calls $hash_method to convert any blocks.
*   Stops when the first opening tag closes.
*   $md_attr indicate if the use of the `markdown="1"` attribute is allowed.
(it is not inside clean tags)

Returns an array of that form: ( processed text , remaining text )
return: array
param: string $text
param: string $hash_method
param: bool $md_attr Handle `markdown="1"` attribute

hashClean($text)   X-Ref
Called whenever a tag must be hashed when a function inserts a "clean" tag
in $text, it passes through this function and is automaticaly escaped,
blocking invalid nested overlap.

return: string
param: string $text

doAnchors($text)   X-Ref
Turn Markdown link shortcuts into XHTML <a> tags.

return: string
param: string $text

_doAnchors_reference_callback($matches)   X-Ref
Callback for reference anchors

return: string
param: array $matches

_doAnchors_inline_callback($matches)   X-Ref
Callback for inline anchors

return: string
param: array $matches

doImages($text)   X-Ref
Turn Markdown image shortcuts into <img> tags.

return: string
param: string $text

_doImages_reference_callback($matches)   X-Ref
Callback for referenced images

return: string
param: array $matches

_doImages_inline_callback($matches)   X-Ref
Callback for inline images

return: string
param: array $matches

doHeaders($text)   X-Ref
Process markdown headers. Redefined to add ID and class attribute support.

return: string
param: string $text

_doHeaders_callback_setext($matches)   X-Ref
Callback for setext headers

return: string
param: array $matches

_doHeaders_callback_atx($matches)   X-Ref
Callback for atx headers

return: string
param: array $matches

doTables($text)   X-Ref
Form HTML tables.

return: string
param: string $text

_doTable_leadingPipe_callback($matches)   X-Ref
Callback for removing the leading pipe for each row

return: string
param: array $matches

_doTable_makeAlignAttr($alignname)   X-Ref
Make the align attribute in a table

return: string
param: string $alignname

_doTable_callback($matches)   X-Ref
Calback for processing tables

return: string
param: array $matches

doDefLists($text)   X-Ref
Form HTML definition lists.

return: string
param: string $text

_doDefLists_callback($matches)   X-Ref
Callback for processing definition lists

return: string
param: array $matches

processDefListItems($list_str)   X-Ref
Process the contents of a single definition list, splitting it
into individual term and definition list items.

return: string
param: string $list_str

_processDefListItems_callback_dt($matches)   X-Ref
Callback for <dt> elements in definition lists

return: string
param: array $matches

_processDefListItems_callback_dd($matches)   X-Ref
Callback for <dd> elements in definition lists

return: string
param: array $matches

doFencedCodeBlocks($text)   X-Ref
Adding the fenced code block syntax to regular Markdown:

~~~
Code block
~~~

return: string
param: string $text

_doFencedCodeBlocks_callback($matches)   X-Ref
Callback to process fenced code blocks

return: string
param: array $matches

_doFencedCodeBlocks_newlines($matches)   X-Ref
Replace new lines in fenced code blocks

return: string
param: array $matches

formParagraphs($text, $wrap_in_p = true)   X-Ref
Parse text into paragraphs

return: string       HTML output
param: string $text String to process in paragraphs
param: boolean $wrap_in_p Whether paragraphs should be wrapped in <p> tags

stripFootnotes($text)   X-Ref
Footnotes - Strips link definitions from text, stores the URLs and
titles in hash references.

return: string
param: string $text

_stripFootnotes_callback($matches)   X-Ref
Callback for stripping footnotes

return: string
param: array $matches

doFootnotes($text)   X-Ref
Replace footnote references in $text [^id] with a special text-token
which will be replaced by the actual footnote marker in appendFootnotes.

return: string
param: string $text

appendFootnotes($text)   X-Ref
Append footnote list to text

return: string
param: string $text

_doFootnotes()   X-Ref
Generates the HTML for footnotes.  Called by appendFootnotes, even if
footnotes are not being appended.

return: void

_appendFootnotes_callback($matches)   X-Ref
Callback for appending footnotes

return: string
param: array $matches

parseFootnotePlaceholders($label, $footnote_number, $reference_number)   X-Ref
Build footnote label by evaluating any placeholders.
- ^^  footnote number
- %%  footnote reference number (Nth reference to footnote number)

return: string
param: string $label
param: int    $footnote_number
param: int    $reference_number

stripAbbreviations($text)   X-Ref
Abbreviations - strips abbreviations from text, stores titles in hash
references.

return: string
param: string $text

_stripAbbreviations_callback($matches)   X-Ref
Callback for stripping abbreviations

return: string
param: array $matches

doAbbreviations($text)   X-Ref
Find defined abbreviations in text and wrap them in <abbr> elements.

return: string
param: string $text

_doAbbreviations_callback($matches)   X-Ref
Callback for processing abbreviations

return: string
param: array $matches

Class: name  - X-Ref

_hashHTMLBlocks_inHTML($text, $hash_method, $md_attr)   X-Ref
Parse HTML, calling _HashHTMLBlocks_InMarkdown for block tags.

*   Calls $hash_method to convert any blocks.
*   Stops when the first opening tag closes.
*   $md_attr indicate if the use of the `markdown="1"` attribute is allowed.
(it is not inside clean tags)

Returns an array of that form: ( processed text , remaining text )
return: array
param: string $text
param: string $hash_method
param: bool $md_attr Handle `markdown="1"` attribute

hashClean($text)   X-Ref
Called whenever a tag must be hashed when a function inserts a "clean" tag
in $text, it passes through this function and is automaticaly escaped,
blocking invalid nested overlap.

return: string
param: string $text

doAnchors($text)   X-Ref
Turn Markdown link shortcuts into XHTML <a> tags.

return: string
param: string $text

_doAnchors_reference_callback($matches)   X-Ref
Callback for reference anchors

return: string
param: array $matches

_doAnchors_inline_callback($matches)   X-Ref
Callback for inline anchors

return: string
param: array $matches

doImages($text)   X-Ref
Turn Markdown image shortcuts into <img> tags.

return: string
param: string $text

_doImages_reference_callback($matches)   X-Ref
Callback for referenced images

return: string
param: array $matches

_doImages_inline_callback($matches)   X-Ref
Callback for inline images

return: string
param: array $matches

doHeaders($text)   X-Ref
Process markdown headers. Redefined to add ID and class attribute support.

return: string
param: string $text

_doHeaders_callback_setext($matches)   X-Ref
Callback for setext headers

return: string
param: array $matches

_doHeaders_callback_atx($matches)   X-Ref
Callback for atx headers

return: string
param: array $matches

doTables($text)   X-Ref
Form HTML tables.

return: string
param: string $text

_doTable_leadingPipe_callback($matches)   X-Ref
Callback for removing the leading pipe for each row

return: string
param: array $matches

_doTable_makeAlignAttr($alignname)   X-Ref
Make the align attribute in a table

return: string
param: string $alignname

_doTable_callback($matches)   X-Ref
Calback for processing tables

return: string
param: array $matches

doDefLists($text)   X-Ref
Form HTML definition lists.

return: string
param: string $text

_doDefLists_callback($matches)   X-Ref
Callback for processing definition lists

return: string
param: array $matches

processDefListItems($list_str)   X-Ref
Process the contents of a single definition list, splitting it
into individual term and definition list items.

return: string
param: string $list_str

_processDefListItems_callback_dt($matches)   X-Ref
Callback for <dt> elements in definition lists

return: string
param: array $matches

_processDefListItems_callback_dd($matches)   X-Ref
Callback for <dd> elements in definition lists

return: string
param: array $matches

doFencedCodeBlocks($text)   X-Ref
Adding the fenced code block syntax to regular Markdown:

~~~
Code block
~~~

return: string
param: string $text

_doFencedCodeBlocks_callback($matches)   X-Ref
Callback to process fenced code blocks

return: string
param: array $matches

_doFencedCodeBlocks_newlines($matches)   X-Ref
Replace new lines in fenced code blocks

return: string
param: array $matches

formParagraphs($text, $wrap_in_p = true)   X-Ref
Parse text into paragraphs

return: string       HTML output
param: string $text String to process in paragraphs
param: boolean $wrap_in_p Whether paragraphs should be wrapped in <p> tags

stripFootnotes($text)   X-Ref
Footnotes - Strips link definitions from text, stores the URLs and
titles in hash references.

return: string
param: string $text

_stripFootnotes_callback($matches)   X-Ref
Callback for stripping footnotes

return: string
param: array $matches

doFootnotes($text)   X-Ref
Replace footnote references in $text [^id] with a special text-token
which will be replaced by the actual footnote marker in appendFootnotes.

return: string
param: string $text

appendFootnotes($text)   X-Ref
Append footnote list to text

return: string
param: string $text

_doFootnotes()   X-Ref
Generates the HTML for footnotes.  Called by appendFootnotes, even if
footnotes are not being appended.

return: void

_appendFootnotes_callback($matches)   X-Ref
Callback for appending footnotes

return: string
param: array $matches

parseFootnotePlaceholders($label, $footnote_number, $reference_number)   X-Ref
Build footnote label by evaluating any placeholders.
- ^^  footnote number
- %%  footnote reference number (Nth reference to footnote number)

return: string
param: string $label
param: int    $footnote_number
param: int    $reference_number

stripAbbreviations($text)   X-Ref
Abbreviations - strips abbreviations from text, stores titles in hash
references.

return: string
param: string $text

_stripAbbreviations_callback($matches)   X-Ref
Callback for stripping abbreviations

return: string
param: array $matches

doAbbreviations($text)   X-Ref
Find defined abbreviations in text and wrap them in <abbr> elements.

return: string
param: string $text

_doAbbreviations_callback($matches)   X-Ref
Callback for processing abbreviations

return: string
param: array $matches