smiley 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +5 -0
- data/lib/smiley.rb +37 -0
- data/spec/smiley_spec.rb +67 -0
- data/spec/spec_helper.rb +1 -0
- metadata +104 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
rake (0.9.2.2)
|
6
|
+
rspec (2.10.0)
|
7
|
+
rspec-core (~> 2.10.0)
|
8
|
+
rspec-expectations (~> 2.10.0)
|
9
|
+
rspec-mocks (~> 2.10.0)
|
10
|
+
rspec-core (2.10.1)
|
11
|
+
rspec-expectations (2.10.0)
|
12
|
+
diff-lcs (~> 1.1.3)
|
13
|
+
rspec-mocks (2.10.1)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
rake
|
20
|
+
rspec
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Johannes Barre
|
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/Rakefile
ADDED
data/lib/smiley.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
class Smiley
|
4
|
+
def self.smiley_file=(file)
|
5
|
+
@@smiley_file = file
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.smiley_file
|
9
|
+
return @@smiley_file if defined?(@@smiley_file)
|
10
|
+
return File.join(Rails.root, "config", "smileys.yml") if defined?(Rails) && Rails.respond_to?(:root)
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse(text)
|
15
|
+
load_smileys
|
16
|
+
|
17
|
+
text.gsub(@@regex) do
|
18
|
+
%(#{$1}<em class="smiley smiley-#{@@smileys[$2].downcase}"></em>#{$3})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def load_smileys
|
24
|
+
unless defined?(@@smileys) && defined?(@@regex)
|
25
|
+
@@smileys = {}
|
26
|
+
@@regex = []
|
27
|
+
YAML.load(File.read(Smiley.smiley_file)).each do |smiley, options|
|
28
|
+
options["tokens"].split(/\s+/).each do |token|
|
29
|
+
@@smileys[token] = smiley
|
30
|
+
@@regex << Regexp.escape(token)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
before_and_after = "[.,;:!\\?\\(\\[\\{\\)\\]\\}\\-]|\\s"
|
34
|
+
@@regex = Regexp.compile "(^|#{before_and_after})(" + @@regex.join("|") + ")(#{before_and_after}|$)"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/smiley_spec.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'smiley'
|
3
|
+
|
4
|
+
describe Smiley do
|
5
|
+
describe '.smiley_file' do
|
6
|
+
before do
|
7
|
+
Smiley.class_eval do
|
8
|
+
remove_class_variable :@@smiley_file if class_variable_defined?(:@@smiley_file)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
Object.send(:remove_const, :Rails) if defined?(Rails)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return the value configured" do
|
17
|
+
Smiley.smiley_file = "/etc/smileys.yml"
|
18
|
+
|
19
|
+
Smiley.smiley_file.should == "/etc/smileys.yml"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return nil, if not configured" do
|
23
|
+
Smiley.smiley_file.should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return \#{Rails.root}/config/smileys.yml, if Rails.root is defined" do
|
27
|
+
Rails = double("Rails")
|
28
|
+
Rails.stub(:root) { "/home/igel/dev/example.com/" }
|
29
|
+
|
30
|
+
Smiley.smiley_file.should == "/home/igel/dev/example.com/config/smileys.yml"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#parse' do
|
35
|
+
before do
|
36
|
+
Smiley.smiley_file = File.join(File.dirname(__FILE__), "smileys.yml")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should parse a smiley" do
|
40
|
+
Smiley.new.parse('That is so funny! :-) Will tell my grandma about that!').should == 'That is so funny! <em class="smiley smiley-smile"></em> Will tell my grandma about that!'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should work with alternative forms" do
|
44
|
+
Smiley.new.parse("That's so funny! :P Will tell my grandma about that :rolleyes: ").should == %(That's so funny! <em class="smiley smiley-razz"></em> Will tell my grandma about that <em class="smiley smiley-rolleyes"></em> )
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should parse smileys at the beginning and end of a string" do
|
48
|
+
Smiley.new.parse(":D So funny! ;-)").should == %(<em class="smiley smiley-grin"></em> So funny! <em class="smiley smiley-wink"></em>)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should parse smileys at the beginning and end of a line" do
|
52
|
+
Smiley.new.parse("\n:D So funny! ;-)\n").should == %(\n<em class="smiley smiley-grin"></em> So funny! <em class="smiley smiley-wink"></em>\n)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should parse smileys after and before a comma, a dot, a question or exclamation mark, a semicolon, a colon, or a dash" do
|
56
|
+
Smiley.new.parse("before: ,;-) .:-) ?:-) !;-) ;:D ::rolleyes: -:P\nafter: ;-), :-). :-)? ;-)! :D; :rolleyes:: :P-").should == %(before: ,<em class="smiley smiley-wink"></em> .<em class="smiley smiley-smile"></em> ?<em class="smiley smiley-smile"></em> !<em class="smiley smiley-wink"></em> ;<em class="smiley smiley-grin"></em> :<em class="smiley smiley-rolleyes"></em> -<em class="smiley smiley-razz"></em>\nafter: <em class="smiley smiley-wink"></em>, <em class="smiley smiley-smile"></em>. <em class="smiley smiley-smile"></em>? <em class="smiley smiley-wink"></em>! <em class="smiley smiley-grin"></em>; <em class="smiley smiley-rolleyes"></em>: <em class="smiley smiley-razz"></em>-)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should parse smileys after and before round, box or curly brackets" do
|
60
|
+
Smiley.new.parse(%! (:-)) [:-)] {:-)} !).should == %! (<em class="smiley smiley-smile"></em>) [<em class="smiley smiley-smile"></em>] {<em class="smiley smiley-smile"></em>} !
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should not parse smileys directly before or after a word" do
|
64
|
+
Smiley.new.parse(" :-)word:-) ").should == " :-)word:-) "
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smiley
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Johannes Barre
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-06-17 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 25
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 9
|
32
|
+
version: "0.9"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 39
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 10
|
47
|
+
- 0
|
48
|
+
version: 2.10.0
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description:
|
52
|
+
email: igel@igels.net
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- MIT-LICENSE
|
61
|
+
- Rakefile
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
64
|
+
- lib/smiley.rb
|
65
|
+
- spec/smiley_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
homepage: https://github.com/igel/smiley
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 23
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 3
|
93
|
+
- 6
|
94
|
+
version: 1.3.6
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.8.24
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: A small lib to parse smileys. Use CSS to display them!
|
102
|
+
test_files:
|
103
|
+
- spec/smiley_spec.rb
|
104
|
+
- spec/spec_helper.rb
|