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

Misc bugfix and improved attribute parsing #101

Open
wants to merge 3 commits into
base: master
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
29 changes: 26 additions & 3 deletions ParsedownExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ protected function inlineLink($Excerpt)
{
$Link = parent::inlineLink($Excerpt);

// may return null, so abort if it does
if (!$Link) return $Link;

$remainder = substr($Excerpt['text'], $Link['extent']);

if (preg_match('/^[ ]*{('.$this->regexAttribute.'+)}/', $remainder, $matches))
Expand Down Expand Up @@ -433,18 +436,27 @@ protected function parseAttributeData($attributeString)
{
$Data = array();

$attributes = preg_split('/[ ]+/', $attributeString, - 1, PREG_SPLIT_NO_EMPTY);
$attributes = preg_split('/\s+/', $attributeString, null, PREG_SPLIT_NO_EMPTY);

foreach ($attributes as $attribute)
{
// If it's an id...
if ($attribute[0] === '#')
{
$Data['id'] = substr($attribute, 1);
}
else # "."

// Else if it's a class
elseif ($attribute[0] == ".")
{
$classes []= substr($attribute, 1);
}

// Else it must be an attribute
else {
$attr = explode('=', $attribute);
$Data[array_shift($attr)] = implode('=', $attr);
}
}

if (isset($classes))
Expand Down Expand Up @@ -522,5 +534,16 @@ protected function sortFootnotes($A, $B) # callback
# Fields
#

protected $regexAttribute = '(?:[#.][-\w]+[ ]*)';
/**
* Regex for finding attribute strings inside curly braces (e.g., {lang=en .nav-link}
*
* Explained:
*
* * Attribute may start with any alpha-numeric character, optionally preceded by `#` or `.`
* * Attribute may optionally contain `=`, `-`, or any other "word" character (`[a-zA-Z0-9_]`)
* * Attribute may optionally be followed by a space and then another attribute
*
*/
protected $regexAttribute = '(?:[#.]?[A-Za-z0-9][\w=-]* ?)';
}