plain text html-decorated on the client side

This commit is contained in:
Jakub Pavlík jn 2013-07-14 21:17:37 +02:00
parent ef09b0fde3
commit ffb4ec4e4c
4 changed files with 47 additions and 38 deletions

View File

@ -48,8 +48,7 @@ class MailCatcher::Web < Sinatra::Base
"formats" => [
"source",
("html" if MailCatcher::Mail.message_has_html? id),
("plain" if MailCatcher::Mail.message_has_plain? id),
("lessplain" if MailCatcher::Mail.message_has_plain? id),
("plain" if MailCatcher::Mail.message_has_plain? id)
].compact,
"attachments" => MailCatcher::Mail.message_attachments(id).map do |attachment|
attachment.merge({"href" => "/messages/#{escape(id)}/parts/#{escape(attachment['cid'])}"})
@ -70,9 +69,6 @@ class MailCatcher::Web < Sinatra::Base
# Rewrite body to link to embedded attachments served by cid
body.gsub! /cid:([^'"> ]+)/, "#{id}/parts/\\1"
# Rewrite body to open links in a new window
body.gsub! /<a\s+/, '<a target="_blank" '
body
else
not_found
@ -89,19 +85,6 @@ class MailCatcher::Web < Sinatra::Base
end
end
get "/messages/:id.lessplain" do
id = params[:id].to_i
if part = MailCatcher::Mail.message_part_plain(id)
content_type "text/html", :charset => (part["charset"] || "utf8")
body = part["body"]
body.gsub!(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/, '<a href="\\1">\\1</a>')
body.gsub!("\n", "<br/>\n")
"<html><body>" + body + "</body></html>"
else
not_found
end
end
get "/messages/:id.source" do
id = params[:id].to_i
if message = MailCatcher::Mail.message(id)

View File

@ -252,6 +252,23 @@ class MailCatcher
if id?
$('#message iframe').attr "src", "/messages/#{id}.#{format}"
app = this
$('#message iframe').load ( -> app.decorateMessageBody format )
decorateMessageBody: (format) ->
if format == 'html'
setTimeout ( ->
body = $('#message iframe').contents().find('body')
$("a", body).attr("target", "_blank")
), 10
else if format == 'plain'
message_iframe = $('#message iframe').contents()
text = message_iframe.text()
text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, '<a href="$1" target="_blank">$1</a>')
text = text.replace(/\n/g, '<br/>')
message_iframe.find('html').html('<html><body>' + text + '</html></body>')
loadMessageAnalysis: (id) ->
id ||= @selectedMessage()

View File

@ -1,6 +1,6 @@
(function() {
var MailCatcher,
_this = this;
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
jQuery.expr[':'].icontains = function(a, i, m) {
var _ref, _ref1;
@ -8,24 +8,13 @@
};
MailCatcher = (function() {
function MailCatcher() {
this.nextTab = __bind(this.nextTab, this);
this.previousTab = __bind(this.previousTab, this);
this.openTab = __bind(this.openTab, this);
this.selectedTab = __bind(this.selectedTab, this);
this.getTab = __bind(this.getTab, this);
var _this = this;
this.nextTab = function(tab) {
return MailCatcher.prototype.nextTab.apply(_this, arguments);
};
this.previousTab = function(tab) {
return MailCatcher.prototype.previousTab.apply(_this, arguments);
};
this.openTab = function(i) {
return MailCatcher.prototype.openTab.apply(_this, arguments);
};
this.selectedTab = function() {
return MailCatcher.prototype.selectedTab.apply(_this, arguments);
};
this.getTab = function(i) {
return MailCatcher.prototype.getTab.apply(_this, arguments);
};
$('#messages tr').live('click', function(e) {
e.preventDefault();
return _this.loadMessage($(e.currentTarget).attr('data-message-id'));
@ -334,13 +323,35 @@
};
MailCatcher.prototype.loadMessageBody = function(id, format) {
var app;
id || (id = this.selectedMessage());
format || (format = $('#message .views .tab.format.selected').attr('data-message-format'));
format || (format = 'html');
$("#message .views .tab[data-message-format=\"" + format + "\"]:not(.selected)").addClass('selected');
$("#message .views .tab:not([data-message-format=\"" + format + "\"]).selected").removeClass('selected');
if (id != null) {
return $('#message iframe').attr("src", "/messages/" + id + "." + format);
$('#message iframe').attr("src", "/messages/" + id + "." + format);
app = this;
return $('#message iframe').load((function() {
return app.decorateMessageBody(format);
}));
}
};
MailCatcher.prototype.decorateMessageBody = function(format) {
var message_iframe, text;
if (format === 'html') {
return setTimeout((function() {
var body;
body = $('#message iframe').contents().find('body');
return $("a", body).attr("target", "_blank");
}), 10);
} else if (format === 'plain') {
message_iframe = $('#message iframe').contents();
text = message_iframe.text();
text = text.replace(/((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)/g, '<a href="$1" target="_blank">$1</a>');
text = text.replace(/\n/g, '<br/>');
return message_iframe.find('html').html('<html><body>' + text + '</html></body>');
}
};

View File

@ -53,8 +53,6 @@
%a{:href => '#'} HTML
%li.format.tab.plain{'data-message-format' => 'plain'}
%a{:href => '#'}Plain Text
%li.format.tab.plain{'data-message-format' => 'lessplain'}
%a{:href => '#'}Less Plain Text
%li.format.tab.source{'data-message-format' => 'source'}
%a{:href => '#'}Source
%li.tab.analysis