summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/src/totext.js
blob: 6e71cd35de74ec56cc238af8e8d539da584605e0 (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

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

class ToText {
    constructor(markup) {
        Object.assign(this, markup);
    }

    // Break line
    onBL() {
        return '\n';
    }

    onText(text) {
        return text;
    }

    onHR() {
        return '<hr />';
    }

    // ---- TITLES

    onTitleStart(level) {
        return '<h' + level + '>';
    }
    onTitleEnd(level) {
        return '</h' + level + '>';
    }

    // ---- PARAGRAPHS / SECTIONS
    onParagraphStart() {
        return '<p>';
    }
    onParagraphEnd() {
        return '</p>';
    }


    onSection() {
        return this.onBL();
    }

    // ---- LINKS
    onLinkStart(href) {
        return '<a href="' + href + '">';
    }
    onLinkEnd(href) {
        return '</a>';
    }

    // ---- LISTS
    onListItemStart(level) {
        return this._spaces((level + 1) * 4) + '<li>';
    }
    onListItemEnd(level) {
        return this._spaces((level + 1) * 4) + '</li>' + this.onBL();
    }
    onListStart(level) {
        return this._spaces(level * 4) + '<ul>' + this.onBL();
    }
    onListEnd(level) {
        return this._spaces(level * 4) + '</ul>' + this.onBL();
    }

    // ------ LANGS

    langs(languages) {
        let content = '';
        content += this.onTitleStart(1) + this.onText('Languages') + this.onTitleEnd(1);
        content += this.onSection();

        content += this._summaryArticles(languages);

        return content;
    }

    // ------ GLOSSARY

    glossary(glossary) {
        let content = '';

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

        glossary.forEach((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

    _summaryArticle(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;
    }
    _summaryArticles(articles, level) {
        let content = '';

        level = level || 0;

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

        return content;
    }
    _summaryPart(part) {
        let content = '';

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

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

        return content;
    }

    summary(summary) {
        let content = '';

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

        summary.parts.forEach((part, i) => {
            const next = summary.parts[i + 1];

            content += this._summaryPart(part);

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

        });

        return content;
    }

    // ---- Utilities

    _spaces(n, s) {
        return Array(n + 1).join(s || ' ');
    }
}

module.exports = ToText;