-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
121 lines (104 loc) · 3.06 KB
/
index.js
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
var _ = require('lodash');
var Q = require('q-plus');
var cheerio = require('cheerio');
var DEFAULT_LANGUAGES = require('./languages');
var configLanguages = [];
function generateMethod(book, body, examples) {
// Main container
var $ = cheerio.load('<div class="code-method"></div>'),
$apiMethod = $('div.code-method'),
// Method definition
$apiDefinition = $('<div class="code-method-definition"></div>'),
// Method code
$apiCode = $('<div class="code-method-code"></div>');
// Append elements
$apiMethod.append($apiDefinition);
$apiMethod.append($apiCode);
// Render method body
return Q()
.then(function() {
return book.renderBlock('markdown', body);
})
.then(function(apiDefinition) {
$apiDefinition.html(apiDefinition);
// Set method examples
return Q(examples).eachSeries(function(example) {
var $example;
// Common text
if (example.type == 'common') {
$example = $('<div class="code-method-example"></div>');
}
// Example code snippet
if (example.type == 'sample') {
$example = $(
'<div class="code-method-sample" data-lang="' +
example.lang +
'" data-name="' +
example.name +
'"></div>'
);
}
return book.renderBlock('markdown', example.body).then(function(body) {
$example.html(body);
$apiCode.append($example);
});
});
})
.then(function() {
// Return whole HTML
return $.html('div.code-method');
});
}
module.exports = {
book: {
assets: './assets',
js: ['api-language-selector.js'],
css: ['api-language-selector.css']
},
blocks: {
method: {
blocks: ['sample', 'common'],
process: function(blk) {
var examples = [];
_.each(blk.blocks, function(_blk) {
var languageName;
// Search if is user-defined language
if (_blk.name == 'sample') {
// Sample blocks should have a lang argument
if (!_blk.kwargs.lang) {
throw Error('sample blocks must provide a "lang" argument');
}
var language = _.find(configLanguages, { lang: _blk.kwargs.lang });
if (!!language) {
languageName = language.name;
} else {
// Default to upper-cased lang
languageName = _blk.kwargs.lang.toUpperCase();
}
}
examples.push({
type: _blk.name,
body: _blk.body.trim(),
lang: _blk.kwargs.lang,
name: languageName
});
});
return {
parse: true,
body: generateMethod(this, blk.body.trim(), examples)
};
}
}
},
hooks: {
config: function(config) {
// Merge user configured languages with default languages
configLanguages = _.unionBy(
config.pluginsConfig['api-language-selector'].languages,
DEFAULT_LANGUAGES,
'lang'
);
return config;
}
}
};