summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/lib/totext.js
blob: 0baa48a6f2c20004c5e3da80f6f255ad407c1430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/*
    This class is extended by gitbook-markdown and gitbook-asciidoc
    to generate back markdown/asciidoc from GitBook metadata.
*/

var ToText = function () {
    function ToText(markup) {
        _classCallCheck(this, ToText);

        Object.assign(this, markup);
    }

    // Break line


    _createClass(ToText, [{
        key: 'onBL',
        value: function onBL() {
            return '\n';
        }
    }, {
        key: 'onText',
        value: function onText(text) {
            return text;
        }
    }, {
        key: 'onHR',
        value: function onHR() {
            return '<hr />';
        }

        // ---- TITLES

    }, {
        key: 'onTitleStart',
        value: function onTitleStart(level) {
            return '<h' + level + '>';
        }
    }, {
        key: 'onTitleEnd',
        value: function onTitleEnd(level) {
            return '</h' + level + '>';
        }

        // ---- PARAGRAPHS / SECTIONS

    }, {
        key: 'onParagraphStart',
        value: function onParagraphStart() {
            return '<p>';
        }
    }, {
        key: 'onParagraphEnd',
        value: function onParagraphEnd() {
            return '</p>';
        }
    }, {
        key: 'onSection',
        value: function onSection() {
            return this.onBL();
        }

        // ---- LINKS

    }, {
        key: 'onLinkStart',
        value: function onLinkStart(href) {
            return '<a href="' + href + '">';
        }
    }, {
        key: 'onLinkEnd',
        value: function onLinkEnd(href) {
            return '</a>';
        }

        // ---- LISTS

    }, {
        key: 'onListItemStart',
        value: function onListItemStart(level) {
            return this._spaces((level + 1) * 4) + '<li>';
        }
    }, {
        key: 'onListItemEnd',
        value: function onListItemEnd(level) {
            return this._spaces((level + 1) * 4) + '</li>' + this.onBL();
        }
    }, {
        key: 'onListStart',
        value: function onListStart(level) {
            return this._spaces(level * 4) + '<ul>' + this.onBL();
        }
    }, {
        key: 'onListEnd',
        value: function onListEnd(level) {
            return this._spaces(level * 4) + '</ul>' + this.onBL();
        }

        // ------ LANGS

    }, {
        key: 'langs',
        value: function langs(languages) {
            var content = '';
            content += this.onTitleStart(1) + this.onText('Languages') + this.onTitleEnd(1);
            content += this.onSection();

            content += this._summaryArticles(languages);

            return content;
        }

        // ------ GLOSSARY

    }, {
        key: 'glossary',
        value: function glossary(_glossary) {
            var _this = this;

            var content = '';

            content += this.onTitleStart(1) + this.onText('Glossary') + this.onTitleEnd(1);
            content += this.onSection();

            _glossary.forEach(function (entry) {
                content += _this.onTitleStart(2) + _this.onText(entry.name) + _this.onTitleEnd(2);
                content += _this.onParagraphStart();
                content += _this.onText(entry.description);
                content += _this.onParagraphEnd();
                content += _this.onSection();
            });

            return content;
        }

        // ------ SUMMARY

    }, {
        key: '_summaryArticle',
        value: function _summaryArticle(article, level) {
            var 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;
        }
    }, {
        key: '_summaryArticles',
        value: function _summaryArticles(articles, level) {
            var _this2 = this;

            var content = '';

            level = level || 0;

            content += this.onListStart(level);
            articles.forEach(function (article) {
                content += _this2._summaryArticle(article, level);
            });
            content += this.onListEnd(level);

            return content;
        }
    }, {
        key: '_summaryPart',
        value: function _summaryPart(part) {
            var content = '';

            if (part.title) content += this.onTitleStart(2) + this.onText(part.title) + this.onTitleEnd(2);

            content += this._summaryArticles(part.articles);

            return content;
        }
    }, {
        key: 'summary',
        value: function summary(_summary) {
            var _this3 = this;

            var content = '';

            content += this.onTitleStart(1) + this.onText('Summary') + this.onTitleEnd(1);
            content += this.onSection();

            _summary.parts.forEach(function (part, i) {
                var next = _summary.parts[i + 1];

                content += _this3._summaryPart(part);

                if (next && !next.title) {
                    content += _this3.onBL() + _this3.onHR() + _this3.onBL();
                } else {
                    content += _this3.onSection();
                }
            });

            return content;
        }

        // ---- Utilities

    }, {
        key: '_spaces',
        value: function _spaces(n, s) {
            return Array(n + 1).join(s || ' ');
        }
    }]);

    return ToText;
}();

module.exports = ToText;
//# sourceMappingURL=totext.js.map