Skip to content

Instantly share code, notes, and snippets.

@bryanlarsen
Forked from mojombo/gfm.rb
Created January 13, 2010 13:18

Revisions

  1. bryanlarsen revised this gist Jan 13, 2010. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gfm.rb
    Original file line number Diff line number Diff line change
    @@ -69,6 +69,10 @@ class GFMTest < Test::Unit::TestCase
    assert_equal "\n foil\ntest foo\\_bar\\_baz and another\\_test\\_obj", gfm("\n foil\ntest foo_bar_baz and another_test_obj")
    end

    should "escape two or more underscores inside words not at front of line twice on three lines" do
    assert_equal "\n foil_me_twice\ntest foo\\_bar\\_baz \n and_again_test\n", gfm("\n foil_me_twice\ntest foo_bar_baz\n and_again_test\n")
    end

    should "turn newlines into br tags in simple cases" do
    assert_equal "foo \nbar", gfm("foo\nbar")
    end
  2. bryanlarsen revised this gist Jan 13, 2010. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions gfm.rb
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,10 @@ def gfm(text)
    "{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] == '__'
    text.gsub!(/^(?! {4}|\t).*$/) do |line|
    line.gsub(/\w+_\w+_\w[\w_]*/) do |x|
    x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
    end
    end

    # in very clear cases, let newlines become <br /> tags
    @@ -60,6 +61,14 @@ class GFMTest < Test::Unit::TestCase
    assert_equal "test foo\\_bar\\_baz", gfm("test foo_bar_baz")
    end

    should "escape two or more underscores inside words not at front of line twice" do
    assert_equal "test foo\\_bar\\_baz and another\\_test\\_obj", gfm("test foo_bar_baz and another_test_obj")
    end

    should "escape two or more underscores inside words not at front of line twice on two lines" do
    assert_equal "\n foil\ntest foo\\_bar\\_baz and another\\_test\\_obj", gfm("\n foil\ntest foo_bar_baz and another_test_obj")
    end

    should "turn newlines into br tags in simple cases" do
    assert_equal "foo \nbar", gfm("foo\nbar")
    end
  3. bryanlarsen revised this gist Jan 13, 2010. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion gfm.rb
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,7 @@ def gfm(text)

    if $0 == __FILE__
    require 'test/unit'
    require 'rubygems'
    require 'shoulda'

    class GFMTest < Test::Unit::TestCase
    @@ -55,6 +56,10 @@ class GFMTest < Test::Unit::TestCase
    assert_equal "foo\\_bar\\_baz", gfm("foo_bar_baz")
    end

    should "escape two or more underscores inside words not at front of line" do
    assert_equal "test foo\\_bar\\_baz", gfm("test foo_bar_baz")
    end

    should "turn newlines into br tags in simple cases" do
    assert_equal "foo \nbar", gfm("foo\nbar")
    end
    @@ -75,4 +80,4 @@ class GFMTest < Test::Unit::TestCase
    end
    end
    end
    end
    end
  4. @mojombo mojombo revised this gist Aug 21, 2009. 1 changed file with 16 additions and 5 deletions.
    21 changes: 16 additions & 5 deletions gfm.rb
    Original file line number Diff line number Diff line change
    @@ -15,13 +15,13 @@ def gfm(text)
    end

    # in very clear cases, let newlines become <br /> tags
    text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
    x.gsub(/^(.+)$/, "\\1 ")
    text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
    x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
    end

    # Insert pre block extractions
    text.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
    extractions[$1]
    "\n\n" + extractions[$1]
    end

    text
    @@ -42,22 +42,33 @@ class GFMTest < Test::Unit::TestCase
    end

    should "not touch underscores in pre blocks" do
    assert_equal "<pre>\nfoo_bar_baz\n</pre>", gfm("<pre>\nfoo_bar_baz\n</pre>")
    assert_equal "\n\n<pre>\nfoo_bar_baz\n</pre>", gfm("<pre>\nfoo_bar_baz\n</pre>")
    end

    should "not treat pre blocks with pre-text differently" do
    a = "\n\n<pre>\nthis is `a\\_test` and this\\_too\n</pre>"
    b = "hmm<pre>\nthis is `a\\_test` and this\\_too\n</pre>"
    assert_equal gfm(a)[2..-1], gfm(b)[3..-1]
    end

    should "escape two or more underscores inside words" do
    assert_equal "foo\\_bar\\_baz", gfm("foo_bar_baz")
    end

    should "turn newlines into br tags in simple cases" do
    assert_equal "foo \nbar ", gfm("foo\nbar")
    assert_equal "foo \nbar", gfm("foo\nbar")
    end

    should "convert newlines in all groups" do
    assert_equal "apple \npear \norange\n\nruby \npython \nerlang",
    gfm("apple\npear\norange\n\nruby\npython\nerlang")
    end

    should "convert newlines in even long groups" do
    assert_equal "apple \npear \norange \nbanana\n\nruby \npython \nerlang",
    gfm("apple\npear\norange\nbanana\n\nruby\npython\nerlang")
    end

    should "not convert newlines in lists" do
    assert_equal "# foo\n# bar", gfm("# foo\n# bar")
    assert_equal "* foo\n* bar", gfm("* foo\n* bar")
  5. @mojombo mojombo revised this gist Jun 8, 2009. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions gfm.rb
    Original file line number Diff line number Diff line change
    @@ -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
  6. @mojombo mojombo created this gist May 27, 2009.
    44 changes: 44 additions & 0 deletions gfm.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    def gfm(text)
    # 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] == '__'
    end

    # in very clear cases, let newlines become <br /> tags
    text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
    x.gsub(/^(.+)$/, "\\1 ")
    end

    text
    end

    if $0 == __FILE__
    require 'test/unit'
    require 'shoulda'

    class GFMTest < Test::Unit::TestCase
    context "GFM" do
    should "not touch single underscores inside words" do
    assert_equal "foo_bar", gfm("foo_bar")
    end

    should "escape two or more underscores inside words" do
    assert_equal "foo\\_bar\\_baz", gfm("foo_bar_baz")
    end

    should "turn newlines into br tags in simple cases" do
    assert_equal "foo \nbar ", gfm("foo\nbar")
    end

    should "convert newlines in all groups" do
    assert_equal "apple \npear \norange\n\nruby \npython \nerlang",
    gfm("apple\npear\norange\n\nruby\npython\nerlang")
    end

    should "not convert newlines in lists" do
    assert_equal "# foo\n# bar", gfm("# foo\n# bar")
    assert_equal "* foo\n* bar", gfm("* foo\n* bar")
    end
    end
    end
    end