From 98b17e335469878822df87a49a2b5794883026e1 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Fri, 17 Jan 2014 01:23:25 +0200 Subject: [PATCH] setext heading doesn't have to use regex --- Parsedown.php | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 56c8442..c9bf419 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -316,27 +316,35 @@ class Parsedown break; case '-': - - # setext heading (---) - - if ($line[0] === '-' and $element['type'] === 'paragraph' and ! isset($element['interrupted']) and preg_match('/^[-]+[ ]*$/', $line)) - { - $element['type'] = 'heading'; - $element['level'] = 2; - - continue 2; - } - - break; - case '=': - # setext heading (===) + # setext heading - if ($line[0] === '=' and $element['type'] === 'paragraph' and ! isset($element['interrupted']) and preg_match('/^[=]+[ ]*$/', $line)) + if ($element['type'] === 'paragraph' and isset($element['interrupted']) === false) { - $element['type'] = 'heading'; - $element['level'] = 1; + $is_heading = true; + + $chopped_line = rtrim($line); + + $i = 1; + + while (isset($chopped_line[$i])) + { + if ($chopped_line[$i] !== $line[0]) + { + $is_heading = false; + + break; + } + + $i++; + } + + if ($is_heading) + { + $element['type'] = 'heading'; + $element['level'] = $line[0] === '-' ? 2 : 1; + } continue 2; }