Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-generate preconfigured class attributes #355

Open
wants to merge 1 commit into
base: lib
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions Michelf/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public static function defaultTransform($text) {
* @var bool
*/
public $enhanced_ordered_list = false;

public $class_attributes = null;

/**
* Parser implementation
Expand Down Expand Up @@ -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</a>";
$result = $this->hashPart($result);
Expand Down Expand Up @@ -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</a>";

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 '';
}
}
}