summaryrefslogtreecommitdiffstats
path: root/lib/models/parser.js
blob: d64542f0be9537f45515265e8e1a258257890912 (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
var Immutable = require('immutable');
var Promise = require('../utils/promise');

var Parser = Immutable.Record({
    name:           String(),

    // List of extensions that can be processed using this parser
    extensions:     Immutable.List(),

    // Parsing functions
    readme:         Function(),
    langs:          Function(),
    summary:        Function(),
    glossary:       Function(),
    page:           Function(),
    inline:         Function()
});

Parser.prototype.getName = function() {
    return this.get('name');
};

Parser.prototype.getExtensions = function() {
    return this.get('extensions');
};

// PARSE

Parser.prototype.parseReadme = function(content) {
    var readme = this.get('readme');
    return Promise(readme(content));
};

Parser.prototype.parseSummary = function(content) {
    var summary = this.get('summary');
    return Promise(summary(content));
};

Parser.prototype.parseGlossary = function(content) {
    var glossary = this.get('glossary');
    return Promise(glossary(content));
};

Parser.prototype.preparePage = function(content) {
    var page = this.get('page');
    if (!page.prepare) {
        return Promise(content);
    }

    return Promise(page.prepare(content));
};

Parser.prototype.parsePage = function(content) {
    var page = this.get('page');
    return Promise(page(content));
};

Parser.prototype.parseInline = function(content) {
    var inline = this.get('inline');
    return Promise(inline(content));
};

Parser.prototype.parseLanguages = function(content) {
    var langs = this.get('langs');
    return Promise(langs(content));
};

Parser.prototype.parseInline = function(content) {
    var inline = this.get('inline');
    return Promise(inline(content));
};

// TO TEXT

Parser.prototype.renderLanguages = function(content) {
    var langs = this.get('langs');
    return Promise(langs.toText(content));
};

Parser.prototype.renderSummary = function(content) {
    var summary = this.get('summary');
    return Promise(summary.toText(content));
};

Parser.prototype.renderGlossary = function(content) {
    var glossary = this.get('glossary');
    return Promise(glossary.toText(content));
};

/**
    Test if this parser matches an extension

    @param {String} ext
    @return {Boolean}
*/
Parser.prototype.matchExtension = function(ext) {
    var exts = this.getExtensions();
    return exts.includes(ext.toLowerCase());
};

/**
    Create a new parser using a module (gitbook-markdown, etc)

    @param {String} name
    @param {Array<String>} extensions
    @param {Object} module
    @return {Parser}
*/
Parser.create = function(name, extensions, module) {
    return new Parser({
        name: name,
        extensions: Immutable.List(extensions),
        readme:         module.readme,
        langs:          module.langs,
        summary:        module.summary,
        glossary:       module.glossary,
        page:           module.page,
        inline:         module.inline
    });
};

module.exports = Parser;