From 99dd2a3ecf6b0002b5ebdab2f8887bb66c3c03a3 Mon Sep 17 00:00:00 2001 From: Michael McClimon Date: Mon, 30 Jul 2012 19:54:04 -0400 Subject: [PATCH] Add support for header classes via {.class} --- markdown.php | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/markdown.php b/markdown.php index f548fc2..d1c7b84 100644 --- a/markdown.php +++ b/markdown.php @@ -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 = "".$this->runSpanGamut($matches[1]).""; + $level = $matches[4]{0} == '=' ? 1 : 2; + $headerId = $this->_doHeaders_id($id =& $matches[2]); + $headerClass = $this->_doHeaders_class($class =& $matches[3]); + $block = "".$this->runSpanGamut($matches[1]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } + function _doHeaders_callback_atx($matches) { $level = strlen($matches[1]); - $attr = $this->_doHeaders_attr($id =& $matches[3]); - $block = "".$this->runSpanGamut($matches[2]).""; + $headerId = $this->_doHeaders_id($id =& $matches[3]); + $headerClass = $this->_doHeaders_class($class =& $matches[4]); + $block = "".$this->runSpanGamut($matches[2]).""; return "\n" . $this->hashBlock($block) . "\n\n"; } + function doTables($text) { # # Form HTML tables.