Commit 2c73fbe4 authored by Isabella Skořepová's avatar Isabella Skořepová

Implement image minification

TODO: Implement caching mechanism to speed up the process
parent a9d4c6de
Pipeline #227 passed with stages
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
var youtube = require('./utils/youtube.js'); var youtube = require('./utils/youtube.js');
var path = require('path'); var path = require('path');
var url = require('url');
var jobs = require('./jobs');
var rewriteURL = require('./urls').rewriteURL;
var generateImage = function(file, targetDir, width, height) {
return jobs.run('imageResizer', file, targetDir, width, height);
}
var paramInHrefParser = function(href) { var paramInHrefParser = function(href) {
var sepIndex = href.indexOf(' ='); var sepIndex = href.indexOf(' =');
...@@ -42,7 +49,7 @@ var renderYoutube = function(href, options) { ...@@ -42,7 +49,7 @@ var renderYoutube = function(href, options) {
return out; return out;
} }
module.exports = function(href, title, text, curFilename) { module.exports = function(href, title, text, curFile, cfg) {
var parsed = paramInHrefParser(href); var parsed = paramInHrefParser(href);
href = parsed.href; href = parsed.href;
var options = parsed.options; var options = parsed.options;
...@@ -66,28 +73,51 @@ module.exports = function(href, title, text, curFilename) { ...@@ -66,28 +73,51 @@ module.exports = function(href, title, text, curFilename) {
if(youtube.isVideo(href)) { if(youtube.isVideo(href)) {
return renderYoutube(href, options); return renderYoutube(href, options);
} else { } else {
var rel = path.relative(process.cwd(),path.resolve(curFilename,href)); var genHTML = function(file) {
var rel = path.relative(process.cwd(),file);
var out = '<img src="' + href + '" alt="' + text + '"'; var out = '<img src="' + rel + '" alt="' + text + '"';
if(options.width) { if(options.width) {
out += ' width="' + options.width + '"'; out += ' width="' + options.width + '"';
} }
if(options.height) { if(options.height) {
out += ' height="' + options.height + '"'; out += ' height="' + options.height + '"';
} }
if (title) { if (title) {
out += ' title="' + title + '"'; out += ' title="' + title + '"';
}
out += '>';
if(!options.nolightbox) { // if lightbox
var a = '<a href="'+href+'"';
a += ' data-lightbox="group"';
a += ' data-title="' + text + '"';
a += '>';
out = a+out+'</a>';
}
return out;
} }
var parsedHref = url.parse(href);
if(parsedHref.host) {
genHTML(href);
} else if(path.isAbsolute(href)) {
genHTML(href);
} else {
curFile = curFile.replace(/\.md$/,'');
var targetDir = rewriteURL(curFile);
targetDir = path.resolve(cfg.builddir, targetDir);
var absSrc = path.resolve(cfg.staticDir, curFile, href);
out += '>'; if(!options.size) options.size = [];
if(!options.nolightbox) { // if lightbox var w = options.size[0],
var a = '<a href="'+href+'"'; h = options.size[1];
a += ' data-lightbox="group"'; if(w === '') w = undefined; else w = Number(w);
a += ' data-title="' + text + '"'; if(h === '') h = undefined; else h = Number(h);
a += '>'; if(w === undefined && h === undefined) w = 512;
out = a+out+'</a>'; var file = generateImage(absSrc, targetDir, w, h);
return genHTML(file);
} }
return out;
} }
} }
......
var lwip;
var path = require('path');
try {lwip = require('lwip');}
catch(e) {
lwip = null
console.log('WARNING! LWIP is not installed - image minification won\'t work.');
console.log('You can try to run npm install.');
console.log('If it does not help try to make npm install lwip work.');
}
module.exports = function(file, targetDir, width, height) {
if(lwip && (width || height)) {
var tmp = path.basename(file).split('.');
tmp.splice(-1,1);
var base = tmp.join('.');
var targetFile = base+'_thumb_';
if(width) targetFile += width;
targetFile += 'x';
if(height) targetFile += height;
targetFile += '.png';
var ret = targetFile;
targetFile = path.resolve(targetDir,targetFile);
lwip.open(file, function(err, image) {
function doResize() {
image.batch()
.resize(width, height)
.writeFile(targetFile, function(err) {
if(err) console.log(err);
});
}
if(err) return console.log(err);
var w = image.width(), h = image.height();
if(width && height) {
doResize();
} else if(height) {
if(h > height) {
width = Math.round(height*w/h);
doResize();
}
} else if(width) {
if(w > width) {
height = Math.round(width*h/w);
doResize();
}
} else {
console.log(new Error('Panic! Code should never get here!'));
}
})
return ret;
} else {
return file;
}
}
...@@ -40,8 +40,8 @@ var toURL = function(url) { ...@@ -40,8 +40,8 @@ var toURL = function(url) {
return jobs.run('toURL', url); return jobs.run('toURL', url);
} }
var renderImage = function(href, title, text, curFilename) { var renderImage = function(href, title, text, curFilename, cfg) {
return jobs.run('image', href, title, text, curFilename); return jobs.run('image', href, title, text, curFilename, cfg);
} }
renderer.heading = function(text, level, raw) { renderer.heading = function(text, level, raw) {
...@@ -81,7 +81,7 @@ module.exports = function(obj) { ...@@ -81,7 +81,7 @@ module.exports = function(obj) {
obj.pages.forEach(function(article) { obj.pages.forEach(function(article) {
renderer.image = function(href, title, text) { renderer.image = function(href, title, text) {
return renderImage(href, title, text, article.filename); return renderImage(href, title, text, article.file, obj.config);
}; };
lang = article.metadata.lang; lang = article.metadata.lang;
article.content = parser.parse(lexer.lex(article.content)); article.content = parser.parse(lexer.lex(article.content));
......
...@@ -10,9 +10,9 @@ module.exports = function(jobs) { ...@@ -10,9 +10,9 @@ module.exports = function(jobs) {
'parseHugo', 'parseHugo',
'parseRedirects', 'parseRedirects',
'gitInfo', 'gitInfo',
'markdown',
'urls', 'urls',
'sitemap', 'sitemap',
'markdown',
'tags', 'tags',
'theme', 'theme',
'writeFiles', 'writeFiles',
......
...@@ -19,6 +19,7 @@ module.exports = function(config) { ...@@ -19,6 +19,7 @@ module.exports = function(config) {
['sitemap', './sitemap'], ['sitemap', './sitemap'],
['parseRedirects','./parseRedirects'], ['parseRedirects','./parseRedirects'],
['image', './image'], ['image', './image'],
['imageResizer', './imageResizer'],
['pipeline','./pipeline'] ['pipeline','./pipeline']
) )
......
...@@ -2,15 +2,20 @@ ...@@ -2,15 +2,20 @@
* This jobs translates files to resulting URL * This jobs translates files to resulting URL
*/ */
var rewriteURL = function(file) {
file = file.replace(/\.md$/,'');
file = file.replace(/^articles/,'clanek');
return file;
}
module.exports = function(obj) { module.exports = function(obj) {
console.log('Build step: URLs'); console.log('Build step: URLs');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
obj.pages.forEach(function(page) { obj.pages.forEach(function(page) {
var file = page.file; page.file = rewriteURL(page.file);
file = file.replace(/\.md$/,'');
file = file.replace(/^articles/,'clanek');
page.file = file;
}) })
resolve(obj) resolve(obj)
}); });
} }
module.exports.rewriteURL = rewriteURL;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment