Created
April 10, 2016 12:09
-
-
Save magadanskiuchen/74aa03205f2167c89ded7bffcf412ee2 to your computer and use it in GitHub Desktop.
Partial update to WP-MarkDown's Markdownify class to add support for GitHub flavor-like codeblocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/markdownify/markdownify.php b/markdownify/markdownify.php | |
index e9b43bd..24ddac3 100644 | |
--- a/markdownify/markdownify.php | |
+++ b/markdownify/markdownify.php | |
@@ -816,15 +816,39 @@ class Markdownify { | |
*/ | |
public function handleTag_pre() { | |
if ($this->keepHTML && $this->parser->isStartTag) { | |
- # check if a simple <code> follows | |
- if (!preg_match('#^\s*<code\s*>#Us', $this->parser->html)) { | |
+ # check if a simple <code> or <code class=""> follows | |
+ $this->parser->prelang = ''; | |
+ $code_match = preg_match('#^\s*<code\s*(class="' . preg_quote(MARKDOWN_CODE_CLASS_PREFIX, '#') . '([^"]+)\s*)?">#Us', $this->parser->html, $language); | |
+ | |
+ if ($code_match) { | |
+ $this->parser->prelang = !empty($language[2]) ? $language[2] : ''; | |
+ | |
+ $this->out('```' . $this->parser->prelang . "\n"); | |
+ | |
+ $this->buffer(); | |
+ } else { | |
# this is no standard markdown code block | |
$this->handleTagToText(); | |
return; | |
} | |
} | |
- $this->indent(' '); | |
+ | |
+ if ($this->parser->prelang == '') { | |
+ $this->indent(' '); | |
+ } | |
+ | |
if (!$this->parser->isStartTag) { | |
+ if ($this->parser->prelang != '') { | |
+ $buffer = $this->unbuffer(); | |
+ $buffer = preg_replace('#</?code[^>]*>#', '', $buffer); | |
+ | |
+ $this->out($buffer); | |
+ $this->setLineBreaks(1); | |
+ $this->out('```'); | |
+ | |
+ $this->parser->prelang = ''; | |
+ } | |
+ | |
$this->setLineBreaks(2); | |
} else { | |
$this->parser->html = ltrim($this->parser->html); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment