quesadilla 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/Readme.markdown +3 -2
- data/lib/quesadilla.rb +4 -1
- data/lib/quesadilla/extractor.rb +6 -4
- data/lib/quesadilla/extractor/hashtags.rb +12 -5
- data/lib/quesadilla/version.rb +1 -1
- data/quesadilla.gemspec +2 -2
- data/test/quesadilla/hashtags_test.rb +22 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70dc2e9472fde307002d6826fc06e0d2d316e00b
|
4
|
+
data.tar.gz: f8ef3bceef62a578e65a846b03cdb4d3348d71ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a325502695825843f55530bc6b61e72b9428103b45a5d75a1a1b098e84b5bdf525a9d330ddc09db60baafa41dd11f912d85dc74f2001eafe01fe6378c8020640
|
7
|
+
data.tar.gz: e9fafa4a7fd9d679e192286beaa837dd9ec9d2c1de64beeebdcada523a3bcf6d5a20135c8f7d50fa1bb72d41eb02abc487d7bb2def1d9d9baeebebb733caa9ca
|
data/.travis.yml
CHANGED
data/Readme.markdown
CHANGED
@@ -60,6 +60,7 @@ Option | Description
|
|
60
60
|
`:markdown_emphasis` | Markdown italic
|
61
61
|
`:markdown_strikethrough` | Markdown Extra strikethrough
|
62
62
|
`:hashtags` | Hashtags
|
63
|
+
`:hashtags_validator` | Callable object to validate hashtags
|
63
64
|
`:autolinks` | Automatically detect links
|
64
65
|
`:emoji` | GitHub-style named emoji
|
65
66
|
`:users` | User mentions
|
@@ -93,7 +94,7 @@ extraction = Quesadilla.extract('Some #awesome text', html_renderer: CustomRende
|
|
93
94
|
extraction[:display_html] #=> 'Some <a href="http://example.com/tags/awesome" class="tag">#awesome</a> text'
|
94
95
|
```
|
95
96
|
|
96
|
-
Take a look at [Quesadilla::HTMLRenderer](lib/quesadilla/html_renderer.
|
97
|
+
Take a look at [Quesadilla::HTMLRenderer](lib/quesadilla/html_renderer.rb) for more details on creating a custom renderer.
|
97
98
|
|
98
99
|
### Users
|
99
100
|
|
@@ -112,7 +113,7 @@ Assuming there is a user named `soffes` in your database, it would extract `@sof
|
|
112
113
|
|
113
114
|
## Supported Ruby Versions
|
114
115
|
|
115
|
-
Quesadilla is tested under 1.9.3, 2.0.0, and
|
116
|
+
Quesadilla is tested under 1.9.2, 1.9.3, 2.0.0, JRuby 1.7.2 (1.9 mode), and Rubinius 2.0.0 (1.9 mode).
|
116
117
|
|
117
118
|
[![Build Status](https://travis-ci.org/soffes/quesadilla.png?branch=master)](https://travis-ci.org/soffes/quesadilla)
|
118
119
|
|
data/lib/quesadilla.rb
CHANGED
@@ -31,7 +31,7 @@ module Quesadilla
|
|
31
31
|
module_function
|
32
32
|
|
33
33
|
# Extract entities from text
|
34
|
-
# @param text the text to extract
|
34
|
+
# @param text [String] the text to extract
|
35
35
|
# @option options markdown_code [Boolean] Should extract Markdown code. Defaults to `true`.
|
36
36
|
# @option options markdown_links [Boolean] Should extract Markdown links. Defaults to `true`.
|
37
37
|
# @option options markdown_triple_emphasis [Boolean] Should extract Markdown triple emphasis (bold italic). Defaults to `true`.
|
@@ -39,8 +39,11 @@ module_function
|
|
39
39
|
# @option options markdown_emphasis [Boolean] Should extract Markdown emphasis (italic). Defaults to `true`.
|
40
40
|
# @option options markdown_strikethrough [Boolean] Should extract Markdown strikethrough. Defaults to `true`.
|
41
41
|
# @option options hashtags [Boolean] Should extract hashtags. Defaults to `true`.
|
42
|
+
# @option options hashtag_validator A callable object to validate a hashtag. This should return `true` or `false`. Invalid hashtags will be left as plain text. If the validator is `nil`, all hashtags will be extracted. Defaults to `nil`.
|
42
43
|
# @option options autolinks [Boolean] Should automatically detect links. Defaults to `true`.
|
43
44
|
# @option options emoji [Boolean] Should extract named emoji. Defaults to `true`.
|
45
|
+
# @option options users [Boolean] Should extract user mentions. Defaults to `false`.
|
46
|
+
# @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
47
|
# @option options html [Boolean] Should generate HTML. Defaults to `true`.
|
45
48
|
# @option options html_renderer [Class] class to use as HTML renderer. Defaults to `Quesadilla::HTMLRenderer`.
|
46
49
|
# @return [Hash] hash containing the display text, html text, and entities
|
data/lib/quesadilla/extractor.rb
CHANGED
@@ -24,6 +24,7 @@ module Quesadilla
|
|
24
24
|
markdown_emphasis: true,
|
25
25
|
markdown_strikethrough: true,
|
26
26
|
hashtags: true,
|
27
|
+
hashtag_validator: nil,
|
27
28
|
autolinks: true,
|
28
29
|
emoji: true,
|
29
30
|
users: false,
|
@@ -42,10 +43,11 @@ module Quesadilla
|
|
42
43
|
# @option options markdown_emphasis [Boolean] Should extract Markdown emphasis (italic). Defaults to `true`.
|
43
44
|
# @option options markdown_strikethrough [Boolean] Should extract Markdown strikethrough. Defaults to `true`.
|
44
45
|
# @option options hashtags [Boolean] Should extract hashtags. Defaults to `true`.
|
46
|
+
# @option options hashtag_validator A callable object to validate a hashtag. This should return `true` or `false`. Invalid hashtags will be left as plain text. If the validator is `nil`, all hashtags will be extracted. Defaults to `nil`.
|
45
47
|
# @option options autolinks [Boolean] Should automatically detect links. Defaults to `true`.
|
46
48
|
# @option options emoji [Boolean] Should extract named emoji. Defaults to `true`.
|
47
49
|
# @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
|
50
|
+
# @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`.
|
49
51
|
# @option options html [Boolean] Should generate HTML. Defaults to `true`.
|
50
52
|
# @option options html_renderer [Class] class to use as HTML renderer. Defaults to `Quesadilla::HTMLRenderer`.
|
51
53
|
def initialize(options = {})
|
@@ -54,10 +56,10 @@ module Quesadilla
|
|
54
56
|
end
|
55
57
|
|
56
58
|
# Extract entities from text
|
57
|
-
# @param
|
59
|
+
# @param text [String] the text to extract from
|
58
60
|
# @return [Hash] hash containing the display text, html text, and entities
|
59
|
-
def extract(
|
60
|
-
@original_text =
|
61
|
+
def extract(text)
|
62
|
+
@original_text = text.dup
|
61
63
|
|
62
64
|
# Emoji colon-syntax
|
63
65
|
replace_emoji if @options[:emoji]
|
@@ -12,15 +12,22 @@ module Quesadilla
|
|
12
12
|
|
13
13
|
def extract_hashtags
|
14
14
|
Twitter::Extractor::extract_hashtags_with_indices(@working_text).each do |entity|
|
15
|
-
|
15
|
+
hashtag = entity[:hashtag]
|
16
|
+
|
17
|
+
# Validate
|
18
|
+
if validator = @options[:hashtag_validator]
|
19
|
+
next unless validator.call(hashtag)
|
20
|
+
end
|
21
|
+
|
22
|
+
display_text = "##{hashtag}"
|
16
23
|
@entities << {
|
17
24
|
type: ENTITY_TYPE_HASHTAG,
|
18
|
-
text:
|
19
|
-
display_text:
|
25
|
+
text: display_text,
|
26
|
+
display_text: display_text,
|
20
27
|
indices: entity[:indices],
|
21
|
-
hashtag:
|
28
|
+
hashtag: hashtag.downcase
|
22
29
|
}
|
23
|
-
@working_text.sub!(
|
30
|
+
@working_text.sub!(display_text, REPLACE_TOKEN * display_text.length)
|
24
31
|
end
|
25
32
|
end
|
26
33
|
end
|
data/lib/quesadilla/version.rb
CHANGED
data/quesadilla.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.authors = ['Sam Soffes']
|
10
10
|
gem.email = ['sam@soff.es']
|
11
11
|
gem.description = 'Entity-style text parsing'
|
12
|
-
gem.summary =
|
12
|
+
gem.summary = 'Entity-style text parsing extracted from Cheddar'
|
13
13
|
gem.homepage = 'https://github.com/soffes/quesadilla'
|
14
14
|
gem.license = 'MIT'
|
15
15
|
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ['lib']
|
20
20
|
|
21
|
-
gem.required_ruby_version = '>= 1.9.
|
21
|
+
gem.required_ruby_version = '>= 1.9.2'
|
22
22
|
|
23
23
|
# Hashtag and autolink parsing
|
24
24
|
gem.add_dependency 'twitter-text', '~> 1.5.0'
|
@@ -48,5 +48,27 @@ module Quesadilla
|
|
48
48
|
}
|
49
49
|
assert_equal expected, extraction
|
50
50
|
end
|
51
|
+
|
52
|
+
def test_that_it_validates
|
53
|
+
validator = lambda do |hashtag|
|
54
|
+
hashtag == 'awesome'
|
55
|
+
end
|
56
|
+
extraction = extract('A task with some #tags that are #awesome', hashtag_validator: validator)
|
57
|
+
expected = {
|
58
|
+
display_text: 'A task with some #tags that are #awesome',
|
59
|
+
display_html: 'A task with some #tags that are <a href="#hashtag-awesome" class="hashtag">#awesome</a>',
|
60
|
+
entities: [
|
61
|
+
{
|
62
|
+
type: 'hashtag',
|
63
|
+
text: '#awesome',
|
64
|
+
display_text: '#awesome',
|
65
|
+
hashtag: 'awesome',
|
66
|
+
indices: [32, 40],
|
67
|
+
display_indices: [32, 40]
|
68
|
+
}
|
69
|
+
]
|
70
|
+
}
|
71
|
+
assert_equal expected, extraction
|
72
|
+
end
|
51
73
|
end
|
52
74
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Soffes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: twitter-text
|
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
86
86
|
- - '>='
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: 1.9.
|
88
|
+
version: 1.9.2
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
91
|
- - '>='
|
@@ -96,7 +96,7 @@ rubyforge_project:
|
|
96
96
|
rubygems_version: 2.0.0
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
|
-
summary: Entity-style text parsing
|
99
|
+
summary: Entity-style text parsing extracted from Cheddar
|
100
100
|
test_files:
|
101
101
|
- test/quesadilla/autolink_test.rb
|
102
102
|
- test/quesadilla/emoji_test.rb
|