Autoría | Ultima modificación | Ver Log |
PHP Markdown============by Michel Fortin<https://michelf.ca/>based on Markdown by John Gruber<https://daringfireball.net/>Introduction------------This is a library package that includes the PHP Markdown parser and itssibling PHP Markdown Extra with additional features.Markdown is a text-to-HTML conversion tool for web writers. Markdownallows you to write using an easy-to-read, easy-to-write plain textformat, then convert it to structurally valid XHTML (or HTML)."Markdown" is actually two things: a plain text markup syntax, and asoftware tool, originally written in Perl, that converts the plain textmarkup to HTML. PHP Markdown is a port to PHP of the original Markdownprogram by John Gruber.* [Full documentation of the Markdown syntax](<https://daringfireball.net/projects/markdown/>)— Daring Fireball (John Gruber)* [Markdown Extra syntax additions](<https://michelf.ca/projects/php-markdown/extra/>)— Michel FortinRequirement-----------This library package requires PHP 7.4 or later.Note: The older plugin/library hybrid package for PHP Markdown andPHP Markdown Extra is no longer maintained but will work with PHP 4.0.5 andlater.You might need to set pcre.backtrack_limit higher than 1 000 000(the default), though the default is usually fine.Usage-----To use this library with Composer, first install it with:$ composer require michelf/php-markdownThen include Composer's generated vendor/autoload.php to [enable autoloading]:require 'vendor/autoload.php';Without Composer, for autoloading to work, your project needs an autoloadercompatible with PSR-4 or PSR-0. See the included Readme.php file for a minimalautoloader setup. (If you cannot use autoloading, see below.)With class autoloading in place: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 functionbuilt to parse HTML, you should filter the text *after* the `transform`function call. This is an example with [PHP SmartyPants]: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 functionfound inside the parser class. If you want to customize the parserconfiguration, you can also instantiate it directly and change someconfiguration variables:use Michelf\MarkdownExtra;$parser = new MarkdownExtra;$parser->fn_id_prefix = "post22-";$my_html = $parser->transform($my_text);To learn more, see the full list of [configuration variables].[enable autoloading]: https://getcomposer.org/doc/01-basic-usage.md#autoloading[PHP SmartyPants]: https://michelf.ca/projects/php-smartypants/[configuration variables]: https://michelf.ca/projects/php-markdown/configuration/### Usage without an autoloaderIf 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 beloaded on demand if you were using autoloading.Public API and Versioning Policy---------------------------------Version numbers are of the form *major*.*minor*.*patch*.The public API of PHP Markdown consist of the two parser classes `Markdown`and `MarkdownExtra`, their constructors, the `transform` and `defaultTransform`functions and their configuration variables. The public API is stable fora given major version number. It might get additions when the minor versionnumber increments.**Protected members are not considered public API.** This is unconventionaland deserves an explanation. Incrementing the major version number every timethe underlying implementation of something changes is going to givenonessential version numbers for the vast majority of people who just use theparser. Protected members are meant to create parser subclasses that behave indifferent ways. Very few people create parser subclasses. I don't want todiscourage it by making everything private, but at the same time I can'tguarantee any stable hook between versions if you use protected members.**Syntax changes** will increment the minor number for new features, and thepatch number for small corrections. A *new feature* is something that needs achange in the syntax documentation. Note that since PHP Markdown Lib includestwo parsers, a syntax change for either of them will increment the minornumber. Also note that there is nothing perfectly backward-compatible with theMarkdown syntax: all inputs are always valid, so new features always replacesomething that was previously legal, although generally nonsensical to do.Bugs----To file bug reports please send email to:<michel.fortin@michelf.ca>Please include with your report: (1) the example input; (2) the output youexpected; (3) the output PHP Markdown actually produced.If you have a problem where Markdown gives you an empty result, first checkthat 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 aregoing to be meticulously reviewed -- taking into account backward compatibility,potential side effects, and future extensibility -- before deciding onacceptance or rejection.If you make a pull request that includes changes to the parser please addtests for what is being changed to the `test/` directory. This can be assimple as adding a `.text` (input) file with a corresponding `.xhtml`(output) file to proper category under `./test/resources/`.Traditionally tests were in a separate repository, [MDTest](https://github.com/michelf/mdtest)but they are now located here, alongside the source code.Donations---------If you wish to make a donation that will help me devote more time toPHP Markdown, please visit [michelf.ca/donate].[michelf.ca/donate]: https://michelf.ca/donate/#!Thanks%20for%20PHP%20MarkdownVersion History---------------PHP Markdown Lib 2.0.0 (26 Sep 2022)* Now requiring PHP version 7.4 or later.* Added type annotations to configuration properties of the parser.(Thanks to Tac Tacelosky.)* Fixing a TypeError in PHP 8 caused by invalid counter variable.(Thanks to Alexey Kopytko.)* Composer package now excludes development files.(Thanks to Cédric Anne.)PHP Markdown Lib 1.9.1 (23 Nov 2021)* Now treating `<details>` and `<summary>` as block level so they don'tget wrapped in `<p>`.(Thanks to Thomas Hochstein for the fix.)* Fix for unintended blank title attribute when adding supplementary attributesto a link in Markdown Extra.(Thanks to Richie Black for the fix.)PHP Markdown Lib 1.9.0 (1 Dec 2019)* Added `fn_backlink_label` configuration variable to put some text in the`aria-label` attribute.(Thanks to Sunny Walker for the implementation.)* Occurances of "`^^`" in `fn_backlink_html`, `fn_backlink_class`,`fn_backlink_title`, and `fn_backlink_label` will be replaced by thecorresponding footnote number in the HTML output. Occurances of "`%%`" will bereplaced by a number for the reference (footnotes can have multiple references).(Thanks to Sunny Walker for the implementation.)* Added configuration variable `omit_footnotes`. When `true` footnotes are notappended at the end of the generated HTML and the `footnotes_assembled`variable will contain the HTML for the footnote list, allowing footnotes to bemoved somewhere else on the page.(Thanks to James K. for the implementation.)Note: when placing the content of `footnotes_assembled` on the page, consideradding the attribute `role="doc-endnotes"` to the `<div>` or `<section>` that willenclose the list of footnotes so they are reachable to accessibility tools thesame way they would be with the default HTML output.* Fixed deprecation warnings from PHP about usage of curly braces to accesscharacters in text strings.(Thanks to Remi Collet and Frans-Willem Post.)PHP Markdown Lib 1.8.0 (14 Jan 2018)* Autoloading with Composer now uses PSR-4.* HTML output for Markdown Extra footnotes now include `role` attributeswith values from [WAI-ARIA](https://www.w3.org/TR/dpub-aria/) tomake them more accessible.(Thanks to Tobias Bengfort)* In Markdown Extra, added the `hashtag_protection` configuration variable.When set to `true` it prevents ATX-style headers with no space after the initialhash from being interpreted as headers. This way your precious hashtagsare preserved.(Thanks to Jaussoin Timothée for the implementation.)PHP Markdown Lib 1.7.0 (29 Oct 2016)* Added a `hard_wrap` configuration variable to make all newline charactersin the text become `<br>` tags in the HTML output. By default, accordingto the standard Markdown syntax these newlines are ignored unless they apreceded by two spaces. Thanks to Jonathan Cohlmeyer for the implementation.* Improved the parsing of list items to fix problematic cases that came tolight with the addition of `hard_wrap`. This should have no effect on theoutput except span-level list items that ended with two spaces (and thusended with a line break).* Added a `code_span_content_func` configuration variable which takes afunction that will convert the content of the code span to HTML. This canbe useful to implement syntax highlighting. Although contrary to itscode block equivalent, there is no syntax for specifying a language.Credits to styxit for the implementation.* Fixed a Markdown Extra issue where two-space-at-end-of-line hard breakswouldn't work inside of HTML block elements such as `<p markdown="1">`where the element expects only span-level content.* In the parser code, switched to PHPDoc comment format. Thanks toRobbie Averill for the help.PHP Markdown Lib 1.6.0 (23 Dec 2015)Note: this version was incorrectly released as 1.5.1 on Dec 22, a numberthat contradicted the versioning policy.* For fenced code blocks in Markdown Extra, can now set a class name for thecode block's language before the special attribute block. Previously, thisclass name was only allowed in the absence of the special attribute block.* Added a `code_block_content_func` configuration variable which takes afunction that will convert the content of the code block to HTML. This ismost useful for syntax highlighting. For fenced code blocks in MarkdownExtra, the function has access to the language class name (the one outsideof the special attribute block). Credits to Mario Konrad for providing theimplementation.* The curled arrow character for the backlink in footnotes is now followedby a Unicode variant selector to prevent it from being displayed in emojiform on iOS.Note that in older browsers the variant selector is often interpreted as aseparate character, making it visible after the arrow. So there is now aalso a `fn_backlink_html` configuration variable that can be used to setthe link text to something else. Credits to Dana for providing theimplementation.* Fixed an issue in MarkdownExtra where long header lines followed by aspecial attribute block would hit the backtrack limit an cause an emptystring to be returned.PHP Markdown Lib 1.5.0 (1 Mar 2015)* Added the ability start ordered lists with a number different from 1 andand have that reflected in the HTML output. This can be enabled withthe `enhanced_ordered_lists` configuration variable for the Markdownparser; it is enabled by default for Markdown Extra.Credits to Matt Gorle for providing the implementation.* Added the ability to insert custom HTML attributes with simple valueseverywhere an extra attribute block is allowed (links, images, headers).The value must be unquoted, cannot contains spaces and is limited toalphanumeric ASCII characters.Credits to Peter Droogmans for providing the implementation.* Added a `header_id_func` configuration variable which takes a functionthat can generate an `id` attribute value from the header text.Credits to Evert Pot for providing the implementation.* Added a `url_filter_func` configuration variable which takes a functionthat can rewrite any link or image URL to something different.PHP Markdown Lib 1.4.1 (4 May 2014)* The HTML block parser will now treat `<figure>` as a block-level element(as it should) and no longer wrap it in `<p>` or parse it's content withthe as Markdown syntax (although with Extra you can use `markdown="1"`if you wish to use the Markdown syntax inside it).* The content of `<style>` elements will now be left alone, its contentwon't be interpreted as Markdown.* Corrected an bug where some inline links with spaces in them would notwork even when surounded with angle brackets:[link](<s p a c e s>)* Fixed an issue where email addresses with quotes in them would not alwayshave the quotes escaped in the link attribute, causing broken links (andinvalid HTML).* Fixed the case were a link definition following a footnote definition wouldbe swallowed by the footnote unless it was separated by a blank line.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 fromGithub-Flavored Markdown.* Added an interface called MarkdownInterface implemented by boththe Markdown and MarkdownExtra parsers. You can use the interface ifyou want to create a mockup parser object for unit testing.* For those of you who cannot use class autoloading, you can nowinclude `Michelf/Markdown.inc.php` or `Michelf/MarkdownExtra.inc.php` (notethe `.inc.php` extension) to automatically include other files requiredby the parser.PHP Markdown Lib 1.3 (11 Apr 2013)This is the first release of PHP Markdown Lib. This package requires PHPversion 5.3 or later and is designed to work with PSR-0 autoloading and,optionally with Composer. Here is a list of the changes sincePHP Markdown Extra 1.2.6:* Plugin interface for WordPress and other systems is no longer present inthe Lib package. The classic package is still available if you need it:<https://michelf.ca/projects/php-markdown/classic/>* Added `public` and `protected` protection attributes, plus a section aboutwhat is "public API" and what isn't in the Readme file.* Changed HTML output for footnotes: now instead of adding `rel` and `rev`attributes, footnotes links have the class name `footnote-ref` andbacklinks `footnote-backref`.* Fixed some regular expressions to make PCRE not shout warnings about POSIXcollation classes (dependent on your version of PCRE).* Added optional class and id attributes to images and links using the samesyntax as for headers:[link](url){#id .class}{#id .class}It work too for reference-style links and images. In this case you needto put those attributes at the reference definition:[link][linkref] or [linkref]![img][linkref][linkref]: url "optional title" {#id .class}* Fixed a PHP notice message triggered when some table column separatormarkers are missing on the separator line below column headers.* Fixed a small mistake that could cause the parser to retain an invalidstate related to parsing links across multiple runs. This was neverobserved (that I know of), but it's still worth fixing.Copyright and License---------------------PHP Markdown LibCopyright (c) 2004-2022 Michel Fortin<https://michelf.ca/>All rights reserved.Based on MarkdownCopyright (c) 2003-2005 John Gruber<https://daringfireball.net/>All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:* Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with thedistribution.* Neither the name "Markdown" nor the names of its contributors maybe used to endorse or promote products derived from this softwarewithout specific prior written permission.This software is provided by the copyright holders and contributors "asis" and any express or implied warranties, including, but not limitedto, the implied warranties of merchantability and fitness for aparticular purpose are disclaimed. In no event shall the copyright owneror contributors be liable for any direct, indirect, incidental, special,exemplary, or consequential damages (including, but not limited to,procurement of substitute goods or services; loss of use, data, orprofits; or business interruption) however caused and on any theory ofliability, whether in contract, strict liability, or tort (includingnegligence or otherwise) arising in any way out of the use of thissoftware, even if advised of the possibility of such damage.