linkize 0.0.1

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/COPYING ADDED
@@ -0,0 +1,280 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Library General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 / 2008-06-23
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,44 @@
1
+ Linkize is copyrighted free software by Phillip Calvin <pnc1138@gmail.com>.
2
+ You can redistribute it and/or modify it under either the terms of the GPL
3
+ (see COPYING file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
41
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
+ PURPOSE.
44
+
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ COPYING
2
+ History.txt
3
+ LICENSE
4
+ Manifest.txt
5
+ README.txt
6
+ Rakefile
7
+ lib/linkize.rb
8
+ lib/linkize/version.rb
9
+ spec/helpers/uris.rb
10
+ spec/linkize_spec.rb
11
+ spec/spec.opts
data/README.txt ADDED
@@ -0,0 +1,51 @@
1
+ == About
2
+
3
+ Linkize discovers and parses URLs within normal text. It does this in a way
4
+ similar to Adium, with both fully-qualified and more intelligent link parsing.
5
+
6
+ == Basic Usage
7
+
8
+ Creating links:
9
+ require 'linkize'
10
+ "You should see example.com.".recognize_links
11
+ => 'You should see <a href="http://example.com">example.com</a>.'
12
+
13
+ With custom formatting and in-place replacement:
14
+ "You should see example.com.".recognize_links! do |normalized, raw|
15
+ "#{raw} (#normalized)"
16
+ end
17
+ => 'You should see example.com (http://example.com).'
18
+
19
+ Just finding links:
20
+ text = "A cool website: example.com"
21
+ text.links
22
+ # => [#<URI::Generic:0x32dd4 uri:example.com>]
23
+
24
+ == Documentation
25
+
26
+ Documentation is supplied using RDoc. Check out the doc/ directory.
27
+
28
+ == Issues
29
+ Please let me know via the RubyForge tracker or email about any bugs you
30
+ find. Even better by letting me know with a spec test. Even better than that
31
+ is letting me know with a spec test and a patch. Thanks!
32
+
33
+ == Requirements
34
+
35
+ You'll need the RSpec gem to run the spec tests.
36
+
37
+ == TODO
38
+
39
+ * Use +memoize+ to cache found URIs, to keep from regexing more than once
40
+ * Optionally crawl links using +hpricot+ and return relevant images and
41
+ descriptive text.
42
+
43
+ == Author
44
+
45
+ Phillip Calvin <pnc1138@gmail.com>
46
+ http://phillip.toasterlogic.com
47
+
48
+ == License
49
+
50
+ This library is licensed under the Ruby License, for maximum compatibility
51
+ with other Ruby software. See the LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,175 @@
1
+ # This Rakefile was lifted from Luke Redpath. Many thanks to him.
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rake/packagetask'
7
+ require 'rake/gempackagetask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+
12
+ begin
13
+ require 'spec/rake/spectask'
14
+ rescue LoadError
15
+ puts 'To use rspec for testing you must install rspec gem:'
16
+ puts '$ sudo gem install rspec'
17
+ exit
18
+ end
19
+
20
+ include FileUtils
21
+
22
+ # Load the version of the library
23
+ require File.join(File.dirname(__FILE__), 'lib', 'linkize', 'version')
24
+
25
+ AUTHOR = 'Phillip Calvin' # can also be an array of Authors
26
+ EMAIL = "pnc@toasterlogic.com"
27
+ DESCRIPTION = "URI link parsing and recognition library"
28
+ GEM_NAME = 'linkize' # what people type to install the gem
29
+
30
+ @config_file = "~/.rubyforge/user-config.yml"
31
+ @config = nil
32
+
33
+ def rubyforge_username
34
+ unless @config
35
+ begin
36
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
37
+ rescue
38
+ puts <<-EOS
39
+ ERROR: No rubyforge config file found: #{@config_file}"
40
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
41
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
42
+ EOS
43
+ exit
44
+ end
45
+ end
46
+ @rubyforge_username ||= @config["username"]
47
+ end
48
+
49
+ RUBYFORGE_PROJECT = 'linkize' # The unix name for your project
50
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
51
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
52
+
53
+ NAME = "linkize"
54
+ REV = nil
55
+ # UNCOMMENT IF REQUIRED:
56
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
57
+ VERS = Linkize::VERSION::STRING + (REV ? ".#{REV}" : "")
58
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store', '**/*.log']
59
+ RDOC_OPTS = ['--quiet', '--title', 'Linkize Documentation',
60
+ "--opname", "index.html",
61
+ "--line-numbers",
62
+ "--main", "README",
63
+ "--inline-source"]
64
+
65
+ class Hoe
66
+ def extra_deps
67
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
68
+ end
69
+ end
70
+
71
+ # Generate all the Rake tasks
72
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
73
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
74
+ p.author = AUTHOR
75
+ p.description = DESCRIPTION
76
+ p.email = EMAIL
77
+ p.summary = DESCRIPTION
78
+ p.url = HOMEPATH
79
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
80
+ p.test_globs = ["test/**/test_*.rb"]
81
+ p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
82
+
83
+ # == Optional
84
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
85
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
86
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
87
+ end
88
+
89
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
90
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
91
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
92
+
93
+ desc 'Upload website files to rubyforge'
94
+ task :website_upload do
95
+ host = "#{rubyforge_username}@rubyforge.org"
96
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
97
+ local_dir = 'website'
98
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
99
+ end
100
+
101
+ desc 'Upload website files'
102
+ task :website => [:website_upload, :publish_docs]
103
+
104
+ desc 'Release the website and new gem version'
105
+ task :deploy => [:check_version, :website, :release] do
106
+ puts "Remember to create SVN tag:"
107
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
108
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
109
+ puts "Suggested comment:"
110
+ puts "Tagging release #{CHANGES}"
111
+ end
112
+
113
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
114
+ task :local_deploy => [:website_generate, :install_gem]
115
+
116
+ task :check_version do
117
+ unless ENV['VERSION']
118
+ puts 'Must pass a VERSION=x.y.z release version'
119
+ exit
120
+ end
121
+ unless ENV['VERSION'] == VERS
122
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
123
+ exit
124
+ end
125
+ end
126
+
127
+ desc "Run the specs under spec/models"
128
+ Spec::Rake::SpecTask.new do |t|
129
+ t.spec_opts = ['--options', "spec/spec.opts"]
130
+ t.spec_files = FileList['spec/*_spec.rb']
131
+ end
132
+
133
+ desc "Generate a list of all tested URIs"
134
+ task :output_tested_uris do
135
+ output_file = File.new('doc/tested_uris.txt', 'w')
136
+ # Load the RSpec constants
137
+ require 'spec/helpers/uris'
138
+
139
+ # Output each protocol
140
+ TEST_PROTOCOLS.each do |protocol|
141
+ # With each URI
142
+ TEST_URIS.each do |uri|
143
+ output_file.write("#{protocol}#{uri}\n")
144
+ end
145
+ end
146
+ end
147
+
148
+
149
+ # Helper to delete rake tasks
150
+ Rake::TaskManager.class_eval do
151
+ def remove_task(task_name)
152
+ @tasks.delete(task_name.to_s)
153
+ end
154
+ end
155
+
156
+ def remove_task(task_name)
157
+ Rake.application.remove_task(task_name)
158
+ end
159
+
160
+ # Hoe won't let you use a different theme, delete its :docs task
161
+ remove_task :docs
162
+ desc "Generate the documentation"
163
+ Rake::RDocTask.new("docs") { |rdoc|
164
+ rdoc.rdoc_dir = 'doc/'
165
+ rdoc.title = "Linkize Documentation"
166
+ rdoc.template = "/opt/local/lib/ruby/gems/1.8/gems/allison-2.0.3/lib/allison.rb"
167
+ rdoc.options << '--line-numbers' << '--inline-source'
168
+ rdoc.rdoc_files.include('README.txt')
169
+ rdoc.rdoc_files.include('lib/**/*.rb')
170
+ }
171
+
172
+ Rake::Task['default'].prerequisites.clear
173
+
174
+ desc "Default task is to run specs"
175
+ task :default => :spec
data/lib/linkize.rb ADDED
@@ -0,0 +1,183 @@
1
+ require 'uri'
2
+ require 'logger'
3
+
4
+ =begin rdoc
5
+ == Basic Usage
6
+ Linkize's methods are accessed through Ruby's String object. This allows
7
+ Linkize to find and/or create hyperlinks from URIs in that String.
8
+ =end
9
+ class Linkize
10
+ # Store all the useful IANA TLDs so that we can match text like
11
+ # "example.com" without http://
12
+ TLDS = %w(ac ad ae aero af ag ai al am an ao aq ar arpa as asia at au aw
13
+ ax az ba bb bd be bf bg bh bi biz bj bl bm bn bo br bs bt bv bw by bz ca
14
+ cat cc cd cf cg ch ci ck cl cm cn co com coop cr cu cv cx cy cz de dj dk
15
+ dm do dz ec edu ee eg eh er es et eu fi fj fk fm fo fr ga gb gd ge gf gg
16
+ gh gi gl gm gn gov gp gq gr gs gt gu gw gy hk hm hn hr ht hu id ie il im
17
+ in info int io iq ir is it je jm jo jobs jp ke kg kh ki km kn kp kr kw ky
18
+ kz la lb lc li lk lr ls lt lu lv ly ma mc md me mf mg mh mil mk ml mm mn
19
+ mo mobi mp mq mr ms mt mu museum mv mw mx my mz na name nc ne net nf ng
20
+ ni nl no np nr nu nz om org pa pe pf pg ph pk pl pm pn pr pro ps pt pw py
21
+ qa re ro rs ru rw sa sb sc sd se sg sh si sj sk sl sm sn so sr st su sv sy
22
+ sz tc td tel tf tg th tj tk tl tm tn to tp tr travel tt tv tw tz ua ug uk
23
+ um us uy uz va vc ve vg vi vn vu wf ws ye yt yu za zm zw)
24
+
25
+ # Match similar schemes
26
+ SCHEMES = %w(http:// https:// ftp:// mailto:)
27
+
28
+ # Match text that looks like a URI
29
+ ADDRESS_REGEXP = /
30
+ \b # Match a word boundary
31
+ ( # Match group for the entire uri
32
+ ( # Protocol specification
33
+ (#{SCHEMES.join('|')}) # Matchs cheme identifier
34
+ )? # Protocol is optional
35
+ (\w+(:\w+)?@)? # Optional username and or password
36
+ (
37
+ ( # Match either a domain...
38
+ ([\w]+\.)+ # Match at least one domain and delimeter
39
+ (#{TLDS.join('|')}) # Match any of the existing IANA TLDs
40
+ )
41
+ | # or an IPv4 address...
42
+ (
43
+ (\d{1,3}\.){3} # Exactly three 255. matches
44
+ \d{1,3} # Exactly one final 255 match
45
+ )
46
+ )
47
+ (:[\d]{1,6})? # Match a port, if one is provided
48
+ (\/[^\s]*)? # Match any remaining uri, such as
49
+ # path or parameters
50
+ )
51
+ \b # End at another word boundary
52
+ /ix # Ignore case and whitespace
53
+
54
+ # Create a URI object from a given string, using some
55
+ # smarts to determine if the URI is degenerate and lacks
56
+ # a specific scheme.
57
+ def self.uri_from_string(str)
58
+ # Determine if this URI is degenerate and has no protocol
59
+ begin
60
+ uri = URI.parse(str) if str
61
+ rescue URI::InvalidURIError
62
+ uri = str
63
+ end
64
+
65
+ # Did URI identify the protocol?
66
+ case uri
67
+ when URI::HTTP, URI::HTTPS, URI::FTP
68
+ return uri.normalize
69
+ when URI::Generic, String
70
+ begin
71
+ return URI.parse("http://#{uri}").normalize
72
+ rescue URI::InvalidURIError
73
+ # Completely unable to parse
74
+ end
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ class String
81
+ # Returns an Array of all the URI objects in this string. Returns a
82
+ # blank Array if none are found.
83
+ # Example:
84
+ # text = "I enjoy example.com, or sometimes iana.org."
85
+ # text.links
86
+ # # => [#<URI::Generic:0x32dd4 uri:example.com>,
87
+ # #<URI::Generic:0x32da4 uri:iana.org>]
88
+ #
89
+ # As strings:
90
+ # text = "Go visit example.com!"
91
+ # "Links in the document: #{text.links.join(', ')}"
92
+ # # => "Links in the document: http://example.com"
93
+ def links
94
+ # Iterate each raw URIs
95
+ uris = raw_uris.collect do |raw_uri|
96
+ Linkize.uri_from_string(raw_uri)
97
+ end
98
+
99
+ uris.compact!
100
+
101
+ # XXX: This could return nil for blank, but Array makes more duck sense
102
+ return uris || []
103
+ end
104
+
105
+ # Returns a new String with any URIs in the original string converted
106
+ # into hyperlinks. Optionally takes a +format+ block. See recognize_links!
107
+ #
108
+ # Example:
109
+ # "You should see example.com.".recognize_links
110
+ # # => 'You should see <a href="http://example.com">example.com</a>.'
111
+ def recognize_links(&format)
112
+ format ? dup.recognize_links!(&format) : dup.recognize_links!
113
+ end
114
+
115
+ alias linkize recognize_links
116
+
117
+ # Modifies the String destructively by converting URIs into hyperlinks.
118
+ # Takes an optional +format+ block for custom conversion.
119
+ #
120
+ # Example:
121
+ # "You should see example.com.".recognize_links!
122
+ # # => 'You should see <a href="http://example.com">example.com</a>.'
123
+ #
124
+ # With custom formatting:
125
+ # "You should see example.com.".recognize_links! do |normalized, raw|
126
+ # "#{raw} (#normalized)"
127
+ # end
128
+ # # => 'You should see example.com (http://example.com)'
129
+ def recognize_links!(&format)
130
+ gsub!(Linkize::ADDRESS_REGEXP) do |raw_uri|
131
+ # Try to turn this URI into a compliant, normalized, escaped URI
132
+ normalized_uri = URI.escape(Linkize.uri_from_string(raw_uri).to_s)
133
+
134
+ if normalized_uri
135
+ if block_given?
136
+ # Yield to the format block
137
+ link = yield normalized_uri, raw_uri
138
+ else
139
+ # Use the default HTML formatter
140
+ link = html_link_format normalized_uri, raw_uri
141
+ end
142
+ else
143
+ # Leave the text unchanged
144
+ link = raw_uri
145
+ end
146
+
147
+ # Return the link to substitute back in
148
+ link
149
+ end
150
+ end
151
+
152
+ alias linkize! recognize_links!
153
+
154
+ private
155
+
156
+ # A default hyperlink format for creating links.
157
+ def html_link_format(normalized_uri, raw_uri)
158
+ "<a href=\"%s\">%s</a>" % [normalized_uri, raw_uri]
159
+ end
160
+
161
+ # Locates URI candidates in the string using the URI regular expression.
162
+ # Returns an Array of Strings, or an empty Array if none are found.
163
+ def raw_uris
164
+ # Run the regexp against the contents of this string
165
+ matches = scan(Linkize::ADDRESS_REGEXP) || []
166
+
167
+ if matches.size > 0
168
+ # Consolidate matches based on the outer match group
169
+ uris = matches.collect do |match|
170
+ unless match.join().empty?
171
+ # Return the entire matched URI string
172
+ match[0]
173
+ end
174
+ end
175
+
176
+ # Cull matches that are blank
177
+ uris.compact!
178
+ end
179
+
180
+ # This could return nil for blank, but I'd rather keep it an array
181
+ return uris || []
182
+ end
183
+ end
@@ -0,0 +1,9 @@
1
+ module Linkize #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ TEST_PROTOCOLS = [
2
+ "", # None, degenerate HTTP
3
+ "http://",
4
+ "https://",
5
+ "ftp://"
6
+ ]
7
+
8
+ TEST_URIS = [
9
+ # Test various types of URIs
10
+ "example.com",
11
+ "example.com/with/path",
12
+ "www.example.com",
13
+ "www.example.com/with/path",
14
+ "purchase.example.com/?tokens=5233&XXFWEF=%20%20hello",
15
+ "user@www.example.com",
16
+ "user@www.example.com/with/path",
17
+ "user:password@www.example.com",
18
+ "user:password@www.example.com/with/path",
19
+ "example.com:334",
20
+ "example.com:334/with/path",
21
+ "user@example.com:334/with/path",
22
+ "user:password@example.com:334/with/path",
23
+ "user:password@example.com:334/with/path;parameter?argument=foo&other=bar",
24
+
25
+ # IPv4 addresses
26
+ "1.1.1.1",
27
+ "123.123.123.123",
28
+ "1.1.1.1:443",
29
+ "123.123.123.123:443/with/path",
30
+ "user@1.1.1.1:443",
31
+ "user:pass@123.123.123.123:443/with/path"
32
+ ]
@@ -0,0 +1,182 @@
1
+ require File.dirname(__FILE__) + '/../lib/linkize'
2
+ require File.dirname(__FILE__) + '/helpers/uris'
3
+ require 'uri'
4
+
5
+ #== Testing Strategy
6
+ # Each test iterates over combinations of protocols and example URIs.
7
+ # TODO: test mailto this way
8
+ # TODO: test IPv6 once supported
9
+
10
+ class Array
11
+ def each_with_uri
12
+ # Take each interpolation string
13
+ each do |text|
14
+ # Test it with each protocol
15
+ TEST_PROTOCOLS.each do |protocol|
16
+ # Test it with each test URI
17
+ TEST_URIS.each do |uri|
18
+ # Determine the actual or implied protocol (if degenerate, imply http://)
19
+ implied_protocol = protocol.empty? ? "http://" : protocol
20
+ yield text % "#{protocol}#{uri}", "#{implied_protocol}#{uri}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ describe String do
28
+ it "should provide the links method" do
29
+ lambda{ "a string".links }.should_not raise_error(NoMethodError)
30
+ end
31
+
32
+ it "should give an empty Array for a blank string" do
33
+ "".links.should == []
34
+ end
35
+
36
+ it "should find no links in a string with no links" do
37
+ [
38
+ "hello, my name is bill",
39
+ "i look a littp:// like a url",
40
+ "i am enjoyable! i also live /over/here",
41
+ "http://",
42
+ "http://nothing",
43
+ "ilike you. i ran period.into sentence toomuch.bla",
44
+ "as.q",
45
+ "blah...com"
46
+ ].each do |str|
47
+ str.links.should == []
48
+ end
49
+ end
50
+
51
+ it "should find a link by itself" do
52
+ [
53
+ "%s",
54
+ " %s\t"
55
+ ].each_with_uri do |text, expect|
56
+ text.links.should_not be_nil
57
+ begin
58
+ text.links.should == [URI.parse(expect).normalize]
59
+ rescue
60
+ puts "Test URI: #{text}"
61
+ raise
62
+ end
63
+ end
64
+ end
65
+
66
+ it "should find a link within text" do
67
+ [
68
+ "You should check out my site: %s",
69
+ "Wrapped on %s both sides",
70
+ "Evenrun %s </a>!but ends",
71
+ "Also with %s stuff...com"
72
+ ].each_with_uri do |text, expect|
73
+ text.links.should_not be_nil
74
+ begin
75
+ text.links.should == [URI.parse(expect)]
76
+ rescue
77
+ puts "Test URI: #{text}"
78
+ raise
79
+ end
80
+ end
81
+ end
82
+
83
+ it "should find a link in a multi-lined string" do
84
+ [
85
+ "It's good to be the king, \n even if your website is %s \n and nothing else.",
86
+ "%s\n both sides",
87
+ "Evenrun \n%s\n right in the middle.",
88
+ "Couple of lines \n \n%s"
89
+ ].each_with_uri do |text, expect|
90
+ text.links.should_not be_nil
91
+ begin
92
+ text.links.should == [URI.parse(expect)]
93
+ rescue
94
+ puts "Test URI: #{text}"
95
+ raise
96
+ end
97
+ end
98
+ end
99
+
100
+ it "should find a link within matched brackets" do
101
+ [
102
+ "(%s)",
103
+ "{%s}",
104
+ "\"%s\"",
105
+ "'%s'",
106
+ "[%s]"
107
+ ].each_with_uri do |text, expect|
108
+ text.links.should_not be_nil
109
+ begin
110
+ text.links.should == [URI.parse(expect)]
111
+ rescue
112
+ puts "Test URI: #{text}"
113
+ raise
114
+ end
115
+ end
116
+ end
117
+
118
+ it "has a recognize_links method" do
119
+ lambda{ "a string".recognize_links }.should_not raise_error(NoMethodError)
120
+ end
121
+
122
+ it "should be non-destructive" do
123
+ text = "example.com"
124
+ text.recognize_links
125
+ text.should == "example.com"
126
+ end
127
+
128
+ it "should be the same text if no links" do
129
+ text = "No links here."
130
+ text.recognize_links
131
+ text.should == "No links here."
132
+ end
133
+
134
+ it "should return a hyperlinked string if provided link" do
135
+ text = "Go visit example.com."
136
+ text.recognize_links.should == "Go visit <a href=\"http://example.com/\">example.com</a>."
137
+ end
138
+
139
+ it "should not clobber previous links when replacing" do
140
+ text = "Go visit http://example.com and then example.com."
141
+ text.recognize_links.should == "Go visit <a href=\"http://example.com/\">http://example.com</a> and then <a href=\"http://example.com/\">example.com</a>."
142
+ end
143
+
144
+ it "should replace over multiple lines" do
145
+ text = "Go visit\na new site called example.com\nover there."
146
+ text.recognize_links.should == "Go visit\na new site called <a href=\"http://example.com/\">example.com</a>\nover there."
147
+ end
148
+
149
+ it "has a recognize_links! method" do
150
+ lambda{ "a string".recognize_links! }.should_not raise_error(NoMethodError)
151
+ end
152
+
153
+ it "has an alias method called linkize" do
154
+ lambda{ "a string".linkize }.should_not raise_error(NoMethodError)
155
+ end
156
+
157
+ it "has an alias method called linkize!" do
158
+ lambda{ "a string".linkize! }.should_not raise_error(NoMethodError)
159
+ end
160
+
161
+ it "should replace links in-place with recognize_links!" do
162
+ text = "Go visit example.com."
163
+ text.recognize_links!
164
+ text.should == "Go visit <a href=\"http://example.com/\">example.com</a>."
165
+ end
166
+
167
+ it "should use the provided fromat block non-destructively" do
168
+ text = "Go visit example.com."
169
+ changed = text.recognize_links do |normalized, raw|
170
+ "<%s>" % normalized
171
+ end
172
+ changed.should == "Go visit <http://example.com/>."
173
+ end
174
+
175
+ it "should use a provided format block" do
176
+ text = "Go visit example.com."
177
+ text.recognize_links! do |normalized, raw|
178
+ "<%s>" % normalized
179
+ end
180
+ text.should == "Go visit <http://example.com/>."
181
+ end
182
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Phillip Calvin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-27 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: URI link parsing and recognition library
17
+ email: pnc@toasterlogic.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - History.txt
24
+ - Manifest.txt
25
+ - README.txt
26
+ files:
27
+ - COPYING
28
+ - History.txt
29
+ - LICENSE
30
+ - Manifest.txt
31
+ - README.txt
32
+ - Rakefile
33
+ - lib/linkize.rb
34
+ - lib/linkize/version.rb
35
+ - spec/helpers/uris.rb
36
+ - spec/linkize_spec.rb
37
+ - spec/spec.opts
38
+ has_rdoc: true
39
+ homepage: http://linkize.rubyforge.org
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --main
43
+ - README.txt
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project: linkize
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: URI link parsing and recognition library
65
+ test_files: []
66
+