diff --git a/sitegin/image.js b/sitegin/image.js index 776cf8e61f52e109cbbb8fd3e5ea9e1160b8579d..7a5a3638feb30e31b440f71d1548f3da092d87fa 100644 --- a/sitegin/image.js +++ b/sitegin/image.js @@ -2,6 +2,13 @@ var youtube = require('./utils/youtube.js'); 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 sepIndex = href.indexOf(' ='); @@ -42,7 +49,7 @@ var renderYoutube = function(href, options) { return out; } -module.exports = function(href, title, text, curFilename) { +module.exports = function(href, title, text, curFile, cfg) { var parsed = paramInHrefParser(href); href = parsed.href; var options = parsed.options; @@ -66,28 +73,51 @@ module.exports = function(href, title, text, curFilename) { if(youtube.isVideo(href)) { return renderYoutube(href, options); } 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 + '"'; - if(options.width) { - out += ' width="' + options.width + '"'; - } - if(options.height) { - out += ' height="' + options.height + '"'; - } - if (title) { - out += ' title="' + title + '"'; + var out = '<img src="' + rel + '" alt="' + text + '"'; + if(options.width) { + out += ' width="' + options.width + '"'; + } + if(options.height) { + out += ' height="' + options.height + '"'; + } + if (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.nolightbox) { // if lightbox - var a = '<a href="'+href+'"'; - a += ' data-lightbox="group"'; - a += ' data-title="' + text + '"'; - a += '>'; - out = a+out+'</a>'; + if(!options.size) options.size = []; + var w = options.size[0], + h = options.size[1]; + if(w === '') w = undefined; else w = Number(w); + if(h === '') h = undefined; else h = Number(h); + if(w === undefined && h === undefined) w = 512; + var file = generateImage(absSrc, targetDir, w, h); + return genHTML(file); } - return out; } } diff --git a/sitegin/imageResizer.js b/sitegin/imageResizer.js new file mode 100644 index 0000000000000000000000000000000000000000..204325fba229a287f77385ec49a9d34057f50f3b --- /dev/null +++ b/sitegin/imageResizer.js @@ -0,0 +1,54 @@ +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; + } +} diff --git a/sitegin/markdown.js b/sitegin/markdown.js index e6482e157ce8629375388f44bc2dd1493763c5eb..e7fe5ecc6eedf8a60546e2a1f158080d537705b3 100644 --- a/sitegin/markdown.js +++ b/sitegin/markdown.js @@ -40,8 +40,8 @@ var toURL = function(url) { return jobs.run('toURL', url); } -var renderImage = function(href, title, text, curFilename) { - return jobs.run('image', href, title, text, curFilename); +var renderImage = function(href, title, text, curFilename, cfg) { + return jobs.run('image', href, title, text, curFilename, cfg); } renderer.heading = function(text, level, raw) { @@ -81,7 +81,7 @@ module.exports = function(obj) { obj.pages.forEach(function(article) { 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; article.content = parser.parse(lexer.lex(article.content)); diff --git a/sitegin/pipeline.js b/sitegin/pipeline.js index 1a16514831ac92d72e6b60155c16ffad433f7165..26b791b337099c058e1f126457c7d6e7c47bfca7 100644 --- a/sitegin/pipeline.js +++ b/sitegin/pipeline.js @@ -10,9 +10,9 @@ module.exports = function(jobs) { 'parseHugo', 'parseRedirects', 'gitInfo', + 'markdown', 'urls', 'sitemap', - 'markdown', 'tags', 'theme', 'writeFiles', diff --git a/sitegin/sitegin.js b/sitegin/sitegin.js index 9ab6fbc1edde413f41925b33fb4904cb4e454fb7..e23fae145e4e6558e7c69aa71570656c548c6655 100644 --- a/sitegin/sitegin.js +++ b/sitegin/sitegin.js @@ -19,6 +19,7 @@ module.exports = function(config) { ['sitemap', './sitemap'], ['parseRedirects','./parseRedirects'], ['image', './image'], + ['imageResizer', './imageResizer'], ['pipeline','./pipeline'] ) diff --git a/sitegin/urls.js b/sitegin/urls.js index e4263f0815c701abe5980ea0e46dc22a9fb28716..5704539841d90ae7c5945e815e58cc2fb7a737fd 100644 --- a/sitegin/urls.js +++ b/sitegin/urls.js @@ -2,15 +2,20 @@ * 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) { console.log('Build step: URLs'); return new Promise(function(resolve, reject) { obj.pages.forEach(function(page) { - var file = page.file; - file = file.replace(/\.md$/,''); - file = file.replace(/^articles/,'clanek'); - page.file = file; + page.file = rewriteURL(page.file); }) resolve(obj) }); } + +module.exports.rewriteURL = rewriteURL;