Skip to content

Commit d90b6e1

Browse files
committed
Reworked structure of original plugin
1 parent b02edbc commit d90b6e1

15 files changed

+3772
-0
lines changed

.eslintrc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"rules": {
3+
"no-extra-boolean-cast": [ 0 ],
4+
"indent": [ 2, 2 ],
5+
"quotes": [ 2, "single" ],
6+
"linebreak-style": [ 2, "unix" ],
7+
"semi": [ 2, "always" ],
8+
"no-unused-vars": [ 2, {
9+
"vars": "all",
10+
"args": "none"
11+
} ],
12+
"spaced-comment": [ 2, "always" ]
13+
},
14+
"env": {
15+
"node": true,
16+
"mocha": true,
17+
"browser": true
18+
},
19+
"extends": "eslint:recommended"
20+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.vscode

assets/api-language-selector.css

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/api-language-selector.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
var _ = require('lodash');
2+
var Q = require('q-plus');
3+
var cheerio = require('cheerio');
4+
5+
var DEFAULT_LANGUAGES = require('./languages');
6+
var configLanguages = [];
7+
8+
function generateMethod(book, body, examples) {
9+
// Main container
10+
var $ = cheerio.load('<div class="code-method"></div>'),
11+
$apiMethod = $('div.code-method'),
12+
// Method definition
13+
$apiDefinition = $('<div class="code-method-definition"></div>'),
14+
// Method code
15+
$apiCode = $('<div class="code-method-code"></div>');
16+
17+
// Append elements
18+
$apiMethod.append($apiDefinition);
19+
$apiMethod.append($apiCode);
20+
21+
// Render method body
22+
return Q()
23+
.then(function() {
24+
return book.renderBlock('markdown', body);
25+
})
26+
.then(function(apiDefinition) {
27+
$apiDefinition.html(apiDefinition);
28+
29+
// Set method examples
30+
return Q(examples).eachSeries(function(example) {
31+
var $example;
32+
33+
// Common text
34+
if (example.type == 'common') {
35+
$example = $('<div class="code-method-example"></div>');
36+
}
37+
38+
// Example code snippet
39+
if (example.type == 'sample') {
40+
$example = $(
41+
'<div class="code-method-sample" data-lang="' +
42+
example.lang +
43+
'" data-name="' +
44+
example.name +
45+
'"></div>'
46+
);
47+
}
48+
49+
return book.renderBlock('markdown', example.body).then(function(body) {
50+
$example.html(body);
51+
$apiCode.append($example);
52+
});
53+
});
54+
})
55+
.then(function() {
56+
// Return whole HTML
57+
return $.html('div.code-method');
58+
});
59+
}
60+
61+
module.exports = {
62+
book: {
63+
assets: './assets',
64+
js: ['api-language-selector.js'],
65+
css: ['api-language-selector.css']
66+
},
67+
68+
blocks: {
69+
method: {
70+
blocks: ['sample', 'common'],
71+
process: function(blk) {
72+
var examples = [];
73+
74+
_.each(blk.blocks, function(_blk) {
75+
var languageName;
76+
77+
// Search if is user-defined language
78+
if (_blk.name == 'sample') {
79+
// Sample blocks should have a lang argument
80+
if (!_blk.kwargs.lang) {
81+
throw Error('sample blocks must provide a "lang" argument');
82+
}
83+
84+
var language = _.find(configLanguages, { lang: _blk.kwargs.lang });
85+
86+
if (!!language) {
87+
languageName = language.name;
88+
} else {
89+
// Default to upper-cased lang
90+
languageName = _blk.kwargs.lang.toUpperCase();
91+
}
92+
}
93+
94+
examples.push({
95+
type: _blk.name,
96+
body: _blk.body.trim(),
97+
lang: _blk.kwargs.lang,
98+
name: languageName
99+
});
100+
});
101+
102+
return {
103+
parse: true,
104+
body: generateMethod(this, blk.body.trim(), examples)
105+
};
106+
}
107+
}
108+
},
109+
110+
hooks: {
111+
config: function(config) {
112+
// Merge user configured languages with default languages
113+
configLanguages = _.unionBy(
114+
config.pluginsConfig['api-language-selector'].languages,
115+
DEFAULT_LANGUAGES,
116+
'lang'
117+
);
118+
return config;
119+
}
120+
}
121+
};

0 commit comments

Comments
 (0)