RedCloth librairy is now present in Redmine lib directory. git-svn-id: http://redmine.rubyforge.org/svn/trunk@699 e93f8b46-1217-0410-a6f0-8f06a7374b81pull/351/head
parent
324b904ed5
commit
8a3e713f2f
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,79 @@ |
||||
require 'redcloth' |
||||
|
||||
module Redmine |
||||
module WikiFormatting |
||||
|
||||
private |
||||
|
||||
class TextileFormatter < RedCloth |
||||
RULES = [:inline_auto_link, :inline_auto_mailto, :textile ] |
||||
|
||||
def initialize(*args) |
||||
super |
||||
self.hard_breaks=true |
||||
end |
||||
|
||||
def to_html |
||||
super(*RULES).to_s |
||||
end |
||||
|
||||
private |
||||
|
||||
# Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet. |
||||
# <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a> |
||||
def hard_break( text ) |
||||
text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks |
||||
end |
||||
|
||||
AUTO_LINK_RE = %r{ |
||||
( # leading text |
||||
<\w+.*?>| # leading HTML tag, or |
||||
[^=<>!:'"/]| # leading punctuation, or |
||||
^ # beginning of line |
||||
) |
||||
( |
||||
(?:https?://)| # protocol spec, or |
||||
(?:www\.) # www.* |
||||
) |
||||
( |
||||
[-\w]+ # subdomain or domain |
||||
(?:\.[-\w]+)* # remaining subdomains or domain |
||||
(?::\d+)? # port |
||||
(?:/(?:(?:[~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path |
||||
(?:\?[\w\+%&=.;-]+)? # query string |
||||
(?:\#[\w\-]*)? # trailing anchor |
||||
) |
||||
([[:punct:]]|\s|<|$) # trailing text |
||||
}x unless const_defined?(:AUTO_LINK_RE) |
||||
|
||||
# Turns all urls into clickable links (code from Rails). |
||||
def inline_auto_link(text) |
||||
text.gsub!(AUTO_LINK_RE) do |
||||
all, a, b, c, d = $&, $1, $2, $3, $4 |
||||
if a =~ /<a\s/i || a =~ /![<>=]?/ |
||||
# don't replace URL's that are already linked |
||||
# and URL's prefixed with ! !> !< != (textile images) |
||||
all |
||||
else |
||||
text = b + c |
||||
%(#{a}<a href="#{b=="www."?"http://www.":b}#{c}">#{text}</a>#{d}) |
||||
end |
||||
end |
||||
end |
||||
|
||||
# Turns all email addresses into clickable links (code from Rails). |
||||
def inline_auto_mailto(text) |
||||
text.gsub!(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do |
||||
text = $1 |
||||
%{<a href="mailto:#{$1}" class="email">#{text}</a>} |
||||
end |
||||
end |
||||
end |
||||
|
||||
public |
||||
|
||||
def self.to_html(text, options = {}) |
||||
TextileFormatter.new(text).to_html |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,35 @@ |
||||
# Re-raise errors caught by the controller. |
||||
class StubController < ApplicationController |
||||
def rescue_action(e) raise e end; |
||||
attr_accessor :request, :url |
||||
end |
||||
|
||||
class HelperTestCase < Test::Unit::TestCase |
||||
|
||||
# Add other helpers here if you need them |
||||
include ActionView::Helpers::ActiveRecordHelper |
||||
include ActionView::Helpers::TagHelper |
||||
include ActionView::Helpers::FormTagHelper |
||||
include ActionView::Helpers::FormOptionsHelper |
||||
include ActionView::Helpers::FormHelper |
||||
include ActionView::Helpers::UrlHelper |
||||
include ActionView::Helpers::AssetTagHelper |
||||
include ActionView::Helpers::PrototypeHelper |
||||
|
||||
def setup |
||||
super |
||||
|
||||
@request = ActionController::TestRequest.new |
||||
@controller = StubController.new |
||||
@controller.request = @request |
||||
|
||||
# Fake url rewriter so we can test url_for |
||||
@controller.url = ActionController::UrlRewriter.new @request, {} |
||||
|
||||
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default |
||||
end |
||||
|
||||
def test_dummy |
||||
# do nothing - required by test/unit |
||||
end |
||||
end |
@ -0,0 +1,66 @@ |
||||
# redMine - project management software |
||||
# Copyright (C) 2006-2007 Jean-Philippe Lang |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
|
||||
require File.dirname(__FILE__) + '/../../test_helper' |
||||
|
||||
class ApplicationHelperTest < HelperTestCase |
||||
include ApplicationHelper |
||||
fixtures :projects |
||||
|
||||
def setup |
||||
super |
||||
end |
||||
|
||||
def test_auto_links |
||||
to_test = { |
||||
'http://foo.bar' => '<a href="http://foo.bar">http://foo.bar</a>', |
||||
'www.foo.bar' => '<a href="http://www.foo.bar">www.foo.bar</a>', |
||||
'http://foo.bar/page?p=1&t=z&s=' => '<a href="http://foo.bar/page?p=1&t=z&s=">http://foo.bar/page?p=1&t=z&s=</a>', |
||||
'http://foo.bar/page#125' => '<a href="http://foo.bar/page#125">http://foo.bar/page#125</a>' |
||||
} |
||||
to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } |
||||
end |
||||
|
||||
def test_auto_mailto |
||||
assert_equal '<p><a href="mailto:test@foo.bar" class="email">test@foo.bar</a></p>', |
||||
textilizable('test@foo.bar') |
||||
end |
||||
|
||||
def test_textile_tags |
||||
to_test = { |
||||
# inline images |
||||
'!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />', |
||||
'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>', |
||||
# textile links |
||||
'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar">link</a>', |
||||
'"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title">link</a>' |
||||
} |
||||
to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } |
||||
end |
||||
|
||||
def test_redmine_links |
||||
issue_link = link_to('#52', {:controller => 'issues', :action => 'show', :id => 52}, :class => 'issue') |
||||
changeset_link = link_to('r19', {:controller => 'repositories', :action => 'revision', :id => 1, :rev => 19}, :class => 'changeset') |
||||
|
||||
to_test = { |
||||
'#52, #52 and #52.' => "#{issue_link}, #{issue_link} and #{issue_link}.", |
||||
'r19' => changeset_link |
||||
} |
||||
@project = Project.find(1) |
||||
to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } |
||||
end |
||||
end |
Loading…
Reference in new issue