Skip to content

Commit

Permalink
Improve handling of ratios in ifHasAspectRatio
Browse files Browse the repository at this point in the history
  • Loading branch information
fboes committed Oct 16, 2018
1 parent 2edafe9 commit 4fd7db6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A live example of this blog generator can be found at [3960! Journal](http://jou

* The Blogophon generates static HTML files from [Markdown articles](docs/markdown.md). These HTML files can be synchronized to remote servers and are fully independent of the Blogophon generator.
* It uses simple, fast and [hackable templating](docs/development.md) via Mustache. It also supports multiple themes.
* Generates a bunch of way to find your articles: Regular index pages, tag pages, author pages.
* Generates a bunch of ways to find your articles: Regular index pages, tag pages, author pages.
* The Blogophon comes with a built-in image-scaler for responsive images.
* The default theme puts all relevant meta stuff into `<head>` for SEO and social sharing (via schema.org and OpenGraph) as well as the [IndieWeb](https://indieweb.org/).
* A ton of [special features](docs/special-features.md) like RSS/ATOM newsfeeds, Accelerated Mobile Pages (AMP), Progressive Web App (PWA), Facebook Instant Articles, sending Webmentions, etc.
Expand Down
8 changes: 8 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ Last but not least have a look at [`lib/helpers/blogophon-handlebars-quoters.js`
<!-- Test if first string matches against regular expression -->
{{#ifMatches tag '[Nn]ew'}}It's new!{{/ifMatches}}

{{#ifHasAspectRatio img.src 16 9}}
<img src="{{img.src}}" alt="" /><!-- has an aspect ratio of 16:9 -->
{{/ifHasAspectRatio}}

{{#ifHasMaxDimensions img.src 270 270}}
<img src="{{img.src}}" alt="" /><!-- is not bigger than 270×270 px -->
{{/ifHasMaxDimensions}}

<!-- Export template variables to Javascript (e.g. for Google Tag Manager) -->
<script>{{{dataLayer post.meta}}}</script>
```
Expand Down
44 changes: 36 additions & 8 deletions lib/helpers/blogophon-handlebars-quoters.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,25 @@ const blogophonHandlebarsQuoters = {

/**
* Check if a given image URL has dimension included (e.g. `image-123x456.jpg`)
* and if its aspect ratio matches the given aspect ratio
* and if its aspect ratio matches the given aspect ratio.
* If no dimensions are found, this will return `true` as well. See `ifHasDimensions`.
* @param {String} imageUrl [description]
* @param {Number} width [description]
* @param {Number} height [description]
* @param {Object} options [description]
* @return {Boolean} If aspect ratio matches
*/
ifHasAspectRatio: function(imageUrl, width, height, options) {
width = Number(width);
height = Number(height);
const dimensions = blogophonHandlebarsQuoters._getDimensions(imageUrl);
const imageUrlRatio = dimensions.height ? (dimensions.width / dimensions.height) : 0;
return (imageUrlRatio === 0 || imageUrlRatio === (width / height)) ? options.fn(this) : options.inverse(this);
const dimensions = blogophonHandlebarsQuoters._getDimensions(imageUrl);
const imageUrlRatio = blogophonHandlebarsQuoters._getRatio(dimensions.width, dimensions.height);
const givenRatio = blogophonHandlebarsQuoters._getRatio(width, height);
return (imageUrlRatio === 0 || imageUrlRatio === givenRatio) ? options.fn(this) : options.inverse(this);
},

/**
* Check if a given image URL has dimension included (e.g. `image-123x456.jpg`)
* and if its dimensions are bigger than the given ratios
* and if its dimensions are bigger than the given ratios.
* If no dimensions are found, this will return `true` as well. See `ifHasDimensions`.
* @param {String} imageUrl [description]
* @param {Number} width [description]
* @param {Number} height [description]
Expand All @@ -188,7 +189,8 @@ const blogophonHandlebarsQuoters = {

/**
* Check if a given image URL has dimension included (e.g. `image-123x456.jpg`)
* and if its dimensions are smaller than the given ratios
* and if its dimensions are smaller than the given ratios.
* If no dimensions are found, this will return `true` as well. See `ifHasDimensions`.
* @param {String} imageUrl [description]
* @param {Number} width [description]
* @param {Number} height [description]
Expand All @@ -205,6 +207,20 @@ const blogophonHandlebarsQuoters = {
;
},

/**
* Check if a given image URL has dimension included (e.g. `image-123x456.jpg`)
* @param {String} imageUrl [description]
* @param {Object} options [description]
* @return {Boolean} if it has dimensions
*/
ifHasDimensions: function(imageUrl, options) {
const dimensions = blogophonHandlebarsQuoters._getDimensions(imageUrl);
return (dimensions.width && dimensions.height)
? options.fn(this)
: options.inverse(this)
;
},

/**
*
* @param {String} imageUrl [descriptuion]
Expand All @@ -216,6 +232,18 @@ const blogophonHandlebarsQuoters = {
width: dimensions ? Number(dimensions[1]) : 0,
height: dimensions ? Number(dimensions[2]) : 0
};
},

/**
* Return ratio of width/height
* @param {Number} width [description]
* @param {Number} height [description]
* @return {Number} Ratio
*/
_getRatio: function(width, height) {
width = Number(width);
height = Number(height);
return height ? Math.round(100 * width / height) / 100 : 0;
}
};

Expand Down
12 changes: 12 additions & 0 deletions test/blogophon-handlebars-quoters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,17 @@ describe('Blogophon Handlebars Quoters', function() {
assert.ok(tested);
assert.strictEqual(tested.width, 0);
assert.strictEqual(tested.height, 0);

tested = blogophonHandlebarsQuoter._getRatio(16, 9);
assert.strictEqual(tested, 1.78);

tested = blogophonHandlebarsQuoter._getRatio(1920, 1080);
assert.strictEqual(tested, 1.78);

tested = blogophonHandlebarsQuoter._getRatio(4, 3);
assert.strictEqual(tested, 1.33);

tested = blogophonHandlebarsQuoter._getRatio(640, 480);
assert.strictEqual(tested, 1.33);
});
});

0 comments on commit 4fd7db6

Please sign in to comment.