quesadilla 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.markdown +23 -1
- data/lib/quesadilla/extractor/html.rb +4 -2
- data/lib/quesadilla/extractor/markdown.rb +1 -1
- data/lib/quesadilla/extractor/users.rb +37 -0
- data/lib/quesadilla/extractor.rb +6 -0
- data/lib/quesadilla/html_renderer.rb +19 -10
- data/lib/quesadilla/version.rb +1 -1
- data/lib/quesadilla.rb +6 -1
- data/test/quesadilla/emoji_test.rb +13 -6
- data/test/quesadilla/hashtags_test.rb +4 -2
- data/test/quesadilla/markdown_test.rb +26 -13
- data/test/quesadilla/multi_test.rb +6 -3
- data/test/quesadilla/users_test.rb +50 -0
- data/test/quesadilla_test.rb +4 -0
- data/test/test_helper.rb +1 -1
- metadata +4 -3
- data/test/support/extractor_macros.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46c4870ebc2332f165ba715f3765a9407f22f247
|
4
|
+
data.tar.gz: fc7da4a6eba9dc99ac253abaefe6434f955ec6dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dfada0f0d96994a6228a6e416ecb588d77075ea9cf2b5d098d065f130abc390968de9c9e62e7cad94c0f79b809243f54a0a24de2082568461cfc621bbfdd767
|
7
|
+
data.tar.gz: 4609a722036391bec45d1d24590c76a47bd72dc8dbaa76ae788b98dbc888e34ef2b18f30a6fe277e342f90cb5565bed83c550e98fc11aa1e561026842775f875
|
data/Readme.markdown
CHANGED
@@ -4,6 +4,9 @@ Entity-style text parsing. Quesadilla was extracted from [Cheddar](https://chedd
|
|
4
4
|
|
5
5
|
See the [Cheddar text guide](https://cheddarapp.com/text) for more information about how to type entities.
|
6
6
|
|
7
|
+
Quesadilla's API is fully documented. Read the [online documentation](http://rubydoc.info/github/soffes/quesadilla/master/frames).
|
8
|
+
|
9
|
+
|
7
10
|
## Installation
|
8
11
|
|
9
12
|
Add this line to your application's Gemfile:
|
@@ -20,6 +23,7 @@ Or install it yourself as:
|
|
20
23
|
|
21
24
|
$ gem install quesadilla
|
22
25
|
|
26
|
+
|
23
27
|
## Usage
|
24
28
|
|
25
29
|
To extract entites from text, simply call extract:
|
@@ -58,9 +62,11 @@ Option | Description
|
|
58
62
|
`:hashtags` | Hashtags
|
59
63
|
`:autolinks` | Automatically detect links
|
60
64
|
`:emoji` | GitHub-style named emoji
|
65
|
+
`:users` | User mentions
|
66
|
+
`:user_validator` | Callable object to validate usernames
|
61
67
|
`:html` | Generate HTML representations for entities and the entire string
|
62
68
|
|
63
|
-
Everything is enabled by deafult. If you don't want to extract Markdown, you should call the extractor this like:
|
69
|
+
Everything is enabled by deafult except user mentions. If you don't want to extract Markdown, you should call the extractor this like:
|
64
70
|
|
65
71
|
``` ruby
|
66
72
|
Quesadilla.extract('Some text', markdown: false)
|
@@ -89,12 +95,28 @@ extraction[:display_html] #=> 'Some <a href="http://example.com/tags/awesome" cl
|
|
89
95
|
|
90
96
|
Take a look at [Quesadilla::HTMLRenderer](lib/quesadilla/html_renderer.html) for more details on creating a custom renderer.
|
91
97
|
|
98
|
+
### Users
|
99
|
+
|
100
|
+
To enable user mention extraction, pass `users: true` as an option. You can optionally pass a callable object to validate a username. Here's a quick example:
|
101
|
+
|
102
|
+
``` ruby
|
103
|
+
validator = lambda do |username|
|
104
|
+
User.where('LOWER(username) = ?', username.downcase).first.try(:id)
|
105
|
+
end
|
106
|
+
|
107
|
+
extraction = extract('Real @soffes and fake @nobody', users: true, user_validator: validator)
|
108
|
+
```
|
109
|
+
|
110
|
+
Assuming there is a user named `soffes` in your database, it would extract `@soffes`. Assuming there isn't a user named `nobody`, that would remain plain text. Obviously feel free to do whatever you want here. Quesadilla makes no assumptions about your user system.
|
111
|
+
|
112
|
+
|
92
113
|
## Supported Ruby Versions
|
93
114
|
|
94
115
|
Quesadilla is tested under 1.9.3, 2.0.0, and JRuby (1.9 mode).
|
95
116
|
|
96
117
|
[![Build Status](https://travis-ci.org/soffes/quesadilla.png?branch=master)](https://travis-ci.org/soffes/quesadilla)
|
97
118
|
|
119
|
+
|
98
120
|
## Contributing
|
99
121
|
|
100
122
|
See the [contributing guide](Contributing.markdown).
|
@@ -68,6 +68,8 @@ module Quesadilla
|
|
68
68
|
@renderer.code(display_text)
|
69
69
|
when ENTITY_TYPE_HASHTAG
|
70
70
|
@renderer.hashtag(display_text, html_pre_escape(entity[:hashtag]))
|
71
|
+
when ENTITY_TYPE_USER
|
72
|
+
@renderer.user(display_text, html_pre_escape(entity[:username]), html_pre_escape(entity[:user_id]))
|
71
73
|
when ENTITY_TYPE_LINK
|
72
74
|
@renderer.link(display_text, entity[:url], html_pre_escape(entity[:title]))
|
73
75
|
else
|
@@ -81,7 +83,7 @@ module Quesadilla
|
|
81
83
|
def html_pre_escape(string)
|
82
84
|
return '' unless string
|
83
85
|
HTML_ESCAPE_MAP.each do |escape|
|
84
|
-
string = string.gsub(escape[:pattern], escape[:placeholder])
|
86
|
+
string = string.to_s.gsub(escape[:pattern], escape[:placeholder])
|
85
87
|
end
|
86
88
|
string
|
87
89
|
end
|
@@ -96,7 +98,7 @@ module Quesadilla
|
|
96
98
|
|
97
99
|
def html_escape(string)
|
98
100
|
return '' unless string
|
99
|
-
string.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>').gsub(/"/, '"').gsub(/'/, ''').gsub(/\//, '/')
|
101
|
+
string.to_s.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>').gsub(/"/, '"').gsub(/'/, ''').gsub(/\//, '/')
|
100
102
|
end
|
101
103
|
end
|
102
104
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Quesadilla
|
2
|
+
class Extractor
|
3
|
+
# Extract user mentions.
|
4
|
+
#
|
5
|
+
# This module has no public methods.
|
6
|
+
module Users
|
7
|
+
private
|
8
|
+
|
9
|
+
require 'twitter-text'
|
10
|
+
|
11
|
+
def extract_users
|
12
|
+
Twitter::Extractor::extract_mentioned_screen_names_with_indices(@working_text).each do |entity|
|
13
|
+
display_text = '@' + entity[:screen_name]
|
14
|
+
username = entity[:screen_name]
|
15
|
+
|
16
|
+
user_id = nil
|
17
|
+
if validator = @options[:user_validator]
|
18
|
+
user_id = validator.call(username)
|
19
|
+
next unless user_id
|
20
|
+
end
|
21
|
+
|
22
|
+
hash = {
|
23
|
+
type: ENTITY_TYPE_USER,
|
24
|
+
text: display_text,
|
25
|
+
display_text: display_text,
|
26
|
+
indices: entity[:indices],
|
27
|
+
username: entity[:screen_name],
|
28
|
+
}
|
29
|
+
hash[:user_id] = user_id if user_id
|
30
|
+
|
31
|
+
@entities << hash
|
32
|
+
@working_text.sub!(display_text, REPLACE_TOKEN * display_text.length)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/quesadilla/extractor.rb
CHANGED
@@ -11,6 +11,7 @@ module Quesadilla
|
|
11
11
|
include Hashtags
|
12
12
|
include HTML
|
13
13
|
include Markdown
|
14
|
+
include Users
|
14
15
|
|
15
16
|
# @return [Hash] default extractor options
|
16
17
|
def self.default_options
|
@@ -25,6 +26,8 @@ module Quesadilla
|
|
25
26
|
hashtags: true,
|
26
27
|
autolinks: true,
|
27
28
|
emoji: true,
|
29
|
+
users: false,
|
30
|
+
user_validator: nil,
|
28
31
|
html: true,
|
29
32
|
html_renderer: Quesadilla::HTMLRenderer
|
30
33
|
}
|
@@ -41,6 +44,8 @@ module Quesadilla
|
|
41
44
|
# @option options hashtags [Boolean] Should extract hashtags. Defaults to `true`.
|
42
45
|
# @option options autolinks [Boolean] Should automatically detect links. Defaults to `true`.
|
43
46
|
# @option options emoji [Boolean] Should extract named emoji. Defaults to `true`.
|
47
|
+
# @option options users [Boolean] Should extract user mentions. Defaults to `false`.
|
48
|
+
# @option options user_validator A callable object to validate a username. This should return the user ID of the user or nil if it is invalid. Invalid users will be left as plain text. If the validator is nil, all usernames will be extracted. Defaults to `nil`.
|
44
49
|
# @option options html [Boolean] Should generate HTML. Defaults to `true`.
|
45
50
|
# @option options html_renderer [Class] class to use as HTML renderer. Defaults to `Quesadilla::HTMLRenderer`.
|
46
51
|
def initialize(options = {})
|
@@ -64,6 +69,7 @@ module Quesadilla
|
|
64
69
|
extract_markdown if @options[:markdown]
|
65
70
|
extract_hashtags if @options[:hashtags]
|
66
71
|
extract_autolinks if @options[:autolinks]
|
72
|
+
extract_users if @options[:users]
|
67
73
|
|
68
74
|
# Sort entities
|
69
75
|
@entities.sort! do |a, b|
|
@@ -2,52 +2,61 @@ module Quesadilla
|
|
2
2
|
# Default HTML renderer for generating HTML
|
3
3
|
class HTMLRenderer
|
4
4
|
# HTML representation of italic text
|
5
|
-
# @param display_text the italic text
|
5
|
+
# @param display_text [String] the italic text
|
6
6
|
# @return [String] HTML representation of the italic text
|
7
7
|
def emphasis(display_text)
|
8
8
|
%Q{<em>#{display_text}</em>}
|
9
9
|
end
|
10
10
|
|
11
11
|
# HTML representation of bold text
|
12
|
-
# @param display_text the bold text
|
12
|
+
# @param display_text [String] the bold text
|
13
13
|
# @return [String] HTML representation of the bold text
|
14
14
|
def double_emphasis(display_text)
|
15
15
|
%Q{<strong>#{display_text}</strong>}
|
16
16
|
end
|
17
17
|
|
18
18
|
# HTML representation of bold italic text
|
19
|
-
# @param display_text the bold italic text
|
19
|
+
# @param display_text [String] the bold italic text
|
20
20
|
# @return [String] HTML representation of the bold italic text
|
21
21
|
def triple_emphasis(display_text)
|
22
22
|
%Q{<strong><em>#{display_text}</em></strong>}
|
23
23
|
end
|
24
24
|
|
25
25
|
# HTML representation of strikethrough text
|
26
|
-
# @param display_text the strikethrough text
|
26
|
+
# @param display_text [String] the strikethrough text
|
27
27
|
# @return [String] HTML representation of the strikethrough text
|
28
28
|
def strikethrough(display_text)
|
29
29
|
%Q{<del>#{display_text}</del>}
|
30
30
|
end
|
31
31
|
|
32
32
|
# HTML representation of code
|
33
|
-
# @param display_text the text of the code
|
33
|
+
# @param display_text [String] the text of the code
|
34
34
|
# @return [String] HTML representation of the code
|
35
35
|
def code(display_text)
|
36
36
|
%Q{<code>#{display_text}</code>}
|
37
37
|
end
|
38
38
|
|
39
39
|
# HTML representation of a hashtag
|
40
|
-
# @param display_text the hashtag text (`#awesome`)
|
41
|
-
# @param hashtag the hashtag (just `awesome`)
|
40
|
+
# @param display_text [String] the hashtag text (`#awesome`)
|
41
|
+
# @param hashtag [String] the hashtag (just `awesome`)
|
42
42
|
# @return [String] HTML representation of the hashtag
|
43
43
|
def hashtag(display_text, hashtag)
|
44
44
|
%Q{<a href="#hashtag-#{hashtag}" class="hashtag">#{display_text}</a>}
|
45
45
|
end
|
46
46
|
|
47
|
+
# HTML representation of a user mention
|
48
|
+
# @param display_text [String] the user mention text (`@soffes`)
|
49
|
+
# @param username [String] the username (just `soffes`)
|
50
|
+
# @param user_id [String] optional user_id
|
51
|
+
# @return [String] HTML representation of the user mention
|
52
|
+
def user(display_text, username, user_id = nil)
|
53
|
+
%Q{<a href="/#{username}" class="user">#{display_text}</a>}
|
54
|
+
end
|
55
|
+
|
47
56
|
# HTML representation of a link
|
48
|
-
# @param display_text the text of the link
|
49
|
-
# @param url the url of the link
|
50
|
-
# @param title the title of the link
|
57
|
+
# @param display_text [String] the text of the link
|
58
|
+
# @param url [String] the url of the link
|
59
|
+
# @param title [String] the title of the link
|
51
60
|
# @return [String] HTML representation of the link
|
52
61
|
def link(display_text, url, title = nil)
|
53
62
|
title_attr = (title && title.length > 0) ? %Q{ title="#{title}"} : ''
|
data/lib/quesadilla/version.rb
CHANGED
data/lib/quesadilla.rb
CHANGED
@@ -22,9 +22,14 @@ module Quesadilla
|
|
22
22
|
# Hashtag entity type
|
23
23
|
ENTITY_TYPE_HASHTAG = 'hashtag'.freeze
|
24
24
|
|
25
|
+
# User entity type
|
26
|
+
ENTITY_TYPE_USER = 'user'.freeze
|
27
|
+
|
25
28
|
# Link entity type
|
26
29
|
ENTITY_TYPE_LINK = 'link'.freeze
|
27
30
|
|
31
|
+
module_function
|
32
|
+
|
28
33
|
# Extract entities from text
|
29
34
|
# @param text the text to extract
|
30
35
|
# @option options markdown_code [Boolean] Should extract Markdown code. Defaults to `true`.
|
@@ -39,7 +44,7 @@ module Quesadilla
|
|
39
44
|
# @option options html [Boolean] Should generate HTML. Defaults to `true`.
|
40
45
|
# @option options html_renderer [Class] class to use as HTML renderer. Defaults to `Quesadilla::HTMLRenderer`.
|
41
46
|
# @return [Hash] hash containing the display text, html text, and entities
|
42
|
-
def
|
47
|
+
def extract(text, options = {})
|
43
48
|
Extractor.new(options).extract(text)
|
44
49
|
end
|
45
50
|
end
|
@@ -5,16 +5,17 @@ module Quesadilla
|
|
5
5
|
class EmojiTest < TestCase
|
6
6
|
def test_that_it_supports_emoji
|
7
7
|
extraction = extract('Something with 👨 beardface')
|
8
|
-
|
8
|
+
expected = {
|
9
9
|
display_text: 'Something with 👨 beardface',
|
10
10
|
display_html: 'Something with 👨 beardface',
|
11
11
|
entities: []
|
12
12
|
}
|
13
|
+
assert_equal expected, extraction
|
13
14
|
end
|
14
15
|
|
15
16
|
def test_that_it_supports_emoji_with_other_entities
|
16
17
|
extraction = extract('Something #tagged with 👨 beardface')
|
17
|
-
|
18
|
+
expected = {
|
18
19
|
display_text: 'Something #tagged with 👨 beardface',
|
19
20
|
display_html: 'Something <a href="#hashtag-tagged" class="hashtag">#tagged</a> with 👨 beardface',
|
20
21
|
entities: [
|
@@ -28,9 +29,10 @@ module Quesadilla
|
|
28
29
|
}
|
29
30
|
]
|
30
31
|
}
|
32
|
+
assert_equal expected, extraction
|
31
33
|
|
32
34
|
extraction = extract('After 💇 #foo 👮 **Yep**')
|
33
|
-
|
35
|
+
expected = {
|
34
36
|
display_text: 'After 💇 #foo 👮 Yep',
|
35
37
|
display_html: 'After 💇 <a href="#hashtag-foo" class="hashtag">#foo</a> 👮 <strong>Yep</strong>',
|
36
38
|
entities: [
|
@@ -51,18 +53,20 @@ module Quesadilla
|
|
51
53
|
}
|
52
54
|
]
|
53
55
|
}
|
56
|
+
assert_equal expected, extraction
|
54
57
|
end
|
55
58
|
|
56
59
|
def test_that_it_support_the_colon_syntax
|
57
60
|
extraction = extract('Beardface is :man:')
|
58
|
-
|
61
|
+
expected = {
|
59
62
|
display_text: 'Beardface is 👨',
|
60
63
|
display_html: 'Beardface is 👨',
|
61
64
|
entities: []
|
62
65
|
}
|
66
|
+
assert_equal expected, extraction
|
63
67
|
|
64
68
|
extraction = extract('Beardface is `not here :man:` :man:')
|
65
|
-
|
69
|
+
expected = {
|
66
70
|
display_text: 'Beardface is not here :man: 👨',
|
67
71
|
display_html: 'Beardface is <code>not here :man:</code> 👨',
|
68
72
|
entities: [
|
@@ -75,9 +79,11 @@ module Quesadilla
|
|
75
79
|
}
|
76
80
|
]
|
77
81
|
}
|
82
|
+
assert_equal expected, extraction
|
78
83
|
|
84
|
+
# TODO: This is broken. Original indicies are messed up if a named emoji is before another entity
|
79
85
|
# extraction = extract('Something #tagged with :man: **beardface**')
|
80
|
-
#
|
86
|
+
# expected = {
|
81
87
|
# display_text: 'Something #tagged with 👨 beardface',
|
82
88
|
# display_html: 'Something <a href="#hashtag-tagged" class="hashtag">#tagged</a> with 👨 <strong>beardface</strong>',
|
83
89
|
# entities: [
|
@@ -98,6 +104,7 @@ module Quesadilla
|
|
98
104
|
# }
|
99
105
|
# ]
|
100
106
|
# }
|
107
|
+
# assert_equal expected, extraction
|
101
108
|
end
|
102
109
|
end
|
103
110
|
end
|
@@ -5,7 +5,7 @@ module Quesadilla
|
|
5
5
|
class HashtagsTest < TestCase
|
6
6
|
def test_that_it_extracts_tags
|
7
7
|
extraction = extract('Something #tagged')
|
8
|
-
|
8
|
+
expected = {
|
9
9
|
display_text: 'Something #tagged',
|
10
10
|
display_html: 'Something <a href="#hashtag-tagged" class="hashtag">#tagged</a>',
|
11
11
|
entities: [
|
@@ -19,11 +19,12 @@ module Quesadilla
|
|
19
19
|
}
|
20
20
|
]
|
21
21
|
}
|
22
|
+
assert_equal expected, extraction
|
22
23
|
end
|
23
24
|
|
24
25
|
def test_that_it_extracts_multiple_tags
|
25
26
|
extraction = extract('A task with some #tags that are #awesome')
|
26
|
-
|
27
|
+
expected = {
|
27
28
|
display_text: 'A task with some #tags that are #awesome',
|
28
29
|
display_html: 'A task with some <a href="#hashtag-tags" class="hashtag">#tags</a> that are <a href="#hashtag-awesome" class="hashtag">#awesome</a>',
|
29
30
|
entities: [
|
@@ -45,6 +46,7 @@ module Quesadilla
|
|
45
46
|
}
|
46
47
|
]
|
47
48
|
}
|
49
|
+
assert_equal expected, extraction
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
@@ -5,7 +5,7 @@ module Quesadilla
|
|
5
5
|
class MarkdownTest < TestCase
|
6
6
|
def test_that_it_extracts_links
|
7
7
|
extraction = extract('Read [my resume](http://samsoff.es/resume) if you want')
|
8
|
-
|
8
|
+
expected = {
|
9
9
|
display_text: 'Read my resume if you want',
|
10
10
|
display_html: 'Read <a href="http://samsoff.es/resume" rel="external nofollow" class="link">my resume</a> if you want',
|
11
11
|
entities: [
|
@@ -19,11 +19,12 @@ module Quesadilla
|
|
19
19
|
}
|
20
20
|
]
|
21
21
|
}
|
22
|
+
assert_equal expected, extraction
|
22
23
|
end
|
23
24
|
|
24
25
|
def test_that_it_extracts_links_with_titles
|
25
26
|
extraction = extract('Read [my resume](http://samsoff.es/resume "Sam\'s Resume") if you want')
|
26
|
-
|
27
|
+
expected = {
|
27
28
|
display_text: 'Read my resume if you want',
|
28
29
|
display_html: 'Read <a href="http://samsoff.es/resume" rel="external nofollow" class="link" title="Sam's Resume">my resume</a> if you want',
|
29
30
|
entities: [
|
@@ -38,11 +39,12 @@ module Quesadilla
|
|
38
39
|
}
|
39
40
|
]
|
40
41
|
}
|
42
|
+
assert_equal expected, extraction
|
41
43
|
end
|
42
44
|
|
43
45
|
def test_that_it_extracts_links_with_brackets
|
44
46
|
extraction = extract('Something with a link: <http://samsoff.es/posts/hire-sam>')
|
45
|
-
|
47
|
+
expected = {
|
46
48
|
display_text: 'Something with a link: samsoff.es/posts/hire-sam',
|
47
49
|
display_html: 'Something with a link: <a href="http://samsoff.es/posts/hire-sam" rel="external nofollow" class="link">samsoff.es/posts/hire-sam</a>',
|
48
50
|
entities: [
|
@@ -56,11 +58,12 @@ module Quesadilla
|
|
56
58
|
}
|
57
59
|
]
|
58
60
|
}
|
61
|
+
assert_equal expected, extraction
|
59
62
|
end
|
60
63
|
|
61
64
|
def test_that_it_extracts_email_addresses_with_brackets
|
62
65
|
extraction = extract('Email <support@cheddarapp.com>')
|
63
|
-
|
66
|
+
expected = {
|
64
67
|
display_text: 'Email support@cheddarapp.com',
|
65
68
|
display_html: 'Email <a href="mailto:support@cheddarapp.com" rel="external nofollow" class="link">support@cheddarapp.com</a>',
|
66
69
|
entities: [
|
@@ -74,13 +77,14 @@ module Quesadilla
|
|
74
77
|
}
|
75
78
|
]
|
76
79
|
}
|
80
|
+
assert_equal expected, extraction
|
77
81
|
end
|
78
82
|
|
79
83
|
# it 'should extract plain email addresses'
|
80
84
|
|
81
85
|
def test_that_it_extracts_emphasis
|
82
86
|
extraction = extract('Something *cool* is awesome')
|
83
|
-
|
87
|
+
expected = {
|
84
88
|
display_text: 'Something cool is awesome',
|
85
89
|
display_html: 'Something <em>cool</em> is awesome',
|
86
90
|
entities: [
|
@@ -93,9 +97,10 @@ module Quesadilla
|
|
93
97
|
}
|
94
98
|
]
|
95
99
|
}
|
100
|
+
assert_equal expected, extraction
|
96
101
|
|
97
102
|
extraction = extract('Something _cool_ is awesome')
|
98
|
-
|
103
|
+
expected = {
|
99
104
|
display_text: 'Something cool is awesome',
|
100
105
|
display_html: 'Something <em>cool</em> is awesome',
|
101
106
|
entities: [
|
@@ -108,11 +113,12 @@ module Quesadilla
|
|
108
113
|
}
|
109
114
|
]
|
110
115
|
}
|
116
|
+
assert_equal expected, extraction
|
111
117
|
end
|
112
118
|
|
113
119
|
def test_that_it_extracts_double_emphasis
|
114
120
|
extraction = extract('Something **cool** is awesome')
|
115
|
-
|
121
|
+
expected = {
|
116
122
|
display_text: 'Something cool is awesome',
|
117
123
|
display_html: 'Something <strong>cool</strong> is awesome',
|
118
124
|
entities: [
|
@@ -125,9 +131,10 @@ module Quesadilla
|
|
125
131
|
}
|
126
132
|
]
|
127
133
|
}
|
134
|
+
assert_equal expected, extraction
|
128
135
|
|
129
136
|
extraction = extract('Something __cool__ is awesome')
|
130
|
-
|
137
|
+
expected = {
|
131
138
|
display_text: 'Something cool is awesome',
|
132
139
|
display_html: 'Something <strong>cool</strong> is awesome',
|
133
140
|
entities: [
|
@@ -140,11 +147,12 @@ module Quesadilla
|
|
140
147
|
}
|
141
148
|
]
|
142
149
|
}
|
150
|
+
assert_equal expected, extraction
|
143
151
|
end
|
144
152
|
|
145
153
|
def test_that_it_extracts_triple_emphasis
|
146
154
|
extraction = extract('Something ***cool*** is awesome')
|
147
|
-
|
155
|
+
expected = {
|
148
156
|
display_text: 'Something cool is awesome',
|
149
157
|
display_html: 'Something <strong><em>cool</em></strong> is awesome',
|
150
158
|
entities: [
|
@@ -157,9 +165,10 @@ module Quesadilla
|
|
157
165
|
}
|
158
166
|
]
|
159
167
|
}
|
168
|
+
assert_equal expected, extraction
|
160
169
|
|
161
170
|
extraction = extract('Something ___cool___ is awesome')
|
162
|
-
|
171
|
+
expected = {
|
163
172
|
display_text: 'Something cool is awesome',
|
164
173
|
display_html: 'Something <strong><em>cool</em></strong> is awesome',
|
165
174
|
entities: [
|
@@ -172,11 +181,12 @@ module Quesadilla
|
|
172
181
|
}
|
173
182
|
]
|
174
183
|
}
|
184
|
+
assert_equal expected, extraction
|
175
185
|
end
|
176
186
|
|
177
187
|
def test_that_it_extracts_strikethrough
|
178
188
|
extraction = extract('Something ~~cool~~ awesome')
|
179
|
-
|
189
|
+
expected = {
|
180
190
|
display_text: 'Something cool awesome',
|
181
191
|
display_html: 'Something <del>cool</del> awesome',
|
182
192
|
entities: [
|
@@ -189,11 +199,12 @@ module Quesadilla
|
|
189
199
|
}
|
190
200
|
]
|
191
201
|
}
|
202
|
+
assert_equal expected, extraction
|
192
203
|
end
|
193
204
|
|
194
205
|
def test_that_it_extracts_multiple_strikethroughs
|
195
206
|
extraction = extract('Something ~~cool~~ awesome ~~foo~~')
|
196
|
-
|
207
|
+
expected = {
|
197
208
|
display_text: 'Something cool awesome foo',
|
198
209
|
display_html: 'Something <del>cool</del> awesome <del>foo</del>',
|
199
210
|
entities: [
|
@@ -213,11 +224,12 @@ module Quesadilla
|
|
213
224
|
}
|
214
225
|
]
|
215
226
|
}
|
227
|
+
assert_equal expected, extraction
|
216
228
|
end
|
217
229
|
|
218
230
|
def test_that_it_extracts_code
|
219
231
|
extraction = extract('Something with `code` is awesome')
|
220
|
-
|
232
|
+
expected = {
|
221
233
|
display_text: 'Something with code is awesome',
|
222
234
|
display_html: 'Something with <code>code</code> is awesome',
|
223
235
|
entities: [
|
@@ -230,6 +242,7 @@ module Quesadilla
|
|
230
242
|
}
|
231
243
|
]
|
232
244
|
}
|
245
|
+
assert_equal expected, extraction
|
233
246
|
end
|
234
247
|
end
|
235
248
|
end
|
@@ -5,7 +5,7 @@ module Quesadilla
|
|
5
5
|
class MultiTest < TestCase
|
6
6
|
def test_that_it_extracts_links_and_tags
|
7
7
|
extraction = extract('Something #tagged with a link http://samsoff.es/posts/hire-sam')
|
8
|
-
|
8
|
+
expected = {
|
9
9
|
display_text: 'Something #tagged with a link samsoff.es/posts/hire-sam',
|
10
10
|
display_html: 'Something <a href="#hashtag-tagged" class="hashtag">#tagged</a> with a link <a href="http://samsoff.es/posts/hire-sam" rel="external nofollow" class="link">samsoff.es/posts/hire-sam</a>',
|
11
11
|
entities: [
|
@@ -27,11 +27,12 @@ module Quesadilla
|
|
27
27
|
}
|
28
28
|
]
|
29
29
|
}
|
30
|
+
assert_equal expected, extraction
|
30
31
|
end
|
31
32
|
|
32
33
|
def test_that_it_doesnt_lose_the_last_character
|
33
34
|
extraction = extract('Something that is **bold**?')
|
34
|
-
|
35
|
+
expected = {
|
35
36
|
display_text: 'Something that is bold?',
|
36
37
|
display_html: 'Something that is <strong>bold</strong>?',
|
37
38
|
entities: [
|
@@ -44,9 +45,10 @@ module Quesadilla
|
|
44
45
|
}
|
45
46
|
]
|
46
47
|
}
|
48
|
+
assert_equal expected, extraction
|
47
49
|
|
48
50
|
extraction = extract('Something that is **bold**')
|
49
|
-
|
51
|
+
expected = {
|
50
52
|
display_text: 'Something that is bold',
|
51
53
|
display_html: 'Something that is <strong>bold</strong>',
|
52
54
|
entities: [
|
@@ -59,6 +61,7 @@ module Quesadilla
|
|
59
61
|
}
|
60
62
|
]
|
61
63
|
}
|
64
|
+
assert_equal expected, extraction
|
62
65
|
end
|
63
66
|
end
|
64
67
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
module Quesadilla
|
5
|
+
class AutolinkTest < TestCase
|
6
|
+
def test_extracting_users
|
7
|
+
extraction = extract('You should follow @soffes on Twitter', users: true)
|
8
|
+
expected = {
|
9
|
+
display_text: 'You should follow @soffes on Twitter',
|
10
|
+
display_html: 'You should follow <a href="/soffes" class="user">@soffes</a> on Twitter',
|
11
|
+
entities: [
|
12
|
+
{
|
13
|
+
type: 'user',
|
14
|
+
text: '@soffes',
|
15
|
+
display_text: '@soffes',
|
16
|
+
username: 'soffes',
|
17
|
+
indices: [18, 25],
|
18
|
+
display_indices: [18, 25]
|
19
|
+
}
|
20
|
+
]
|
21
|
+
}
|
22
|
+
assert_equal expected, extraction
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_user_validation
|
26
|
+
validator = lambda do |username|
|
27
|
+
username == 'soffes' ? 4 : nil
|
28
|
+
end
|
29
|
+
|
30
|
+
extraction = extract('You should follow @soffes on Twitter, but not @crap', users: true, user_validator: validator)
|
31
|
+
expected = {
|
32
|
+
display_text: 'You should follow @soffes on Twitter, but not @crap',
|
33
|
+
display_html: 'You should follow <a href="/soffes" class="user">@soffes</a> on Twitter, but not @crap',
|
34
|
+
entities: [
|
35
|
+
{
|
36
|
+
type: 'user',
|
37
|
+
text: '@soffes',
|
38
|
+
display_text: '@soffes',
|
39
|
+
username: 'soffes',
|
40
|
+
user_id: 4,
|
41
|
+
indices: [18, 25],
|
42
|
+
display_indices: [18, 25]
|
43
|
+
}
|
44
|
+
]
|
45
|
+
}
|
46
|
+
assert_equal expected, extraction
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/quesadilla_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quesadilla
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Soffes
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- lib/quesadilla/extractor/hashtags.rb
|
61
61
|
- lib/quesadilla/extractor/html.rb
|
62
62
|
- lib/quesadilla/extractor/markdown.rb
|
63
|
+
- lib/quesadilla/extractor/users.rb
|
63
64
|
- lib/quesadilla/html_renderer.rb
|
64
65
|
- lib/quesadilla/version.rb
|
65
66
|
- quesadilla.gemspec
|
@@ -69,8 +70,8 @@ files:
|
|
69
70
|
- test/quesadilla/html_test.rb
|
70
71
|
- test/quesadilla/markdown_test.rb
|
71
72
|
- test/quesadilla/multi_test.rb
|
73
|
+
- test/quesadilla/users_test.rb
|
72
74
|
- test/quesadilla_test.rb
|
73
|
-
- test/support/extractor_macros.rb
|
74
75
|
- test/test_helper.rb
|
75
76
|
homepage: https://github.com/soffes/quesadilla
|
76
77
|
licenses:
|
@@ -103,7 +104,7 @@ test_files:
|
|
103
104
|
- test/quesadilla/html_test.rb
|
104
105
|
- test/quesadilla/markdown_test.rb
|
105
106
|
- test/quesadilla/multi_test.rb
|
107
|
+
- test/quesadilla/users_test.rb
|
106
108
|
- test/quesadilla_test.rb
|
107
|
-
- test/support/extractor_macros.rb
|
108
109
|
- test/test_helper.rb
|
109
110
|
has_rdoc:
|