Browse code

Add support using github backtick code blocks without supplying a language

Brandon Mathis authored on 28/07/2011 at 19:59:27
Showing 1 changed files
... ...
@@ -27,24 +27,36 @@ module OctopressFilters
27 27
   # ```
28 28
   def backtick_codeblock(input)
29 29
     # Markdown support
30
-    input = input.gsub /<p>`{3}\s(\w+)<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do
30
+    input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do
31 31
       lang = $1
32
-      str  = $2.gsub('&lt;','<').gsub('&gt;','>')
33
-      highlight(str, lang)
32
+      if lang != ''
33
+        str  = $2.gsub('&lt;','<').gsub('&gt;','>')
34
+        highlight(str, lang)
35
+      else
36
+        "<pre><code>#{$2}</code></pre>"
37
+      end
34 38
     end
35 39
 
36 40
     # Textile support
37
-    input = input.gsub /<p>`{3}\s(\w+)<br\s*\/>\n(.+?)`{3}<\/p>/m do
41
+    input = input.gsub /<p>`{3}\s*(\w+)?<br\s*\/>\n(.+?)`{3}<\/p>/m do
38 42
       lang = $1
39 43
       str  = $2.gsub('&lt;','<').gsub('&gt;','>').gsub(/^\s{4}/, '').gsub(/(<br\s\/>)?$/, '')
40
-      highlight(str, lang)
44
+      if lang != ''
45
+        highlight(str, lang)
46
+      else
47
+        "<pre><code>#{$2}</code></pre>"
48
+      end
41 49
     end
42 50
 
43 51
     # Regular HTML support
44
-    input.gsub /^`{3}\s(\w+)\n(.+?)\n`{3}/m do
52
+    input.gsub /^`{3}\s*(\w+)?\n(.+?)\n`{3}/m do
45 53
       lang = $1
46 54
       str  = $2.gsub(/^\s{4}/, '')
47
-      highlight(str, lang)
55
+      if lang != ''
56
+        highlight(str, lang)
57
+      else
58
+        "<pre><code>#{$2.gsub('<','&lt;').gsub('>','&gt;')}</code></pre>"
59
+      end
48 60
     end
49 61
   end
50 62