html-pipeline-nico_link 0.0.5 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9326910aebea057e21d0f74ee6617c7f01c68926
4
- data.tar.gz: da223434d219d76b6775fff001cb831dbb8313ce
3
+ metadata.gz: 366a0e350574d225a5afaa5dd1842866abf4eee4
4
+ data.tar.gz: 5b1b0c477d9629e5cf7d1a1522e307577e72b990
5
5
  SHA512:
6
- metadata.gz: f187d6daf42de61d0ea51c12cc261dac5fa0aa70a463b2bc66141eea28d186cce17737b1b42dc6dbcfd3e97ff2902fc5821587df32b4e186454dc1e7e1af443f
7
- data.tar.gz: 775cc0b910afc46db7b3770f3777767f83ed589bcfaaa6a5c98379af4d8870a4357377e3b30fab8be525a54a64ea191c0d28b26a90fc932f7d961e23366d0e39
6
+ metadata.gz: 3191efafc28421a8cacc3521a46bc07d7080102be4ea5091d7330d65c762e588bd8d8e85c37e646702bf15026058e0eb14474e51037b02132769c591172f97b3
7
+ data.tar.gz: e567142ee2b3caec8a59d75ee2c83c33ff7e82ec160b2394f8fca821dce1fd4a0c662d194577e9e1943b28d98cd2efc49eb49a6eff6bd7714e64a014012e8dc9
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/**/*
4
+ - tmp/**/*
5
+ - vendor/**/*
6
+
7
+ SpecialGlobalVars:
8
+ Enabled: false
9
+
10
+ AsciiComments:
11
+ Enabled: false
12
+
13
+ ClassLength:
14
+ Max: 200
15
+
16
+ Documentation:
17
+ Enabled: false
18
+
19
+ LineLength:
20
+ Max: 120
21
+ Exclude:
22
+ - spec/**/*
23
+
24
+ MethodLength:
25
+ Max: 30
26
+
27
+ TrailingCommaInLiteral:
28
+ EnforcedStyleForMultiline: comma
29
+
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.0
7
+ - ruby-head
8
+ before_install:
9
+ - gem install bundler
10
+ script:
11
+ - bundle exec rubocop
12
+ - bundle exec rake
13
+
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Html::Pipeline::NicoLink
2
2
 
3
+ [![Build Status](https://travis-ci.org/rutan/html-pipeline-nico_link.svg?branch=master)](https://travis-ci.org/rutan/html-pipeline-nico_link)
4
+
3
5
  An HTML::Pipeline filter for niconico(http://www.nicovideo.jp) description links.
4
6
 
5
7
  niconicoの動画説明文中のオートリンクです。mylist2.jsの定義を参考にしています。
data/Rakefile CHANGED
@@ -2,5 +2,4 @@ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new('spec')
5
- task :default => :spec
6
-
5
+ task default: :spec
@@ -6,11 +6,10 @@ require 'html/pipeline/nico_link/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'html-pipeline-nico_link'
8
8
  spec.version = HTML::Pipeline::NicoLink::VERSION
9
- spec.authors = ['Ru/MuckRu']
9
+ spec.authors = ['ru_shalm']
10
10
  spec.email = ['ru_shalm@hazimu.com']
11
- spec.summary = %q{niconico link for html-pipeline}
12
- spec.description = %q{niconico link for html-pipeline}
13
- spec.homepage = ''
11
+ spec.summary = 'niconico link for html-pipeline'
12
+ spec.homepage = 'https://github.com/rutan/html-pipeline-nico_link/'
14
13
  spec.license = 'MIT'
15
14
 
16
15
  spec.files = `git ls-files -z`.split("\x0")
@@ -18,9 +17,10 @@ Gem::Specification.new do |spec|
18
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
18
  spec.require_paths = ['lib']
20
19
 
21
- spec.add_dependency 'html-pipeline', '~> 2.0'
20
+ spec.add_dependency 'html-pipeline', '~> 2.4'
22
21
 
23
22
  spec.add_development_dependency 'bundler', '~> 1.7'
24
23
  spec.add_development_dependency 'rake', '~> 10.0'
25
24
  spec.add_development_dependency 'rspec', '~> 3.1.0'
25
+ spec.add_development_dependency 'rubocop', '~> 0.40.0'
26
26
  end
@@ -10,26 +10,24 @@ module HTML
10
10
  end
11
11
 
12
12
  def call
13
- doc.search('.//text()').each do |node|
13
+ doc.xpath('.//text()').each do |node|
14
14
  next if has_ancestor?(node, IGNORE_PARENTS)
15
15
  content = node.to_html
16
- has_prev_node = !!(node.previous_element)
17
- html = apply_filter(content, has_prev_node)
16
+ html = apply_filter(content)
18
17
  next if html == content
19
18
  node.replace(html)
20
19
  end
21
20
  doc
22
21
  end
23
22
 
24
- def apply_filter(content, has_prev_node = false)
23
+ def apply_filter(content)
25
24
  content = content.dup
26
- self.patterns.each do |pattern_set|
27
- content.gsub!(pattern_set[:pattern]) do
28
- space = $1
29
- text = $2
30
- url = pattern_set[:link].gsub('%s', text)
31
- "#{space.size > 0 ? space : (has_prev_node ? ' ' : '')}<a href=\"#{ERB::Util.html_escape(url)}\">#{ERB::Util.html_escape(text)}</a>"
32
- end
25
+ content.gsub!(merged_pattern) do |text|
26
+ index = $~.captures.index { |n| n }
27
+ pattern_set = patterns[index]
28
+ value = pattern_set[:convert] ? pattern_set[:convert].call(text) : text
29
+ url = pattern_set[:link].gsub('%s', value)
30
+ "<a href=\"#{ERB::Util.html_escape(url)}\">#{ERB::Util.html_escape(text)}</a>"
33
31
  end
34
32
  content
35
33
  end
@@ -37,97 +35,98 @@ module HTML
37
35
  def patterns
38
36
  @patterns ||= (@context[:nico_link] || DEFAULT_PATTERNS).map do |pattern_set|
39
37
  {
40
- pattern: create_pattern(pattern_set[:pattern]),
38
+ pattern: pattern_set[:pattern],
41
39
  link: pattern_set[:link],
40
+ convert: pattern_set[:convert],
42
41
  }
43
42
  end
44
43
  end
45
44
 
46
- def create_pattern(key)
47
- /
48
- (^|\W)
49
- (#{key})
50
- (?=
51
- \.+[ \t\W]|
52
- \.+$|
53
- [^0-9a-zA-Z_.]|
54
- $
55
- )
56
- /ix
45
+ def merged_pattern
46
+ @merged_pattern ||= /\b(?:(#{patterns.map { |n| n[:pattern] }.join(')|(')}))\b/
57
47
  end
58
48
 
59
- IGNORE_PARENTS = %w[pre code a style script].to_set
49
+ IGNORE_PARENTS = %w(pre code a style script).to_set
60
50
 
61
51
  DEFAULT_PATTERNS = [
62
52
  {
63
53
  pattern: /(?:sm|nm|so|ca|ax|yo|nl|ig|na|cw|z[a-e]|om|sk|yk)\d{1,14}/,
64
- link: 'http://www.nicovideo.jp/watch/%s'
54
+ link: 'http://www.nicovideo.jp/watch/%s',
65
55
  },
66
56
  {
67
- pattern: /(?:watch|user|myvideo|mylist)\d{1,14}/,
68
- link: 'http://www.nicovideo.jp/%s'
57
+ pattern: %r{(?:watch|user|myvideo|mylist)/\d{1,10}},
58
+ link: 'http://www.nicovideo.jp/%s',
69
59
  },
70
60
  {
71
- pattern: /(?:co)\d{1,14}/,
72
- link: 'http://com.nicovideo.jp/%s'
61
+ pattern: /co\d{1,14}/,
62
+ link: 'http://com.nicovideo.jp/%s',
73
63
  },
74
64
  {
75
- pattern: /(?:ch)\d{1,14}/,
76
- link: 'http://ch.nicovideo.jp/%s'
65
+ pattern: /ch\d{1,14}/,
66
+ link: 'http://ch.nicovideo.jp/%s',
77
67
  },
78
68
  {
79
- pattern: /(?:ar)\d{1,14}/,
80
- link: 'http://ch.nicovideo.jp/article/%s'
69
+ pattern: /ar\d{1,14}/,
70
+ link: 'http://ch.nicovideo.jp/article/%s',
81
71
  },
82
72
  {
83
- pattern: /(?:td)\d+/,
73
+ pattern: /td\d+/,
84
74
  link: 'http://3d.nicovideo.jp/works/%s',
85
75
  },
86
76
  {
87
- pattern: /(?:nc)\d{1,14}/,
77
+ pattern: /nc\d{1,14}/,
88
78
  link: 'http://commons.nicovideo.jp/material/%s',
89
79
  },
90
80
  {
91
- pattern: /(?:dw\\d+|az[A-Z0-9]{10}|ys[a-zA-Z0-9-]+_[a-zA-Z0-9-]+|ga\\d+|ip[\\d_]+|gg[a-zA-Z0-9]+-[a-zA-Z0-9-]+)/,
81
+ pattern: /(?:dw\d+|az[A-Z0-9]{10}|ys[a-zA-Z0-9-]+_[a-zA-Z0-9-]+|ga\d+|ip[\d_]+|gg[a-zA-Z0-9]+-[a-zA-Z0-9-]+)/,
92
82
  link: 'http://ichiba.nicovideo.jp/item/%s',
93
83
  },
94
84
  {
95
- pattern: /(?:lv)\d{1,14}/,
85
+ pattern: /lv\d{1,14}/,
96
86
  link: 'http://live.nicovideo.jp/watch/%s',
97
87
  },
98
88
  {
99
- pattern: /(?:[sm]g)\d{1,14}/,
89
+ pattern: /[sm]g\d{1,14}/,
100
90
  link: 'http://seiga.nicovideo.jp/watch/%s',
101
91
  },
102
92
  {
103
- pattern: /(?:bk)\d{1,14}/,
93
+ pattern: /bk\d{1,14}/,
104
94
  link: 'http://seiga.nicovideo.jp/watch/%s',
105
95
  },
106
96
  {
107
- pattern: /(?:im)\d{1,14}/,
97
+ pattern: /im\d{1,14}/,
108
98
  link: 'http://seiga.nicovideo.jp/seiga/%s',
109
99
  },
110
100
  {
111
- pattern: /commons\.nicovideo\.jp\/user\/\d+/,
101
+ pattern: %r{commons\.nicovideo\.jp/user/\d+},
112
102
  link: 'http://%s',
113
103
  },
114
104
  {
115
- pattern: /niconicommons\.jp\/user\/\d+/,
105
+ pattern: %r{niconicommons\.jp/user/\d+},
116
106
  link: 'http://www.%s',
117
107
  },
118
108
  {
119
- pattern: /user\/illust\/\d+/,
109
+ pattern: %r{user/illust/\d+},
120
110
  link: 'http://seiga.nicovideo.jp/%s',
121
111
  },
122
112
  {
123
- pattern: /clip\/\d+/,
113
+ pattern: %r{clip/\d+},
124
114
  link: 'http://seiga.nicovideo.jp/%s',
125
115
  },
126
116
  {
127
- pattern: /ch\.nicovideo\.jp\/[a-zA-Z0-9][-_a-zA-Z0-9]+(?=[^\-_A-Za-z0-9\/]|$)/,
117
+ pattern: %r{ch\.nicovideo\.jp/[a-zA-Z0-9][-_a-zA-Z0-9]+(?=[^\-_A-Za-z0-9/]|$)},
128
118
  link: 'http://%s',
129
119
  },
130
- ]
120
+ {
121
+ pattern: /jps\d{1,14}/,
122
+ link: 'http://jpstore.dwango.jp/products/detail.php?product_id=%s',
123
+ convert: -> (str) { str.sub('jps', '') },
124
+ },
125
+ {
126
+ pattern: /kn\d+/,
127
+ link: 'http://niconare.nicovideo.jp/watch/%s',
128
+ },
129
+ ].freeze
131
130
  end
132
131
  end
133
132
  end
@@ -1,7 +1,7 @@
1
1
  module HTML
2
2
  class Pipeline
3
3
  module NicoLink
4
- VERSION = '0.0.5'
4
+ VERSION = '0.1.0'.freeze
5
5
  end
6
6
  end
7
7
  end
@@ -17,6 +17,21 @@ describe 'NicoLinkFilter' do
17
17
  it { expect(subject).to eq '<a href="http://commons.nicovideo.jp/user/123">commons.nicovideo.jp/user/123</a>' }
18
18
  end
19
19
 
20
+ context 'ch.nicovideo.jp/kmmk' do
21
+ let(:text) { 'ch.nicovideo.jp/kmmk' }
22
+ it { expect(subject).to eq '<a href="http://ch.nicovideo.jp/kmmk">ch.nicovideo.jp/kmmk</a>' }
23
+ end
24
+
25
+ context 'ch.nicovideo.jp/kmmk' do
26
+ let(:text) { 'ch.nicovideo.jp/kmmk' }
27
+ it { expect(subject).to eq '<a href="http://ch.nicovideo.jp/kmmk">ch.nicovideo.jp/kmmk</a>' }
28
+ end
29
+
30
+ context 'jps156' do
31
+ let(:text) { 'jps156' }
32
+ it { expect(subject).to eq '<a href="http://jpstore.dwango.jp/products/detail.php?product_id=156">jps156</a>' }
33
+ end
34
+
20
35
  context 'sm12345 ar23456 td34567' do
21
36
  let(:text) { 'sm12345 ar23456 td34567' }
22
37
  it { expect(subject).to eq '<a href="http://www.nicovideo.jp/watch/sm12345">sm12345</a> <a href="http://ch.nicovideo.jp/article/ar23456">ar23456</a> <a href="http://3d.nicovideo.jp/works/td34567">td34567</a>' }
@@ -41,4 +56,16 @@ describe 'NicoLinkFilter' do
41
56
  let(:text) { '<a href="http://www.nicovideo.jp/watch/sm9">http://www.nicovideo.jp/watch/sm9</a>' }
42
57
  it { expect(subject).to eq '<a href="http://www.nicovideo.jp/watch/sm9">http://www.nicovideo.jp/watch/sm9</a>' }
43
58
  end
59
+
60
+ context 'all stars' do
61
+ let :text do
62
+ %w(
63
+ sm123 watch/5321 co9999 ch1414 ar56789 td3232 nc9284 dw111 lv8864 sg9123 bk1313 im58
64
+ commons.nicovideo.jp/user/1 niconicommons.jp/user/2
65
+ user/illust/3 clip/555 ch.nicovideo.jp/h-p
66
+ jps1111 kn1567
67
+ ).join(' ')
68
+ end
69
+ it { expect(subject).to eq '<a href="http://www.nicovideo.jp/watch/sm123">sm123</a> <a href="http://www.nicovideo.jp/watch/5321">watch/5321</a> <a href="http://com.nicovideo.jp/co9999">co9999</a> <a href="http://ch.nicovideo.jp/ch1414">ch1414</a> <a href="http://ch.nicovideo.jp/article/ar56789">ar56789</a> <a href="http://3d.nicovideo.jp/works/td3232">td3232</a> <a href="http://commons.nicovideo.jp/material/nc9284">nc9284</a> <a href="http://ichiba.nicovideo.jp/item/dw111">dw111</a> <a href="http://live.nicovideo.jp/watch/lv8864">lv8864</a> <a href="http://seiga.nicovideo.jp/watch/sg9123">sg9123</a> <a href="http://seiga.nicovideo.jp/watch/bk1313">bk1313</a> <a href="http://seiga.nicovideo.jp/seiga/im58">im58</a> <a href="http://commons.nicovideo.jp/user/1">commons.nicovideo.jp/user/1</a> <a href="http://www.niconicommons.jp/user/2">niconicommons.jp/user/2</a> <a href="http://seiga.nicovideo.jp/user/illust/3">user/illust/3</a> <a href="http://seiga.nicovideo.jp/clip/555">clip/555</a> <a href="http://ch.nicovideo.jp/h-p">ch.nicovideo.jp/h-p</a> <a href="http://jpstore.dwango.jp/products/detail.php?product_id=1111">jps1111</a> <a href="http://niconare.nicovideo.jp/watch/kn1567">kn1567</a>' }
70
+ end
44
71
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,3 @@ require 'rspec'
2
2
  require 'html/pipeline/nico_link'
3
3
 
4
4
  Dir.glob('./support/**/*.rb').each { |f| require f }
5
-
6
- RSpec.configure do |config|
7
- end
8
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-pipeline-nico_link
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Ru/MuckRu
7
+ - ru_shalm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-20 00:00:00.000000000 Z
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: html-pipeline
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '2.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '2.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,21 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.1.0
69
- description: niconico link for html-pipeline
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.40.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.40.0
83
+ description:
70
84
  email:
71
85
  - ru_shalm@hazimu.com
72
86
  executables: []
@@ -74,6 +88,8 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
91
+ - ".rubocop.yml"
92
+ - ".travis.yml"
77
93
  - Gemfile
78
94
  - LICENSE.txt
79
95
  - README.md
@@ -84,7 +100,7 @@ files:
84
100
  - lib/html/pipeline/nico_link/version.rb
85
101
  - spec/nico_link_filter_spec.rb
86
102
  - spec/spec_helper.rb
87
- homepage: ''
103
+ homepage: https://github.com/rutan/html-pipeline-nico_link/
88
104
  licenses:
89
105
  - MIT
90
106
  metadata: {}
@@ -104,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
120
  version: '0'
105
121
  requirements: []
106
122
  rubyforge_project:
107
- rubygems_version: 2.4.5.1
123
+ rubygems_version: 2.5.1
108
124
  signing_key:
109
125
  specification_version: 4
110
126
  summary: niconico link for html-pipeline