]> git.openstreetmap.org Git - rails.git/commitdiff
Treat all newly entered blocks of text as Markdown
authorTom Hughes <tom@compton.nu>
Wed, 15 Feb 2012 00:48:52 +0000 (00:48 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 17 Mar 2012 16:36:55 +0000 (16:36 +0000)
21 files changed:
Gemfile
Gemfile.lock
app/helpers/application_helper.rb
app/models/diary_comment.rb
app/models/diary_entry.rb
app/models/message.rb
app/models/user.rb
app/models/user_block.rb
app/views/diary_entry/_diary_comment.html.erb
app/views/diary_entry/_diary_entry.html.erb
app/views/diary_entry/comments.html.erb
app/views/diary_entry/rss.rss.builder
app/views/message/read.html.erb
app/views/notifier/diary_comment_notification.text.erb
app/views/notifier/message_notification.html.erb
app/views/notifier/message_notification.text.erb
app/views/user/_user.html.erb
app/views/user/view.html.erb
app/views/user_blocks/show.html.erb
db/migrate/20120214210114_add_text_format.rb [new file with mode: 0644]
lib/rich_text.rb [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
index c1bb2f50b0ea194666fe2c26ab10a92ef302dafe..e283179972048a2caa00fac9cd900db6939b4f66 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -22,6 +22,9 @@ gem 'http_accept_language', '>= 1.0.2'
 gem 'paperclip', '~> 2.0'
 gem 'deadlock_retry', '>= 1.2.0'
 
+# Markdown formatting support
+gem 'redcarpet'
+
 # Character conversion support for ruby 1.8
 gem 'iconv', :platforms => :ruby_18
 
index 4709cd1957672fdf359c91764acaeadc7f8b2456..0dcb5ccff15a75642e0bd41def60322531ae5a60 100644 (file)
@@ -120,6 +120,7 @@ GEM
     rake (0.9.2.2)
     rdoc (3.12)
       json (~> 1.4)
+    redcarpet (2.1.0)
     rinku (1.5.1)
     ruby-openid (2.1.8)
     sanitize (2.0.3)
@@ -168,6 +169,7 @@ DEPENDENCIES
   pg
   rails (= 3.2.2)
   rails-i18n (>= 0.5.1)
+  redcarpet
   rinku (>= 1.2.2)
   sanitize
   sass-rails (~> 3.2.3)
index 4aec9f5c16811e79f14f68d9133ea9e30341d2a3..7a72932f55affbbd6550adc3a50a394bb93cfc1d 100644 (file)
@@ -1,14 +1,6 @@
 module ApplicationHelper
   require 'rexml/document'
 
-  def sanitize(text)
-    Sanitize.clean(text, Sanitize::Config::OSM).html_safe
-  end
-
-  def htmlize(text)
-    return linkify(sanitize(simple_format(text)))
-  end
-
   def linkify(text)
     if text.html_safe?
       Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow")).html_safe
index b915e027a9e1f49d62b2443b4f7b2b46f3630224..075d288edb8e0b644970b7b515a4f6967506abbe 100644 (file)
@@ -7,6 +7,12 @@ class DiaryComment < ActiveRecord::Base
 
   attr_accessible :body
 
+  after_initialize :set_defaults
+
+  def body
+    RichText.new(read_attribute(:body_format), read_attribute(:body))
+  end
+
   def digest
     md5 = Digest::MD5.new
     md5 << diary_entry_id.to_s
@@ -15,4 +21,10 @@ class DiaryComment < ActiveRecord::Base
     md5 << body
     md5.hexdigest
   end
+
+private
+
+  def set_defaults
+    self.body_format = "markdown" unless self.attribute_present?(:body_format)
+  end
 end
index 1d836353fee8b30d9f4f1a56396a068966748a49..64a412d28c771a55da936381c04f37737371962a 100644 (file)
@@ -25,4 +25,16 @@ class DiaryEntry < ActiveRecord::Base
   validates_associated :language
 
   attr_accessible :title, :body, :language_code, :latitude, :longitude
+
+  after_initialize :set_defaults
+
+  def body
+    RichText.new(read_attribute(:body_format), read_attribute(:body))
+  end
+
+private
+
+  def set_defaults
+    self.body_format = "markdown" unless self.attribute_present?(:body_format)
+  end
 end
index 0b34160030f980dc41a84f3db1ec47a53627ae10..feceec5c01ff266c7d6b3cfa7107471c3c59285b 100644 (file)
@@ -11,6 +11,12 @@ class Message < ActiveRecord::Base
 
   attr_accessible :title, :body
 
+  after_initialize :set_defaults
+
+  def body
+    RichText.new(read_attribute(:body_format), read_attribute(:body))
+  end
+
   def digest
     md5 = Digest::MD5.new
     md5 << from_user_id.to_s
@@ -20,4 +26,10 @@ class Message < ActiveRecord::Base
     md5 << body
     md5.hexdigest
   end
+
+private
+
+  def set_defaults
+    self.body_format = "markdown" unless self.attribute_present?(:body_format)
+  end
 end
index 0c9e76d54999685bd920542c4daf0aa42d64cbf6..3b55040c63cd486906d0e52e1ab0a4f6e9cddf15 100644 (file)
@@ -44,7 +44,7 @@ class User < ActiveRecord::Base
   attr_accessible :display_name, :email, :email_confirmation, :openid_url,
                   :pass_crypt, :pass_crypt_confirmation, :consider_pd
 
-  after_initialize :set_creation_time
+  after_initialize :set_defaults
   before_save :encrypt_password
 
   has_attached_file :image, 
@@ -101,6 +101,10 @@ class User < ActiveRecord::Base
     return el1
   end
 
+  def description
+    RichText.new(read_attribute(:description_format), read_attribute(:description))
+  end
+
   def languages
     attribute_present?(:languages) ? read_attribute(:languages).split(/ *, */) : []
   end
@@ -220,8 +224,9 @@ class User < ActiveRecord::Base
 
 private
 
-  def set_creation_time
+  def set_defaults
     self.creation_time = Time.now.getutc unless self.attribute_present?(:creation_time)
+    self.description_format = "markdown" unless self.attribute_present?(:description_format)
   end
 
   def encrypt_password
index 7bf8f86b5095dbf4d12697a826d3d6d3a3639203..8821926bbd3b73a8cb37af6c09dbdd0722326239 100644 (file)
@@ -5,8 +5,16 @@ class UserBlock < ActiveRecord::Base
   belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
   belongs_to :revoker, :class_name => "User", :foreign_key => :revoker_id
   
+  after_initialize :set_defaults
+
   PERIODS = USER_BLOCK_PERIODS
 
+  ##
+  # return a renderable version of the reason text.
+  def reason
+    RichText.new(read_attribute(:reason_format), read_attribute(:reason))
+  end
+
   ##
   # returns true if the block is currently active (i.e: the user can't
   # use the API).
@@ -25,7 +33,14 @@ class UserBlock < ActiveRecord::Base
     }, :without_protection => true)
   end
 
-  private
+private
+
+  ##
+  # set default values for new records.
+  def set_defaults
+    self.reason_format = "markdown" unless self.attribute_present?(:reason_format)
+  end
+
   ##
   # validate that only moderators are allowed to change the
   # block. this should be caught and dealt with in the controller,
index 05cb80157d4faeaac7b030ea2986fa110c8e24c4..463c2ad81d53c1bdc74af0282c239564ba0a1a4d 100644 (file)
@@ -1,6 +1,6 @@
 <%= user_thumbnail diary_comment.user %>
 <h4 id="comment<%= diary_comment.id %>"><%= raw(t('diary_entry.diary_comment.comment_from', :link_user => (link_to h(diary_comment.user.display_name), :controller => 'user', :action => 'view', :display_name => diary_comment.user.display_name), :comment_created_at => l(diary_comment.created_at, :format => :friendly))) %></h4>
-<%= htmlize(diary_comment.body) %>
+<%= diary_comment.body.to_html %>
 <%= if_administrator(:span) do %> 
   <%= link_to t('diary_entry.diary_comment.hide_link'), {:action => 'hidecomment', :display_name => diary_comment.diary_entry.user.display_name, :id => diary_comment.diary_entry.id, :comment => diary_comment.id}, {:confirm => t('diary_entry.diary_comment.confirm')} %>
 <% end %>
index 297f74ef9e59b123ba191ef6da3bb8c9d651c222..21c3ef6222868917009f8079a89f3b034f6344e8 100644 (file)
@@ -1,7 +1,7 @@
 <b><%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %></b><br />
 
 <div xml:lang="<%= diary_entry.language_code %>" lang="<%= diary_entry.language_code %>">
-  <%= htmlize(diary_entry.body) %>
+  <%= diary_entry.body.to_html %>
 </div>
 
 <% if diary_entry.latitude and diary_entry.longitude %>
index b506ddfb027609db5b522ec29bb7b7615440aaec..f477ab8b63e97016cb9aca9b3de082b3493a82f2 100644 (file)
@@ -11,7 +11,7 @@
   <tr class="<%= cl %>">
     <td><%= link_to comment.diary_entry.title, :action => :view, :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id %></td>
     <td><span title="<%= l comment.created_at, :format => :friendly %>"><%= t 'diary_entry.comments.ago', :ago => time_ago_in_words(comment.created_at) %></span></td>
-    <td><%= htmlize(comment.body) %></td>
+    <td><%= comment.body.to_html %></td>
   </tr>
   <% end -%>
 </table>
index 4c9670a6d5f118230a536fe24961723cf769fb88..7ee36e9bdb20e93d8cc093e0bd7984b335d88d74 100644 (file)
@@ -20,7 +20,7 @@ xml.rss("version" => "2.0",
         xml.title h(entry.title)
         xml.link url_for(:action => "view", :id => entry.id, :display_name => entry.user.display_name, :only_path => false)
         xml.guid url_for(:action => "view", :id => entry.id, :display_name => entry.user.display_name, :only_path => false)
-        xml.description htmlize(entry.body)
+        xml.description entry.body.to_html
         xml.author entry.user.display_name
         xml.pubDate entry.created_at.to_s(:rfc822)
         xml.comments url_for(:action => "view", :id => entry.id, :display_name => entry.user.display_name, :anchor => "comments", :only_path => false)
index 6bad015cc8c111b7059c8bdc556348914e801f83..d829c79e94520a3245f31a378fa02249f2266848 100644 (file)
@@ -20,7 +20,7 @@
   </tr>
   <tr>
     <th></th>
-    <td><%= htmlize(@message.body) %></td>
+    <td><%= @message.body.to_html %></td>
     <td></td>
   </tr>
 </table>
@@ -57,7 +57,7 @@
   </tr>
   <tr>
     <th></th>
-    <td><%= htmlize(@message.body) %></td>
+    <td><%= @message.body.to_html %></td>
     <td></td>
   </tr>
 </table>
index 193e10de9727d3fe9e858e9a6349f1862638c22d..7d112a42b6d09845dc60ed774aeed88c7436103a 100644 (file)
@@ -3,7 +3,7 @@
 <%= t'notifier.diary_comment_notification.header', :from_user => @from_user, :subject => @title %>
 
 ==
-<%= raw @text %>
+<%= raw @text.to_text %>
 ==
 
 <%= t'notifier.diary_comment_notification.footer', :readurl => @readurl, :commenturl => @commenturl, :replyurl => @replyurl %>
index ef55b9b96af68e3def52d07a5f1026a2543e3853..0b2ff9e69480c2ddf54250a2177811f3ad37aa91 100644 (file)
@@ -3,7 +3,7 @@
 <p><%= raw t'notifier.message_notification.header', :from_user => link_to(@from_user, :host => SERVER_URL, :controller => :user, :action => :view, :display_name => @from_user), :subject => @title %></p>
 
 ==
-<%= htmlize @text %>
+<%= @text.to_html %>
 ==
 
 <p>
index 25b3c0498d3c4090e1d2b4f6308fea8460c29d37..10a9ff361ea7d7d449d63b2858ca422a4ec3abcf 100644 (file)
@@ -3,7 +3,7 @@
 <%= raw t'notifier.message_notification.header', :from_user => @from_user, :subject => @title %>
 
 ==
-<%= raw @text %>
+<%= raw @text.to_text %>
 ==
 
 <%= raw t'notifier.message_notification.footer1', :readurl => @readurl %>
index 4f62a51e927416432564bf72060454309130ba3c..4f2f59c92d20782576845477d1f5b25c769a52c9 100644 (file)
@@ -19,7 +19,7 @@
         %>
       <% end %>
     </p>
-    <%= htmlize(user.description) %>
+    <%= user.description.to_html %>
   </td>
   <td>
     <%= check_box_tag "user_#{user.id}", "", false, :name => "user[#{user.id}]" %>
index b323ff1580d5901c7d1ffff86ac1645baddf8dbb..d9a65b3429ea5cc022df69413663b917bdc8923b 100644 (file)
 
 <h3><%= t 'user.view.description' %></h3>
 
-<div id="description"><%= htmlize(@this_user.description) %></div>
+<div id="description"><%= @this_user.description.to_html %></div>
 
 <% if @user and @this_user.id == @user.id %>
   <div id="map" class="user_map">
index 4ba69a73f6cb7e2fe9ec71c46ce550edab02c414..2311541d9f49f247050bf022a7106370568eec5a 100644 (file)
@@ -18,7 +18,7 @@
 <p><b><%= t'user_block.show.status' %></b>: <%= block_status(@user_block) %></p>
 
 <p><b><%= t'user_block.show.reason' %></b></p>
-<%= htmlize(@user_block.reason) %>
+<%= @user_block.reason.to_html %>
 
 <% if @user_block.ends_at > Time.now.getutc %>
 <% if @user and @user.id == @user_block.creator_id %>
diff --git a/db/migrate/20120214210114_add_text_format.rb b/db/migrate/20120214210114_add_text_format.rb
new file mode 100644 (file)
index 0000000..f448223
--- /dev/null
@@ -0,0 +1,21 @@
+require 'migrate'
+
+class AddTextFormat < ActiveRecord::Migration
+  def up
+    create_enumeration :format_enum, ["html", "markdown"]
+    add_column :users, :description_format, :format_enum, :null => false, :default => "html"
+    add_column :user_blocks, :reason_format, :format_enum, :null => false, :default => "html"
+    add_column :diary_entries, :body_format, :format_enum, :null => false, :default => "html"
+    add_column :diary_comments, :body_format, :format_enum, :null => false, :default => "html"
+    add_column :messages, :body_format, :format_enum, :null => false, :default => "html"
+  end
+
+  def down
+    remove_column :messages, :body_format
+    remove_column :diary_comments, :body_format
+    remove_column :diary_entries, :body_format
+    remove_column :user_blocks, :reason_format
+    remove_column :users, :description_format
+    drop_enumeration :format_enum
+  end
+end
diff --git a/lib/rich_text.rb b/lib/rich_text.rb
new file mode 100644 (file)
index 0000000..ec5e9e4
--- /dev/null
@@ -0,0 +1,57 @@
+module RichText
+  def self.new(format, text)
+    case format
+    when "html"; HTML.new(text || "")
+    when "markdown"; Markdown.new(text || "")
+    else; nil
+    end
+  end
+
+  class HTML < String
+    include ActionView::Helpers::TextHelper
+    include ActionView::Helpers::TagHelper
+
+    def to_html
+      linkify(sanitize(simple_format(self)))
+    end
+
+    def to_text
+      self
+    end
+
+  private
+
+    def sanitize(text)
+      Sanitize.clean(text, Sanitize::Config::OSM).html_safe
+    end
+
+    def linkify(text)
+      if text.html_safe?
+        Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow")).html_safe
+      else
+        Rinku.auto_link(text, :urls, tag_options(:rel => "nofollow"))
+      end
+    end
+  end
+
+  class Markdown < String
+    def to_html
+      html_parser.render(self).html_safe
+    end
+
+    def to_text
+      self
+    end
+
+  private
+
+    def html_parser
+      @@html_renderer ||= Redcarpet::Render::XHTML.new({
+        :filter_html => true, :safe_links_only => true
+      })
+      @@html_parser ||= Redcarpet::Markdown.new(@@html_renderer, {
+        :no_intra_emphasis => true, :autolink => true, :space_after_headers => true
+      })
+    end
+  end
+end