diff --git a/Michelf/Markdown.php b/Michelf/Markdown.php index 9cf4c77..2186b04 100644 --- a/Michelf/Markdown.php +++ b/Michelf/Markdown.php @@ -125,6 +125,8 @@ public static function defaultTransform($text) { * @var bool */ public $enhanced_ordered_list = false; + + public $class_attributes = null; /** * Parser implementation @@ -752,6 +754,7 @@ protected function _doAnchors_reference_callback($matches) { $result .= " title=\"$title\""; } + $result .= $this->build_class_attributes("a"); $link_text = $this->runSpanGamut($link_text); $result .= ">$link_text"; $result = $this->hashPart($result); @@ -786,6 +789,7 @@ protected function _doAnchors_inline_callback($matches) { $result .= " title=\"$title\""; } + $result .= $this->build_class_attributes("a"); $link_text = $this->runSpanGamut($link_text); $result .= ">$link_text"; @@ -869,6 +873,7 @@ protected function _doImages_reference_callback($matches) { $title = $this->encodeAttribute($title); $result .= " title=\"$title\""; } + $result .= $this->build_class_attributes("img"); $result .= $this->empty_element_suffix; $result = $this->hashPart($result); } else { @@ -897,6 +902,7 @@ protected function _doImages_inline_callback($matches) { $title = $this->encodeAttribute($title); $result .= " title=\"$title\""; // $title already quoted } + $result .= $this->build_class_attributes("img"); $result .= $this->empty_element_suffix; return $this->hashPart($result); @@ -1906,4 +1912,25 @@ protected function unhash($text) { protected function _unhash_callback($matches) { return $this->html_hashes[$matches[0]]; } + + /** + * Build html class attribute as preconfigured with the class_attributes field + * + * @param $tag string + * @return string + */ + protected function build_class_attributes($tag) { + if($this->class_attributes === null || !isset($this->class_attributes[$tag])) { + return ""; + } + + $attributes = $this->class_attributes[$tag]; + if(is_string($attributes)) { + return ' class="' . $attributes . '"'; + } else if(is_array($attributes)) { + return ' class="' . implode(' ', $attributes) . '"'; + } else { + return ''; + } + } }