Add support for header classes via {.class}

This commit is contained in:
Michael McClimon 2012-07-30 19:54:04 -04:00
parent 62a640e2ba
commit 99dd2a3ecf

View file

@ -2223,28 +2223,30 @@ class MarkdownExtra_Parser extends Markdown_Parser {
function doHeaders($text) {
#
# Redefined to add id attribute support.
# Redefined to add class attribute support
#
# Setext-style headers:
# Header 1 {#header1}
# Header 1 {#header1} {.class1}
# ========
#
# Header 2 {#header2}
# Header 2 {#header2} {.class2}
# --------
#
$text = preg_replace_callback(
'{
(^.+?) # $1: Header text
(?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # $2: Id attribute
[ ]*\n(=+|-+)[ ]*\n+ # $3: Header footer
(?:[ ]+\{\.([-_:a-zA-Z0-9]+)\})? # $3: class attribute
[ ]*\n(=+|-+)[ ]*\n+ # $4: Header footer
}mx',
array(&$this, '_doHeaders_callback_setext'), $text);
# atx-style headers:
# # Header 1 {#header1}
# ## Header 2 {#header2}
# ## Header 2 with closing hashes ## {#header3}
# # Header 1 {#header1} {.class1}
# ## Header 2 {#header2} {.class2}
# ## Header 2 with closing hashes ## {#header3} {.class3}
# ...
# ###### Header 6 {#header2}
# ###### Header 6 {#header2} {.class2}
#
$text = preg_replace_callback('{
^(\#{1,6}) # $1 = string of #\'s
@ -2252,7 +2254,8 @@ class MarkdownExtra_Parser extends Markdown_Parser {
(.+?) # $2 = Header text
[ ]*
\#* # optional closing #\'s (not counted)
(?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # id attribute
(?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # $3 = id attribute
(?:[ ]+\{\.([-_:a-zA-Z0-9]+)\})? # $4 = class attribute
[ ]*
\n+
}xm',
@ -2260,26 +2263,34 @@ class MarkdownExtra_Parser extends Markdown_Parser {
return $text;
}
function _doHeaders_attr($attr) {
if (empty($attr)) return "";
return " id=\"$attr\"";
function _doHeaders_id($header_Id) {
if (empty($header_Id)) return "";
return " id=\"$header_Id\"";
}
function _doHeaders_class($header_Class) {
if (empty($header_Class)) return "";
return " class=\"$header_Class\"";
}
function _doHeaders_callback_setext($matches) {
if ($matches[3] == '-' && preg_match('{^- }', $matches[1]))
if ($matches[4] == '-' && preg_match('{^- }', $matches[1]))
return $matches[0];
$level = $matches[3]{0} == '=' ? 1 : 2;
$attr = $this->_doHeaders_attr($id =& $matches[2]);
$block = "<h$level$attr>".$this->runSpanGamut($matches[1])."</h$level>";
$level = $matches[4]{0} == '=' ? 1 : 2;
$headerId = $this->_doHeaders_id($id =& $matches[2]);
$headerClass = $this->_doHeaders_class($class =& $matches[3]);
$block = "<h$level$headerId$headerClass>".$this->runSpanGamut($matches[1])."</h$level>";
return "\n" . $this->hashBlock($block) . "\n\n";
}
function _doHeaders_callback_atx($matches) {
$level = strlen($matches[1]);
$attr = $this->_doHeaders_attr($id =& $matches[3]);
$block = "<h$level$attr>".$this->runSpanGamut($matches[2])."</h$level>";
$headerId = $this->_doHeaders_id($id =& $matches[3]);
$headerClass = $this->_doHeaders_class($class =& $matches[4]);
$block = "<h$level$headerId$headerClass>".$this->runSpanGamut($matches[2])."</h$level>";
return "\n" . $this->hashBlock($block) . "\n\n";
}
function doTables($text) {
#
# Form HTML tables.