html-pipeline-gitlab 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2152131cb71f56ec9919f5fd035e3f3419f2e09
4
+ data.tar.gz: 882d2777949b385ded5924d1d78a9c5d1b99ede7
5
+ SHA512:
6
+ metadata.gz: a2f2b7a5e95970e8a4b1c342a9bd9c39c706a00a49a5852b7200103fda226ce7fea99098a5361d197b96a1a1dde461130a09aeabae07a99e437970d2d60a9d3e
7
+ data.tar.gz: 299f4f94a6a33cb0d424a80662c6cbf17d3c01e6cea370209727ae06c2f65e20975122cbdba29a5134f5034745c27765111b736e83139297ff1f10ab2558a933
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ before_install:
7
+ - sudo apt-get install -y libicu-dev
8
+ install:
9
+ - travis_retry bundle config build.nokogiri --use-system-libraries
10
+ - travis_retry bundle install
11
+ script: rake test
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html-pipeline-gitlab.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robert Schilling
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Html::Pipeline::Gitlab
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'html-pipeline-gitlab'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install html-pipeline-gitlab
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/html-pipeline-gitlab/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'html/pipeline/gitlab/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'html-pipeline-gitlab'
8
+ spec.version = Html::Pipeline::Gitlab::VERSION
9
+ spec.authors = ['Robert Schilling']
10
+ spec.email = ['schilling.ro@gmail.com']
11
+ spec.summary = %q{Extension filters for html-pipeline used by GitLab}
12
+ spec.description = %q{Extension filters for html-pipeline used by GitLab}
13
+ spec.homepage = 'https://github.com/Razer6/html-pipeline-gitlab'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^test/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+
24
+ spec.add_runtime_dependency 'html-pipeline', '~> 1.9.0'
25
+ spec.add_runtime_dependency 'gitlab_emoji', '~> 0.0.1.1'
26
+ end
@@ -0,0 +1,9 @@
1
+ require "html/pipeline/gitlab/version"
2
+
3
+ module Html
4
+ module Pipeline
5
+ module Gitlab
6
+ # Your code goes here...
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,90 @@
1
+ require 'cgi'
2
+ require 'gitlab_emoji'
3
+ require 'html/pipeline/filter'
4
+
5
+
6
+ module HTML
7
+ class Pipeline
8
+ # HTML filter that replaces :emoji: with images.
9
+ #
10
+ # Context:
11
+ # :asset_root (required) - base url to link to emoji sprite
12
+ # :asset_path (optional) - url path to link to emoji sprite. :file_name can be used as a placeholder for the sprite file name. If no asset_path is set "emoji/:file_name" is used.
13
+ class GitLabEmojiFilter < Filter
14
+ def call
15
+ doc.search('text()').each do |node|
16
+ content = node.to_html
17
+ next if !content.include?(':')
18
+ next if has_ancestor?(node, %w(pre code))
19
+ html = emoji_image_filter(content)
20
+ next if html == content
21
+ node.replace(html)
22
+ 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
+
32
+ # Replace :emoji: with corresponding images.
33
+ #
34
+ # text - String text to replace :emoji: in.
35
+ #
36
+ # Returns a String with :emoji: replaced with images.
37
+ def emoji_image_filter(text)
38
+ return text unless text.include?(':')
39
+
40
+ text.gsub(emoji_pattern) do |match|
41
+ name = $1
42
+ "<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
43
+ end
44
+ end
45
+
46
+ # The base url to link emoji sprites
47
+ #
48
+ # Raises ArgumentError if context option has not been provided.
49
+ # Returns the context's asset_root.
50
+ def asset_root
51
+ context[:asset_root]
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))
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def emoji_url(name)
69
+ File.join(asset_root, asset_path(name))
70
+ end
71
+
72
+ # Build a regexp that matches all valid :emoji: names.
73
+ def self.emoji_pattern
74
+ @emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
75
+ end
76
+
77
+ def emoji_pattern
78
+ self.class.emoji_pattern
79
+ end
80
+
81
+ def self.emoji_names
82
+ Emoji.names
83
+ end
84
+
85
+ def emoji_filename(name)
86
+ "#{::CGI.escape(name)}.png"
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,7 @@
1
+ module Html
2
+ module Pipeline
3
+ module Gitlab
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+ require 'html/pipeline/gitlab/gitlab_emoji_filter'
3
+
4
+ class HTML::Pipeline::GitLabEmojiFilterTest < Minitest::Test
5
+ GitLabEmojiFilter = HTML::Pipeline::GitLabEmojiFilter
6
+
7
+ def test_emojify
8
+ filter = GitLabEmojiFilter.new("<p>:heart:</p>", {:asset_root => 'https://foo.com'})
9
+ doc = filter.call
10
+ assert_match "https://foo.com/emoji/heart.png", doc.search('img').attr('src').value
11
+ end
12
+
13
+ def test_unsupported_emoji
14
+ block = "<p>:sheep:</p>"
15
+ filter = GitLabEmojiFilter.new(block, {:asset_root => 'https://foo.com'})
16
+ doc = filter.call
17
+ assert_match block, doc.to_html
18
+ end
19
+
20
+ def test_uri_encoding
21
+ filter = GitLabEmojiFilter.new("<p>:+1:</p>", {:asset_root => 'https://foo.com'})
22
+ doc = filter.call
23
+ assert_match "https://foo.com/emoji/%2B1.png", doc.search('img').attr('src').value
24
+ end
25
+
26
+ def test_required_context_validation
27
+ exception = assert_raises(ArgumentError) {
28
+ GitLabEmojiFilter.call("", {})
29
+ }
30
+ assert_match /:asset_root/, exception.message
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
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler/setup'
2
+ require 'html/pipeline'
3
+ require 'minitest/autorun'
4
+
5
+ require 'active_support/core_ext/string'
6
+
7
+ module TestHelpers
8
+ # Asserts that two html fragments are equivalent. Attribute order
9
+ # will be ignored.
10
+ def assert_equal_html(expected, actual)
11
+ assert_equal Nokogiri::HTML::DocumentFragment.parse(expected).to_hash,
12
+ Nokogiri::HTML::DocumentFragment.parse(actual).to_hash
13
+ end
14
+ end
15
+
16
+ Minitest::Test.send(:include, TestHelpers)
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html-pipeline-gitlab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Schilling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: html-pipeline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: gitlab_emoji
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.1.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.1.1
69
+ description: Extension filters for html-pipeline used by GitLab
70
+ email:
71
+ - schilling.ro@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - html-pipeline-gitlab.gemspec
83
+ - lib/html/pipeline/gitlab.rb
84
+ - lib/html/pipeline/gitlab/gitlab_emoji_filter.rb
85
+ - lib/html/pipeline/gitlab/version.rb
86
+ - test/html/pipeline/gitlab_gemoji_filter_test.rb
87
+ - test/test_helper.rb
88
+ homepage: https://github.com/Razer6/html-pipeline-gitlab
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Extension filters for html-pipeline used by GitLab
112
+ test_files:
113
+ - test/html/pipeline/gitlab_gemoji_filter_test.rb
114
+ - test/test_helper.rb