totext.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const lodash_1 = __importDefault(require("lodash"));
  7. /*
  8. This class is extended by gitbook-markdown and gitbook-asciidoc
  9. to generate back markdown/asciidoc from HonKit metadata.
  10. */
  11. function ToText(markup) {
  12. if (!(this instanceof ToText)) {
  13. // @ts-expect-error
  14. return new ToText(markup);
  15. }
  16. lodash_1.default.extend(this, markup || {});
  17. lodash_1.default.bindAll(this, lodash_1.default.functionsIn(this));
  18. }
  19. // Break line
  20. ToText.prototype.onBL = function () {
  21. return "\n";
  22. };
  23. ToText.prototype.onText = function (text) {
  24. return text;
  25. };
  26. ToText.prototype.onHR = function () {
  27. return "<hr />";
  28. };
  29. // ---- TITLES
  30. ToText.prototype.onTitleStart = function (level) {
  31. return `<h${level}>`;
  32. };
  33. ToText.prototype.onTitleEnd = function (level) {
  34. return `</h${level}>`;
  35. };
  36. // ---- PARAGRAPHS / SECTIONS
  37. ToText.prototype.onParagraphStart = function () {
  38. return "<p>";
  39. };
  40. ToText.prototype.onParagraphEnd = function () {
  41. return "</p>";
  42. };
  43. ToText.prototype.onSection = function () {
  44. return this.onBL();
  45. };
  46. // ---- LINKS
  47. ToText.prototype.onLinkStart = function (href) {
  48. return `<a href="${href}">`;
  49. };
  50. ToText.prototype.onLinkEnd = function (href) {
  51. return "</a>";
  52. };
  53. // ---- LISTS
  54. ToText.prototype.onListItemStart = function (level) {
  55. return `${this._spaces((level + 1) * 4)}<li>`;
  56. };
  57. ToText.prototype.onListItemEnd = function (level) {
  58. return `${this._spaces((level + 1) * 4)}</li>${this.onBL()}`;
  59. };
  60. ToText.prototype.onListStart = function (level) {
  61. return `${this._spaces(level * 4)}<ul>${this.onBL()}`;
  62. };
  63. ToText.prototype.onListEnd = function (level) {
  64. return `${this._spaces(level * 4)}</ul>${this.onBL()}`;
  65. };
  66. // ------ LANGS
  67. ToText.prototype.langs = function (languages) {
  68. let content = "";
  69. content += this.onTitleStart(1) + this.onText("Languages") + this.onTitleEnd(1);
  70. content += this.onSection();
  71. content += this._summaryArticles(languages);
  72. return content;
  73. };
  74. // ------ GLOSSARY
  75. ToText.prototype.glossary = function (glossary) {
  76. const that = this;
  77. let content = "";
  78. content += that.onTitleStart(1) + that.onText("Glossary") + that.onTitleEnd(1);
  79. content += that.onSection();
  80. lodash_1.default.each(glossary, (entry) => {
  81. content += that.onTitleStart(2) + that.onText(entry.name) + that.onTitleEnd(2);
  82. content += that.onParagraphStart();
  83. content += that.onText(entry.description);
  84. content += that.onParagraphEnd();
  85. content += that.onSection();
  86. });
  87. return content;
  88. };
  89. // ------ SUMMARY
  90. ToText.prototype._summaryArticle = function (article, level) {
  91. let content = "";
  92. content += this.onListItemStart(level);
  93. if (article.ref)
  94. content += this.onLinkStart(article.ref);
  95. content += this.onText(article.title);
  96. if (article.ref)
  97. content += this.onLinkEnd(article.ref);
  98. content += this.onBL();
  99. if (article.articles && article.articles.length > 0) {
  100. content += this._summaryArticles(article.articles, level + 1);
  101. }
  102. content += this.onListItemEnd(level);
  103. return content;
  104. };
  105. ToText.prototype._summaryArticles = function (articles, level) {
  106. const that = this;
  107. let content = "";
  108. level = level || 0;
  109. content += that.onListStart(level);
  110. lodash_1.default.each(articles, (article) => {
  111. content += that._summaryArticle(article, level);
  112. });
  113. content += that.onListEnd(level);
  114. return content;
  115. };
  116. ToText.prototype._summaryPart = function (part) {
  117. let content = "";
  118. if (part.title)
  119. content += this.onTitleStart(2) + this.onText(part.title) + this.onTitleEnd(2);
  120. content += this._summaryArticles(part.articles);
  121. return content;
  122. };
  123. ToText.prototype.summary = function (summary) {
  124. const that = this;
  125. let content = "";
  126. content += that.onTitleStart(1) + that.onText("Summary") + that.onTitleEnd(1);
  127. content += that.onSection();
  128. lodash_1.default.each(summary.parts, (part, i) => {
  129. const next = summary.parts[i + 1];
  130. content += that._summaryPart(part);
  131. if (next && !next.title) {
  132. content += that.onBL() + that.onHR() + that.onBL();
  133. }
  134. else {
  135. content += that.onSection();
  136. }
  137. });
  138. return content;
  139. };
  140. // ---- Utilities
  141. ToText.prototype._spaces = function (n, s) {
  142. return Array(n + 1).join(s || " ");
  143. };
  144. exports.default = ToText;