Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

ignore pre blocks

  • Loading branch information...
commit a28709dfc97cfec9ca7909f014562e644344b167 1 parent 5be9a3e
Tom Preston-Werner mojombo authored

Showing 1 changed file with 23 additions and 0 deletions. Show diff stats Hide diff stats

  1. +23 0 code.rb
23 code.rb
... ... @@ -1,4 +1,14 @@
  1 +require 'digest/md5'
  2 +
1 3 def gfm(text)
  4 + # Extract pre blocks
  5 + extractions = {}
  6 + text.gsub!(%r{<pre>.*?</pre>}m) do |match|
  7 + md5 = Digest::MD5.hexdigest(match)
  8 + extractions[md5] = match
  9 + "{gfm-extraction-#{md5}}"
  10 + end
  11 +
2 12 # prevent foo_bar_baz from ending up with an italic word in the middle
3 13 text.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
4 14 x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
@@ -9,6 +19,11 @@ def gfm(text)
9 19 x.gsub(/^(.+)$/, "\\1 ")
10 20 end
11 21
  22 + # Insert pre block extractions
  23 + text.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
  24 + extractions[$1]
  25 + end
  26 +
12 27 text
13 28 end
14 29
@@ -22,6 +37,14 @@ class GFMTest < Test::Unit::TestCase
22 37 assert_equal "foo_bar", gfm("foo_bar")
23 38 end
24 39
  40 + should "not touch underscores in code blocks" do
  41 + assert_equal " foo_bar_baz", gfm(" foo_bar_baz")
  42 + end
  43 +
  44 + should "not touch underscores in pre blocks" do
  45 + assert_equal "<pre>\nfoo_bar_baz\n</pre>", gfm("<pre>\nfoo_bar_baz\n</pre>")
  46 + end
  47 +
25 48 should "escape two or more underscores inside words" do
26 49 assert_equal "foo\\_bar\\_baz", gfm("foo_bar_baz")
27 50 end

0 comments on commit a28709d

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