Cscott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/96514
Change subject: Rename image files to have TeX-safe filenames.
......................................................................
Rename image files to have TeX-safe filenames.
Change-Id: I375167a04b74ee5956cbe5a6c5af49a2bff6717a
---
M lib/index.js
1 file changed, 37 insertions(+), 2 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection/OfflineContentGenerator/latex_renderer
refs/changes/14/96514/1
diff --git a/lib/index.js b/lib/index.js
index 04e5a51..bb7c2fb 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -5,6 +5,7 @@
var domino = require('domino');
var fs = require('fs');
var gammalatex = require('gammalatex');
+var guard = require('when/guard');
var nodefn = require('when/node/function');
var path = require('path');
var spawn = require('child_process').spawn;
@@ -494,6 +495,32 @@
});
};
+// return a promise to have renamed a file. uses 'guard' to ensure that
+// renames aren't executed in parallel (and thus we can ensure that
+// filenames are unique without tying ourself in knots). Returns the
+// new name (which might differ from the basename given)
+var renameFile = guard(guard.n(1), function(dir, oldname, newbase) {
+ var deferred = when.defer(); // stores the safe uniq-ified name
+ pcall(fs.exists, fs, path.join(dir, newbase)).then(function(exists) {
+ if (!exists) {
+ return deferred.resolve(path.join(dir, newbase));
+ }
+ // use the tmp module to come up with a unique alternative
+ return pcall(tmp.tmpName, tmp, {
+ dir: dir,
+ prefix: '',
+ postfix: newbase
+ }).then(function(name) {
+ deferred.resolve(name);
+ });
+ }).done();
+ return deferred.promise.then(function(uniqname) {
+ // rename the file, then return the new filename
+ return pcall(fs.rename, fs, path.join(dir, oldname), uniqname).
+ then(function() { return uniqname; });
+ });
+});
+
// return a promise for a map from file resource URLs to on-disk filenames
// (after image processing / renaming has been done)
var processImages = function(metabook, builddir, options) {
@@ -510,9 +537,17 @@
var filename = val.filename;
if (!filename) { return; }
// XXX convert gifs to pngs, and svgs to pngs
- imagemap.set(key, path.join(builddir, 'images', filename));
+ // rename file if it is not TeX safe.
+ var pp = when.resolve().then(function() {
+ var safe = filename.replace(/[^A-Za-z0-9.:]+/g, '-');
+ return (safe === filename) ? path.join(builddir,
'images', safe) :
+ renameFile(path.join(builddir, 'images'),
filename, safe);
+ }).then(function(newname) {
+ imagemap.set(key, newname);
+ });
+ p = p.then(function() { return pp; }); // serialize completion
}).then(function() {
- // do the queued image conversions
+ // do the queued image renames/conversions/etc.
return p;
}).then(function() {
// return the promised imagemap
--
To view, visit https://gerrit.wikimedia.org/r/96514
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I375167a04b74ee5956cbe5a6c5af49a2bff6717a
Gerrit-PatchSet: 1
Gerrit-Project:
mediawiki/extensions/Collection/OfflineContentGenerator/latex_renderer
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits