From b9bf2c028f39e4efb3a1d5100aa1e4faf5658e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurrie=CC=88n=20Dokter?= Date: Thu, 16 May 2013 12:24:22 +0200 Subject: [PATCH] transform file / id's classes for blockquotes - Allowing ID's and classes to be added to blockquotes as well. - added a transformFile method --- Michelf/MarkdownExtra.php | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Michelf/MarkdownExtra.php b/Michelf/MarkdownExtra.php index 267bf16d..e8a9c20d 100644 --- a/Michelf/MarkdownExtra.php +++ b/Michelf/MarkdownExtra.php @@ -34,6 +34,57 @@ class MarkdownExtra extends \Michelf\_MarkdownExtra_TmpImpl { # Temporarily, the implemenation is in the _MarkdownExtra_TmpImpl class. # See note above. + /** + * @param string $file + * + * @throws \InvalidArgumentException + * @return string + */ + public function transformFile( $file ) { + if(file_exists($file)) { + + return $this->transform(file_get_contents($file)); + + } + throw new \InvalidArgumentException("File {$file} does not exist"); + } + + /** + * @param array $matches + * + * @return string + */ + protected function _doBlockQuotes_callback($matches) { + $bq = $matches[1]; + + # trim one level of quoting - trim whitespace-only lines + $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq); + + $class = ""; + // check class + if(preg_match('/^\{([\.a-z\-\_\#\ ]+?)\}$/m', $bq, $bqm)) { + $class = $bqm[1]; + // remove first line + + $bq = implode("\n", array_slice(explode("\n", $bq), 1)); + } + + $bq = $this->runBlockGamut($bq); # recurse + + $bq = preg_replace('/^/m', " ", $bq); + # These leading spaces cause problem with
 content,
+        # so we need to fix that:
+        $bq = preg_replace_callback('{(\s*
.+?
)}sx', + array(&$this, '_doBlockQuotes_callback2'), $bq); + + if(!empty($class)) { + $attr = $this->doExtraAttributes('blockquote', $class); + return "\n". $this->hashBlock("\n$bq\n")."\n\n"; + } else { + return "\n". $this->hashBlock("
\n$bq\n
")."\n\n"; + } + } + }