Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update paper 2.x branch to pull in latest helpers #204

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
sudo: false
node_js:
- 6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this as we no longer use 6.

- 8
- 10
script:
Expand Down
14 changes: 14 additions & 0 deletions helpers/getContentImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const { getObjectStorageImage } = require('../lib/getObjectStorageImage')

function helper(paper) {
paper.handlebars.registerHelper('getContentImage', function(path) {
const cdnUrl = paper.settings['cdn_url'] || '';

const options = arguments[arguments.length - 1];

return getObjectStorageImage(cdnUrl, 'content', path, options);
});
}

module.exports = helper;
12 changes: 12 additions & 0 deletions helpers/getContentImageSrcset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const { getObjectStorageImageSrcset } = require('../lib/getObjectStorageImage')

function helper(paper) {
paper.handlebars.registerHelper('getContentImageSrcset', function(path) {
const cdnUrl = paper.settings['cdn_url'] || '';

return getObjectStorageImageSrcset(cdnUrl, 'content', path);
});
}

module.exports = helper;
14 changes: 14 additions & 0 deletions helpers/getImageManagerImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const { getObjectStorageImage } = require('../lib/getObjectStorageImage')

function helper(paper) {
paper.handlebars.registerHelper('getImageManagerImage', function(path) {
const cdnUrl = paper.settings['cdn_url'] || '';

const options = arguments[arguments.length - 1];

return getObjectStorageImage(cdnUrl, 'image-manager', path, options);
});
}

module.exports = helper;
12 changes: 12 additions & 0 deletions helpers/getImageManagerImageSrcset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const { getObjectStorageImageSrcset } = require('../lib/getObjectStorageImage')

function helper(paper) {
paper.handlebars.registerHelper('getImageManagerImageSrcset', function(path) {
const cdnUrl = paper.settings['cdn_url'] || '';

return getObjectStorageImageSrcset(cdnUrl, 'image-manager', path);
});
}

module.exports = helper;
84 changes: 84 additions & 0 deletions helpers/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';
const utils = require('handlebars-utils');
const max_length = 1024;
const max_keys = 50;

function helper(paper) {
paper.handlebars.registerHelper('getVar', function (key) {
if (!utils.isString(key)) {
throw new Error("getVar helper key must be a string");
}

return paper.variables[key]
});

paper.handlebars.registerHelper('assignVar', function (key, value) {
// Validate that key is a string
if (!utils.isString(key)) {
throw new Error("assignVar helper key must be a string");
}

// Validate that value is a string or integer
if (!utils.isString(value) && !Number.isInteger(value)) {
throw new Error("assignVar helper value must be a string or a number (integer)");
}

// Validate that string is not longer than the max length
if (utils.isString(value) && value.length >= max_length) {
throw new Error(`assignVar helper value must be less than ${max_length} characters,
but a ${value.length} character value was set to ${key}`);
}

// Make sure the number of total keys is within the limit
if (Object.keys(paper.variables).length >= max_keys) {
throw new Error(`Unique keys in variable storage may not exceed ${max_keys} in total`);
}

// Store value for later use by getVar helper
paper.variables[key] = value;
});

paper.handlebars.registerHelper('incrementVar', function (key) {
if (!utils.isString(key)) {
throw new Error("incrementVar helper key must be a string");
}

if (Number.isInteger(paper.variables[key])) {
// Increment value if it already exists
paper.variables[key] += 1;
} else {
// Make sure the number of total keys is within the limit
if (Object.keys(paper.variables).length >= max_keys) {
throw new Error(`Unique keys in variable storage may not exceed ${max_keys} in total`);
}
// Initialize or re-initialize value
paper.variables[key] = 0;
}

// Return current value
return paper.variables[key];
});

paper.handlebars.registerHelper('decrementVar', function (key) {
if (!utils.isString(key)) {
throw new Error("decrementVar helper key must be a string");
}

if (Number.isInteger(paper.variables[key])) {
// Decrement value if it already exists
paper.variables[key] -= 1;
} else {
// Make sure the number of total keys is within the limit
if (Object.keys(paper.variables).length >= max_keys) {
throw new Error(`Unique keys in variable storage may not exceed ${max_keys} in total`);
}
// Initialize or re-initialize value
paper.variables[key] = 0;
}

// Return current value
return paper.variables[key];
});
}

module.exports = helper;
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Paper {
this.handlebars.templates = {};
this.translator = null;
this.inject = {};
this.variables = {};
this.decorators = [];

this.settings = settings || {};
Expand Down Expand Up @@ -242,7 +243,7 @@ class Paper {
if (path[0] !== '/') {
path = '/' + path;
}

return path;
}

Expand Down Expand Up @@ -346,4 +347,4 @@ class Paper {

}

module.exports = Paper;
module.exports = Paper;
51 changes: 51 additions & 0 deletions lib/getObjectStorageImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const utils = require('handlebars-utils');
const common = require('./common');
const SafeString = require('handlebars').SafeString;

const srcsets = {
'80w': '80w',
'160w': '160w',
'320w': '320w',
'640w': '640w',
'960w': '960w',
'1280w': '1280w',
'1920w': '1920w',
'2560w': '2560w',
};

function getObjectStorageImage(cdnUrl, source, path, options) {
if (!utils.isString (path) || common.isValidURL(path)) {
throw new TypeError("Invalid image path - please use a filename or folder path starting from the appropriate folder");
}

// Return original image if there are no arguments
let size = 'original';

if (Number.isInteger(options.hash['width'])) {
if (Number.isInteger(options.hash['height'])) {
// Return image of specified size
size = `${options.hash['width']}x${options.hash['height']}`
} else {
// Return image of specified width
size = `${options.hash['width']}w`
}
}

return new SafeString(`${cdnUrl}/images/stencil/${size}/${source}/${path}`);
}

function getObjectStorageImageSrcset(cdnUrl, source, path) {
if (!utils.isString (path) || common.isValidURL(path)) {
throw new TypeError("Invalid image path - please use a filename or folder path starting from the appropriate folder");
}

return new SafeString(Object.keys(srcsets).map(descriptor => {
return ([`${cdnUrl}/images/stencil/${srcsets[descriptor]}/${source}/${path} ${descriptor}`].join(' '));
}).join(', '));
}

module.exports = {
getObjectStorageImage,
getObjectStorageImageSrcset
};
Loading