Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ignore pre blocks

  • Loading branch information...
commit a28709dfc97cfec9ca7909f014562e644344b167 1 parent 5be9a3e
@mojombo mojombo authored
Showing with 23 additions and 0 deletions.
  1. +23 −0 code.rb
View
23 code.rb
@@ -1,4 +1,14 @@
+require 'digest/md5'
+
def gfm(text)
+ # Extract pre blocks
+ extractions = {}
+ text.gsub!(%r{<pre>.*?</pre>}m) do |match|
+ md5 = Digest::MD5.hexdigest(match)
+ extractions[md5] = match
+ "{gfm-extraction-#{md5}}"
+ end
+
# prevent foo_bar_baz from ending up with an italic word in the middle
text.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
@@ -9,6 +19,11 @@ def gfm(text)
x.gsub(/^(.+)$/, "\\1 ")
end
+ # Insert pre block extractions
+ text.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
+ extractions[$1]
+ end
+
text
end
@@ -22,6 +37,14 @@ class GFMTest < Test::Unit::TestCase
assert_equal "foo_bar", gfm("foo_bar")
end
+ should "not touch underscores in code blocks" do
+ assert_equal " foo_bar_baz", gfm(" foo_bar_baz")
+ end
+
+ should "not touch underscores in pre blocks" do
+ assert_equal "<pre>\nfoo_bar_baz\n</pre>", gfm("<pre>\nfoo_bar_baz\n</pre>")
+ end
+
should "escape two or more underscores inside words" do
assert_equal "foo\\_bar\\_baz", gfm("foo_bar_baz")
end

0 comments on commit a28709d

Please sign in to comment.
Something went wrong with that request. Please try again.