Skip to content

Instantly share code, notes, and snippets.

@eleclerc
Created May 17, 2009 12:56
Show Gist options
  • Save eleclerc/113004 to your computer and use it in GitHub Desktop.
Save eleclerc/113004 to your computer and use it in GitHub Desktop.
patch for PHP Markdown Extra 1.2.4 to use GeSHi to add syntax coloring to code block. first line of your codeblock need to be the language id line: ie: #!php (github and posterous' way)
--- markdown.php 2009-10-10 10:11:04.000000000 -0400
+++ markdown-geshi.php 2010-06-16 16:54:51.000000000 -0400
@@ -2,6 +2,11 @@
#
# Markdown Extra - A text-to-HTML conversion tool for web writers
#
+# GeSHI addition by Eric Leclerc
+# uses GeSHI 1.0.8.8. add a shebang line as the first line of your
+# code block to trigger GeSHI parsing/highlighting, ie:
+# #!php
+#
# PHP Markdown & Extra
# Copyright (c) 2004-2009 Michel Fortin
# <http://michelf.com/projects/php-markdown/>
@@ -15,6 +20,7 @@
define( 'MARKDOWN_VERSION', "1.0.1n" ); # Sat 10 Oct 2009
define( 'MARKDOWNEXTRA_VERSION', "1.2.4" ); # Sat 10 Oct 2009
+require_once realpath(dirname(__FILE__) . '/../geshi-1.0.8.8/geshi.php');
#
# Global default settings:
@@ -1121,12 +1127,20 @@
$codeblock = $matches[1];
$codeblock = $this->outdent($codeblock);
- $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
# trim leading newlines and trailing newlines
$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
- $codeblock = "<pre><code>$codeblock\n</code></pre>";
+ // if it start with a language tag (ie: #!php), use GeSHi to convert the code
+ if (preg_match('/^#!(\w+)/', $codeblock, $match)) {
+ $codeblock = str_replace("#!${match[1]}\n", '', $codeblock);
+ $geshi = new GeSHi($codeblock, $match[1]);
+ $codeblock = $geshi->parse_code();
+ } else {
+ $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
+ $codeblock = "<pre><code>$codeblock\n</code></pre>";
+ }
+
return "\n\n".$this->hashBlock($codeblock)."\n\n";
}
@@ -2842,6 +2856,14 @@
}
+// If there's command line argument, markdownify the file from the first argument
+if (!empty($argv[1])) {
+ $filename = realpath($argv[1]);
+ if (file_exists($filename)) {
+ $rawtext = file_get_contents($filename);
+ echo Markdown($rawtext);
+ }
+}
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment