UNPKG

@awesome-fe/translate

Version:
219 lines 8.93 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tinyHtmlToAdoc = exports.tinyHtmlDomToAdoc = void 0; var dom_models_1 = require("../../parse5/dom-models"); var create_asciidoctor_1 = require("../utils/create-asciidoctor"); var adoc_1 = require("../utils/adoc"); var quotes_1 = require("./quotes"); var add_quotes_1 = require("../adoc-builder/renderers/utils/add-quotes"); function extractValue(value, type) { if (type === 'object') { return JSON.parse(value); } else if (type === 'boolean') { return value === 'true'; } else if (type === 'number') { return +value; } else { return value; } } function loadAttributes(domNode, adocNode) { var attributes = domNode.getAttributes(); attributes.filter(function (it) { return it.name.startsWith('data-') && it.value !== undefined && it.value !== null; }) .forEach(function (attr) { var type = domNode.getAttribute("type-".concat(attr.name)); var name = attr.name.replace(/^data-/g, ''); var value = extractValue(attr.value, type); // setAttribute 无法正确处理 number 类型的参数,因此要绕过它 if (typeof value === 'number') { adocNode.attributes.$$smap[name] = value; } else { adocNode.setAttribute(name, value); } }); } function isUnconstrained(domNode) { var _a, _b, _c, _d; return /\w$/.test((_b = (_a = domNode.previousSibling()) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : '') || /^\w/.test((_d = (_c = domNode.nextSibling()) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : ''); } function buildInlineQuoted(domNode, content) { var _a, _b; if (domNode.getAttribute('prop-type') === 'asciimath') { return "stem:[".concat(content, "]"); } var times = isUnconstrained(domNode) ? 2 : 1; switch (domNode.getAttribute('prop-type')) { case 'double': return "\"`".concat(content, "`\""); case 'single': return "'`".concat(content, "`'"); case 'unquoted': return "[.".concat(domNode.getAttribute('data-role'), "]#").concat(content, "#"); default: var quote = (_b = (_a = (0, quotes_1.quoteTypeToChar)(domNode)) === null || _a === void 0 ? void 0 : _a.repeat(times)) !== null && _b !== void 0 ? _b : ''; return [quote, content, quote].join(''); } } function buildInlineAnchor(domNode, content) { var role = domNode.getAttribute('data-role'); var window = domNode.getAttribute('data-window'); var target = domNode.getAttribute('prop-target').replace(/\.html\b/, '.adoc'); if (role === undefined) { if (target.startsWith('mailto:')) { return content; } else { if (target === '#' + content) { return "<<".concat(content, ">>"); } else { return "<<".concat(target.replace(/^#/, ''), ",").concat(content, ">>"); } } } else if (role === 'bare') { return target; } else if (role && target) { return "".concat(target, "[").concat(content).concat(window === '_blank' ? '^' : '', ", role=").concat(role, "]"); } else { return ''; } } function buildInlineImage(domNode) { var alt = domNode.getAttribute('prop-alt'); var defaultAlt = domNode.getAttribute('data-default-alt'); var nonDefaultAlt = alt === defaultAlt ? '' : alt; var target = domNode.getAttribute('prop-target'); var type = domNode.getAttribute('prop-type'); switch (type) { case 'icon': var link = domNode.getAttribute('data-link'); var window = domNode.getAttribute('data-window'); return "icon:".concat(target, "[link=").concat(link, ",window=").concat(window, "]"); default: return "image:".concat(target, "[").concat(nonDefaultAlt, "]"); } } function buildInlineKbd(domNode) { var keys = domNode.querySelectorAll(function (it) { return it.isTagOf('kbd'); }).map(function (it) { return it.textContent; }); return "kbd:[".concat(keys.join('+'), "]"); } function buildInlineButton(domNode) { return "btn:[".concat(domNode.textContent, "]"); } function buildInlineMenu(domNode) { var items = domNode.querySelectorAll(function (it) { return it.isTagOf('span'); }).map(function (it) { return it.textContent; }); var menu = items.slice(0, 1); var submenus = items.slice(1, items.length - 1); var menuItem = items.slice(items.length - 1, items.length); return "menu:".concat(menu, "[").concat(__spreadArray(__spreadArray([], submenus, true), [menuItem], false).filter(function (it) { return !!it; }).join(' > '), "]"); } function buildInlineIndexTerm(domNode) { var _a; var terms = domNode.querySelectorAll(function (it) { return it.hasClass('term'); }).map(function (it) { return (0, add_quotes_1.addQuotes)(it.textContent); }); var isInline = domNode.getAttribute('prop-type') === 'visible'; if (isInline) { return "((".concat(terms.join(', '), "))"); } else { if ((_a = domNode.nextSibling()) === null || _a === void 0 ? void 0 : _a.textContent.startsWith('\n')) { return "(((".concat(terms.join(', '), ")))"); } else { return "(((".concat(terms.join(', '), ")))\n"); } } } function buildInlineFootNote(domNode) { var _a, _b; var type = domNode.getAttribute('prop-type'); if (!type) { return "footnote:[".concat(domNode.textContent, "]"); } else if (type === 'ref') { var id = (_a = domNode.getAttribute('attr-id')) !== null && _a !== void 0 ? _a : ''; return "footnote:".concat(id, "[").concat(domNode.textContent, "]"); } else if (type === 'xref') { var target = (_b = domNode.getAttribute('prop-target')) !== null && _b !== void 0 ? _b : ''; return "footnote:".concat(target, "[]"); } } function toAdocLines(domNode) { if (domNode instanceof dom_models_1.DomElement) { var content = domNode.childNodes.map(function (it) { return toAdocLines(it); }).join(''); var adocName = domNode.getAttribute('adoc-name'); switch (adocName) { case 'inline_quoted': return buildInlineQuoted(domNode, content); case 'inline_anchor': return buildInlineAnchor(domNode, content); case 'inline_image': return buildInlineImage(domNode); case 'inline_kbd': return buildInlineKbd(domNode); case 'inline_button': return buildInlineButton(domNode); case 'inline_menu': return buildInlineMenu(domNode); case 'inline_indexterm': return buildInlineIndexTerm(domNode); case 'inline_footnote': return buildInlineFootNote(domNode); default: return content; } } else if (domNode instanceof dom_models_1.DomText) { // 如果网址没有包含在链接中,说明它是被专门转义过的 if (!domNode.queryAncestor(function (it) { return it.isTagOf('a'); })) { return domNode.textContent .replace(/\b(https?:)/, '\\$1') .replace(/\b([\w.-]+@[\w.-]+)/, '\\$1'); } return domNode.textContent; } } function decompile(adocNode, domNode) { if (!adocNode || !domNode) { return; } var name = domNode.getAttribute('adoc-name'); var node = adoc_1.adoc.createNode(adocNode, name); loadAttributes(domNode, node); domNode.children.forEach(function (it) { decompile(node, it); }); if (adoc_1.adoc.hasLines(node)) { node.lines = toAdocLines(domNode).split('\n'); } } function tinyHtmlDomToAdoc(tinyHtmlDoc) { var doc = (0, create_asciidoctor_1.createAsciidoctor)(); var root = doc.load('', { backend: 'adoc' }); decompile(root, tinyHtmlDoc.querySelector(function (it) { return it.isTagOf('article'); })); return root; } exports.tinyHtmlDomToAdoc = tinyHtmlDomToAdoc; function tinyHtmlToAdoc(tinyHtml) { var doc = tinyHtmlDomToAdoc(dom_models_1.DomDocument.parse(tinyHtml)); return doc.convert({ backend: 'adoc' }).trim(); } exports.tinyHtmlToAdoc = tinyHtmlToAdoc; //# sourceMappingURL=tiny-html-to-adoc.js.map