Santhosh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/381960 )

Change subject: MWImage: Support Inline images with figute-inline tag
......................................................................

MWImage: Support Inline images with figute-inline tag

Support translation of caption in data-mw if present and an
MT Client is available for the language pair.

Whitelist figure-inline in DOM sanitization.

Change-Id: I8db09d11bb64701811720a45b526cc6395fbd84e
---
M bin/adapt
M lib/lineardoc/Parser.js
M lib/mt/MTClient.js
M lib/translationunits/MWImage.js
4 files changed, 23 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver 
refs/changes/60/381960/1

diff --git a/bin/adapt b/bin/adapt
index 7e90699..4232801 100755
--- a/bin/adapt
+++ b/bin/adapt
@@ -2,6 +2,7 @@
 const fs = require( 'fs' ),
        yaml = require( 'js-yaml' ),
        Adapter = require( __dirname + '/../lib/Adapter' ),
+       TestClient = require( __dirname + '/../lib/mt' ).TestClient,
        Normalizer = require( __dirname + '/../lib/lineardoc' ).Normalizer;
 
 let config = yaml.load( fs.readFileSync( 'config.yaml' ) );
@@ -36,6 +37,8 @@
 
 }
 
+cxConfig.conf.mtClient = new TestClient( cxConfig );
+
 let from = process.argv[ 2 ];
 let to = process.argv[ 3 ];
 let adapter = new Adapter( from, to, cxConfig );
diff --git a/lib/lineardoc/Parser.js b/lib/lineardoc/Parser.js
index f34cdc6..5af4172 100644
--- a/lib/lineardoc/Parser.js
+++ b/lib/lineardoc/Parser.js
@@ -25,7 +25,7 @@
        'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup',
        // HTML5 sectioning content
        'article', 'aside', 'body', 'nav', 'section', 'footer', 'header', 
'figure',
-       'figcaption', 'fieldset', 'details', 'blockquote',
+       'figcaption', 'figure-inline', 'fieldset', 'details', 'blockquote',
        // other
        'hr', 'button', 'canvas', 'center', 'col', 'colgroup', 'embed',
        'map', 'object', 'pre', 'progress', 'video',
diff --git a/lib/mt/MTClient.js b/lib/mt/MTClient.js
index 755283d..0722166 100644
--- a/lib/mt/MTClient.js
+++ b/lib/mt/MTClient.js
@@ -107,6 +107,7 @@
 
                return this.DOMPurify.sanitize( html, {
                        // These are not known attributes for DOMPurify
+                       ADD_TAGS: [ 'figure-inline' ],
                        ADD_ATTR: [ 'typeof', 'resource', 'src', 'srcset' ],
                        ADD_URI_SAFE_ATTR: [ 'rel', 'typeof' ] // Without this 
rel="mw:WikiLink" attributes will be removed.
                } );
diff --git a/lib/translationunits/MWImage.js b/lib/translationunits/MWImage.js
index d99903a..bb48fbf 100644
--- a/lib/translationunits/MWImage.js
+++ b/lib/translationunits/MWImage.js
@@ -9,6 +9,9 @@
 class MWImage extends TranslationUnit {
        constructor( node, sourceLanguage, targetLanguage, context ) {
                super( node, sourceLanguage, targetLanguage, context );
+               this.imageSource = null;
+               this.sourceResource = null;
+               this.isInlineImage = node.name === 'figure-inline';
        }
 
        /**
@@ -52,10 +55,10 @@
 }
 
 MWImage.prototype.adapt = cxutil.async( function* () {
-       var i, len, chunk, sourceImage, imageLink, targetResource, 
namespaceAlias;
+       var sourceImage, imageLink;
 
-       for ( i = 0, len = this.node.children.textChunks.length; i < len; i++ ) 
{
-               chunk = this.node.children.textChunks[ i ];
+       for ( let i = 0, len = this.node.children.textChunks.length; i < len; 
i++ ) {
+               let chunk = this.node.children.textChunks[ i ];
                if ( chunk.tags[ 0 ] && chunk.tags[ 0 ].name === 'a' ) {
                        imageLink = chunk.tags[ 0 ];
                }
@@ -72,23 +75,31 @@
        this.sourceResource = sourceImage.attributes.resource;
        this.adaptImageAlignment();
 
+       if ( this.isInlineImage && this.context.conf.mtClient && 
this.node.attributes[ 'data-mw' ] ) {
+               const caption = JSON.parse( this.node.attributes[ 'data-mw' ] 
).caption;
+               const translatedCaption = yield 
this.context.conf.mtClient.translate(
+                       this.sourceLanguage, this.targetLanguage, caption
+               );
+               this.node.attributes[ 'data-mw' ] = JSON.stringify( { caption: 
translatedCaption } );
+       }
+
        if ( this.isCommonsImage( sourceImage.attributes.src ) ) {
-               namespaceAlias = yield new MWApiRequestManager( this.context 
).getNamespaceAlias( 'File', this.targetLanguage );
-               targetResource = this.sourceResource.replace( 
/^(\.\.?\/)*([^:]+)(:)/, '$1' + namespaceAlias + '$3' );
+               let namespaceAlias = yield new MWApiRequestManager( 
this.context ).getNamespaceAlias( 'File', this.targetLanguage );
+               let targetResource = this.sourceResource.replace( 
/^(\.\.?\/)*([^:]+)(:)/, '$1' + namespaceAlias + '$3' );
                sourceImage.attributes.resource = imageLink.attributes.href = 
targetResource;
        } else {
                // TODO: This format is not decided yet. We do need to inform 
client about failed
                // adaptations somehow.
                this.node.attributes[ 'data-cx' ] = JSON.stringify( {
                        adapted: false,
-                       imageSource: this.imageSource,
+                       imageSource: sourceImage.attributes.src,
                        resource: this.sourceResource
                } );
        }
        return this.node;
 } );
 
-MWImage.matchTagNames = [ 'figure' ];
+MWImage.matchTagNames = [ 'figure', 'figure-inline' ];
 MWImage.matchRdfaTypes = [ 'mw:Image', 'mw:Image/Thumb', 'mw:Image/Frame', 
'mw:Image/Frameless' ];
 
 module.exports = MWImage;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8db09d11bb64701811720a45b526cc6395fbd84e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/cxserver
Gerrit-Branch: master
Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com>

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

Reply via email to