Cscott has uploaded a new change for review.

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


Change subject: Allow env/line/paragraph breaks in the middle of 'inline' 
decorations.
......................................................................

Allow env/line/paragraph breaks in the middle of 'inline' decorations.

We simply close the inline style, do the break, and then open it again.
This allows <br/> inside of <i>, for instance.

Change-Id: Iac9e3823b94f85ec959a9d0b6cfeacda830318a3
---
M lib/index.js
1 file changed, 19 insertions(+), 17 deletions(-)


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

diff --git a/lib/index.js b/lib/index.js
index 4e71b0e..85e7218 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -150,7 +150,7 @@
        this.options = options;
        this.buffer = [];
        this.decorations = [];
-       this.inline = 0;
+       this.stack = []; // active inline decorations
        this.pos = 0;
        this.newEnv = this.newLine = this.newPara = true;
        this.dir = 'ltr'; // XXX
@@ -158,6 +158,7 @@
 Formatter.prototype.flush = function() {
        var deferred = when.defer();
        this.envBreak();
+       console.assert(this.stack.length === 0); // all inline styles closed
        this.stream.write('', 'utf8', function() {
                deferred.resolve();
        });
@@ -174,7 +175,6 @@
                paraLevel: (this.dir==='ltr') ? ubidi.DEFAULT_LTR : 
ubidi.DEFAULT_RTL
        });
 
-       var stack = [];
        var emitDecoration = function(d, opts) {
                switch (d.type) {
                case 'start-inline':
@@ -182,15 +182,15 @@
                        var isStart = /^start-/.test(d.type) ^ (!!opts.invert);
                        if (isStart) {
                                if (opts.updateStack) {
-                                       stack.push(d);
+                                       this.stack.push(d);
                                }
                                this._writeRaw(d.value);
                                this._writeRaw(d.delimiter || '{');
                        } else {
                                if (opts.updateStack) {
-                                       console.assert(stack.length, 
this.decorations);
-                                       
console.assert(stack[stack.length-1].value === d.value);
-                                       stack.pop();
+                                       console.assert(this.stack.length, 
this.decorations);
+                                       
console.assert(this.stack[this.stack.length-1].value === d.value);
+                                       this.stack.pop();
                                }
                                this._writeRaw(d.delimiter || '}');
                        }
@@ -208,7 +208,7 @@
        for (i=j=pos=0; i < runs; i++) {
                var run = p.getVisualRun(i);
                // open any decorations on stack
-               stack.forEach(function(d) { emitDecoration(d, { invert: false 
}); });
+               this.stack.forEach(function(d){ emitDecoration(d, { invert: 
false });});
                // XXX FIX ME XXX do something with run.dir
                var runEnd = run.logicalStart + run.length;
                for (;; j++) {
@@ -226,14 +226,20 @@
                this._writeRaw(texEscape(text.slice(pos, runEnd)));
                pos = runEnd;
                // close any decorations on the stack
-               stack.forEach(function(d) { emitDecoration(d, { invert: true 
}); });
+               this.stack.forEach(function(d) { emitDecoration(d, { invert: 
true }); });
        }
-       // emit decorations at end
-       for (; j<this.decorations.length-1; j++) {
-               d = this.decorations[j];
-               emitDecoration(d, { updateStack: true });
+       // emit decorations at end (not including sentinel)
+       if (j < (this.decorations.length-1)) {
+               // open any decorations on stack
+               this.stack.forEach(function(d){ emitDecoration(d, { invert: 
false });});
+               // emit trailing decorations
+               for (; j < (this.decorations.length-1); j++) {
+                       d = this.decorations[j];
+                       emitDecoration(d, { updateStack: true });
+               }
+               // close any decorations on the stack
+               this.stack.forEach(function(d) { emitDecoration(d, { invert: 
true }); });
        }
-       console.assert(stack.length===0, this.decorations, text);
        // done; clear all the buffers
        this.buffer.length = this.decorations.length = this.pos = 0;
 };
@@ -248,14 +254,12 @@
 };
 Formatter.prototype.paragraphBreak = function() {
        if (this.newPara) { return; }
-       if (this.inline) { return this.write(' '); }
        this.envBreak();
        this._writeRaw('\n');
        this.newPara = this.newLine = true;
 };
 Formatter.prototype.lineBreak = function() {
        if (this.newLine) { return; }
-       if (this.inline) { return this.write(' '); }
        this.envBreak();
        this._writeRaw('\\\\\n');
        this.newLine = true;
@@ -264,8 +268,6 @@
        d.pos = this.pos;
        this.decorations.push(d);
        this.newEnv = this.newLine = this.newPara = false;
-       if (d.type==='start-inline') { this.inline++; }
-       else if (d.type==='end-inline') { this.inline--; }
 };
 Formatter.prototype.write = function(text) {
        if (this.newEnv || this.newLine || this.newPara) {

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

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