Skip to content
Snippets Groups Projects
Commit c1dbc12b authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Make lwip optional dependency

This allows it to run without compiler
(namely VS on Windows).

Image minification won't work when LWIP is
not available. App will warn you about it.

It will also generate lightbox for every image
even those which are under 512.

It also normalizes every path to path/to/file
from path\to\file.
parent 75fad533
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,6 @@
"chokidar": "^1.4.2",
"express": "^4.13.4",
"highlight.js": "^9.2.0",
"lwip": "0.0.8",
"marked": "^0.3.5",
"mkdirp": "^0.5.1",
"moment": "^2.11.2",
......@@ -30,5 +29,8 @@
"istanbul": "^0.4.2",
"mocha": "^2.3.4",
"unit.js": "^2.0.0"
},
"optionalDependencies": {
"lwip": "0.0.8"
}
}
var input = new (require('./input-hugo'));
var lwip = require('lwip');
var lwip;
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 doesn't help try to make npm install lwip work.");
}
var path = require('path');
var builddir = "build";
......@@ -21,40 +28,50 @@ var minify = function() {
})).then(function(fileList) {
var promises = [];
fileList.forEach((file) => {
file = file.split(path.sep).join('/');
var f = file.split(".");
var ext = f.splice(-1,1);
f = f.join(".");
f = f+"_thumbnail."+ext;
f = f.split(path.sep);
f = f.split('/');
f.splice(0,1,builddir);
f = f.join(path.sep).replace('articles','clanek');
f = f.join('/').replace('articles','clanek');
var minifyPromise = function(resolve, reject) {
lwip.open(file, function(err, image) {
if(err) throw err;
var w = image.width(), h = image.height();
if(w <= 512) {
file = file.replace("static"+path.sep,"").replace('articles','clanek');
resolve({
minified: false,
file: file,
thumb: file
});
} else {
image.batch()
.resize(512, Math.round(512*h/w))
.writeFile(f, function(err) {
if(err) throw err;
if(lwip) {
lwip.open(file, function(err, image) {
if(err) throw err;
var w = image.width(), h = image.height();
if(w <= 512) {
file = file.replace("static/","").replace('articles','clanek');
resolve({
minified: true,
file: file.replace("static"+path.sep,"").replace('articles','clanek'),
thumb: f.replace(builddir+path.sep,"")
minified: false,
file: file,
thumb: file
});
} else {
image.batch()
.resize(512, Math.round(512*h/w))
.writeFile(f, function(err) {
if(err) throw err;
resolve({
minified: true,
file: file.replace("static/","").replace('articles','clanek'),
thumb: f.replace(builddir+'/',"")
});
});
});
}
})
}
})
} else {
file = file.replace("static/","").replace('articles','clanek');
resolve({
minified: false,
file: file,
thumb: file
});
}
}
promises.push(new Promise(minifyPromise));
});
return Promise.all(promises);
......@@ -73,4 +90,3 @@ module.exports = function() {
.catch(function(err) {console.log(err.stack); cb();});
}
}
......@@ -3,6 +3,11 @@ var renderer = new marked.Renderer();
var toURL = require('./transformer-urlizetags.js').toURL;
var highlightjs = require('highlight.js');
var path = require('path');
var minificationOK = true;
try {require('lwip');}
catch(e) {
minificationOK = false;
}
var images = {};
......@@ -37,18 +42,23 @@ module.exports = function() {
list.images.forEach((img) => {
images[img.file] = img;
});
console.log(images);
cb();
};
tr.forEachPage = function(article, cb) {
article.origContent = article.content;
renderer.image = function(href, title, text) {
href = href.split(path.sep).join("/");
var rel = path.relative(process.cwd(),path.resolve(article.file,href));
rel = rel.split(path.sep).join("/");
var img = images[rel];
var full;
if(img) {
if(img.minified) {
full = href;
href = path.relative(article.file, img.thumb);
}else if(!minificationOK) {
full = href;
}
}
else if(process.argv[3] !== "final") {
......@@ -56,11 +66,14 @@ module.exports = function() {
href = "https://ok1kvk.cz"+href;
}
}
var out = '<img src="' + href + '" alt="' + text + '"';
if (title) {
out += ' title="' + title + '"';
}
if(!minificationOK) {
out += ' style="max-width:512px"';
}
out += '>';
if(full) {
var a = '<a href="'+full+'"';
......@@ -69,10 +82,10 @@ module.exports = function() {
a += '>';
out = a+out+'</a>';
}
return out;
};
article.content = marked(article.content, {
highlight: function (code) {
var h;
......@@ -87,4 +100,3 @@ module.exports = function() {
cb();
}
}
var path = require('path');
module.exports = function() {
var tr = this;
tr.fancyname = "transformer other";
tr.forEachPage = function(article, cb) {
var img = article.metadata.image;
article.file = article.file.split(path.sep).join("/");
if(img) {
if(!img.match(/^\//)) {
article.metadata.image = article.file+"/"+img;
......@@ -10,8 +12,7 @@ module.exports = function() {
article.metadata.image = img.replace("/","");
}
}
cb();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment