Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up the normalize functions

  • Loading branch information...
commit f5a9b1ddc409f792108b9f3a4b5747426ff4a441 1 parent 49a9d39
@PeeHaa authored
Showing with 43 additions and 34 deletions.
  1. +43 −34 Markdown.php
View
77 Markdown.php
@@ -274,12 +274,10 @@ protected function prepareItalicsAndBold()
*/
public function transform($text)
{
+ // first let's normalize the text
$text = $this->removeBom($text);
-
$text = $this->normalizeLinebreaks($text);
-
- # Convert all tabs to spaces.
- $text = $this->detab($text);
+ $text = $this->normalizeTabs($text);
# Turn block-level HTML blocks into hash entries
$text = $this->hashHTMLBlocks($text);
@@ -326,6 +324,47 @@ public function normalizeLinebreaks($text)
return preg_replace('{\r\n?}', "\n", $text) . "\n\n";
}
+ /**
+ * Normalizes tabs
+ * Replaces all tab characters with spaces defined in the TAB_WIDTH const
+ *
+ * @param string $text The text
+ *
+ * @return string The text with normalized tab characters
+ */
+ protected function normalizeTabs($text)
+ {
+ return preg_replace_callback('/^.*\t.*$/m', array(&$this, '_detab_callback'), $text);
+ }
+
+ /**
+ * Callback function to replace tabs by spaces
+ *
+ * @param array $matches The matches returned by the preg_replace call
+ *
+ * @return string The text with normalized tab characters
+ */
+ protected function _detab_callback($matches) {
+ $line = $matches[0];
+
+ $blocks = explode("\t", $line);
+ $line = $blocks[0];
+
+ // prevent adding the first block twice
+ unset($blocks[0]);
+
+ foreach ($blocks as $block) {
+ $amount = self::TAB_WIDTH - mb_strlen($line, 'UTF-8') % self::TAB_WIDTH;
+
+ $line.= str_repeat(' ', $amount) . $block;
+ }
+
+ return $line;
+ }
+
+
+
+
function stripLinkDefinitions($text) {
#
# Strips link definitions from text, stores the URLs and titles in
@@ -1519,36 +1558,6 @@ function outdent($text) {
return preg_replace('/^(\t|[ ]{1,'.self::TAB_WIDTH.'})/m', '', $text);
}
- function detab($text) {
- #
- # Replace tabs with the appropriate amount of space.
- #
- # For each line we separate the line in blocks delemited by
- # tab characters. Then we reconstruct every line by adding the
- # appropriate number of space between each blocks.
-
- $text = preg_replace_callback('/^.*\t.*$/m',
- array(&$this, '_detab_callback'), $text);
-
- return $text;
- }
- function _detab_callback($matches) {
- $line = $matches[0];
-
- # Split in blocks.
- $blocks = explode("\t", $line);
- # Add each blocks to the line.
- $line = $blocks[0];
- unset($blocks[0]); # Do not add first block twice.
- foreach ($blocks as $block) {
- # Calculate amount of space, insert spaces, insert block.
- $amount = self::TAB_WIDTH - mb_strlen($line, 'UTF-8') % self::TAB_WIDTH;
- $line .= str_repeat(" ", $amount) . $block;
- }
- return $line;
- }
-
-
function unhash($text) {
#
# Swap back in all the tags hashed by _HashHTMLBlocks.
Please sign in to comment.
Something went wrong with that request. Please try again.