tattoo 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 zbelzer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ ## Tattoo
2
+
3
+ A fun little gem for adding links to text based off of regular expressions and template URLs. It is important
4
+ to note that I know very little about Tattoos and their terminology. I thought it would be a fun theme since
5
+ you're simply adding some awesomness to regular old text.
6
+
7
+ ## Usage
8
+
9
+ It is important to note that the Gun works both ways, you can get at the markings in a piece of text as well
10
+ as produce a marked up version. This is why it's not a JQuery plugin.
11
+
12
+ Create some "Ink" by supplying a name and a regexp. If you want it to go somewhere, give it a `:host` and `:url` as well:
13
+ tags = Tattoo::Ink.new(:tags, /([a-z]+)/i, :host => "example.com", :url => "/tags/:id")
14
+
15
+ Create a gun and load up some Ink:
16
+ gun = Tattoo::Gun.new("Some #sweet tags", [tags])
17
+
18
+ The gun can either produce a 'tattoo' for you:
19
+ gun.tattoo => "Some <a href="http://example.com/tags/sweet">#sweet</a> tags"
20
+
21
+ Or you can look at what you would have written:
22
+ gun.look => {:tags => ["sweet"]}
23
+
24
+ To specify a different prefix or token, supply them to the Ink (defaults to '#' and 'id' respectively):
25
+ Tattoo::Ink.new(:users, /([a-z]+)/i, :host => "example.com", :url => "/users/:user", :token => "user", :prefix => "@")
26
+
27
+ The latter is useful in parsing things like tweets and saving the tags or whatnot to the database.
28
+
29
+ ## Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
37
+ * Send me a pull request. Bonus points for topic branches.
38
+
39
+ ## Copyright
40
+
41
+ Copyright (c) 2009 Zachary Belzer (zbelzer). See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "tattoo"
8
+ gem.summary = %Q{Tattoo: Ink some mad links on everyday text}
9
+ gem.description = %Q{Tattoo is a (very) simple tool to decorate text as links based off of regular expressions and url templates.}
10
+ gem.email = "zbelzer@gmail.com"
11
+ gem.homepage = "http://github.com/zbelzer/tattoo"
12
+ gem.authors = ["zbelzer"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "tattoo #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,6 @@
1
+ $:.unshift File.dirname(__FILE__) + "/lib"
2
+
3
+ require 'uri'
4
+
5
+ require 'tattoo/gun'
6
+ require 'tattoo/ink'
@@ -0,0 +1,27 @@
1
+ module Tattoo
2
+ class Gun
3
+ def initialize(text, ink=[])
4
+ @text = text
5
+ @ink = ink.inject({}) {|coll, i| coll[i.name] = i; coll}
6
+ end
7
+
8
+ def look
9
+ @ink.values.inject({}) do |markings, ink|
10
+ markings[ink.name] = ink.find_markings(@text)
11
+ markings
12
+ end
13
+ end
14
+
15
+ def tattoo
16
+ text = @text.dup
17
+
18
+ look.each do |name, markings|
19
+ markings.each do |marking|
20
+ text = @ink[name].mark(marking, text)
21
+ end
22
+ end
23
+
24
+ text
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ module Tattoo
2
+ DEFAULT_HOST = "www.example.com"
3
+ DEFAULT_MARK_IDENTIFIER = "#"
4
+ DEFAULT_TOKEN = "id"
5
+
6
+ class Ink
7
+ attr_reader :name
8
+
9
+ def initialize(name, regexp, opts={})
10
+ @name = name.to_sym
11
+ @regexp = regexp
12
+
13
+ options = opts.dup
14
+ @prefix = options.delete(:prefix) || DEFAULT_MARK_IDENTIFIER
15
+ @token = options.delete(:token) || DEFAULT_TOKEN
16
+ @host = options.delete(:host) || DEFAULT_HOST
17
+ @url = options.delete(:url).to_s
18
+ @attributes = options
19
+ end
20
+
21
+ def mark(marking, text)
22
+ text.gsub(/#{@prefix}#{marking}/, link_tag_for(marking))
23
+ end
24
+
25
+ def find_markings(text)
26
+ text.scan(/#{prefix_regexp}#{@regexp}/).flatten
27
+ end
28
+
29
+ private
30
+ def prefix_regexp
31
+ self.class.create_prefix_regexp(@prefix)
32
+ end
33
+
34
+ def link_tag_for(tag)
35
+ attributes = @attributes.map {|k, v| " #{k}=\"#{v}\""}
36
+ %{<a href="#{url_for(tag)}"#{attributes}>#{@prefix}#{tag}</a>}
37
+ end
38
+
39
+ def url_for(tag)
40
+ tag_url = @url.gsub(/:#{@token}/, tag)
41
+ URI.join("http://#{@host}", tag_url).to_s
42
+ end
43
+
44
+ def self.create_prefix_regexp(prefix)
45
+ /(?:\s?#{prefix})/
46
+ end
47
+ end
48
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'tattoo'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,118 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ TAG_REGEXP = /([a-z][a-z\-_0-9]+)/i
4
+ USER_REGEXP = /([a-z\-\._0-9]+)/i
5
+
6
+ module Tattoo
7
+ describe Gun do
8
+ describe "single marking" do
9
+ before do
10
+ @tags = [Ink.new(:tags, TAG_REGEXP)]
11
+ end
12
+
13
+ it "should find leading tag" do
14
+ text = "#foo"
15
+ gun = Gun.new(text, @tags)
16
+ gun.look[:tags].should == ["foo"]
17
+ end
18
+
19
+ it "should find tag between words" do
20
+ text = "lorem #foo ipsum"
21
+ gun = Gun.new(text, @tags)
22
+ gun.look[:tags].should == ["foo"]
23
+ end
24
+
25
+ it "should find tag with dashes" do
26
+ text = "lorem #foo-bar ipsum"
27
+ gun = Gun.new(text, @tags)
28
+ gun.look[:tags].should == ["foo-bar"]
29
+ end
30
+
31
+ it "should find tag with underbar words" do
32
+ text = "lorem #foo_bar ipsum"
33
+ gun = Gun.new(text, @tags)
34
+ gun.look[:tags].should == ["foo_bar"]
35
+ end
36
+
37
+ it "should find tag with numbers" do
38
+ text = "lorem #foo23 ipsum"
39
+ gun = Gun.new(text, @tags)
40
+ gun.look[:tags].should == ["foo23"]
41
+ end
42
+
43
+ it "should not find tag starting with number" do
44
+ text = "lorem #23foo ipsum"
45
+ gun = Gun.new(text, @tags)
46
+ gun.look[:tags].should be_empty
47
+ end
48
+
49
+ it "should find tags next to eachother" do
50
+ text = "#foo #bar"
51
+ gun = Gun.new(text, @tags)
52
+ gun.look[:tags].should == ["foo", "bar"]
53
+ end
54
+
55
+ it "should find multiple tags" do
56
+ text = "lorem #foo ipsum #bar"
57
+ gun = Gun.new(text, @tags)
58
+ gun.look[:tags].should == ["foo", "bar"]
59
+ end
60
+
61
+ it "should find tags next to non word characters" do
62
+ text = "[#bar]"
63
+ gun = Gun.new(text, @tags)
64
+ gun.look[:tags].should == ["bar"]
65
+ end
66
+ end
67
+
68
+ describe "prefixes" do
69
+ before do
70
+ @tags = [Ink.new(:tags, TAG_REGEXP), Ink.new(:users, USER_REGEXP, :prefix => "@")]
71
+ end
72
+
73
+ it "should find all markings" do
74
+ text = "#foo @bar"
75
+ gun = Gun.new(text, @tags)
76
+ gun.look[:tags].should == ["foo"]
77
+ gun.look[:users].should == ["bar"]
78
+ end
79
+ end
80
+
81
+ describe "tattoo" do
82
+ before do
83
+ @tags = Ink.new(:tags, TAG_REGEXP)
84
+ end
85
+
86
+ it "should use host" do
87
+ tags = Ink.new(:tags, TAG_REGEXP, :host => "example.com")
88
+
89
+ gun = Gun.new("#foo", [tags])
90
+ gun.tattoo.should == '<a href="http://example.com">#foo</a>'
91
+ end
92
+
93
+ it "should substitute :id with tag" do
94
+ tags = Ink.new(:tags, TAG_REGEXP, :host => "example.com", :url => "/tags/:id")
95
+ gun = Gun.new("#foo", [tags])
96
+ gun.tattoo.should == '<a href="http://example.com/tags/foo">#foo</a>'
97
+ end
98
+
99
+ it "should substitute :tag with tag" do
100
+ tags = Ink.new(:tags, TAG_REGEXP, :host => "example.com", :url => "/tags/:tag", :token => "tag")
101
+ gun = Gun.new("#foo", [tags])
102
+ gun.tattoo.should == '<a href="http://example.com/tags/foo">#foo</a>'
103
+ end
104
+
105
+ it "should decorate as substitution" do
106
+ tags = Ink.new(:tags, TAG_REGEXP, :host => "example.com", :url => "/tags/:id")
107
+ gun = Gun.new("lorem #foo ipsum", [tags])
108
+ gun.tattoo.should == 'lorem <a href="http://example.com/tags/foo">#foo</a> ipsum'
109
+ end
110
+
111
+ it "should consider other options html attributes" do
112
+ tags = Ink.new(:tags, TAG_REGEXP, :host => "example.com", :name => "some name")
113
+ gun = Gun.new("#foo", [tags])
114
+ gun.tattoo.should == '<a href="http://example.com" name="some name">#foo</a>'
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tattoo}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["zbelzer"]
12
+ s.date = %q{2009-12-29}
13
+ s.description = %q{Tattoo is a (very) simple tool to decorate text as links based off of regular expressions and url templates.}
14
+ s.email = %q{zbelzer@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/tattoo.rb",
27
+ "lib/tattoo/gun.rb",
28
+ "lib/tattoo/ink.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb",
31
+ "spec/tattoo_spec.rb",
32
+ "tattoo.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/zbelzer/tattoo}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Tattoo: Ink some mad links on everyday text}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/tattoo_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ end
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tattoo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - zbelzer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-29 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: Tattoo is a (very) simple tool to decorate text as links based off of regular expressions and url templates.
26
+ email: zbelzer@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.md
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/tattoo.rb
42
+ - lib/tattoo/gun.rb
43
+ - lib/tattoo/ink.rb
44
+ - spec/spec.opts
45
+ - spec/spec_helper.rb
46
+ - spec/tattoo_spec.rb
47
+ - tattoo.gemspec
48
+ has_rdoc: true
49
+ homepage: http://github.com/zbelzer/tattoo
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: "Tattoo: Ink some mad links on everyday text"
76
+ test_files:
77
+ - spec/spec_helper.rb
78
+ - spec/tattoo_spec.rb