jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/372833 )
Change subject: MWReference: add reference adaptation
..
MWReference: add reference adaptation
* Some misc doc tweaks noticed while inspecting the code
* Introduced TestClient for MT. Speeds up the tests and
removed dependency on an actual MT client. It also always
provides consistent results.
Change-Id: I1fb5b29c6d65ad6da83e4b0140138e43a3e84fd2
---
M lib/Adapter.js
A lib/mt/TestClient.js
M lib/mt/index.js
A lib/translationunits/MWReference.js
M lib/translationunits/index.js
M test/adaptation/AdaptationTests.json
M test/translationunits/MWLink.test.json
A test/translationunits/MWReference.test.js
A test/translationunits/MWReference.test.json
9 files changed, 194 insertions(+), 7 deletions(-)
Approvals:
jenkins-bot: Verified
Santhosh: Looks good to me, approved
diff --git a/lib/Adapter.js b/lib/Adapter.js
index d7aa71d..167a415 100644
--- a/lib/Adapter.js
+++ b/lib/Adapter.js
@@ -23,7 +23,7 @@
/**
* Get the adapter for the given tag(translation unit).
* @param {Object} element
-* @return {TranslationUnit}
+* @return {TranslationUnit|null}
*/
getAdapter( element ) {
let name, match = false,
diff --git a/lib/mt/TestClient.js b/lib/mt/TestClient.js
new file mode 100644
index 000..1ba0cca
--- /dev/null
+++ b/lib/mt/TestClient.js
@@ -0,0 +1,19 @@
+'use strict';
+
+const MTClient = require( './MTClient.js' );
+
+class TestClient extends MTClient {
+ /**
+* Translate plain text for tests.
+*
+* @param {string} sourceLang Source language code
+* @param {string} targetLang Target language code
+* @param {string} sourceText Source language text
+* @return {Promise} Promise of the translated text
+*/
+ translateText( sourceLang, targetLang, sourceText ) {
+ return Promise.resolve(
`[${sourceLang}→${targetLang}]${sourceText}` );
+ }
+}
+
+module.exports = TestClient;
diff --git a/lib/mt/index.js b/lib/mt/index.js
index 505fa97..d05029a 100644
--- a/lib/mt/index.js
+++ b/lib/mt/index.js
@@ -1,8 +1,9 @@
'use strict';
module.exports = {
- Apertium: require( './Apertium.js' ),
- Yandex: require( './Yandex.js' ),
- Youdao: require( './Youdao.js' ),
- Matxin: require( './Matxin.js' )
+ Apertium: require( './Apertium' ),
+ Matxin: require( './Matxin' ),
+ TestClient: require( './TestClient' ),
+ Yandex: require( './Yandex' ),
+ Youdao: require( './Youdao' )
};
diff --git a/lib/translationunits/MWReference.js
b/lib/translationunits/MWReference.js
new file mode 100644
index 000..ff0294c
--- /dev/null
+++ b/lib/translationunits/MWReference.js
@@ -0,0 +1,65 @@
+'use strict';
+
+const cxutil = require( '../util.js' ),
+ TranslationUnit = require( './TranslationUnit.js' );
+
+/**
+ * This class handles the `` wikitext tag.
+ *
+ * This tag rendered as span with typeof="mw:Extension/ref" and
rel="dc:references" by parsoid.
+ * By default parsoid also adds data-mw.body.id that refers to the rendered
element produced by
+ * the `` tag. But here we expect that data-mw.body.html is also
provided so that
+ * it can be adapted without any dependencies to other sections of the article.
+ */
+class MWReference extends TranslationUnit {}
+
+MWReference.prototype.adapt = cxutil.async( function* () {
+ let refData, refBody, wrappedRefBody, translatedRefBody,
unwrappedTranslatedRefBody;
+
+ // TODO: This format is not decided yet. We do need to inform client
about failed
+ // adaptations somehow.
+ // This will be reset later if adaptation is succesful
+ this.node.attributes[ 'data-cx' ] = JSON.stringify( {
+ adapted: false
+ } );
+
+ try {
+ refData = JSON.parse( this.node.attributes[ 'data-mw' ] );
+ } catch ( e ) {
+ this.log( 'error', 'Not-adapting a reference node with non-JSON
data-mw: ' + this.node.attributes.id );
+ return this.node;
+ }
+
+ if ( !refData ) {
+ this.log( 'error', 'Not-adapting a reference node without
data-mw: ' + this.node.attributes.id );
+ return this.node;
+ }
+
+ refBody = refData.body && refData.body.html;
+ if ( !refBody ) {
+ this.log( 'debug', 'Not-adapting a reference node without
data-mw.body.html: ' + this.node.attributes.id );
+ return this.node;
+ }
+
+ // MTClient only accepts content wrapped in block element tags in HTML
mode.
+ wrappedRefBody = '' + refBody + '';
+ // TODO: Parse recursively instead of just MTing to handle templates
+ // Translate reference contents
+ translatedRefBody = yield this.context.conf.mtClient.translate(
+