drop custom markdowner for rdiscount

This commit is contained in:
joshua stein 2012-07-01 20:09:22 -05:00
parent b5b2c92779
commit 8dd109c49f
11 changed files with 21 additions and 263 deletions

View file

@ -25,6 +25,8 @@ gem "uglifier"
gem "nokogiri"
gem "htmlentities"
gem "rdiscount"
group :test, :development do
gem "rspec-rails", "~> 2.6"
gem "machinist"

View file

@ -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

View file

@ -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;
}

View file

@ -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!

View file

@ -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

View file

@ -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

View file

@ -13,7 +13,7 @@
<div class="markdown_help_toggler">
<div class="markdown_help_label">
<span class="fakea">Limited Markdown formatting available</span>
<span class="fakea">Markdown formatting available</span>
</div>
<%= button_tag "Post Comment", :class => "comment-post",

View file

@ -13,11 +13,12 @@
<div class="box">
<div class="boxline markdown_help_toggler">
<div class="markdown_help_label">
<span class="fakea">Limited Markdown formatting available</span>
<span class="fakea">Markdown formatting available</span>
</div>
<%= submit_tag "Save" %>
&nbsp;or <a href="<%= story_url(@story.short_id) %>">cancel editing</a>
&nbsp;or <a href="<%= story_url(@story.short_id) %>">cancel
editing</a>
<div style="clear: both;"></div>

View file

@ -12,7 +12,7 @@
<div class="box">
<div class="boxline markdown_help_toggler">
<div class="markdown_help_label">
<span class="fakea">Limited Markdown formatting available</span>
<span class="fakea">Markdown formatting available</span>
</div>
<%= submit_tag "Submit" %>

View file

@ -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(/&lt;(\/?)(em|u|strike)&gt;/, '<\1\2>')
end
def self.markdown(string)
lines = string.to_s.rstrip.split(/\r?\n/)
out = "<p>"
inpre = false
lines.each do |line|
# [ ][ ]blah -> <pre> blah</pre>
if line.match(/^( |\t)/)
if !inpre
out << "<p><pre>"
end
out << ERB::Util.h(line) << "\n"
inpre = true
next
elsif inpre
out << "</pre></p>\n<p>"
inpre = false
end
line = self.h(line)
# lines starting with > are quoted
if m = line.match(/^&gt;(.*)/)
line = "<blockquote> " << m[1] << " </blockquote>"
end
lead = '\A|\s|[><]'
trail = '[<>]|\z|\s'
# *text* -> <em>text</em>
line.gsub!(/(#{lead}|_|~)\*([^\* \t][^\*]*)\*(#{trail}|_|~)/) do |m|
"#{$1}<em>" << self.h($2) << "</em>#{$3}"
end
# _text_ -> <u>text</u>
line.gsub!(/(#{lead}|~)_([^_ \t][^_]*)_(#{trail}|~)/) do |m|
"#{$1}<u>" << self.h($2) << "</u>#{$3}"
end
# ~~text~~ -> <strike>text</strike> (from reddit)
line.gsub!(/(#{lead})\~\~([^~ \t][^~]*)\~\~(#{trail})/) do |m|
"#{$1}<strike>" << self.h($2) << "</strike>#{$3}"
end
# [link text](http://url) -> <a href="http://url">link text</a>
line.gsub!(/(#{lead})\[([^\]]+)\]\((http(s?):\/\/[^\)]+)\)(#{trail})/i) do |m|
"#{$1}<a href=\"" << self.h($3) << "\" rel=\"nofollow\">" <<
self.h($2) << "</a>#{$5}"
end
# find bare links that are not inside tags
# http://blah -> <a href=...>
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 << "<br>\n"
end
if inpre
out << "</pre>"
end
out << "</p>"
# multiple br's into a p
out.gsub!(/<br>\n?<br>\n?/, "</p><p>")
# collapse things
out.gsub!(/<br>\n?<\/p>/, "</p>\n")
out.gsub!(/<p>\n?<br>\n?/, "<p>")
out.gsub!(/<p>\n?<\/p>/, "\n")
out.gsub!(/<p>\n?<p>/, "\n<p>")
out.gsub!(/<\/p><p>/, "</p>\n<p>")
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}<a href=\"#{url}\" rel=\"nofollow\">#{url_text}</a>#{post}"
end
end
end

View file

@ -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 <pre>" do
m " This is some\n text.\n",
"<p><pre> This is some\n text.\n</pre></p>"
m " blah <script>alert('hi');</script>",
"<p><pre> blah &lt;script&gt;alert('hi');&lt;/script&gt;\n</pre></p>"
end
it "converts text surrounded by * to <em>" do
m "oh hullo *there*",
"<p>oh hullo <em>there</em></p>"
m "*hi*",
"<p><em>hi</em></p>"
m "* hi hello*zap zap*",
"<p>* hi hello*zap zap*</p>"
m "oh hullo * there*",
"<p>oh hullo * there*</p>"
m " oh hullo *there*",
"<p><pre> oh hullo *there*\n</pre></p>"
m "oh hullo*there*",
"<p>oh hullo*there*</p>"
end
it "converts text surrounded by _ to <u>" do
m "oh hullo _there_",
"<p>oh hullo <u>there</u></p>"
m "oh hullo _ there_",
"<p>oh hullo _ there_</p>"
m "oh hullo _there_ and *yes* i see",
"<p>oh hullo <u>there</u> and <em>yes</em> i see</p>"
end
it "combines conversions" do
m "oh _*hullo*_ there_",
"<p>oh <u><em>hullo</em></u> there_</p>"
m "oh *_hullo_* there_",
"<p>oh <em><u>hullo</u></em> there_</p>"
m "oh *[hello](http://jcs.org/)* there_",
"<p>oh <em><a href=\"http://jcs.org/\" rel=\"nofollow\">hello</a>" <<
"</em> there_</p>"
end
it "converts domain names to links" do
m "oh hullo www.google.com",
"<p>oh hullo <a href=\"http://www.google.com\" rel=\"nofollow\">" <<
"www.google.com</a></p>"
end
it "converts urls to links" do
# no trailing question mark
m "do you mean http://jcs.org? or",
"<p>do you mean <a href=\"http://jcs.org\" rel=\"nofollow\">" <<
"jcs.org</a>? or</p>"
m "do you mean http://jcs.org?a",
"<p>do you mean <a href=\"http://jcs.org?a\" rel=\"nofollow\">" <<
"jcs.org?a</a></p>"
# no trailing dot in url
m "i like http://jcs.org.",
"<p>i like <a href=\"http://jcs.org\" rel=\"nofollow\">" <<
"jcs.org</a>.</p>"
m "i like http://jcs.org/goose_blah_here",
"<p>i like <a href=\"http://jcs.org/goose_blah_here\" " <<
"rel=\"nofollow\">jcs.org/goose_blah_here</a></p>"
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",
"<p>a long <a href=\"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&amp;such+ok\" rel=\"nofollow\">" <<
"www.example.com/goes/here/and/this/is/a/long/url/w...</a></p>"
end
it "converts markdown url format to links" do
m "this is a *[link](http://example.com/)*",
"<p>this is a <em><a href=\"http://example.com/\" rel=\"nofollow\">" <<
"link</a></em></p>"
m "this is a [link](http://example.com/)",
"<p>this is a <a href=\"http://example.com/\" rel=\"nofollow\">" <<
"link</a></p>"
end
end