elevenlabs-amp-jekyll 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f45ee50f086413315d2361107d9775e3000e3747
4
+ data.tar.gz: b54c687cac2700ddccce7e86d9352a22dd27e2b0
5
+ SHA512:
6
+ metadata.gz: 09c11eb9b159f83021d815cf5bd5be772cedb3fa9c727610a9d14fc958d5f105a08127084166a7b11941ebdf401654e7dd634cdb61e0ae3540f455ac2322c82d
7
+ data.tar.gz: 67320d6831f41a41e27e6d1cf302bd538a591227b5f97996938967debb1ba7d4e22fef5b68c08346e85b18ae9b8478fe928236d52d499bf7f66829d2b2daaebf
data/lib/amp-jekyll.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'jekyll/amp_generate'
2
+ require 'jekyll/amp_filter'
@@ -0,0 +1,89 @@
1
+ require 'nokogiri'
2
+ require 'fastimage'
3
+
4
+ module Jekyll
5
+ module AmpFilter
6
+ # Filter for HTML 'img' elements.
7
+ # Converts elements to 'amp-img' and adds additional attributes
8
+ # Parameters:
9
+ # input - the content of the post
10
+ # responsive - boolean, whether to add layout=responsive, true by default
11
+ def amp_images(input, responsive = true, wi = nil, he = nil)
12
+ doc = Nokogiri::HTML.fragment(input);
13
+ # Add width and height to img elements lacking them
14
+ doc.css('img:not([width])').each do |image|
15
+ if wi && he
16
+ image['width'] = wi
17
+ image['height'] = he
18
+ else
19
+ if image['src'].start_with?('http://', 'https://')
20
+ src = image['src']
21
+ else
22
+ # FastImage doesn't seem to handle local paths when used with Jekyll
23
+ # so let's just force the path
24
+ src = File.join(Dir.pwd, '', image['src'])
25
+ end
26
+ # Jekyll generates static assets after the build process.
27
+ # This causes problems when trying to determine the dimensions of a locally stored image.
28
+ # For now, the only solution is to skip the build and generate the AMP files after the site has beem successfully built.
29
+ # TODO: find a better solution.
30
+ begin
31
+ size = FastImage.size(src)
32
+ image['width'] = size[0]
33
+ image['height'] = size[1]
34
+ rescue Exception => e
35
+ puts 'Unable to get image dimensions for "' + src + '". For local files, build the site with \'--skip-initial-build\' for better results. [Error: ' + e.to_s + ']'
36
+ end
37
+ end
38
+ end
39
+ # Change 'img' elements to 'amp-img', add responsive attribute when needed
40
+ doc.css('img').each do |image|
41
+ image.name = "amp-img"
42
+
43
+ image['layout'] = "responsive" if responsive
44
+ end
45
+
46
+ # Picture elements are not accepted in amp pages, convert them to amp-img
47
+ #<picture>
48
+ # <source srcset="mdn-logo-wide.webp" type="image/webp">
49
+ # <source srcset="mdn-logo-wide.png" media="(min-width: 600px)">
50
+ # <img src="mdn-logo-narrow.png" alt="MDN">
51
+ #</picture>
52
+ # Move amp-img elements inside picture elements outside of it and remove picture elements
53
+ doc.css('picture').each do |picture|
54
+ # Get img element from picture
55
+ amp_img = picture.css('amp-img')
56
+ picture.add_next_sibling(amp_img) unless amp_img.empty?
57
+
58
+ # Remove picture element
59
+ picture.remove
60
+ end
61
+
62
+ # Added <img /> tag wrapped with <noscript /> in case js is not enabled
63
+ # but image will still show up. The element would look like this:
64
+ # <amp-img ...>
65
+ # <noscript>
66
+ # <img ... />
67
+ # </noscript>
68
+ # </ampimg ...>
69
+ # Duplicate amp-img, remove layout attribut, wrap it with noscript, and add
70
+ # it as amp-img child
71
+ doc.css('amp-img').each do |amp_img|
72
+ noscript = Nokogiri::XML::Node.new "noscript", doc
73
+
74
+ noscript_img = amp_img.dup
75
+ noscript_img.remove_attribute('layout')
76
+ noscript_img.name = 'img'
77
+
78
+ noscript.add_child(noscript_img)
79
+
80
+ amp_img.add_child(noscript)
81
+ end
82
+
83
+ # Return the html as plaintext string
84
+ doc.to_s
85
+ end
86
+ end
87
+ end
88
+
89
+ Liquid::Template.register_filter(Jekyll::AmpFilter)
@@ -0,0 +1,40 @@
1
+ module Jekyll
2
+ # Defines the base class of AMP posts
3
+ class AmpPost < Page
4
+ def initialize(site, base, dir, post)
5
+ @site = site
6
+ @base = base
7
+ @dir = dir
8
+ # Needed for posts with permalink
9
+ @url = dir
10
+ @name = 'index.html'
11
+ self.process(@name)
12
+ self.read_yaml(File.join(base, '_layouts'), 'amp.html')
13
+ self.content = post.content
14
+ self.data['body'] = (Liquid::Template.parse post.content).render site.site_payload
15
+
16
+ # Merge all data from post so that keys from self.data have higher priority
17
+ self.data = post.data.merge(self.data)
18
+
19
+ # Remove non needed keys from data
20
+ # Excerpt will cause an error if kept
21
+ self.data.delete('excerpt')
22
+ # Generating the page fails silently if page has a permalink and it is copied
23
+ # over to the AMP version
24
+ self.data.delete('permalink')
25
+
26
+ self.data['canonical_url'] = post.url
27
+ end
28
+ end
29
+ # Generates a new AMP post for each existing post
30
+ class AmpGenerator < Generator
31
+ priority :low
32
+ def generate(site)
33
+ dir = site.config['ampdir'] || 'amp'
34
+ site.posts.docs.each do |post|
35
+ next if post.data['skip_amp'] == true
36
+ site.pages << AmpPost.new(site, site.source, File.join(dir, post.id), post)
37
+ end
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elevenlabs-amp-jekyll
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Juuso Mikkonen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: fastimage
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.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.8.0
55
+ description: A Jekyll plugin to generate Accelerated Mobile Pages.
56
+ email:
57
+ - hello@juusomikkonen.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/amp-jekyll.rb
63
+ - lib/jekyll/amp_filter.rb
64
+ - lib/jekyll/amp_generate.rb
65
+ homepage: https://github.com/juusaw/amp-jekyll
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.5.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A Jekyll plugin to generate Accelerated Mobile Pages.
89
+ test_files: []