UNPKG

1.44 kBJavaScriptView Raw
1'use strict';
2
3const Theme = require('./theme');
4const config = require('./config');
5
6// The page structure is passed to this function, and then the theme is applied
7// to different parts of the page and rendered to the console
8exports.toANSI = (page) => {
9 // Creating the theme object
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 // every second part is a token
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 // Creating an array where each line is an element in it
25 let output = [];
26
27 // Pushing each line by extracting the page parts and applying the theme to it
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};