From ba7f377290a7d0b38bce56bd4bc3aa1bc8d0aab4 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Wed, 5 Feb 2014 14:03:23 +0200 Subject: [PATCH] resolve #88, resolve #81 --- Parsedown.php | 65 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index efb11dc..30444b0 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -426,18 +426,69 @@ class Parsedown # reference - if (preg_match('/^\[(.+?)\]:[ ]*(.+?)(?:[ ]+[\'"](.+?)[\'"])?[ ]*$/', $outdented_line, $matches)) + $position = strpos($outdented_line, ']:'); + + if ($position) { - $label = strtolower($matches[1]); + $reference = array(); - $this->reference_map[$label] = array( - '»' => trim($matches[2], '<>'), - ); + $label = substr($outdented_line, 1, $position - 1); - if (isset($matches[3])) + $substring = substr($outdented_line, $position + 2); + $substring = trim($substring); + + if ($substring[0] === '<') { - $this->reference_map[$label]['#'] = $matches[3]; + $position = strpos($substring, '>'); + + if ($position === false) + { + break; + } + + $reference['»'] = substr($substring, 1, $position - 1); + + $substring = substr($substring, $position + 1); } + else + { + $position = strpos($substring, ' '); + + if ($position === false) + { + $reference['»'] = $substring; + + $substring = false; + } + else + { + $reference['»'] = substr($substring, 0, $position); + + $substring = substr($substring, $position + 1); + } + } + + if ($substring !== false) + { + $last_char = substr($substring, -1); + + switch (true) + { + case $substring[0] === '"' and $substring[0] === $last_char: + case $substring[0] === "'" and $substring[0] === $last_char: + case $substring[0] === '(' and $last_char === ')': + + $reference['#'] = substr($substring, 1, -1); + + break; + + default: + + break 2; + } + } + + $this->reference_map[$label] = $reference; continue 2; }