1 | 'use strict';
|
2 |
|
3 | const Theme = require('./theme');
|
4 | const config = require('./config');
|
5 |
|
6 |
|
7 |
|
8 | exports.toANSI = (page) => {
|
9 |
|
10 | let themeOptions = config.get().themes[config.get().theme];
|
11 | let theme = new Theme(themeOptions);
|
12 |
|
13 | function highlight(code) {
|
14 | let parts = code.split(/\{\{(.*?)\}\}/);
|
15 |
|
16 | return ' ' + parts.reduce(function(memo, item, i) {
|
17 | if (i % 2) {
|
18 | return memo + theme.renderExampleToken(item);
|
19 | }
|
20 | return memo + theme.renderExampleCode(item);
|
21 | }, '');
|
22 | }
|
23 |
|
24 |
|
25 | let output = [];
|
26 |
|
27 |
|
28 | output.push(' ' + theme.renderCommandName(page.name));
|
29 | output.push('');
|
30 | output.push(' ' + theme.renderMainDescription(page.description.replace(/\n/g, '\n ')));
|
31 | output.push('');
|
32 |
|
33 | page.examples.forEach((example) => {
|
34 | output.push(theme.renderExampleDescription(' - ' + example.description));
|
35 | output.push(highlight(example.code));
|
36 | output.push('');
|
37 | });
|
38 |
|
39 | if (page.seeAlso && page.seeAlso.length > 0) {
|
40 | output.push('');
|
41 | output.push('See also: ' + page.seeAlso.join(', '));
|
42 | output.push('');
|
43 | }
|
44 |
|
45 | return '\n' + output.join('\n') + '\n';
|
46 | };
|