Linkify message:// URLs in notes. Those links are used by Mail.app on

Mac OS X to link to a mail message by message id.
This commit is contained in:
Patrice Neff 2010-01-10 02:47:54 +08:00 committed by Eric Allen
parent e844b5aa5b
commit 5299490c83
5 changed files with 69 additions and 5 deletions

View file

@ -325,4 +325,28 @@ module TodosHelper
return entries.map{|e| e.specification()}.join("\n") rescue '' return entries.map{|e| e.specification()}.join("\n") rescue ''
end end
AUTO_LINK_MESSAGE_RE = %r{message://<[^>]+>} unless const_defined?(:AUTO_LINK_MESSAGE_RE)
# Converts message:// links to href. This URL scheme is used on Mac OS X
# to link to a mail message in Mail.app.
def auto_link_message(text)
text.gsub(AUTO_LINK_MESSAGE_RE) do
href = $&
left, right = $`, $'
# detect already linked URLs and URLs in the middle of a tag
if left =~ /<[^>]+$/ && right =~ /^[^>]*>/
# do not change string; URL is alreay linked
href
else
content_tag(:a, h(href), :href => h(href))
end
end
end
def format_note(note)
note = auto_link_message(note)
note = auto_link(note)
note = markdown(note)
note = sanitize(note)
end
end end

View file

@ -2,7 +2,7 @@
<div id="<%= dom_id(note, 'container') %>"> <div id="<%= dom_id(note, 'container') %>">
<h2><%= link_to("Note #{note.id}", note_path(note), :title => "Show note #{note.id}" ) %></h2> <h2><%= link_to("Note #{note.id}", note_path(note), :title => "Show note #{note.id}" ) %></h2>
<div class="project_notes" id="<%= dom_id(note) %>"> <div class="project_notes" id="<%= dom_id(note) %>">
<%= sanitize(markdown(auto_link(note.body))) %> <%= format_note(note.body) %>
<div class="note_footer"> <div class="note_footer">
<%= link_to_remote( <%= link_to_remote(

View file

@ -1,5 +1,5 @@
<%= link_to(image_tag( 'blank.png', :width=>'16', :height=>'16', :border=>'0' ), "#", {:class => 'show_notes', :title => 'Show notes'}) %> <%= link_to(image_tag( 'blank.png', :width=>'16', :height=>'16', :border=>'0' ), "#", {:class => 'show_notes', :title => 'Show notes'}) %>
<div class="todo_notes" id="<%= dom_id(item, 'notes') %>" style="display:none"> <div class="todo_notes" id="<%= dom_id(item, 'notes') %>" style="display:none">
<%= sanitize(markdown( auto_link(item.notes)) ) %> <%= format_note(item.notes) %>
</div> </div>

View file

@ -58,7 +58,7 @@ Rails::Initializer.run do |config|
# allow other protocols in urls for sanitzer. Add to your liking, for example # allow other protocols in urls for sanitzer. Add to your liking, for example
# config.action_view.sanitized_allowed_protocols = 'onenote', 'blah', 'proto' # config.action_view.sanitized_allowed_protocols = 'onenote', 'blah', 'proto'
# to enable "link":onenote://... or "link":blah://... hyperlinks # to enable "link":onenote://... or "link":blah://... hyperlinks
config.action_view.sanitized_allowed_protocols = 'onenote' config.action_view.sanitized_allowed_protocols = 'onenote', 'message'
# See Rails::Configuration for more options # See Rails::Configuration for more options
end end

View file

@ -510,4 +510,44 @@ class TodosControllerTest < ActionController::TestCase
assert_select("a[href=#{url}]") assert_select("a[href=#{url}]")
end end
def test_format_note_normal
login_as(:admin_user)
todo = users(:admin_user).todos.first
todo.notes = "A normal description."
todo.save!
get :index
assert_select("div#notes_todo_#{todo.id}", "A normal description.")
end
def test_format_note_markdown
login_as(:admin_user)
todo = users(:admin_user).todos.first
todo.notes = "A *bold description*."
todo.save!
get :index
assert_select("div#notes_todo_#{todo.id}", "A bold description.")
assert_select("div#notes_todo_#{todo.id} strong", "bold description")
end
def test_format_note_link
login_as(:admin_user)
todo = users(:admin_user).todos.first
todo.notes = "A link to http://github.com/."
todo.save!
get :index
assert_select("div#notes_todo_#{todo.id}", 'A link to http://github.com/.')
assert_select("div#notes_todo_#{todo.id} a[href=http://github.com/]", 'http://github.com/')
end
def test_format_note_link_message
login_as(:admin_user)
todo = users(:admin_user).todos.first
todo.notes = "A Mail.app message://<ABCDEF-GHADB-123455-FOO-BAR@example.com> link"
todo.save!
get :index
# puts css_select("div#notes_todo_#{todo.id}")
assert_select("div#notes_todo_#{todo.id}", 'A Mail.app message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt; link')
assert_select("div#notes_todo_#{todo.id} a", 'message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt;')
assert_select("div#notes_todo_#{todo.id} a[href=message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt;]", 'message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt;')
end
end end