html-pipeline-gitlab 0.0.1 → 0.0.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.md +7 -6
- data/lib/html/pipeline/gitlab/gitlab_emoji_filter.rb +59 -67
- data/lib/html/pipeline/gitlab/version.rb +1 -1
- data/lib/html/pipeline/gitlab.rb +2 -9
- data/test/html/pipeline/gitlab_gemoji_filter_test.rb +8 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 761eebe497d43f7d750b204380a1a96aa97ea18a
|
4
|
+
data.tar.gz: 3359741032f9c25e5ad31dda0aada680ea5d9081
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bae915c13ad3623c4e788f4c8b021cee84569392095f884ad85ea6ecb1aa651f1dde1ec39c7b4e2cf0c69357a5470e5ea28162304aaca0db8457a484f2899b64
|
7
|
+
data.tar.gz: aead33707d35f534dee9495af9a4d581e5de332b611f3da8a5794c78c84d27090ba3ca5458aa75ee4d7ddf3365848ce81bea01ac9e846a7230dfbb93c8ea6cad
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# Html::Pipeline::Gitlab
|
2
2
|
|
3
|
-
|
3
|
+
This gem implements various filters for [html-pipeline](https://github.com/jch/html-pipeline)
|
4
|
+
used by [GitLab](https://about.gitlab.com).
|
5
|
+
|
6
|
+
## Code Status
|
7
|
+
|
8
|
+
[![Build Status](https://semaphoreapp.com/api/v1/projects/b9b808be-6c72-4e76-ae62-79b3a25a022a/243365/badge.png)](https://semaphoreapp.com/razer6/html-pipeline-gitlab)
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
@@ -18,13 +23,9 @@ Or install it yourself as:
|
|
18
23
|
|
19
24
|
$ gem install html-pipeline-gitlab
|
20
25
|
|
21
|
-
## Usage
|
22
|
-
|
23
|
-
TODO: Write usage instructions here
|
24
|
-
|
25
26
|
## Contributing
|
26
27
|
|
27
|
-
1. Fork it ( https://github.com/
|
28
|
+
1. Fork it ( https://github.com/razer6/html-pipeline-gitlab/fork )
|
28
29
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
30
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
31
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -5,85 +5,77 @@ require 'html/pipeline/filter'
|
|
5
5
|
|
6
6
|
module HTML
|
7
7
|
class Pipeline
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
8
|
+
class GitLab
|
9
|
+
# HTML filter that replaces :emoji: with images.
|
10
|
+
#
|
11
|
+
# Context:
|
12
|
+
# :asset_root (required) - base url to link to emoji sprite
|
13
|
+
# :asset_path (optional) - url path to link to emoji sprite.
|
14
|
+
# :file_name can be used as a placeholder for the sprite file name.
|
15
|
+
# If no asset_path is set "emoji/:file_name" is used.
|
16
|
+
class GitLabEmojiFilter < Filter
|
17
|
+
def call
|
18
|
+
doc.search('text()').each do |node|
|
19
|
+
content = node.to_html
|
20
|
+
next if !content.include?(':')
|
21
|
+
next if has_ancestor?(node, %w(pre code))
|
22
|
+
html = emoji_image_filter(content)
|
23
|
+
next if html == content
|
24
|
+
node.replace(html)
|
25
|
+
end
|
26
|
+
doc
|
27
|
+
end
|
28
|
+
|
29
|
+
# Implementation of validate hook.
|
30
|
+
# Errors should raise exceptions or use an existing validator.
|
31
|
+
def validate
|
32
|
+
needs :asset_root
|
22
33
|
end
|
23
|
-
doc
|
24
|
-
end
|
25
|
-
|
26
|
-
# Implementation of validate hook.
|
27
|
-
# Errors should raise exceptions or use an existing validator.
|
28
|
-
def validate
|
29
|
-
needs :asset_root
|
30
|
-
end
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
# Replace :emoji: with corresponding images.
|
36
|
+
#
|
37
|
+
# text - String text to replace :emoji: in.
|
38
|
+
#
|
39
|
+
# Returns a String with :emoji: replaced with images.
|
40
|
+
def emoji_image_filter(text)
|
41
|
+
return text unless text.include?(':')
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
+
text.gsub(emoji_pattern) do |match|
|
44
|
+
name = $1
|
45
|
+
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
|
46
|
+
end
|
43
47
|
end
|
44
|
-
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
# The url path to link emoji sprites
|
55
|
-
#
|
56
|
-
# :file_name can be used in the asset_path as a placeholder for the sprite file name. If no asset_path is set in the context "emoji/:file_name" is used.
|
57
|
-
# Returns the context's asset_path or the default path if no context asset_path is given.
|
58
|
-
def asset_path(name)
|
59
|
-
if context[:asset_path]
|
60
|
-
context[:asset_path].gsub(":file_name", emoji_filename(name))
|
61
|
-
else
|
62
|
-
File.join("emoji", emoji_filename(name))
|
49
|
+
# The base url to link emoji sprites
|
50
|
+
#
|
51
|
+
# Raises ArgumentError if context option has not been provided.
|
52
|
+
# Returns the context's asset_root.
|
53
|
+
def asset_root
|
54
|
+
context[:asset_root]
|
63
55
|
end
|
64
|
-
end
|
65
56
|
|
66
|
-
|
57
|
+
private
|
67
58
|
|
68
|
-
|
69
|
-
|
70
|
-
|
59
|
+
def emoji_url(name)
|
60
|
+
File.join(asset_root, 'emoji', emoji_filename(name))
|
61
|
+
end
|
71
62
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
63
|
+
# Build a regexp that matches all valid :emoji: names.
|
64
|
+
def self.emoji_pattern
|
65
|
+
@emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
|
66
|
+
end
|
76
67
|
|
77
|
-
|
78
|
-
|
79
|
-
|
68
|
+
def emoji_pattern
|
69
|
+
self.class.emoji_pattern
|
70
|
+
end
|
80
71
|
|
81
|
-
|
82
|
-
|
83
|
-
|
72
|
+
def self.emoji_names
|
73
|
+
Emoji.names
|
74
|
+
end
|
84
75
|
|
85
|
-
|
86
|
-
|
76
|
+
def emoji_filename(name)
|
77
|
+
"#{::CGI.escape(name)}.png"
|
78
|
+
end
|
87
79
|
end
|
88
80
|
end
|
89
81
|
end
|
data/lib/html/pipeline/gitlab.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require 'html/pipeline/gitlab
|
2
|
+
require 'html/pipeline/gitlab'
|
3
3
|
|
4
4
|
class HTML::Pipeline::GitLabEmojiFilterTest < Minitest::Test
|
5
|
-
GitLabEmojiFilter = HTML::Pipeline::GitLabEmojiFilter
|
5
|
+
GitLabEmojiFilter = HTML::Pipeline::GitLab::GitLabEmojiFilter
|
6
6
|
|
7
7
|
def test_emojify
|
8
|
-
filter = GitLabEmojiFilter.new(
|
8
|
+
filter = GitLabEmojiFilter.new('<p>:heart:</p>', {asset_root: 'https://foo.com'})
|
9
9
|
doc = filter.call
|
10
|
-
assert_match
|
10
|
+
assert_match 'https://foo.com/emoji/heart.png', doc.search('img').attr('src').value
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_unsupported_emoji
|
14
|
-
block =
|
15
|
-
filter = GitLabEmojiFilter.new(block, {:
|
14
|
+
block = '<p>:sheep:</p>'
|
15
|
+
filter = GitLabEmojiFilter.new(block, {asset_root: 'https://foo.com'})
|
16
16
|
doc = filter.call
|
17
17
|
assert_match block, doc.to_html
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_uri_encoding
|
21
|
-
filter = GitLabEmojiFilter.new(
|
21
|
+
filter = GitLabEmojiFilter.new('<p>:+1:</p>', {asset_root: 'https://foo.com'})
|
22
22
|
doc = filter.call
|
23
|
-
assert_match
|
23
|
+
assert_match 'https://foo.com/emoji/%2B1.png', doc.search('img').attr('src').value
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_required_context_validation
|
@@ -29,10 +29,4 @@ class HTML::Pipeline::GitLabEmojiFilterTest < Minitest::Test
|
|
29
29
|
}
|
30
30
|
assert_match /:asset_root/, exception.message
|
31
31
|
end
|
32
|
-
|
33
|
-
def test_custom_asset_path
|
34
|
-
filter = GitLabEmojiFilter.new("<p>:+1:</p>", {:asset_path => ':file_name', :asset_root => 'https://foo.com'})
|
35
|
-
doc = filter.call
|
36
|
-
assert_match "https://foo.com/%2B1.png", doc.search('img').attr('src').value
|
37
|
-
end
|
38
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html-pipeline-gitlab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Schilling
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|