summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/lib/totext.js
blob: fd9c5ae37c91270238f9c3aabc2d9a0bbc9e450b (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
var _ = require('lodash');

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


function ToText(markup) {
    _.extend(this, markup || {});
    _.bindAll(this);
};

// Break line
ToText.prototype.onBL = function() {
    return '\n';
};

ToText.prototype.onText = function(text) {
    return text;
};

ToText.prototype.onHR = function() {
    return '<hr />';
};

// ---- TITLES

ToText.prototype.onTitleStart = function(level) {
    return '<h'+level+'>';
};
ToText.prototype.onTitleEnd = function(level) {
    return '</h'+level+'>';
};

// ---- PARAGRAPHS / SECTIONS
ToText.prototype.onParagraphStart = function() {
    return '<p>';
};
ToText.prototype.onParagraphEnd = function() {
    return '</p>';
};


ToText.prototype.onSection = function() {
    return this.onBL();
};

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

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

// ------ LANGS

ToText.prototype.langs = function(languages) {
    var 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) {
    var content = '';
    content += this.onTitleStart(1) + this.onText('Glossary') + this.onTitleEnd(1);
    content += this.onSection();

    _.each(glossary, 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();
    }, this);

    return content;
};

// ------ SUMMARY

ToText.prototype._summaryArticle = function(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;
};
ToText.prototype._summaryArticles = function(articles, level) {
    var content = '';
    level = level || 0;

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

    return content;
};
ToText.prototype._summaryPart = function(part) {
    var 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) {
    var content = '';
    content += this.onTitleStart(1) + this.onText('Summary') + this.onTitleEnd(1);
    content += this.onSection();

    _.each(summary.parts, function(part, i) {
        var next = summary.parts[i + 1];

        content += this._summaryPart(part);

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

    }, this);

    return content;
};

// ---- Utilities

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

module.exports = ToText;