From b06c15a793fe4083aa2adf054324095a89b40a34 Mon Sep 17 00:00:00 2001 From: Won-Kyu Park Date: Wed, 19 Feb 2014 15:03:18 +0900 Subject: [PATCH] extract render() HTML formatting method. --- Parsedown.php | 136 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 30 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index f3f3e41..b3d9fa7 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -764,6 +764,7 @@ class Parsedown # ~ + $markups = array(); $markup = ''; while ($markers) @@ -795,13 +796,19 @@ class Parsedown if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false) { - $markup .= $text; + if (isset($text[0])) + { + $markups[] = $text; + } break; } else { - $markup .= substr($text, 0, $closest_marker_position); + if ($closest_marker_position > 0) + { + $markups[] = substr($text, 0, $closest_marker_position); + } } $text = substr($text, $closest_marker_position); @@ -816,7 +823,7 @@ class Parsedown { case " \n": - $markup .= '
'."\n"; + $markup = array('tag' => 'br'); $offset = 3; @@ -892,34 +899,30 @@ class Parsedown if ($element['!']) { - $markup .= ''.$element['text'].' 'img', 'src' => $element['link'], 'alt' => $element['text']); if (isset($element['title'])) { - $markup .= ' title="'.$element['title'].'"'; + $markup['title'] = $element['title']; } - - $markup .= ' />'; } else { $element['text'] = $this->parse_span_elements($element['text'], $markers); - $markup .= ' 'a', 'href' => $element['link'], 'text' => $element['text']); if (isset($element['title'])) { - $markup .= ' title="'.$element['title'].'"'; + $markup['title'] = $element['title']; } - $markup .= '>'.$element['text'].''; } unset($element); } else { - $markup .= $closest_marker; + $markup = $closest_marker; $offset = $closest_marker === '![' ? 2 : 1; } @@ -930,13 +933,13 @@ class Parsedown if (preg_match('/^&#?\w+;/', $text, $matches)) { - $markup .= $matches[0]; + $markup = $matches[0]; $offset = strlen($matches[0]); } else { - $markup .= '&'; + $markup = '&'; $offset = 1; } @@ -951,14 +954,14 @@ class Parsedown $markers[] = $closest_marker; $matches[1] = $this->parse_span_elements($matches[1], $markers); - $markup .= ''.$matches[1].''; + $markup = array('tag' => 'strong', 'text' => $matches[1]); } elseif (preg_match(self::$em_regex[$closest_marker], $text, $matches)) { $markers[] = $closest_marker; $matches[1] = $this->parse_span_elements($matches[1], $markers); - $markup .= ''.$matches[1].''; + $markup = array('tag' => 'em', 'text' => $matches[1]); } if (isset($matches) and $matches) @@ -967,7 +970,7 @@ class Parsedown } else { - $markup .= $closest_marker; + $markup = $closest_marker; $offset = 1; } @@ -984,32 +987,32 @@ class Parsedown $element_url = str_replace('&', '&', $element_url); $element_url = str_replace('<', '<', $element_url); - $markup .= ''.$element_url.''; + $markup = array('tag' => 'a', 'href' => $element_url, 'text' => $element_url); $offset = strlen($matches[0]); } elseif (strpos($text, '@') > 1 and preg_match('/<(\S+?@\S+?)>/', $text, $matches)) { - $markup .= ''.$matches[1].''; + $markup = array('tag' => 'a', 'href' => 'mailto:'.$matches[1], 'text' => $matches[1]); $offset = strlen($matches[0]); } elseif (preg_match('/^<\/?\w.*?>/', $text, $matches)) { - $markup .= $matches[0]; + $markup = $matches[0]; $offset = strlen($matches[0]); } else { - $markup .= '<'; + $markup = '<'; $offset = 1; } } else { - $markup .= '<'; + $markup = '<'; $offset = 1; } @@ -1020,13 +1023,13 @@ class Parsedown if (in_array($text[1], self::$special_characters)) { - $markup .= $text[1]; + $markup = $text[1]; $offset = 2; } else { - $markup .= '\\'; + $markup = '\\'; $offset = 1; } @@ -1040,13 +1043,13 @@ class Parsedown $element_text = $matches[2]; $element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8'); - $markup .= ''.$element_text.''; + $markup = array('tag' => 'code', 'text' => $element_text); $offset = strlen($matches[0]); } else { - $markup .= '`'; + $markup = '`'; $offset = 1; } @@ -1061,13 +1064,13 @@ class Parsedown $element_url = str_replace('&', '&', $element_url); $element_url = str_replace('<', '<', $element_url); - $markup .= ''.$element_url.''; + $markup = array('tag' => 'a', 'href' => $element_url, 'text' => $element_url); $offset = strlen($matches[0]); } else { - $markup .= 'http'; + $markup = 'http'; $offset = 4; } @@ -1080,13 +1083,13 @@ class Parsedown { $matches[1] = $this->parse_span_elements($matches[1], $markers); - $markup .= ''.$matches[1].''; + $markup = array('tag' => 'del', 'text' => $matches[1]); $offset = strlen($matches[0]); } else { - $markup .= '~~'; + $markup = '~~'; $offset = 2; } @@ -1099,9 +1102,82 @@ class Parsedown $text = substr($text, $offset); } + $markups[] = $markup; + $markers[$closest_marker_index] = $closest_marker; } + return $this->render($markups); + } + + function render($elements) + { + $markup = ''; + $buffer = ''; + + foreach ($elements as $element) + { + if (is_string($element)) + { + # concat string elements + $buffer .= $element; + + continue; + } + + if (isset($buffer[0])) + { + # append string buffer + $markup.= $buffer; + $buffer = ''; + } + + switch ($element['tag']) + { + case 'a': + $markup .= ''; + + break; + + case 'img': + $markup .= ''.$element['text'].''; + + break; + } + } + + # append remaining string + if (isset($buffer[0])) + { + $markup.= $buffer; + } + return $markup; }