Cscott has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/96495


Change subject: Re-add image support.
......................................................................

Re-add image support.

Change-Id: Iaf6709a7b8604f884506df02d5be0eb1d6937711
---
M lib/db.js
M lib/index.js
M package.json
3 files changed, 61 insertions(+), 9 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection/OfflineContentGenerator/latex_renderer
 refs/changes/95/96495/1

diff --git a/lib/db.js b/lib/db.js
index 0238c58..fc044b8 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -36,6 +36,18 @@
        });
 };
 
+// Returns a promise which will be resolved (with the number of keys)
+// when the iteration is complete
+Db.prototype.forEach = function(f, nojson) {
+       var each = function(err, row) {
+               var val = nojson ? row.val : JSON.parse(row.val);
+               f(row.key, val);
+       };
+       return this.db.then(function(db) {
+               return nodefn.call(db.each.bind(db), "SELECT * FROM kv_table;", 
each);
+       });
+};
+
 // Returns a promise to write a value.
 Db.prototype.put = function(key, value) {
        if (typeof(value) !== 'string') { value = JSON.stringify(value); }
diff --git a/lib/index.js b/lib/index.js
index a30a6b8..c20a009 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,3 +1,5 @@
+require('es6-shim');
+
 var json = require('../package.json');
 
 var domino = require('domino');
@@ -379,7 +381,7 @@
                resource, filename;
        if (!img) { return; /* bail */ }
        resource = url.resolve(this.base, img.getAttribute('resource'));
-       filename = (this.options.imagemap || {})[resource];
+       filename = this.options.imagemap.get(resource);
        if (!filename) {
                // couldn't download this image.
                console.error('Skipping', resource);
@@ -468,12 +470,35 @@
                                metabook = JSON.parse(data);
                        });
        }).then(function() {
-               // XXX process images?
                return { metabook: metabook, builddir: builddir };
        });
 };
 
-var generateLatex = function(metabook, builddir, options) {
+var processImages = function(metabook, builddir, options) {
+       options.log('Processing images');
+       var imagemap = new Map();
+       var imagedb =
+               new Db(path.join(builddir, 'imageinfo.db'), { readonly: true });
+       var p = when.resolve();
+       return imagedb.forEach(function(key, val) {
+               if (!/^https?:\/\//.test(key)) {
+                       // compatibility with pediapress format
+                       key = val.resource;
+               }
+               var filename = val.filename;
+               if (!filename) { return; }
+               // XXX convert gifs to pngs, and svgs to pngs
+               imagemap.set(key, path.join(builddir, 'images', filename));
+       }).then(function() {
+               // do the queued image conversions
+               return p;
+       }).then(function() {
+               // return the promised imagemap
+               return { imagemap: imagemap };
+       });
+};
+
+var generateLatex = function(metabook, builddir, imagemap, options) {
        var output = fs.createWriteStream(path.join(builddir, 'output.tex'), {
                encoding: 'utf8'
        });
@@ -487,14 +512,22 @@
        var item = metabook.items[0];
        var revid = item.revision;
        var pdb = new Db(path.join(builddir, 'parsoid.db'), { readonly: true });
-       var document;
+       var document, base = '';
        p = p.then(function() {
                return pdb.get(revid, 'nojson');
        }).then(function(data) {
                document = domino.createDocument(data);
+               var baseElem = document.querySelector('head > base[href]');
+               if (baseElem) {
+                       base = baseElem.getAttribute('href').replace(/^\/\//, 
'https://');
+               }
        });
        p = p.then(function() {
-               var visitor = new Visitor(document, { toc: options.toc });
+               var visitor = new Visitor(document, {
+                       base: base,
+                       toc: options.toc,
+                       imagemap: imagemap
+               });
                visitor.visit(document.body);
                var result = visitor.output.join('\n');
                return pcall(output.write, output, result);
@@ -549,15 +582,21 @@
 };
 
 var convert = function(options) {
-       var metabook, builddir;
+       var metabook, builddir, imagemap;
        return when.resolve().then(function() {
                // unpack the bundle
                return unpackBundle(options);
        }).then(function(args) {
                metabook = args.metabook;
                builddir = args.builddir;
+       }).then(function() {
+               // process images
+               return processImages(metabook, builddir, options);
+       }).then(function(args) {
+               imagemap = args.imagemap;
+       }).then(function() {
                // generate the latex
-               return generateLatex(metabook, builddir, options);
+               return generateLatex(metabook, builddir, imagemap, options);
        }).then(function() {
                // compile it to PDF
                return compileLatex(builddir, options);
diff --git a/package.json b/package.json
index a653632..f1dc976 100644
--- a/package.json
+++ b/package.json
@@ -20,11 +20,12 @@
     "commander": "~2.0.0",
     "domino": "~1.0.13",
     "easyimage": "~0.1.3",
+    "es6-shim": "~0.9.1",
     "gammalatex": 
"git+https://github.com/gammasoft/latex#66a075a82e3b1473404f652ea956a8e4841f3c66";,
     "request": "~2.27.0",
+    "sqlite3": "~2.1.19",
     "tmp": "~0.0.21",
-    "when": "~2.6.0",
-    "sqlite3": "~2.1.19"
+    "when": "~2.6.0"
   },
   "devDependencies": {
     "mocha": "~1.14.0"

-- 
To view, visit https://gerrit.wikimedia.org/r/96495
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaf6709a7b8604f884506df02d5be0eb1d6937711
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

Reply via email to