From a66aa9e30e049614c007142ea58e3977b3c14ca2 Mon Sep 17 00:00:00 2001 From: Michael McClimon Date: Tue, 31 Jul 2012 14:00:55 -0400 Subject: [PATCH] Changes to allow both header id and classes in same curly brace set, but only in atx-style headers --- markdown.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/markdown.php b/markdown.php index 7b21f18..debc5ec 100644 --- a/markdown.php +++ b/markdown.php @@ -2254,9 +2254,9 @@ class MarkdownExtra_Parser extends Markdown_Parser { (.+?) # $2 = Header text [ ]* \#* # optional closing #\'s (not counted) - (?:[ ]+ \{(\#[-_:a-zA-Z0-9]+){0,1} - (?:[ ]+(\.[-_:a-zA-Z0-9]+))*\} )? # $3 = id/class attributes - #(?:[ ]+\{\.([-_:a-zA-Z0-9]+)\})? # $4 = class attribute + (?:[ ]+ \{((?:\#[-_:a-zA-Z0-9]+){0,1} + (?:[ ]*(?:\.[-_:a-zA-Z0-9]+))*)\} )? # $3 = id/class attributes + [ ]* \n+ }xm', @@ -2272,6 +2272,25 @@ class MarkdownExtra_Parser extends Markdown_Parser { if (empty($header_Class)) return ""; return " class=\"$header_Class\""; } + + function _doHeaders_attribs($header_Attr) { + if (empty($header_Attr)) return ""; + if (preg_match("/\#([-_:a-zA-Z0-9]+)[ ]*(.*)/", $header_Attr, $matches)) { + $id = "id=\"$matches[1]\" "; + $classes = preg_split("/[ ]+/", $matches[2]); + } else { + $id = ""; + $classes = preg_split("/[ ]+/", $header_Attr); + } + + echo($classes); + foreach ($classes as &$class) { + $class = preg_replace("/\./", "", $class); + } + $classString = "class=\"" . implode(" ", $classes) . "\""; + return " $id$classString"; + } + function _doHeaders_callback_setext($matches) { if ($matches[4] == '-' && preg_match('{^- }', $matches[1])) return $matches[0]; @@ -2284,9 +2303,8 @@ class MarkdownExtra_Parser extends Markdown_Parser { function _doHeaders_callback_atx($matches) { $level = strlen($matches[1]); - $headerId = $this->_doHeaders_id($id =& $matches[3]); - $headerClass = $this->_doHeaders_class($class =& $matches[4]); - $block = "".$this->runSpanGamut($matches[2]).""; + $attr = $this->_doHeaders_attribs($att =& $matches[3]); + $block = " ".$this->runSpanGamut($matches[2]).""; return "\n" . $this->hashBlock($block) . "\n\n"; }