jekyll-spaceship 0.9.3 → 0.9.4

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
  SHA256:
3
- metadata.gz: 993252decbad6e7d20ed20245f3ba7c7858eb5845402b06605a8b05d6268225f
4
- data.tar.gz: 45012fc8ca11671a98d36b1e1c6dfbfef67cc1c12f01f265ed507760e96f40fd
3
+ metadata.gz: cbba69bcf95b309c36da702cce9670212f7a32985e2522f13b06fdc0058e0b60
4
+ data.tar.gz: 679ee30cad6abfe6c6fb271e3816bff2e16f931b94492d0e0223f4dba11263f4
5
5
  SHA512:
6
- metadata.gz: 139508a04521719d9160e2a0cff09a75600dffc5cae80c9dd75ec047495be87a1a7b786b859d5e335df9226d35617aa9e6470a4905fb497799f2c0c9981d6da2
7
- data.tar.gz: 00b615b28c36a335749d5b3dcd7a60e55b8639b2d86ae8e027243985c939e75333fd4c882ed84999f3de07b43ff07c632db62f88f3641dc4634c744f91a2b279
6
+ metadata.gz: 207137e6fedec9521c2cde3a3215cf7f1ccbc81f6ce0385c385a1c48d31a1ba7d03b3c532e0cbde80ce2f8d33e79a770d299d64cc436acddfc03ed651a2e9fc4
7
+ data.tar.gz: 1c78e2e73e99f7c3e007e3cbcea683b1d4a4db5771737530865be9262d42adf5617e6d7b9cb9931b4947937fdc64d927bf38967121b2dc98d1107d5bec01cace
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'uri'
4
+ require "nokogiri"
4
5
 
5
6
  module Jekyll::Spaceship
6
7
  class MediaProcessor < Processor
@@ -18,23 +19,29 @@ module Jekyll::Spaceship
18
19
  }
19
20
  end
20
21
 
21
- def on_handle_markdown(content)
22
- content = handle_normal_audio(content)
23
- content = handle_normal_video(content)
24
- content = handle_youtube(content)
25
- content = handle_vimeo(content)
26
- content = handle_dailymotion(content)
27
- content = handle_spotify(content)
28
- content = handle_soundcloud(content)
22
+ def on_handle_html(content)
23
+ # use nokogiri to parse html content
24
+ doc = Nokogiri::HTML(content)
25
+ # handle each img tag
26
+ doc.css('img').each do |element|
27
+ handle_normal_audio(element)
28
+ handle_normal_video(element)
29
+ handle_youtube(element)
30
+ handle_vimeo(element)
31
+ handle_dailymotion(element)
32
+ handle_spotify(element)
33
+ handle_soundcloud(element)
34
+ end
35
+ doc.to_html
29
36
  end
30
37
 
31
38
  # Examples:
32
39
  # ![audio](//www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3)
33
40
  # ![audio](//www.expample.com/examples/t-rex-roar.mp3?autoplay=true&loop=true)
34
- def handle_normal_audio(content)
35
- handle_media(content, {
41
+ def handle_normal_audio(element)
42
+ handle_media(element, {
36
43
  media_type: 'audio',
37
- host: '(https?:)?\\/\\/.*\\/',
44
+ host: '(https?:\\/\\/)?.*\\/',
38
45
  id: '(.+?\\.(mp3|wav|ogg|mid|midi|aac|wma))',
39
46
  })
40
47
  end
@@ -44,10 +51,10 @@ module Jekyll::Spaceship
44
51
  # ![video](//www.html5rocks.com/en/tutorials/video/basics/devstories.webm)
45
52
  # ![video](//techslides.com/demos/sample-videos/small.ogv?allow=autoplay)
46
53
  # ![video](//techslides.com/demos/sample-videos/small.mp4?width=400)
47
- def handle_normal_video(content)
48
- handle_media(content, {
54
+ def handle_normal_video(element)
55
+ handle_media(element, {
49
56
  media_type: 'iframe',
50
- host: '(https?:)?\\/\\/.*\\/',
57
+ host: '(https?:\\/\\/)?.*\\/',
51
58
  id: '(.+?\\.(avi|mp4|webm|ogg|ogv|flv|mkv|mov|wmv|3gp|rmvb|asf))'
52
59
  })
53
60
  end
@@ -56,8 +63,8 @@ module Jekyll::Spaceship
56
63
  # ![youtube](https://www.youtube.com/watch?v=XA2WjJbmmoM "title")
57
64
  # ![youtube](http://www.youtube.com/embed/w-m_yZCLF5Q)
58
65
  # ![youtube](//youtu.be/mEP3YXaSww8?height=100%&width=400)
59
- def handle_youtube(content)
60
- handle_media(content, {
66
+ def handle_youtube(element)
67
+ handle_media(element, {
61
68
  media_type: 'iframe',
62
69
  host: '(https?:)?\\/\\/.*youtu.*',
63
70
  id: '(?<=\\?v\\=|embed\\/|\\.be\\/)([a-zA-Z0-9\\_\\-]+)',
@@ -68,8 +75,8 @@ module Jekyll::Spaceship
68
75
  # Examples:
69
76
  # ![vimeo](https://vimeo.com/263856289)
70
77
  # ![vimeo](https://vimeo.com/263856289?height=100%&width=400)
71
- def handle_vimeo(content)
72
- handle_media(content, {
78
+ def handle_vimeo(element)
79
+ handle_media(element, {
73
80
  media_type: 'iframe',
74
81
  host: '(https?:)?\\/\\/vimeo\\.com\\/',
75
82
  id: '([0-9]+)',
@@ -80,8 +87,8 @@ module Jekyll::Spaceship
80
87
  # Examples:
81
88
  # ![dailymotion](https://www.dailymotion.com/video/x7tgcev)
82
89
  # ![dailymotion](https://dai.ly/x7tgcev?height=100%&width=400)
83
- def handle_dailymotion(content)
84
- handle_media(content, {
90
+ def handle_dailymotion(element)
91
+ handle_media(element, {
85
92
  media_type: 'iframe',
86
93
  host: '(https?:)?\\/\\/.*dai.?ly.*',
87
94
  id: '(?<=video\\/|\\/)([a-zA-Z0-9\\_\\-]+)',
@@ -92,8 +99,8 @@ module Jekyll::Spaceship
92
99
  # Examples:
93
100
  # ![spotify](//open.spotify.com/track/4Dg5moVCTqxAb7Wr8Dq2T5)
94
101
  # ![spotify](//open.spotify.com/track/37mEkAaqCE7FXMvnlVA8pp?width=400)
95
- def handle_spotify(content)
96
- handle_media(content, {
102
+ def handle_spotify(element)
103
+ handle_media(element, {
97
104
  media_type: 'iframe',
98
105
  host: '(https?:)?\\/\\/open\\.spotify\\.com\\/track\\/',
99
106
  id: '(?<=track\\/)([a-zA-Z0-9\\_\\-]+)',
@@ -104,8 +111,8 @@ module Jekyll::Spaceship
104
111
 
105
112
  # Examples:
106
113
  # ![soundcloud](//soundcloud.com/aviciiofficial/preview-avicii-vs-lenny)
107
- def handle_soundcloud(content)
108
- handle_media(content, {
114
+ def handle_soundcloud(element)
115
+ handle_media(element, {
109
116
  media_type: 'iframe',
110
117
  id_from: 'html',
111
118
  host: '(https?:)?\\/\\/soundcloud\\.com\\/.+\\/[^\\?]+',
@@ -116,71 +123,53 @@ module Jekyll::Spaceship
116
123
  })
117
124
  end
118
125
 
119
- def handle_media(content, data)
126
+ def handle_media(element, data)
120
127
  host = data[:host]
121
- return content if content.sub(/#{host}/, '').nil?
128
+ src = element.get_attribute('src')
129
+ title = element.get_attribute('title')
130
+ id = data[:id_from] === 'html' ? '()' : data[:id]
131
+ match_data = src.match(/#{host}#{id}\S*/)
132
+ return if match_data.nil?
122
133
 
123
134
  media_type = data[:media_type]
124
135
  base_url = data[:base_url]
125
- id = data[:id_from] === 'html' ? '()' : data[:id]
126
- url = "(#{host}#{id}\\S*)"
127
- title = '("(.*)".*){0,1}'
128
-
129
- # pre-handle reference-style links
130
- regex = /(\[(.*)\]:\s*(#{url}\s*#{title}))/
131
- content.scan regex do |match_data|
132
- match = match_data[0]
133
- ref_name = match_data[1]
134
- ref_value = match_data[2]
135
- content = content.gsub(match, '')
136
- .gsub(/\!\[(.*)\]\s*\[#{ref_name}\]/,
137
- "![\1](#{ref_value})")
136
+ id = data[:id_from] === 'html' \
137
+ ? get_id_from_html(src, data[:id]) \
138
+ : match_data[2]
139
+ qs = src.match(/(?<=\?)(\S*?)$/)
140
+ qs = Hash[URI.decode_www_form(qs.to_s)].reject do |k, v|
141
+ next true if v == id or v == ''
138
142
  end
139
143
 
140
- # handle inline-style links
141
- regex = /(\!\[(.*)\]\(.*#{url}\s*#{title}\))/
142
- content.scan regex do |match_data|
143
- url = match_data[2]
144
- id = data[:id_from] === 'html' \
145
- ? get_id_from_html(url, data[:id]) \
146
- : match_data[4]
147
- title = match_data[6]
148
- qs = url.match(/(?<=\?)(\S*?)$/)
149
- qs = Hash[URI.decode_www_form(qs.to_s)].reject do |k, v|
150
- next true if v == id or v == ''
151
- end
152
-
153
- cfg = self.config['default'].clone
154
- cfg['id'] = qs['id'] || cfg['id']
155
- cfg['class'] = qs['class'] || cfg['class']
156
- cfg['style'] = qs['style'] || cfg['style']
157
- cfg['id'] = cfg['id'].gsub('{id}', id)
158
- cfg['class'] = cfg['class'].gsub('{id}', id)
144
+ cfg = self.config['default'].clone
145
+ cfg['id'] = qs['id'] || cfg['id']
146
+ cfg['class'] = qs['class'] || cfg['class']
147
+ cfg['style'] = qs['style'] || cfg['style']
148
+ cfg['id'] = cfg['id'].gsub('{id}', id)
149
+ cfg['class'] = cfg['class'].gsub('{id}', id)
159
150
 
160
- cfg['src'] = URI(base_url ? "#{base_url}#{id}" : url).tap do |v|
161
- v.query = URI.encode_www_form(qs) if qs.size > 0
162
- end
151
+ cfg['src'] = URI(base_url ? "#{base_url}#{id}" : src).tap do |v|
152
+ v.query = URI.encode_www_form(qs) if qs.size > 0
153
+ end
163
154
 
164
- case media_type
165
- when 'audio'
166
- cfg['autoplay'] = qs['autoplay'] || data[:autoplay] || cfg['autoplay']
167
- cfg['loop'] = qs['loop'] || data[:loop] || cfg['loop']
168
- cfg['style'] += ';display: none;' if qs['hidden']
169
- content = handle_audio(content, { target: match_data[0], cfg: cfg })
170
- when 'iframe'
171
- cfg['title'] = title
172
- cfg['width'] = qs['width'] || data[:width] || cfg['width']
173
- cfg['height'] = qs['height'] || data[:height] || cfg['height']
174
- cfg['frameborder'] = qs['frameborder'] || cfg['frameborder']
175
- cfg['allow'] ||= cfg['allow']
176
- content = handle_iframe(content, { target: match_data[0], cfg: cfg })
177
- end
178
- self.handled = true
155
+ case media_type
156
+ when 'audio'
157
+ cfg['autoplay'] = qs['autoplay'] || data[:autoplay] || cfg['autoplay']
158
+ cfg['loop'] = qs['loop'] || data[:loop] || cfg['loop']
159
+ cfg['style'] += ';display: none;' if qs['hidden']
160
+ handle_audio(element, { cfg: cfg })
161
+ when 'iframe'
162
+ cfg['title'] = title
163
+ cfg['width'] = qs['width'] || data[:width] || cfg['width']
164
+ cfg['height'] = qs['height'] || data[:height] || cfg['height']
165
+ cfg['frameborder'] = qs['frameborder'] || cfg['frameborder']
166
+ cfg['allow'] ||= cfg['allow']
167
+ handle_iframe(element, { cfg: cfg })
179
168
  end
180
- content
169
+ self.handled = true
181
170
  end
182
171
 
183
- def handle_audio(content, data)
172
+ def handle_audio(element, data)
184
173
  cfg = data[:cfg]
185
174
  html = "<audio"\
186
175
  " id=\"#{cfg['id']}\""\
@@ -194,10 +183,11 @@ module Jekyll::Spaceship
194
183
  " Here is a <a href=\"#{cfg['src']}\">link to download the audio</a>"\
195
184
  "instead. </p>"\
196
185
  "</audio>"
197
- content.gsub(data[:target], html)
186
+ doc = Nokogiri::XML(html)
187
+ element.replace(doc.children.first)
198
188
  end
199
189
 
200
- def handle_iframe(content, data)
190
+ def handle_iframe(element, data)
201
191
  cfg = data[:cfg]
202
192
  html = "<iframe"\
203
193
  " id=\"#{cfg['id']}\""\
@@ -211,7 +201,8 @@ module Jekyll::Spaceship
211
201
  " frameborder=\"#{cfg['frameborder']}\""\
212
202
  " allowfullscreen>"\
213
203
  "</iframe>"
214
- content.gsub(data[:target], html)
204
+ doc = Nokogiri::XML(html)
205
+ element.replace(doc.children.first)
215
206
  end
216
207
 
217
208
  def get_id_from_html(url, pattern)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Spaceship
5
- VERSION = "0.9.3"
5
+ VERSION = "0.9.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-spaceship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jeffreytse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2020-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll