Cscott has uploaded a new change for review.

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


Change subject: Support explicit DIR attributes on HTML elements.
......................................................................

Support explicit DIR attributes on HTML elements.

Change-Id: I47a80f153de18242ded0553c73afc160f972285f
---
M lib/index.js
M lib/polyglossia.js
2 files changed, 68 insertions(+), 17 deletions(-)


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

diff --git a/lib/index.js b/lib/index.js
index 65783ec..8c55310 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -31,7 +31,7 @@
        "",
        "\\documentclass[10pt,twocolumn,twoside]{article}",
        "\\pagestyle{headings}",
-       "\\usepackage{fontspec, xunicode, polyglossia, graphicx}",
+       "\\usepackage{fontspec, xunicode, xltxtra, polyglossia, graphicx}",
        "\\usepackage{amsmath,amsthm,amstext,amssymb}",
        "\\usepackage[usenames]{xcolor}",
        "\\definecolor{linkcolor}{rgb}{.27,0,0}",
@@ -95,6 +95,12 @@
        return str;
 };
 
+// Predicate to determine whether the given element will be a
+// paragraph context in LaTeX.
+var isParagraph = function(node) {
+       return 
(/^(BLOCKQUOTE|BODY|CENTER|DIV|DL|FIGURE|H[1-6]|OL|P|TABLE|UL)$/).test(node.nodeName);
 // xxx others?
+};
+
 // Special predicate for some image templates used on enwiki
 // XXX restrict to enwiki content?
 var isMultipleImageTemplate = function(node) {
@@ -149,6 +155,7 @@
        this.templates = Object.create(null);
        this.base = options.base || '';
        this.currentLanguage = options.lang || 'en';
+       this.currentDirectionality = options.dir || 'ltr';
        this.usedLanguages = new Set();
 };
 
@@ -173,7 +180,8 @@
 // Generic node visitor.  Dispatches to specialized visitors based on
 // element typeof/rel attributes or tag name.
 Visitor.prototype.visit = function(node) {
-       var name = node.nodeName, type = node.nodeType;
+       var name = node.nodeName, type = node.nodeType, r;
+       var savedLanguage, savedDirectionality;
        switch(type) {
        case node.ELEMENT_NODE:
                if (isHidden(node)) {
@@ -184,13 +192,16 @@
                // in addition to eliminating no-ops, this condition allows us
                // to recursively invoke visit() inside the LANG handler.
                if (lang !== this.currentLanguage) {
-                       var savedLanguage = this.currentLanguage;
-                       this.currentLanguage = lang;
                        this.usedLanguages.add(lang);
-                       var r = this['visitLANG='].apply(this, arguments);
-                       this.currentLanguage = savedLanguage;
-                       return r;
+                       return this['visitLANG='].apply(this, arguments);
                }
+               // directionality should be set by language handling.  if it 
isn't...
+               var dir = node.getAttribute('dir') || 
this.currentDirectionality;
+               if (dir==='auto') { dir = this.currentDirectionality; /* hack 
*/ }
+               if (dir !== this.currentDirectionality) {
+                       return this['visitDIR='].apply(this, arguments);
+               }
+               // xxx look at lang and dir from css styling xxx
                // use typeof property if possible
                if (node.hasAttribute('typeof')) {
                        var typeo = node.getAttribute('typeof');
@@ -517,20 +528,48 @@
 };
 
 Visitor.prototype['visitLANG='] = function(node) {
+       var r;
+       var savedLanguage = this.currentLanguage;
+       var savedDirectionality = this.currentDirectionality;
+       var lang = node.getAttribute('lang');
+       var poly = Polyglossia(lang);
+       this.currentLanguage = lang;
+       this.currentDirectionality = poly.dir;
        // is this a block or a span context?
-       var isBlock = 
/^(BLOCKQUOTE|BODY|CENTER|DIV|DL|FIGURE|H[1-6]|OL|P|TABLE|UL)$/.test(node.nodeName);
 // XXX others?
-       var poly = Polyglossia(this.currentLanguage);
-       if (isBlock) {
+       if (isParagraph(node)) {
                this.output.push('\\begin{'+poly.env+'}['+poly.options+']%');
-               var r = this.visit(node);
+               r = this.visit(node);
                this.output.push('\\end{'+poly.env+'}%');
-               return r;
        } else {
-               return this.collect(node, function(contents) {
+               r = this.collect(node, function(contents) {
                        
this.output.push('\\text'+poly.lang+'['+poly.options+']' +
                                                         '{' + contents + '}%');
                });
        }
+       this.currentLanguage = savedLanguage;
+       this.currentDirectionality = savedDirectionality;
+       return r;
+};
+Visitor.prototype['visitDIR='] = function(node) {
+       var r;
+       var savedDirectionality = this.currentDirectionality;
+       var dir = node.getAttribute('dir');
+       console.warn("Using non-standard DIR", this.currentLanguage, 
this.currentDirectionality, '->', dir);
+       this.currentDirectionality = dir;
+       if (isParagraph(node)) {
+               var envname = dir.toUpperCase();
+               this.output.push('\\begin{' + envname + '}%');
+               r = this.visit(node);
+               this.output.push('\\end{' + envname + '}%');
+       } else {
+               var cmdname = (dir === 'rtl') ? 'RL' : 'LR';
+               r = this.collect(node, function(contents) {
+                       this.output.push('\\' + cmdname +
+                                                        '{' + contents + '}%');
+               });
+       }
+       this.currentDirectionality = savedDirectionality;
+       return r;
 };
 
 Visitor.prototype['visitTYPEOF=mw:Image'] =
@@ -853,7 +892,8 @@
                                imagemap: imagemap,
                                singleItem: singleItem,
                                hasChapters: hasChapters,
-                               lang: lang
+                               lang: lang,
+                               dir: poly.dir
                        });
                        var h1 = document.createElement('h1');
                        h1.textContent = item.title;
diff --git a/lib/polyglossia.js b/lib/polyglossia.js
index e0d95cf..695f6b0 100644
--- a/lib/polyglossia.js
+++ b/lib/polyglossia.js
@@ -1,9 +1,18 @@
 /** Language and option mappings for the XeLaTeX polyglossia package. */
+var gloss_info = {
+       arabic: { dir: 'rtl', env: 'Arabic' },
+       divehi: { dir: 'rtl' },
+       farsi: { dir: 'rtl' },
+       hebrew: { dir: 'rtl' },
+       nko: { dir: 'rtl' },
+       syriac: { dir: 'rtl' },
+       urdu: { dir: 'rtl' }
+};
 var table = {
        sq: { lang: 'albanian' },
        am: { lang: 'amharic' },
-       ar: { lang: 'arabic', env: 'Arabic' },
-       'und-Arab': { lang: 'arabic', env: 'Arabic' },
+       ar: { lang: 'arabic' },
+       'und-Arab': { lang: 'arabic' },
        hy: { lang: 'armenian' },
        ast: { lang: 'asturian' },
        id: { lang: 'bahasai' },
@@ -93,7 +102,9 @@
                langcode = table.hasOwnProperty(stripped) ? stripped : 'en';
        }
        var r = table[langcode];
-       if (!r.env) { r.env = r.lang; }
+       var g = gloss_info[r.lang] || {};
+       if (!r.env) { r.env = g.env || r.lang; }
+       if (!r.dir) { r.dir = g.dir || 'ltr'; }
        if (!r.options) { r.options = ''; }
        return r;
 };

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I47a80f153de18242ded0553c73afc160f972285f
Gerrit-PatchSet: 1
Gerrit-Project: 
mediawiki/extensions/Collection/OfflineContentGenerator/latex_renderer
Gerrit-Branch: master
Gerrit-Owner: Cscott <canan...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to