ryanlowe-easy_format 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1,14 @@
1
+
2
+ == 0.1.0
3
+
4
+ May 26
5
+ = added GitHub gemspec
6
+
7
+ May 1 2008
8
+ - moved to GitHub
9
+
10
+ Dec 17 2007
11
+ - handle nil and non-string properly
12
+
13
+ Sep 25 2007
14
+ - simple_format plugin created from code in existing projects
data/MIT-LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2007-2008 Ryan Lowe
2
+
3
+ http://ryanlowe.ca
4
+ http://disruptiveagility.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,35 @@
1
+
2
+ Hosted at GitHub since May 1, 2008
3
+ http://github.com/ryanlowe/easy_format
4
+
5
+ Was hosted at RubyForge
6
+ http://rubyforge.org/projects/simple-format/
7
+
8
+
9
+ = easy_format plugin for Ruby on Rails
10
+
11
+ easy_format is a Rails plugin that formats and escapes database
12
+ text for use in HTML views. The result looks a lot like text in
13
+ Gmail: line breaks are converted and links are linked.
14
+
15
+ It will not support tags like BBCode; it just formats plain text.
16
+
17
+ == Installation
18
+
19
+ It looks like Rails 2.1 will support "script/plugin install" with Git
20
+ repositories. Until then you can put this plugin in vendor/plugins with:
21
+
22
+ git clone git://github.com/ryanlowe/easy_format.git
23
+
24
+ and delete the .git directory inside it before committing it to source control.
25
+
26
+ When Rails 2.1 supports Git you should be able to do:
27
+
28
+ script/plugin install git://github.com/ryanlowe/easy_format.git
29
+
30
+ == Usage
31
+
32
+ <%= EasyFormat.format(@post.body) %>
33
+
34
+ Note: HTML angle brackets are escaped by format() so
35
+ you do not need to use h() as well.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the easy_format plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the easy_format plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'EasyFormat'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "easy_format"
3
+ s.version = "0.1.0"
4
+ s.date = "2008-05-26"
5
+ s.summary = "Formats and escapes database text for safe use in Ruby on Rails views"
6
+ s.email = "rails@ryanlowe.ca"
7
+ s.homepage = "http://github.com/ryanlowe/easy_format"
8
+ s.description = "Formats and escapes database text for safe use in Ruby on Rails views"
9
+ s.has_rdoc = false
10
+ s.authors = ["Ryan Lowe"]
11
+ s.files = ["README", "CHANGELOG", "MIT-LICENSE","Rakefile", "easy_format.gemspec", "init.rb","lib/easy_format.rb",
12
+ "test/easy_format_test.rb","test/test_helper.rb"]
13
+ s.test_files = ["test/easy_format_test.rb","test/test_helper.rb"]
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README","CHANGELOG"]
16
+ s.add_dependency("rails", ["> 2.0.0"])
17
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'easy_format'
@@ -0,0 +1,71 @@
1
+ class EasyFormat
2
+ def self.tab
3
+ '<span class="tab">&nbsp;</span>'
4
+ end
5
+
6
+ def self.format(text, break_lines=true)
7
+ return "" if text.nil?
8
+ text = text.to_s
9
+ text.strip!
10
+ output = Array.new
11
+ lines = text.split($/)
12
+ for line in lines
13
+ line = escape_html(line)
14
+ line = replace_tabs(line)
15
+ line.strip!
16
+ line = link_urls(line)
17
+ output.push(line)
18
+ end
19
+ connector = break_lines ? "<br/>\n" : " "
20
+ output.join(connector)
21
+ end
22
+
23
+ def self.replace_tabs(line)
24
+ tabbed = line.split(/\t/)
25
+ output = tabbed.join(" ").strip
26
+ output = tab+output if !tabbed[0].nil? and tabbed[0].strip.length < 1 and output.length > 0
27
+ output
28
+ end
29
+
30
+ def self.link_urls(text)
31
+ protocols = ['http:','https:']
32
+ for protocol in protocols
33
+ re = Regexp.new(protocol+'(\S+)')
34
+ if text =~ re
35
+ md = re.match(text)
36
+ url, ending = strip_last_punctuation(md[1])
37
+ after = md.post_match
38
+ if after =~ re
39
+ after = link_urls(after)
40
+ end
41
+ output = md.pre_match
42
+ url = protocol+url
43
+ output += '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>'
44
+ output += ending
45
+ output += after
46
+ text = output
47
+ end
48
+ end
49
+ text
50
+ end
51
+
52
+ def self.strip_last_punctuation(text)
53
+ url = text
54
+ ending = ""
55
+ re = /([[:punct:]])$/
56
+ if text =~ re
57
+ md = re.match(text)
58
+ url = md.pre_match
59
+ ending = md[1]
60
+ if ending == "/" #directory in URL
61
+ url += ending
62
+ ending = ""
63
+ end
64
+ end
65
+ [url, ending]
66
+ end
67
+
68
+ def self.escape_html(text)
69
+ text.gsub(/[<]/,'&lt;').gsub(/[>]/,'&gt;')
70
+ end
71
+ end
@@ -0,0 +1,147 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/easy_format'
3
+
4
+ class EasyFormatTest < Test::Unit::TestCase
5
+
6
+ def test_corner_inputs
7
+ assert_equal "", EasyFormat.format(nil)
8
+ assert_equal "", EasyFormat.format("")
9
+ assert_equal "42", EasyFormat.format(42)
10
+ end
11
+
12
+ #
13
+ # line breaks
14
+ #
15
+
16
+ def test_format_line_breaks
17
+ assert_equal "", EasyFormat.format("\n")
18
+ assert_equal "hello", EasyFormat.format("hello\n")
19
+ assert_equal "world!", EasyFormat.format("\nworld!")
20
+
21
+ assert_equal "hello<br/>\nworld!", EasyFormat.format("hello\nworld!")
22
+ assert_equal "hello<br/>\n<br/>\nworld!", EasyFormat.format("hello\n\nworld!")
23
+ assert_equal "hello<br/>\n<br/>\n<br/>\nworld!", EasyFormat.format("hello\n\n\nworld!")
24
+ end
25
+
26
+ def test_format_line_breaks
27
+ assert_equal "", EasyFormat.format("\n", false)
28
+ assert_equal "hello", EasyFormat.format("hello\n", false)
29
+ assert_equal "world!", EasyFormat.format("\nworld!", false)
30
+
31
+ assert_equal "hello world!", EasyFormat.format("hello\nworld!", false)
32
+ assert_equal "hello world!", EasyFormat.format("hello\n\nworld!", false)
33
+ assert_equal "hello world!", EasyFormat.format("hello\n\n\nworld!", false)
34
+ end
35
+
36
+ #
37
+ # tabs
38
+ #
39
+
40
+ def test_format_tabs
41
+ assert_equal '<span class="tab">&nbsp;</span>', EasyFormat.tab
42
+
43
+ assert_equal "", EasyFormat.format("\t")
44
+ assert_equal "hello", EasyFormat.format("hello\t")
45
+ assert_equal "world!", EasyFormat.format("\tworld!")
46
+
47
+ #tab beginning of line is replaced with non-breaking spaces
48
+ assert_equal "hello<br/>\n"+EasyFormat.tab+"world!", EasyFormat.format("hello\n\tworld!")
49
+ assert_equal "hello<br/>\n"+EasyFormat.tab+"world!", EasyFormat.format("hello\n \tworld!")
50
+ #except on a blank line
51
+ assert_equal "something<br/>\n<br/>\nanother", EasyFormat.format("something\n\t\nanother")
52
+
53
+ #tab middle of line is replaced with a soft space
54
+ assert_equal "hello world!", EasyFormat.format("hello\tworld!")
55
+ assert_equal "hello world!", EasyFormat.format("hello\t\tworld!")
56
+ assert_equal "hello world!", EasyFormat.format("hello \t\tworld!")
57
+
58
+ original = "Source Information:\n"+
59
+ " \tDwelling \tWalters Cottage Robertsbridge Rd\n"+
60
+ " \tCensus Place\tMountfield, Sussex, England\n"+
61
+ " \tFamily History Library Film \t1341245"
62
+ expected = "Source Information:<br/>\n"+
63
+ EasyFormat.tab+"Dwelling Walters Cottage Robertsbridge Rd<br/>\n"+
64
+ EasyFormat.tab+"Census Place Mountfield, Sussex, England<br/>\n"+
65
+ EasyFormat.tab+"Family History Library Film 1341245"
66
+ assert_equal expected, EasyFormat.format(original)
67
+ end
68
+
69
+ #
70
+ # urls
71
+ #
72
+
73
+ def test_format_url_domain
74
+ url = "http://www.fanconcert.com"
75
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format(url)
76
+ assert_equal 'Go here: <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("Go here: "+url)
77
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a> is cool!', EasyFormat.format(url+" is cool!")
78
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a> a lot!', EasyFormat.format("I really like "+url+" a lot!")
79
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("I really like "+url+"\n")
80
+ end
81
+
82
+ def test_format_url_secure_domain
83
+ url = "https://www.fanconcert.com"
84
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format(url)
85
+ assert_equal 'Go here: <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("Go here: "+url)
86
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a> is cool!', EasyFormat.format(url+" is cool!")
87
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a> a lot!', EasyFormat.format("I really like "+url+" a lot!")
88
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("I really like "+url+"\n")
89
+ end
90
+
91
+ def test_format_url_directory
92
+ url = "http://www.fanconcert.com/search/"
93
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format(url)
94
+ assert_equal 'Go here: <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("Go here: "+url)
95
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a> is cool!', EasyFormat.format(url+" is cool!")
96
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a> a lot!', EasyFormat.format("I really like "+url+" a lot!")
97
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("I really like "+url+"\n")
98
+ end
99
+
100
+ def test_format_url_querystring
101
+ url = "http://www.fanconcert.com/search/concerts?after=now&close_to=1&proximity=200&artist_tagged_by=1"
102
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format(url)
103
+ assert_equal 'Go here: <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("Go here: "+url)
104
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a> is cool!', EasyFormat.format(url+" is cool!")
105
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a> a lot!', EasyFormat.format("I really like "+url+" a lot!")
106
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("I really like "+url+"\n")
107
+ end
108
+
109
+ def test_format_url_page
110
+ url = "http://www.normal.com/max.html"
111
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format(url)
112
+ assert_equal 'Go here: <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("Go here: "+url)
113
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a> is cool!', EasyFormat.format(url+" is cool!")
114
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a> a lot!', EasyFormat.format("I really like "+url+" a lot!")
115
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>', EasyFormat.format("I really like "+url+"\n")
116
+ end
117
+
118
+ def test_format_url_domain_ending_in_period
119
+ url = "http://www.cnn.com"
120
+ assert_equal 'I do not like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>.', EasyFormat.format("I do not like "+url+".")
121
+ end
122
+
123
+ def test_format_url_domain_ending_in_punctuation
124
+ url = "http://www.cnn.com"
125
+ assert_equal 'I really like <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>!', EasyFormat.format("I really like "+url+"!")
126
+ assert_equal 'Have you been to <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>?', EasyFormat.format("Have you been to "+url+"?")
127
+ end
128
+
129
+ def test_format_two_urls
130
+ url = "http://www.cnn.com"
131
+ assert_equal '<a class="external" href="'+url+'" title="'+url+'">'+url+'</a>? WTF is <a class="external" href="'+url+'" title="'+url+'">'+url+'</a>?', EasyFormat.format(url+"? WTF is "+url+"?")
132
+ end
133
+
134
+ # def test_format_www
135
+ # www_url = "www.cnn.com"
136
+ # url = "http://"+www_url
137
+ # assert_equal 'I really like <a class="external" href="'+url+'" title="'+www_url+'">'+www_url+'</a>', EasyFormat.format("I really like "+www_url)
138
+ # end
139
+
140
+ #
141
+ # escaping html
142
+ #
143
+
144
+ def test_escape_html
145
+ assert_equal "&lt;b&gt;bold!&lt;/b&gt;", EasyFormat.escape_html("<b>bold!</b>")
146
+ end
147
+ end
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_support'
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), '../lib')
6
+
7
+ RAILS_ROOT = '.' unless defined?(RAILS_ROOT)
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ryanlowe-easy_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Lowe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ version:
24
+ description: Formats and escapes database text for safe use in Ruby on Rails views
25
+ email: rails@ryanlowe.ca
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - CHANGELOG
33
+ files:
34
+ - README
35
+ - CHANGELOG
36
+ - MIT-LICENSE
37
+ - Rakefile
38
+ - easy_format.gemspec
39
+ - init.rb
40
+ - lib/easy_format.rb
41
+ - test/easy_format_test.rb
42
+ - test/test_helper.rb
43
+ has_rdoc: false
44
+ homepage: http://github.com/ryanlowe/easy_format
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Formats and escapes database text for safe use in Ruby on Rails views
70
+ test_files:
71
+ - test/easy_format_test.rb
72
+ - test/test_helper.rb