summaryrefslogtreecommitdiffstats
path: root/lib/parse/page.js
blob: 4e26455786d54f3d7787dd1b593c609e261d2624 (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
var _ = require('lodash');
var marked = require('marked');
var hljs = require('highlight.js');

var lex = require('./lex');
var renderer = require('./renderer');

var lnormalize = require('../utils/lang').normalize;


// Synchronous highlighting with highlight.js
marked.setOptions({
    highlight: function (code, lang) {
        try {
            return hljs.highlight(lang, code).value;
        } catch(e) {
            return hljs.highlightAuto(code).value;
        }
    }
});


// Render a section using our custom renderer
function render(section, _options) {
    // Copy section
    section = _.toArray(section);

    // marked's Render expects this, we don't use it yet
    section.links = {};

    // Build options using defaults and our custom renderer
    var options = _.extend({}, marked.defaults, {
        renderer: renderer(null, _options)
    });

    return marked.parser(section, options);
}

function quizNodesTest(node) {
  return node.type === 'table' || node.type === 'list';
}

function parsePage(src, options) {
    options = options || {};

    // Lex if not already lexed
    return (_.isArray(src) ? src : lex(src))
    .map(function(section) {
        // Transform given type
        if(section.type === 'exercise') {
            var nonCodeNodes = _.reject(section, {
                'type': 'code'
            });

            var codeNodes = _.filter(section, {
                'type': 'code'
            });

            // Languages in code blocks
            var langs = _.pluck(codeNodes, 'lang').map(lnormalize);

            // Check that they are all the same
            var validLangs = _.all(_.map(langs, function(lang) {
                return lang && lang === langs[0];
            }));

            // Main language
            var lang = validLangs ? langs[0] : null;

            return {
                id: section.id,
                type: section.type,
                content: render(nonCodeNodes),
                lang: lang,
                code: {
                    base: codeNodes[0].text,
                    solution: codeNodes[1].text,
                    validation: codeNodes[2].text,
                    // Context is optional
                    context: codeNodes[3] ? codeNodes[3].text : null,
                }
            };
        } else if (section.type === 'quiz') {
            var nonQuizNodes = _.reject(section, quizNodesTest);
            var quizNodes = _.filter(section, quizNodesTest);
            var feedback = nonQuizNodes.splice(_.findIndex(nonQuizNodes, { type: 'blockquote_start' }), _.findIndex(nonQuizNodes, { type: 'blockquote_end' }));
            return {
                id: section.id,
                type: section.type,
                content: render(nonQuizNodes),
                quiz: {
                    base: render([quizNodes[0]]),
                    solution: render([quizNodes[1]]),
                    feedback: render(feedback.slice(1, feedback.length - 1))
                }
            };
        }

        // Render normal pages
        return {
            id: section.id,
            type: section.type,
            content: render(section, options)
        };
    });
}

// Exports
module.exports = parsePage;