Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

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

Author: Michel Fortin <michel.fortin@michelf.com>
Copyright: 2004-2022 Michel Fortin <https://michelf.com/projects/php-markdown/>
Copyright: (Original Markdown) 2004-2006 John Gruber <https://daringfireball.net/projects/markdown/>
File Size: 1871 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.
param: string $tag_name
param: string $attr
param: mixed  $defaultIdValue
param: array  $classes
return: string

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

param: string $text
return: string

Class: attr  - X-Ref

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

param: array $matches
return: string

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!
param: string $text
return: string

_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 )

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

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 )
param: string $text
param: string $hash_method
param: bool $md_attr Handle `markdown="1"` attribute
return: array

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.

param: string $text
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $alignname
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $list_str
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

~~~
Code block
~~~

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

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

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

param: string $text
return: string

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

param: array $matches
return: string

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.

param: string $text
return: string

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

param: string $text
return: string

_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

param: array $matches
return: string

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)

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

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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 )
param: string $text
param: string $hash_method
param: bool $md_attr Handle `markdown="1"` attribute
return: array

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.

param: string $text
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $alignname
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $list_str
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

~~~
Code block
~~~

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

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

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

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

param: string $text
return: string

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

param: array $matches
return: string

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.

param: string $text
return: string

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

param: string $text
return: string

_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

param: array $matches
return: string

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)

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

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string