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  -  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: 1909 lines (51 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: Markdown  - X-Ref

Markdown Parser Class

defaultTransform($text)   X-Ref
Simple function interface - Initialize the parser and return the result
of its transform method. This will work fine for derived classes too.

return: string
param: string $text

__construct()   X-Ref
Constructor function. Initialize appropriate member variables.

return: void

setup()   X-Ref
Called before the transformation process starts to setup parser states.

return: void

teardown()   X-Ref
Called after the transformation process to clear any variable which may
be taking up memory unnecessarly.

return: void

transform($text)   X-Ref
Main function. Performs some preprocessing on the input text and pass
it through the document gamut.

return: string
param: string $text

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

return: string
param: string $text

_stripLinkDefinitions_callback($matches)   X-Ref
The callback to strip link definitions

return: string
param: array $matches

hashHTMLBlocks($text)   X-Ref
Hashify HTML blocks

return: string
param: string $text

_hashHTMLBlocks_callback($matches)   X-Ref
The callback for hashing HTML blocks

return: string
param: string $matches

hashPart($text, $boundary = 'X')   X-Ref
Called whenever a tag must be hashed when a function insert an atomic
element in the text stream. Passing $text to through this function gives
a unique text-token which will be reverted back when calling unhash.

The $boundary argument specify what character should be used to surround
the token. By convension, "B" is used for block elements that needs not
to be wrapped into paragraph tags at the end, ":" is used for elements
that are word separators and "X" is used in the general case.

return: string
param: string $text
param: string $boundary

hashBlock($text)   X-Ref
Shortcut function for hashPart with block-level boundaries.

return: string
param: string $text

runBlockGamut($text)   X-Ref
Run block gamut tranformations.

We need to escape raw HTML in Markdown source before doing anything
else. This need to be done for each block, and not only at the
begining in the Markdown function since hashed blocks can be part of
list items and could have been indented. Indented blocks would have
been seen as a code block in a previous pass of hashHTMLBlocks.

return: string
param: string $text

runBasicBlockGamut($text)   X-Ref
Run block gamut tranformations, without hashing HTML blocks. This is
useful when HTML blocks are known to be already hashed, like in the first
whole-document pass.

return: string
param: string $text

doHorizontalRules($text)   X-Ref
Convert horizontal rules

return: string
param: string $text

runSpanGamut($text)   X-Ref
Run span gamut transformations

return: string
param: string $text

doHardBreaks($text)   X-Ref
Do hard breaks

return: string
param: string $text

_doHardBreaks_callback($matches)   X-Ref
Trigger part hashing for the hard break (callback method)

return: string
param: array $matches

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

return: string
param: string $text

_doAnchors_reference_callback($matches)   X-Ref
Callback method to parse referenced anchors

return: string
param: string $matches

_doAnchors_inline_callback($matches)   X-Ref
Callback method to parse inline anchors

return: string
param: string $matches

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

return: string
param: string $text

_doImages_reference_callback($matches)   X-Ref
Callback to parse references image tags

return: string
param: array $matches

_doImages_inline_callback($matches)   X-Ref
Callback to parse inline image tags

return: string
param: array $matches

doHeaders($text)   X-Ref
Parse Markdown heading elements to HTML

return: string
param: string $text

_doHeaders_callback_setext($matches)   X-Ref
Setext header parsing callback

return: string
param: array $matches

_doHeaders_callback_atx($matches)   X-Ref
ATX header parsing callback

return: string
param: array $matches

_generateIdFromHeaderValue($headerValue)   X-Ref
If a header_id_func property is set, we can use it to automatically
generate an id attribute.

This method returns a string in the form id="foo", or an empty string
otherwise.
return: string
param: string $headerValue

doLists($text)   X-Ref
Form HTML ordered (numbered) and unordered (bulleted) lists.

return: string
param: string $text

_doLists_callback($matches)   X-Ref
List parsing callback

return: string
param: array $matches

processListItems($list_str, $marker_any_re)   X-Ref
Process the contents of a single ordered or unordered list, splitting it
into individual list items.

return: string
param: string $list_str
param: string $marker_any_re

_processListItems_callback($matches)   X-Ref
List item parsing callback

return: string
param: array $matches

doCodeBlocks($text)   X-Ref
Process Markdown `<pre><code>` blocks.

return: string
param: string $text

_doCodeBlocks_callback($matches)   X-Ref
Code block parsing callback

return: string
param: array $matches

makeCodeSpan($code)   X-Ref
Create a code span markup for $code. Called from handleSpanToken.

return: string
param: string $code

prepareItalicsAndBold()   X-Ref
Prepare regular expressions for searching emphasis tokens in any
context.

return: void

doItalicsAndBold($text)   X-Ref
Convert Markdown italics (emphasis) and bold (strong) to HTML

return: string
param: string $text

doBlockQuotes($text)   X-Ref
Parse Markdown blockquotes to HTML

return: string
param: string $text

_doBlockQuotes_callback($matches)   X-Ref
Blockquote parsing callback

return: string
param: array $matches

_doBlockQuotes_callback2($matches)   X-Ref
Blockquote parsing callback

return: string
param: array $matches

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

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

encodeAttribute($text)   X-Ref
Encode text for a double-quoted HTML attribute. This function
is *not* suitable for attributes enclosed in single quotes.

return: string
param: string $text

encodeURLAttribute($url, &$text = null)   X-Ref
Encode text for a double-quoted HTML attribute containing a URL,
applying the URL filter if set. Also generates the textual
representation for the URL (removing mailto: or tel:) storing it in $text.
This function is *not* suitable for attributes enclosed in single quotes.

return: string        URL
param: string $url
param: string $text Passed by reference

encodeAmpsAndAngles($text)   X-Ref
Smart processing for ampersands and angle brackets that need to
be encoded. Valid character entities are left alone unless the
no-entities mode is set.

return: string
param: string $text

doAutoLinks($text)   X-Ref
Parse Markdown automatic links to anchor HTML tags

return: string
param: string $text

_doAutoLinks_url_callback($matches)   X-Ref
Parse URL callback

return: string
param: array $matches

_doAutoLinks_email_callback($matches)   X-Ref
Parse email address callback

return: string
param: array $matches

encodeEntityObfuscatedAttribute($text, &$tail = null, $head_length = 0)   X-Ref
Input: some text to obfuscate, e.g. "mailto:foo@example.com"

Output: the same text but with most characters encoded as either a
decimal or hex entity, in the hopes of foiling most address
harvesting spam bots. E.g.:

&#109;&#x61;&#105;&#x6c;&#116;&#x6f;&#58;&#x66;o&#111;
&#x40;&#101;&#x78;&#97;&#x6d;&#112;&#x6c;&#101;&#46;&#x63;&#111;
&#x6d;

Note: the additional output $tail is assigned the same value as the
ouput, minus the number of characters specified by $head_length.

Based by a filter by Matthew Wickline, posted to BBEdit-Talk.
With some optimizations by Milian Wolff. Forced encoding of HTML
attribute special characters by Allan Odgaard.

return: string
param: string  $text
param: string  $tail Passed by reference
param: integer $head_length

parseSpan($str)   X-Ref
Take the string $str and parse it into tokens, hashing embeded HTML,
escaped characters and handling code spans.

return: string
param: string $str

handleSpanToken($token, &$str)   X-Ref
Handle $token provided by parseSpan by determining its nature and
returning the corresponding value that should replace it.

return: string
param: string $token
param: string $str Passed by reference

outdent($text)   X-Ref
Remove one level of line-leading tabs or spaces

return: string
param: string $text

detab($text)   X-Ref
Replace tabs with the appropriate amount of spaces.

For each line we separate the line in blocks delemited by tab characters.
Then we reconstruct every line by adding the  appropriate number of space
between each blocks.

return: string
param: string $text

_detab_callback($matches)   X-Ref
Replace tabs callback

return: string
param: string $matches

_initDetab()   X-Ref
Check for the availability of the function in the `utf8_strlen` property
(initially `mb_strlen`). If the function is not available, create a
function that will loosely count the number of UTF-8 characters with a
regular expression.

return: void

unhash($text)   X-Ref
Swap back in all the tags hashed by _HashHTMLBlocks.

return: string
param: string $text

_unhash_callback($matches)   X-Ref
Unhashing callback

return: string
param: array $matches