"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = __importDefault(require("lodash"));
/*
This class is extended by gitbook-markdown and gitbook-asciidoc
to generate back markdown/asciidoc from HonKit metadata.
*/
function ToText(markup) {
if (!(this instanceof ToText)) {
// @ts-expect-error
return new ToText(markup);
}
lodash_1.default.extend(this, markup || {});
lodash_1.default.bindAll(this, lodash_1.default.functionsIn(this));
}
// Break line
ToText.prototype.onBL = function () {
return "\n";
};
ToText.prototype.onText = function (text) {
return text;
};
ToText.prototype.onHR = function () {
return "
";
};
// ---- TITLES
ToText.prototype.onTitleStart = function (level) {
return ``;
};
ToText.prototype.onTitleEnd = function (level) {
return ``;
};
// ---- PARAGRAPHS / SECTIONS
ToText.prototype.onParagraphStart = function () {
return "";
};
ToText.prototype.onParagraphEnd = function () {
return "
";
};
ToText.prototype.onSection = function () {
return this.onBL();
};
// ---- LINKS
ToText.prototype.onLinkStart = function (href) {
return ``;
};
ToText.prototype.onLinkEnd = function (href) {
return "";
};
// ---- LISTS
ToText.prototype.onListItemStart = function (level) {
return `${this._spaces((level + 1) * 4)}`;
};
ToText.prototype.onListItemEnd = function (level) {
return `${this._spaces((level + 1) * 4)}${this.onBL()}`;
};
ToText.prototype.onListStart = function (level) {
return `${this._spaces(level * 4)}${this.onBL()}`;
};
ToText.prototype.onListEnd = function (level) {
return `${this._spaces(level * 4)}
${this.onBL()}`;
};
// ------ LANGS
ToText.prototype.langs = function (languages) {
let content = "";
content += this.onTitleStart(1) + this.onText("Languages") + this.onTitleEnd(1);
content += this.onSection();
content += this._summaryArticles(languages);
return content;
};
// ------ GLOSSARY
ToText.prototype.glossary = function (glossary) {
const that = this;
let content = "";
content += that.onTitleStart(1) + that.onText("Glossary") + that.onTitleEnd(1);
content += that.onSection();
lodash_1.default.each(glossary, (entry) => {
content += that.onTitleStart(2) + that.onText(entry.name) + that.onTitleEnd(2);
content += that.onParagraphStart();
content += that.onText(entry.description);
content += that.onParagraphEnd();
content += that.onSection();
});
return content;
};
// ------ SUMMARY
ToText.prototype._summaryArticle = function (article, level) {
let content = "";
content += this.onListItemStart(level);
if (article.ref)
content += this.onLinkStart(article.ref);
content += this.onText(article.title);
if (article.ref)
content += this.onLinkEnd(article.ref);
content += this.onBL();
if (article.articles && article.articles.length > 0) {
content += this._summaryArticles(article.articles, level + 1);
}
content += this.onListItemEnd(level);
return content;
};
ToText.prototype._summaryArticles = function (articles, level) {
const that = this;
let content = "";
level = level || 0;
content += that.onListStart(level);
lodash_1.default.each(articles, (article) => {
content += that._summaryArticle(article, level);
});
content += that.onListEnd(level);
return content;
};
ToText.prototype._summaryPart = function (part) {
let content = "";
if (part.title)
content += this.onTitleStart(2) + this.onText(part.title) + this.onTitleEnd(2);
content += this._summaryArticles(part.articles);
return content;
};
ToText.prototype.summary = function (summary) {
const that = this;
let content = "";
content += that.onTitleStart(1) + that.onText("Summary") + that.onTitleEnd(1);
content += that.onSection();
lodash_1.default.each(summary.parts, (part, i) => {
const next = summary.parts[i + 1];
content += that._summaryPart(part);
if (next && !next.title) {
content += that.onBL() + that.onHR() + that.onBL();
}
else {
content += that.onSection();
}
});
return content;
};
// ---- Utilities
ToText.prototype._spaces = function (n, s) {
return Array(n + 1).join(s || " ");
};
exports.default = ToText;