Skip to content

Commit

Permalink
ignore pre blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jun 8, 2009
1 parent 5be9a3e commit a28709d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 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] == '__'
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit a28709d

Please sign in to comment.