MDL-44522 import markdown 1.4.0

This commit is contained in:
Petr Škoda
2014-03-14 13:40:11 +08:00
parent c0e88129d1
commit cfc9130ec5
6 changed files with 162 additions and 104 deletions
+45 -35
View File
@@ -17,11 +17,11 @@ namespace Michelf;
# Markdown Parser Class
#
class Markdown {
class Markdown implements MarkdownInterface {
### Version ###
const MARKDOWNLIB_VERSION = "1.3";
const MARKDOWNLIB_VERSION = "1.4.0";
### Simple Function Interface ###
@@ -1269,7 +1269,7 @@ class Markdown {
# Ampersand-encoding based entirely on Nat Irons's Amputator
# MT plugin: <http://bumppo.net/projects/amputator/>
$text = preg_replace('/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/',
'&amp;', $text);;
'&amp;', $text);
}
# Encode remaining <'s
$text = str_replace('<', '&lt;', $text);
@@ -1302,9 +1302,16 @@ class Markdown {
>
}xi',
array(&$this, '_doAutoLinks_email_callback'), $text);
$text = preg_replace_callback('{<(tel:([^\'">\s]+))>}i',array(&$this, '_doAutoLinks_tel_callback'), $text);
return $text;
}
protected function _doAutoLinks_tel_callback($matches) {
$url = $this->encodeAttribute($matches[1]);
$tel = $this->encodeAttribute($matches[2]);
$link = "<a href=\"$url\">$tel</a>";
return $this->hashPart($link);
}
protected function _doAutoLinks_url_callback($matches) {
$url = $this->encodeAttribute($matches[1]);
$link = "<a href=\"$url\">$url</a>";
@@ -1524,7 +1531,7 @@ class Markdown {
# one.
#
class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
abstract class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
### Configuration Variables ###
@@ -1832,9 +1839,6 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
<\?.*?\?> | <%.*?%> # Processing instruction
|
<!\[CDATA\[.*?\]\]> # CData Block
|
# Code span marker
`+
'. ( !$span ? ' # If not in span.
|
# Indented code block
@@ -1846,7 +1850,7 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
|
# Fenced code block marker
(?<= ^ | \n )
[ ]{0,'.($indent+3).'}~{3,}
[ ]{0,'.($indent+3).'}(?:~{3,}|`{3,})
[ ]*
(?:
\.?[-_:a-zA-Z0-9]+ # standalone class name
@@ -1854,8 +1858,14 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
'.$this->id_class_attr_nocatch_re.' # extra attributes
)?
[ ]*
\n
(?= \n )
' : '' ). ' # End (if not is span).
|
# Code span marker
# Note, this regex needs to go after backtick fenced
# code blocks but it should also be kept outside of the
# "if not in span" condition adding backticks to the parser
`+
)
}xs';
@@ -1897,28 +1907,12 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
$text = $parts[2]; # Remaining text after current tag.
$tag_re = preg_quote($tag); # For use in a regular expression.
#
# Check for: Code span marker
#
if ($tag{0} == "`") {
# Find corresponding end marker.
$tag_re = preg_quote($tag);
if (preg_match('{^(?>.+?|\n(?!\n))*?(?<!`)'.$tag_re.'(?!`)}',
$text, $matches))
{
# End marker found: pass text unchanged until marker.
$parsed .= $tag . $matches[0];
$text = substr($text, strlen($matches[0]));
}
else {
# Unmatched marker: just skip it.
$parsed .= $tag;
}
}
#
# Check for: Fenced code block marker.
# Note: need to recheck the whole tag to disambiguate backtick
# fences from code spans
#
else if (preg_match('{^\n?([ ]{0,'.($indent+3).'})(~+)}', $tag, $capture)) {
if (preg_match('{^\n?([ ]{0,'.($indent+3).'})(~{3,}|`{3,})[ ]*(?:\.?[-_:a-zA-Z0-9]+|'.$this->id_class_attr_nocatch_re.')?[ ]*\n?$}', $tag, $capture)) {
# Fenced code block marker: find matching end marker.
$fence_indent = strlen($capture[1]); # use captured indent in re
$fence_re = $capture[2]; # use captured fence in re
@@ -1943,6 +1937,25 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
$parsed .= $tag;
}
#
# Check for: Code span marker
# Note: need to check this after backtick fenced code blocks
#
else if ($tag{0} == "`") {
# Find corresponding end marker.
$tag_re = preg_quote($tag);
if (preg_match('{^(?>.+?|\n(?!\n))*?(?<!`)'.$tag_re.'(?!`)}',
$text, $matches))
{
# End marker found: pass text unchanged until marker.
$parsed .= $tag . $matches[0];
$text = substr($text, strlen($matches[0]));
}
else {
# Unmatched marker: just skip it.
$parsed .= $tag;
}
}
#
# Check for: Opening Block level tag or
# Opening Context Block tag (like ins and del)
# used as a block tag (tag is alone on it's line).
@@ -2767,7 +2780,7 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
(?:\n|\A)
# 1: Opening marker
(
~{3,} # Marker: three tilde or more.
(?:~{3,}|`{3,}) # 3 or more tildes/backticks.
)
[ ]*
(?:
@@ -2786,7 +2799,7 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
)
# Closing marker.
\1 [ ]* \n
\1 [ ]* (?= \n )
}xm',
array(&$this, '_doFencedCodeBlocks_callback'), $text);
@@ -2936,8 +2949,8 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
$text .= "<div class=\"footnotes\">\n";
$text .= "<hr". $this->empty_element_suffix ."\n";
$text .= "<ol>\n\n";
$attr = " rev=\"footnote\"";
$attr = "";
if ($this->fn_backlink_class != "") {
$class = $this->fn_backlink_class;
$class = $this->encodeAttribute($class);
@@ -3091,6 +3104,3 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
}
}
?>
-2
View File
@@ -36,5 +36,3 @@ class MarkdownExtra extends \Michelf\_MarkdownExtra_TmpImpl {
}
?>
+37
View File
@@ -0,0 +1,37 @@
<?php
#
# Markdown - A text-to-HTML conversion tool for web writers
#
# PHP Markdown
# Copyright (c) 2004-2013 Michel Fortin
# <http://michelf.com/projects/php-markdown/>
#
# Original Markdown
# Copyright (c) 2004-2006 John Gruber
# <http://daringfireball.net/projects/markdown/>
#
namespace Michelf;
#
# Markdown Parser Interface
#
interface MarkdownInterface {
#
# Initialize the parser and return the result of its transform method.
# This will work fine for derived classes too.
#
public static function defaultTransform($text);
#
# Main function. Performs some preprocessing on the input text
# and pass it through the document gamut.
#
public function transform($text);
}
?>
+78 -66
View File
@@ -1,7 +1,7 @@
PHP Markdown
============
PHP Markdown Lib 1.3 - 11 Apr 2013
PHP Markdown Lib 1.4.0 - 29 Nov 2013
by Michel Fortin
<http://michelf.ca/>
@@ -14,23 +14,21 @@ Introduction
------------
This is a library package that includes the PHP Markdown parser and its
sibling PHP Markdown Extra which additional features.
sibling PHP Markdown Extra with additional features.
Markdown is a text-to-HTML conversion tool for web writers. Markdown
allows you to write using an easy-to-read, easy-to-write plain text
format, then convert it to structurally valid XHTML (or HTML).
"Markdown" is two things: a plain text markup syntax, and a software
tool, written in Perl, that converts the plain text markup to HTML.
PHP Markdown is a port to PHP of the original Markdown program by
John Gruber.
"Markdown" is actually two things: a plain text markup syntax, and a
software tool, originally written in Perl, that converts the plain text
markup to HTML. PHP Markdown is a port to PHP of the original Markdown
program by John Gruber.
PHP Markdown can work as a plug-in for WordPress, as a modifier for
the Smarty templating engine, or as a replacement for Textile
formatting in any software that supports Textile.
Full documentation of Markdown's syntax is available on John's
Markdown page: <http://daringfireball.net/projects/markdown/>
* [Full documentation of the Markdown syntax](<http://daringfireball.net/projects/markdown/>)
- Daring Fireball (John Gruber)
* [Markdown Extra syntax additions](<http://michelf.ca/projects/php-markdown/extra/>)
- Michel Fortin
Requirement
@@ -51,48 +49,8 @@ Usage
This library package is meant to be used with class autoloading. For autoloading
to work, your project needs have setup a PSR-0-compatible autoloader. See the
included Readme.php file for a minimal autoloader setup. (If you don't want to
use autoloading you can do a classic `require_once` to manually include the
files prior use instead.)
With class autoloading in place, putting the 'Michelf' folder in your
include path should be enough for this to work:
use \Michelf\Markdown;
$my_html = Markdown::defaultTransform($my_text);
Markdown Extra syntax is also available the same way:
use \Michelf\MarkdownExtra;
$my_html = MarkdownExtra::defaultTransform($my_text);
If you wish to use PHP Markdown with another text filter function
built to parse HTML, you should filter the text *after* the `transform`
function call. This is an example with [PHP SmartyPants][psp]:
use \Michelf\Markdown, \Michelf\SmartyPants;
$my_html = Markdown::defaultTransform($my_text);
$my_html = SmartyPants::defaultTransform($my_html);
All these examples are using the static `defaultTransform` static function
found inside the parser class. If you want to customize the parser
configuration, you can also instantiate it directly and change some
configuration variables:
use \Michelf\MarkdownExtra;
$parser = new MarkdownExtra;
$parser->fn_id_prefix = "post22-";
$my_html = $parser->transform($my_text);
Usage
-----
This library package is meant to be used with class autoloading. For autoloading
to work, your project needs have setup a PSR-0-compatible autoloader. See the
included Readme.php file for a minimal autoloader setup. (If you don't want to
use autoloading you can do a classic `require_once` to manually include the
files prior use instead.)
included Readme.php file for a minimal autoloader setup. (If you cannot use
autoloading, see below.)
With class autoloading in place, putting the 'Michelf' folder in your
include path should be enough for this to work:
@@ -125,10 +83,26 @@ configuration variables:
To learn more, see the full list of [configuration variables].
[configuration variables]: http://michelf.ca/project/php-markdown/configuration/
[configuration variables]: http://michelf.ca/projects/php-markdown/configuration/
Public API and Versionning Policy
### Usage without an autoloader
If you cannot use class autoloading, you can still use `include` or `require`
to access the parser. To load the `\Michelf\Markdown` parser, do it this way:
require_once 'Michelf/Markdown.inc.php';
Or, if you need the `\Michelf\MarkdownExtra` parser:
require_once 'Michelf/MarkdownExtra.inc.php';
While the plain `.php` files depend on autoloading to work correctly, using the
`.inc.php` files instead will eagerly load the dependencies that would be
loaded on demand if you were using autoloading.
Public API and Versioning Policy
---------------------------------
Version numbers are of the form *major*.*minor*.*patch*.
@@ -139,22 +113,22 @@ functions and their configuration variables. The public API is stable for
a given major version number. It might get additions when the minor version
number increments.
**Protected members are not considered public API.** This is unconventionnal
**Protected members are not considered public API.** This is unconventional
and deserves an explanation. Incrementing the major version number every time
the underlying implementation of something changes is going to give nonsential
version numbers for the vast majority of people who just use the parser.
Protected members are meant to create parser subclasses that behave in
the underlying implementation of something changes is going to give
nonessential version numbers for the vast majority of people who just use the
parser. Protected members are meant to create parser subclasses that behave in
different ways. Very few people create parser subclasses. I don't want to
discourage it by making everything private, but at the same time I can't
guarenty any stable hook between versions if you use protected members.
guarantee any stable hook between versions if you use protected members.
**Syntax changes** will increment the minor number for new features, and the
patch number for small corrections. A *new feature* is something that needs a
change in the syntax documentation. Note that since PHP Markdown Lib includes
two parsers, a syntax change for either of them will increment the minor
number. Also note that there is nothigng perfectly backward-compatible with the
number. Also note that there is nothing perfectly backward-compatible with the
Markdown syntax: all inputs are always valid, so new features always replace
something that was previously legal, although generally non-sensial to do.
something that was previously legal, although generally nonsensical to do.
Bugs
@@ -171,17 +145,55 @@ that the backtrack limit is not too low by running `php --info | grep pcre`.
See Installation and Requirement above for details.
Development and Testing
-----------------------
Pull requests for fixing bugs are welcome. Proposed new features are
going meticulously reviewed -- taking into account backward compatibility,
potential side effects, and future extensibility -- before deciding on
acceptance or rejection.
If you make a pull request that includes changes to the parser please add
tests for what is being changed to [MDTest][] and make a pull request there
too.
[MDTest]: https://github.com/michelf/mdtest/
Version History
---------------
PHP Markdown Lib 1.3 (11 Apr 2013):
PHP Markdown Lib 1.4.0 (29 Nov 2013)
* Added support for the `tel:` URL scheme in automatic links.
<tel:+1-111-111-1111>
It gets converted to this (note the `tel:` prefix becomes invisible):
<a href="tel:+1-111-111-1111">+1-111-111-1111</a>
* Added backtick fenced code blocks to MarkdownExtra, originally from
Github-Flavored Markdown.
* Added an interface called MarkdownInterface implemented by both
the Markdown and MarkdownExtra parsers. You can use the interface if
you want to create a mockup parser object for unit testing.
* For those of you who cannot use class autoloading, you can now
include `Michelf/Markdown.inc.php` or `Michelf/MarkdownExtra.inc.php` (note
the `.inc.php` extension) to automatically include other files required
by the parser.
PHP Markdown Lib 1.3 (11 Apr 2013)
This is the first release of PHP Markdown Lib. This package requires PHP
version 4.3 or later and is designed to work with PSR-0 autoloading and,
version 5.3 or later and is designed to work with PSR-0 autoloading and,
optionally with Composer. Here is a list of the changes since
PHP Markdown Extra 1.2.6:
* Plugin interface for Wordpress and other systems is no longer present in
* Plugin interface for WordPress and other systems is no longer present in
the Lib package. The classic package is still available if you need it:
<http://michelf.ca/projects/php-markdown/classic/>
+1 -1
View File
@@ -235,7 +235,7 @@
<location>markdown</location>
<name>Markdown original+extra</name>
<license>BSD</license>
<version>1.3.0</version>
<version>1.4.0</version>
<licenseversion></licenseversion>
</library>
<library>
+1
View File
@@ -1784,6 +1784,7 @@ function markdown_to_html($text) {
return $text;
}
require_once($CFG->libdir .'/markdown/MarkdownInterface.php');
require_once($CFG->libdir .'/markdown/Markdown.php');
require_once($CFG->libdir .'/markdown/MarkdownExtra.php');