users-and-hashtags 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/lib/rack/users-and-hashtags.rb +35 -0
- data/lib/rack/users-and-hashtags/version.rb +5 -0
- data/lib/users-and-hashtags.rb +2 -0
- data/users-and-hashtags.gemspec +20 -0
- metadata +91 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Gert Goet, ThinkCreate
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
Users-and-Hashtags
|
2
|
+
========
|
3
|
+
|
4
|
+
I was playing around with rack a little bit and this is what I came up with.
|
5
|
+
|
6
|
+
You can use it to automatically replace @user and #tags in specified areas of your website.
|
7
|
+
|
8
|
+
Requires Nokogiri.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
I. Add it to your Gemfile
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'users-and-hashtags'
|
17
|
+
```
|
18
|
+
|
19
|
+
II. Add users-and-hashtags to your application (Rails 3.1 in this case)
|
20
|
+
|
21
|
+
(config/application.rb or config/environments/[development|production|test].rb)
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
config.middleware.use Rack::UsersAndHashtags, {
|
25
|
+
:user_url => ["http://0.0.0.0:3000/"],
|
26
|
+
:hashtag_url => ["http://0.0.0.0:3000/search?q=%23"],
|
27
|
+
:matcher => '.twitrepl'
|
28
|
+
}
|
29
|
+
```
|
30
|
+
|
31
|
+
In every element with the css class .twitrepl that matches the @user or #tag regex will be replaced with a link:
|
32
|
+
|
33
|
+
```pre
|
34
|
+
@user => <a href="http://0.0.0.0:3000/@user">@user</a>
|
35
|
+
#tag => <a href="http://0.0.0.0:3000/search?q=%23tag">#tag</a>
|
36
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class UsersAndHashtags
|
5
|
+
include Rack::Utils
|
6
|
+
|
7
|
+
def initialize(app, options)
|
8
|
+
@app = app
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
status, headers, body = @app.call(env)
|
14
|
+
|
15
|
+
if body.class == ActionDispatch::Response
|
16
|
+
headers = HeaderHash.new(headers)
|
17
|
+
|
18
|
+
nbody = ""
|
19
|
+
body.each do |b|
|
20
|
+
doc = Nokogiri::HTML(b.to_s)
|
21
|
+
doc.css(@options[:matcher]).each do |d|
|
22
|
+
d.inner_html = d.to_s.gsub!(/(\W)@([\w-]+)?/, '\1<a href="' + "#{@options[:user_url]}" + '\2">@\2</a>')
|
23
|
+
d.inner_html = d.to_s.gsub!(/(\W)#([\w-]+)?/, '\1<a href="' + "#{@options[:hashtag_url]}" + '\2">#\2</a>')
|
24
|
+
end
|
25
|
+
nbody = doc.to_s
|
26
|
+
end
|
27
|
+
body = [nbody]
|
28
|
+
|
29
|
+
headers['Content-Length'] &&= bytesize(nbody).to_s
|
30
|
+
end
|
31
|
+
[status, headers, body]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rack/users-and-hashtags/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "users-and-hashtags"
|
7
|
+
s.version = Rack::UsersAndHashtags::VERSION
|
8
|
+
s.authors = ["Oliver Lillig"]
|
9
|
+
s.email = ["ol@askedo.de"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Replace @user and #tags with corresponding links}
|
12
|
+
s.description = %q{Replace @user and #tags with corresponding links}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
#s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency('nokogiri', '>= 1.5.0')
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: users-and-hashtags
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Oliver Lillig
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-12-03 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: nokogiri
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 5
|
33
|
+
- 0
|
34
|
+
version: 1.5.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: "Replace @user and #tags with corresponding links"
|
38
|
+
email:
|
39
|
+
- ol@askedo.de
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/rack/users-and-hashtags.rb
|
53
|
+
- lib/rack/users-and-hashtags/version.rb
|
54
|
+
- lib/users-and-hashtags.rb
|
55
|
+
- users-and-hashtags.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: ""
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.5.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: "Replace @user and #tags with corresponding links"
|
90
|
+
test_files: []
|
91
|
+
|