amp-jekyll 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 689eb915e450435f5e5d471dfe38d57f5253d67a
4
+ data.tar.gz: 3755eb727d3645a0e3d61214ac18c2a50e6a908a
5
+ SHA512:
6
+ metadata.gz: b486fba67bf0fca3e5ff677af813f822650ff66f9bd1189e1a6482bb8c8d84f4d5140c7bbac420e3651ce62f71548f8c02489f5768c5f3ca66ee244fd58c6122
7
+ data.tar.gz: fdaf1a1c8f04dddb9f5d5426ba8f1ed8c2f1143c0300c4fa12b5e132244c38a248eaddf711f52a5038c58c0ec8ddfa211fba647c9ba13e5892e2e9c250b8adf4
@@ -0,0 +1,2 @@
1
+ require 'amp_generate'
2
+ require '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, '_site', 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,35 @@
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
+ @name = 'index.html'
9
+ self.process(@name)
10
+ self.read_yaml(File.join(base, '_layouts'), 'amp.html')
11
+ self.content = post.content
12
+ self.data['body'] = (Liquid::Template.parse post.content).render site.site_payload
13
+
14
+ # Merge all data from post so that keys from self.data have higher priority
15
+ self.data = post.data.merge(self.data)
16
+
17
+ # Remove non needed keys from data
18
+ # Excerpt will cause an error if kept
19
+ self.data.delete('excerpt')
20
+
21
+ self.data['canonical_url'] = post.url
22
+ end
23
+ end
24
+ # Generates a new AMP post for each existing post
25
+ class AmpGenerator < Generator
26
+ priority :low
27
+ def generate(site)
28
+ dir = site.config['ampdir'] || 'amp'
29
+ site.posts.docs.each do |post|
30
+ next if post.data['skip_amp'] == true
31
+ site.pages << AmpPost.new(site, site.source, File.join(dir, post.id), post)
32
+ end
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amp-jekyll
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juuso Mikkonen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-30 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/amp_filter.rb
64
+ - lib/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.4.5.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A Jekyll plugin to generate Accelerated Mobile Pages.
89
+ test_files: []