From 8dd109c49f1321ee3f26cd161d8af64881bbe00c Mon Sep 17 00:00:00 2001 From: joshua stein Date: Sun, 1 Jul 2012 20:09:22 -0500 Subject: [PATCH] drop custom markdowner for rdiscount --- Gemfile | 2 + Gemfile.lock | 2 + app/assets/stylesheets/application.css | 5 +- app/models/comment.rb | 3 +- app/models/keystore.rb | 12 +- app/models/story.rb | 3 +- app/views/comments/_commentbox.html.erb | 2 +- app/views/stories/edit.html.erb | 5 +- app/views/stories/new.html.erb | 2 +- extras/markdowner.rb | 144 ------------------------ spec/models/markdowner_spec.rb | 104 ----------------- 11 files changed, 21 insertions(+), 263 deletions(-) delete mode 100644 extras/markdowner.rb delete mode 100644 spec/models/markdowner_spec.rb diff --git a/Gemfile b/Gemfile index 2beacc4..75c6be3 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,8 @@ gem "uglifier" gem "nokogiri" gem "htmlentities" +gem "rdiscount" + group :test, :development do gem "rspec-rails", "~> 2.6" gem "machinist" diff --git a/Gemfile.lock b/Gemfile.lock index 1c92a00..f24e5b7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,7 @@ GEM thor (~> 0.14.6) raindrops (0.9.0) rake (0.9.2.2) + rdiscount (1.6.8) rdoc (3.12) json (~> 1.4) rspec (2.9.0) @@ -128,6 +129,7 @@ DEPENDENCIES mysql2 nokogiri rails (= 3.2.2) + rdiscount rspec-rails (~> 2.6) sqlite3 uglifier diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index a2cffbf..6cdbf53 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -441,10 +441,9 @@ div.story_text p { margin: 0.75em 0; } blockquote { - display: inline; font-style: italic; - margin: 0px; - padding: 0px 0px 0px 0.5em; + margin: 0 0 0 1em; + padding: 0 0 0 0.5em; border-left: 2px solid gray; } diff --git a/app/models/comment.rb b/app/models/comment.rb index 115c2db..e3898eb 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -81,7 +81,8 @@ class Comment < ActiveRecord::Base end def linkified_comment - Markdowner.markdown(self.comment) + RDiscount.new(self.comment, :smart, :autolink, :safelink, + :filter_html).to_html end def flag! diff --git a/app/models/keystore.rb b/app/models/keystore.rb index 5ab5d43..a06e0da 100644 --- a/app/models/keystore.rb +++ b/app/models/keystore.rb @@ -8,9 +8,9 @@ class Keystore < ActiveRecord::Base end def self.put(key, value) - Keystore.connection.execute("INSERT INTO #{Keystore.table_name} (" + - "`key`, `value`) VALUES (#{q(key)}, #{q(value)}) ON DUPLICATE KEY " + - "UPDATE `value` = #{q(value)}") + Keystore.connection.execute("INSERT OR REPLACE INTO " << + "#{Keystore.table_name} (`key`, `value`) VALUES " << + "(#{q(key)}, #{q(value)})") true end @@ -21,9 +21,9 @@ class Keystore < ActiveRecord::Base def self.incremented_value_for(key, amount = 1) new_value = nil - Keystore.connection.execute("INSERT INTO #{Keystore.table_name} (" + - "`key`, `value`) VALUES (#{q(key)}, #{q(amount)}) ON DUPLICATE KEY " + - "UPDATE `value` = `value` + #{q(amount)}") + Keystore.connection.execute("INSERT OR REPLACE INTO " << + "#{Keystore.table_name} (`key`, `value`) VALUES " << + "(#{q(key)}, #{q(amount)})") return self.value_for(key) end diff --git a/app/models/story.rb b/app/models/story.rb index e8d6c4c..519c3fb 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -138,7 +138,8 @@ class Story < ActiveRecord::Base end def linkified_text - Markdowner.markdown(self.description) + RDiscount.new(self.description, :smart, :autolink, :safelink, + :filter_html).to_html end def tags_a diff --git a/app/views/comments/_commentbox.html.erb b/app/views/comments/_commentbox.html.erb index 1d0a119..59764d0 100644 --- a/app/views/comments/_commentbox.html.erb +++ b/app/views/comments/_commentbox.html.erb @@ -13,7 +13,7 @@
- Limited Markdown formatting available + Markdown formatting available
<%= button_tag "Post Comment", :class => "comment-post", diff --git a/app/views/stories/edit.html.erb b/app/views/stories/edit.html.erb index 284ec68..264311b 100644 --- a/app/views/stories/edit.html.erb +++ b/app/views/stories/edit.html.erb @@ -13,11 +13,12 @@
- Limited Markdown formatting available + Markdown formatting available
<%= submit_tag "Save" %> -  or cancel editing +  or cancel + editing
diff --git a/app/views/stories/new.html.erb b/app/views/stories/new.html.erb index 9ab5cfe..28bd80b 100644 --- a/app/views/stories/new.html.erb +++ b/app/views/stories/new.html.erb @@ -12,7 +12,7 @@
- Limited Markdown formatting available + Markdown formatting available
<%= submit_tag "Submit" %> diff --git a/extras/markdowner.rb b/extras/markdowner.rb deleted file mode 100644 index d25ef1a..0000000 --- a/extras/markdowner.rb +++ /dev/null @@ -1,144 +0,0 @@ -class Markdowner - MAX_BARE_LINK = 50 - - def self.h(str) - # .to_str is needed because this becomes a SafeBuffer, which breaks gsub - # https://github.com/rails/rails/issues/1555 - ERB::Util.h(str).to_str.gsub(/<(\/?)(em|u|strike)>/, '<\1\2>') - end - - def self.markdown(string) - lines = string.to_s.rstrip.split(/\r?\n/) - - out = "

" - inpre = false - lines.each do |line| - # [ ][ ]blah ->

  blah
- if line.match(/^( |\t)/) - if !inpre - out << "

"
-        end
-
-				out << ERB::Util.h(line) << "\n"
-				inpre = true
-				next
-      elsif inpre
-				out << "

\n

" - inpre = false - end - - line = self.h(line) - - # lines starting with > are quoted - if m = line.match(/^>(.*)/) - line = "

" << m[1] << "
" - end - - lead = '\A|\s|[><]' - trail = '[<>]|\z|\s' - - # *text* -> text - line.gsub!(/(#{lead}|_|~)\*([^\* \t][^\*]*)\*(#{trail}|_|~)/) do |m| - "#{$1}" << self.h($2) << "#{$3}" - end - - # _text_ -> text - line.gsub!(/(#{lead}|~)_([^_ \t][^_]*)_(#{trail}|~)/) do |m| - "#{$1}" << self.h($2) << "#{$3}" - end - - # ~~text~~ -> text (from reddit) - line.gsub!(/(#{lead})\~\~([^~ \t][^~]*)\~\~(#{trail})/) do |m| - "#{$1}" << self.h($2) << "#{$3}" - end - - # [link text](http://url) -> link text - line.gsub!(/(#{lead})\[([^\]]+)\]\((http(s?):\/\/[^\)]+)\)(#{trail})/i) do |m| - "#{$1}" << - self.h($2) << "#{$5}" - end - - # find bare links that are not inside tags - - # http://blah -> - chunk = "" - intag = false - outline = "" - line.each_byte do |n| - c = n.chr - - if intag - outline << c - - if c == ">" - intag = false - next - end - else - if c == "<" - if chunk != "" - outline << Markdowner._linkify_text(chunk) - end - - chunk = "" - intag = true - outline << c - else - chunk << c - end - end - end - - if chunk != "" - outline << Markdowner._linkify_text(chunk) - end - - out << outline << "
\n" - end - - if inpre - out << "" - end - - out << "

" - - # multiple br's into a p - out.gsub!(/
\n?
\n?/, "

") - - # collapse things - out.gsub!(/
\n?<\/p>/, "

\n") - out.gsub!(/

\n?
\n?/, "

") - out.gsub!(/

\n?<\/p>/, "\n") - out.gsub!(/

\n?

/, "\n

") - out.gsub!(/<\/p>

/, "

\n

") - - out.strip.force_encoding("utf-8") - end - - def self._linkify_text(chunk) - chunk.gsub(/ - (\A|\s|[:,]) - (http(s?):\/\/|www\.) - ([^\s]+)/ix) do |m| - pre = $1 - host_and_path = "#{$2 == "www." ? $2 : ""}#{$4}" - post = $5 - - # remove some chars that might be with a url at the end but aren't - # actually part of the url - if m = host_and_path.match(/(.*)([,\?;\!\.]+)\z/) - host_and_path = m[1] - post = "#{m[2]}#{post}" - end - - url = "http#{$3}://#{host_and_path}" - url_text = host_and_path - - if url_text.length > 50 - url_text = url_text[0 ... 50] << "..." - end - - "#{pre}#{url_text}#{post}" - end - end -end diff --git a/spec/models/markdowner_spec.rb b/spec/models/markdowner_spec.rb deleted file mode 100644 index efdede9..0000000 --- a/spec/models/markdowner_spec.rb +++ /dev/null @@ -1,104 +0,0 @@ -require "spec_helper" - -def m(inp, out) - Markdowner::markdown(inp).should == out -end - -describe Markdowner do - it "converts indented text into

" do
-    m "  This is some\n  text.\n",
-      "

  This is some\n  text.\n

" - - m " blah ", - "

  blah <script>alert('hi');</script>\n

" - end - - it "converts text surrounded by * to " do - m "oh hullo *there*", - "

oh hullo there

" - - m "*hi*", - "

hi

" - - m "* hi hello*zap zap*", - "

* hi hello*zap zap*

" - - m "oh hullo * there*", - "

oh hullo * there*

" - - m " oh hullo *there*", - "

  oh hullo *there*\n

" - - m "oh hullo*there*", - "

oh hullo*there*

" - end - - it "converts text surrounded by _ to " do - m "oh hullo _there_", - "

oh hullo there

" - - m "oh hullo _ there_", - "

oh hullo _ there_

" - - m "oh hullo _there_ and *yes* i see", - "

oh hullo there and yes i see

" - end - - it "combines conversions" do - m "oh _*hullo*_ there_", - "

oh hullo there_

" - - m "oh *_hullo_* there_", - "

oh hullo there_

" - - m "oh *[hello](http://jcs.org/)* there_", - "

oh hello" << - " there_

" - end - - it "converts domain names to links" do - m "oh hullo www.google.com", - "

oh hullo " << - "www.google.com

" - end - - it "converts urls to links" do - # no trailing question mark - m "do you mean http://jcs.org? or", - "

do you mean " << - "jcs.org? or

" - - m "do you mean http://jcs.org?a", - "

do you mean " << - "jcs.org?a

" - - # no trailing dot in url - m "i like http://jcs.org.", - "

i like " << - "jcs.org.

" - - m "i like http://jcs.org/goose_blah_here", - "

i like jcs.org/goose_blah_here

" - end - - it "truncates long url titles" do - m "a long http://www.example.com/goes/here/and/this/is/a/long/" << - "url/which/should.get.shortened.html?because=this+will+cause+" << - "the+page+to+wrap&such+ok", - "

a long " << - "www.example.com/goes/here/and/this/is/a/long/url/w...

" - end - - it "converts markdown url format to links" do - m "this is a *[link](http://example.com/)*", - "

this is a " << - "link

" - - m "this is a [link](http://example.com/)", - "

this is a " << - "link

" - end -end