Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions PHP Markdown Extra Readme.text
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ by Michel Fortin
based on Markdown by John Gruber
<http://daringfireball.net/>

table of contents support added by Joe Topjian
<http://terrarum.net/>

Introduction
------------
Expand Down Expand Up @@ -219,6 +221,20 @@ Extra 1.2.5 (8 Jan 2012):
* Fixed an issue where HTML tags inside fenced code blocks were sometime
not encoded with entities.

Extra 1.2.4.1 (24 Sept 2010):
* Added Table of Contents support:

Create Table of Contents by specifying the following tag:

[TOC]

Table of Contents requirements:
* Only headings 2-6
* Headings must have an ID
* Builds TOC with headings _after_ the [TOC] tag

Table of Contents generation technnique was inspired by
[ReMarkable](http://camendesign.com/code/remarkable)

1.0.1n (10 Oct 2009):

Expand Down
34 changes: 28 additions & 6 deletions markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,7 @@ function MarkdownExtra_Parser() {
"stripFootnotes" => 15,
"stripAbbreviations" => 25,
"appendFootnotes" => 50,
"doTOC" => 55,
);
$this->block_gamut += array(
"doFencedCodeBlocks" => 5,
Expand Down Expand Up @@ -2252,23 +2253,23 @@ function doHeaders($text) {
(.+?) # $2 = Header text
[ ]*
\#* # optional closing #\'s (not counted)
(?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # id attribute
(?:[ ]+\{\#([^\}#.]*)\})? # id attribute
[ ]*
\n+
}xm',
array(&$this, '_doHeaders_callback_atx'), $text);

return $text;
}
function _doHeaders_attr($attr) {
if (empty($attr)) return "";
return " id=\"$attr\"";
function _doHeaders_attr($attr, $text) {
$id = empty($attr) ? str_replace(' ', '-', $text) : $attr;
return " id=\"$id\"";
}
function _doHeaders_callback_setext($matches) {
if ($matches[3] == '-' && preg_match('{^- }', $matches[1]))
return $matches[0];
$level = $matches[3]{0} == '=' ? 1 : 2;
$attr = $this->_doHeaders_attr($id =& $matches[2]);
$attr = $this->_doHeaders_attr($id =& $matches[2], $matches[1]);
$block = "<h$level$attr>".$this->runSpanGamut($matches[1])."</h$level>";
return "\n" . $this->hashBlock($block) . "\n\n";
}
Expand Down Expand Up @@ -2840,6 +2841,27 @@ function _doAbbreviations_callback($matches) {
}
}

function doTOC($text) {
#
# Adds TOC support by including the following on a single line:
#
# [TOC]
#
# TOC Requirements:
# * Only headings 2-6
# * Headings must have an ID
# * Builds TOC with headings _after_ the [TOC] tag

if (preg_match ('/\[TOC\]/m', $text, $i, PREG_OFFSET_CAPTURE)) {
$toc = '';
preg_match_all ('/<h([2-6]) id="([^"]+)">(.*?)<\/h\1>/i', $text, $h, PREG_SET_ORDER, $i[0][1]);
foreach ($h as &$m) $toc .= str_repeat ("\t", (int) $m[1]-2)."*\t [${m[3]}](#${m[2]})\n";
$text = preg_replace ('/\[TOC\]/m', Markdown($toc), $text);
}
return trim ($text, "\n");
}


}


Expand Down Expand Up @@ -2929,4 +2951,4 @@ function _doAbbreviations_callback($matches) {
software, even if advised of the possibility of such damage.

*/
?>
?>