Skip to content

Instantly share code, notes, and snippets.

@benjamn
Created December 28, 2008 21:18
Show Gist options
  • Save benjamn/41050 to your computer and use it in GitHub Desktop.
Save benjamn/41050 to your computer and use it in GitHub Desktop.
Index: plugins/Markdown/Markdown.pl
===================================================================
--- plugins/Markdown/Markdown.pl (revision 3290)
+++ plugins/Markdown/Markdown.pl (working copy)
@@ -184,6 +184,8 @@
# contorted like /[ \t]*\n+/ .
$text =~ s/^[ \t]+$//mg;
+ $text = _DoLaTeX($text);
+
# Turn block-level HTML blocks into hash entries
$text = _HashHTMLBlocks($text);
@@ -347,6 +349,63 @@
}
+sub _getLaTeXImg {
+ my $tex = shift;
+ my $env = shift;
+
+ my $url = 'http://dev.httex.org/?';
+ $url .= UrlEncode($env).'='.UrlEncode($tex);
+ return "<img class='".$env."' src='".$url."' />";
+}
+
+
+sub _DoLaTeX {
+ my $text = shift;
+
+ # Handle in-line LaTeX images:
+ #
+ # $\sum_{i=0}^\infty i$
+ #
+ # and display-style LaTeX images:
+ #
+ # $$ \sum_{i=1}^n =
+ # $$ \frac{n(n+1)}{2}
+ #
+ # or just
+ #
+ # $$ \sum_{i=1}^n =
+ # \frac{n(n+1)}{2}
+ #
+ # The environment name ("math" or "align*", respectively) becomes the
+ # class of the image (for easy CSS manipulation).
+
+ $text =~ s{ # Loosely modeled after blockquote
+ ( # Wrap whole match in $1
+ (?:
+ ^\$\$[ \t]? # '$$' at the start of a line
+ .+\n # rest of the first line
+ (?:.+\n)* # subsequent consecutive lines
+ )+
+ )
+ }{
+ my $tex = $1;
+ $tex =~ s/\$\$//gs;
+ _quote(_getLaTeXImg($tex, 'align*'));
+ }xmge;
+
+ $text =~ s{ \\\$ }{ $g_escape_table{'$'}; }xsge;
+
+ $text =~ s{
+ \$ # Initial $
+ (?![\s\$]) # not followed by a space or $.
+ (.+?) # The in-line TeX to be extracted.
+ \$ # Closing $ (just take the closest one).
+ }{ _getLaTeXImg($1, 'math') }xge;
+
+ return $text;
+}
+
+
sub _RunBlockGamut {
#
# These are all the transformations that form block-level
@@ -973,6 +1032,12 @@
}
+sub _quote {
+ my $content = shift;
+ return "<blockquote>\n$content\n</blockquote>\n\n";
+}
+
+
sub _DoBlockQuotes {
my $text = shift;
@@ -1179,6 +1244,21 @@
}
+sub UrlEncode {
+ my $url = shift;
+ $url =~ s/([\W])/"%" . uc(sprintf("%2.2x", ord($1)))/eg;
+ return $url;
+}
+
+
+sub UrlDecode {
+ my $url = shift;
+ $url =~ tr/+/ /; # Necessary?
+ $url =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
+ return $url;
+}
+
+
sub _TokenizeHTML {
#
# Parameter: String containing HTML markup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment