Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

Markdown  -  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: 1889 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(string $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.

param: string $text
return: string

__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(string $text)   X-Ref
Main function. Performs some preprocessing on the input text and pass
it through the document gamut.

param: string $text
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

hashHTMLBlocks($text)   X-Ref
Hashify HTML blocks

param: string $text
return: string

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

param: string $matches
return: string

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.

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

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

param: string $text
return: string

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.

param: string $text
return: string

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.

param: string $text
return: string

doHorizontalRules($text)   X-Ref
Convert horizontal rules

param: string $text
return: string

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

param: string $text
return: string

doHardBreaks($text)   X-Ref
Do hard breaks

param: string $text
return: string

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

param: array $matches
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 method to parse referenced anchors

param: array $matches
return: string

_doAnchors_inline_callback($matches)   X-Ref
Callback method to parse 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 to parse references image tags

param: array $matches
return: string

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: array $matches
return: string

_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.
param: string $headerValue
return: string

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

param: string $text
return: string

_doLists_callback($matches)   X-Ref
List parsing callback

param: array $matches
return: string

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

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

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

param: array $matches
return: string

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

param: string $text
return: string

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

param: array $matches
return: string

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

param: string $code
return: string

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

param: string $text
return: string

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

param: string $text
return: string

_doBlockQuotes_callback($matches)   X-Ref
Blockquote parsing callback

param: array $matches
return: string

_doBlockQuotes_callback2($matches)   X-Ref
Blockquote parsing callback

param: array $matches
return: string

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

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

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

param: string $text
return: string

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.

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

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.

param: string $text
return: string

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

param: string $text
return: string

_doAutoLinks_url_callback($matches)   X-Ref
Parse URL callback

param: array $matches
return: string

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

param: array $matches
return: string

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.

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

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

param: string $str
return: string

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

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

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

param: string $text
return: string

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.

param: string $text
return: string

_detab_callback($matches)   X-Ref
Replace tabs callback

param: string $matches
return: string

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

param: string $text
return: string

_unhash_callback($matches)   X-Ref
Unhashing callback

param: array $matches
return: string