UNPKG

@awesome-fe/translate

Version:
267 lines 12.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AdocTranslator = void 0; var adoc_1 = require("../dom/asciidoctor/utils/adoc"); var tiny_html_to_adoc_1 = require("../dom/asciidoctor/html-adoc/tiny-html-to-adoc"); var adoc_to_tiny_html_1 = require("../dom/asciidoctor/html-adoc/adoc-to-tiny-html"); var adoc_builder_1 = require("../dom/asciidoctor/adoc-builder/adoc-builder"); var abstract_translator_1 = require("./abstract-translator"); var common_1 = require("../dom/common"); var same_except_whitespace_1 = require("./same-except-whitespace"); var AdocTranslator = /** @class */ (function (_super) { __extends(AdocTranslator, _super); function AdocTranslator() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.builder = new adoc_builder_1.AdocBuilder(); _this.inlinePairSeparator = '$$$'; return _this; } AdocTranslator.prototype.translateDoc = function (doc) { var _this = this; if (adoc_1.adoc.isAbstractBlock(doc)) { if (!adoc_1.adoc.isDocument(doc)) { var title_1 = doc.getTitle(); if (title_1 && !doc.hasAttribute("original_title") && !(0, common_1.containsChinese)(title_1)) { this.translateAdoc(title_1).then(function (translation) { if (translation && !(0, same_except_whitespace_1.sameExceptWhitespace)(translation, title_1)) { doc.setAttribute("original_title", title_1); doc.setTitle(translation); } }); } } this.translateAttribute(doc, 'title'); if (adoc_1.adoc.isList(doc)) { doc.getItems().forEach(function (it) { return _this.translateDoc(it); }); } else if (adoc_1.adoc.isDescriptionList(doc)) { doc.getItems().flat(9).forEach(function (it) { return _this.translateDoc(it); }); } else { doc.getBlocks().filter(function (it) { return it !== doc; }).forEach(function (it) { return _this.translateDoc(it); }); } } if (adoc_1.adoc.hasLines(doc) && !['source', 'asciimath', 'literal'].includes(doc.getStyle()) && !this.hasTranslated(doc)) { var text_1 = doc.lines.join('\n'); this.translateAdoc(text_1).then(function (translation) { if ((0, same_except_whitespace_1.sameExceptWhitespace)(text_1, translation)) { return; } if (adoc_1.adoc.isListItem(doc.getParent())) { doc.lines = [[text_1, translation].join(_this.inlinePairSeparator)]; } else { var englishLines = doc.lines.join('\n').split('\n\n'); var chineseLines = translation.replace(/\n\n+/g, '\n').trim().split('\n\n'); doc.lines = _this.mergeLines(englishLines, chineseLines); } }); } if (adoc_1.adoc.isDocument(doc)) { this.translateAttribute(doc, 'doctitle'); this.translateAttribute(doc, 'description'); } if (adoc_1.adoc.isQuote(doc)) { this.translateAttribute(doc, 'title'); this.translateAttribute(doc, 'citetitle'); this.translateAttribute(doc, 'attribution'); } if (adoc_1.adoc.isListItem(doc)) { var text_2 = doc.getText().toString().trim(); var pairs = text_2.split(this.inlinePairSeparator); if (pairs.length === 2) { return; } this.translateAdoc(text_2).then(function (translation) { if (!(0, same_except_whitespace_1.sameExceptWhitespace)(text_2, translation)) { doc.setText([text_2, translation].join(_this.inlinePairSeparator)); } }); } if (adoc_1.adoc.isBlockImage(doc)) { this.translateAttribute(doc, 'alt'); } if (adoc_1.adoc.isBlockResource(doc)) { this.translateAttribute(doc, 'poster'); } if (adoc_1.adoc.isTable(doc)) { var rows = doc.getRows(); this.translateHeadRows(rows.head); this.translateRows(rows.body); this.translateRows(rows.foot); } if (adoc_1.adoc.isVerse(doc)) { this.translateAttribute(doc, 'attribution'); this.translateAttribute(doc, 'citetitle'); } return doc; }; AdocTranslator.prototype.parse = function (text) { return this.builder.parse(text); }; AdocTranslator.prototype.serialize = function (doc) { return this.builder.build(doc); }; AdocTranslator.prototype.translateAdoc = function (text) { return __awaiter(this, void 0, void 0, function () { var html; return __generator(this, function (_a) { switch (_a.label) { case 0: if ((0, common_1.containsChinese)(text)) { return [2 /*return*/, text]; } text = text.toString(); if (!text) { return [2 /*return*/, '']; } html = unwrap((0, adoc_to_tiny_html_1.adocToTinyHtml)(text)); return [4 /*yield*/, this.translateSentence(html, 'html').then(function (translation) { return (0, tiny_html_to_adoc_1.tinyHtmlToAdoc)(wrap(translation)); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; AdocTranslator.prototype.translateAttribute = function (node, attributeName) { if (attributeName.startsWith('original_') || node.hasAttribute("original_".concat(attributeName))) { return; } var attribute = node.getAttribute(attributeName); if (attribute) { this.translateAdoc(attribute).then(function (translation) { if (translation && !(0, same_except_whitespace_1.sameExceptWhitespace)(attribute, translation)) { node.setAttribute(attributeName, translation); if (attribute !== translation) { node.setAttribute("original_".concat(attributeName), attribute); } } }); } }; AdocTranslator.prototype.mergeLines = function (englishLines, chineseLines) { if (englishLines.length !== chineseLines.length) { throw 'Cannot merge!'; } var result = []; for (var i = 0; i < englishLines.length; ++i) { var english = englishLines[i]; var chinese = chineseLines[i]; if (english && english !== chinese) { result.push(english); result.push(''); result.push(chinese); var isLastLine = i === englishLines.length - 1; if (!isLastLine) { result.push(''); } } else { result.push(english); } } return result; }; AdocTranslator.prototype.hasTranslated = function (node) { if ((0, common_1.containsChinese)(node.lines.join('\n'))) { return true; } var parent = node.getParent(); var index = parent.getBlocks().indexOf(node); var next = parent.getBlocks()[index + 1]; if (!next || next.getNodeName() !== node.getNodeName()) { return false; } return (0, common_1.containsChinese)(next.lines.join('\n')); }; AdocTranslator.prototype.translateRows = function (rows) { var _this = this; rows.map(function (row) { return row.map(function (cell) { if ((0, common_1.containsChinese)(cell.getText())) { return; } _this.translateAdoc(cell.text).then(function (translation) { if (translation !== cell.text) { cell.style = 'asciidoc'; cell.text = "".concat(cell.text, "\n\n").concat(translation); } }); }); }); }; AdocTranslator.prototype.translateHeadRows = function (rows) { var _this = this; rows.map(function (row) { return row.map(function (cell) { if ((0, common_1.containsChinese)(cell.getText())) { return; } // 标题行不支持 asciidoc 模式,因此只做简单的替换 _this.translateAdoc(cell.text).then(function (translation) { if (translation !== cell.text) { cell.text = translation; } }); }); }); }; return AdocTranslator; }(abstract_translator_1.AbstractTranslator)); exports.AdocTranslator = AdocTranslator; function unwrap(text) { return text .replace(/\bprop-alt=/g, 'alt=') .replace(/^<article adoc-name="document"><p adoc-name="paragraph">(.*)<\/p><\/article>$/gs, '$1'); } function wrap(text) { var content = text.replace(/\balt=/g, 'prop-alt='); return "<article adoc-name=\"document\"><p adoc-name=\"paragraph\">".concat(content, "</p></article>"); } //# sourceMappingURL=adoc-translator.js.map