From 7ace421f6d3e7e4e0fb5e2a769b044f1d6932c69 Mon Sep 17 00:00:00 2001 From: hkdobrev Date: Mon, 22 Jul 2013 21:29:32 +0300 Subject: [PATCH] Better parsing of emphasis and strong elements - Regex is based on original Perl regex. - Added more tests. --- Parsedown.php | 16 +++++----------- tests/data/emphasis.html | 5 ++++- tests/data/emphasis.md | 8 +++++++- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index 4dec4eb..d1eefe0 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -605,19 +605,13 @@ class Parsedown $index ++; } } - - if (strpos($text, '*') !== FALSE) + + if (strpos($text, '*') !== FALSE or strpos($text, '_') !== FALSE) { - $text = preg_replace('/\*{2}(.*?)\*{2}/', '$1', $text); - $text = preg_replace('/\*(.*?)\*/', '$1', $text); + $text = preg_replace('/(\*\*|__)(.+?[*_]*)(?<=\S)\1/', '$2', $text); + $text = preg_replace('/(\*|_)(.+?)(?<=\S)\1/', '$2', $text); } - - if (strpos($text, '_') !== FALSE) - { - $text = preg_replace('/_{2}(\S.*?\S)_{2}/', '$1', $text); - $text = preg_replace('/_(\S.*?\S)_/', '$1', $text); - } - + $text = strtr($text, $map); return $text; diff --git a/tests/data/emphasis.html b/tests/data/emphasis.html index 2adbd1a..bcb613b 100644 --- a/tests/data/emphasis.html +++ b/tests/data/emphasis.html @@ -1,5 +1,8 @@

Here's an emphasis.

+

A short emphasis a b .

Here's a strong one.

Here's an emphasis that uses underscores.

Here's a strong emphasis that uses underscores.

-

This is _ not an emphasis _.

\ No newline at end of file +

This is _ not an emphasis _ neither is * that * .

+

Empty emphasis ** is not __ an emphasis.

+

Three asterisks are an emphasized asterisk * .

\ No newline at end of file diff --git a/tests/data/emphasis.md b/tests/data/emphasis.md index 45b57c0..1adc218 100644 --- a/tests/data/emphasis.md +++ b/tests/data/emphasis.md @@ -1,9 +1,15 @@ Here's *an emphasis*. +A short emphasis _a_ *b* . + Here's **a strong one**. Here's _an emphasis that uses underscores_. Here's __a strong emphasis that uses underscores__. -This is _ not an emphasis _. \ No newline at end of file +This is _ not an emphasis _ neither is * that * . + +Empty emphasis ** is not __ an emphasis. + +Three asterisks are an emphasized asterisk *** . \ No newline at end of file