forked from pupil-labs/pupil-labs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
992 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
mixin webp-img(src, title, alt, picture_style, img_style) | ||
|
||
//- remove img src path suffix | ||
- var path_nosuffix = src.replace(/[.][a-z]+/,""); | ||
//- get absolute path from the src path and remove suffix | ||
- var absolute_path = (path.resolve("./contents"+src.match(/\/media.+/, ""))); | ||
- var absolute_nosuffix = absolute_path.replace(/[.][a-z]+/,""); | ||
|
||
//- sources is an array of [str : image_type/suffix, bool : file exists] | ||
//- The order of this array is important as it specifies the order we want sources within the picture element. | ||
- var sources = [["webp", false],["jpg", false],["png", false]]; | ||
|
||
//- Update sources array. | ||
//- For each source, check if the file type exists. bool true if exists | ||
- for (i in sources) { | ||
- var suffix = sources[i][0]; | ||
- sources[i][1] = fs.existsSync(absolute_nosuffix+"."+suffix); | ||
- }; | ||
|
||
//- Add all sources with valid file path (all sources that exist). | ||
- var filtered_sources = sources.filter(function(e,i,a){ | ||
- return e[1] === true; | ||
- }); | ||
|
||
//- filtered_sources = [] if no valid source | ||
- var no_sources, preferred_src_suffix; | ||
//- overwrite filtered_sources with new array with jpg only | ||
- if (filtered_sources.length > 0) { | ||
- no_sources = filtered_sources.every(function(e,i,a){ | ||
- return !e[1]; | ||
- }); | ||
- preferred_src_suffix = filtered_sources[0][0]; | ||
- } else { | ||
- console.log("no valid source ! "+src); | ||
- no_sources = true; | ||
- preferred_src_suffix = 'jpg'; | ||
- } | ||
|
||
//- This is a workaround and not a long term solution. | ||
//- This assumes that if there is no function argument specified for picture_style, | ||
//- then we assume that it is not a blog post then calculate img ratio from image dimensions instead | ||
- if (!picture_style && !no_sources){ | ||
- var img_size = imageSize("./contents" + src ); | ||
- var width = img_size.width; | ||
- var height = img_size.height; | ||
- } | ||
|
||
- picture_style = !picture_style ? "" : picture_style; | ||
- img_style = !img_style ? "" : img_style; | ||
- var ratio = picture_style.length > 0 ? picture_style : "padding-bottom: " + (height/width)*100 + "%;" ; | ||
- var img_style = img_style.length > 0 ? img_style : ""; | ||
- var placeholder_img = no_sources | ||
|
||
//- If there is no source img found then a placeholder img link is supplied as the path instead | ||
- if (no_sources){ | ||
- filtered_sources = [['jpg', true]]; | ||
- path_nosuffix = 'http://via.placeholder.com/400'; | ||
- ratio = "padding-bottom: 100%;"; | ||
- img_style = "width:initial;height:initial"; | ||
- } | ||
|
||
picture(style=ratio).ratio-box.lazy-img | ||
for i in filtered_sources | ||
- suffix = i[0] | ||
source(srcset="#{path_nosuffix}.#{suffix}" type="image/#{suffix}") | ||
|
||
//- add src path and use suffix for images that exists | ||
img(src="#{path_nosuffix}.#{preferred_src_suffix}" style=img_style).img-small--webp.lazyload | ||
img(data-src="#{path_nosuffix}.#{preferred_src_suffix}" style=img_style title="#{title}" alt="#{alt}").img-large--webp.lazyload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.