1 |
|
2 |
|
3 |
|
4 | var doc = [
|
5 | 'Dollphie \u2014 A structured document language.',
|
6 | '',
|
7 | 'Usage:',
|
8 | ' dollphie [options] <file>',
|
9 | ' dollphie (--list-formatters | --list-input | --list-output)',
|
10 | ' dollphie --version',
|
11 | ' dollphie --help',
|
12 | '',
|
13 | 'Options:',
|
14 | ' -a, --ast Displays the AST instead of interpreting',
|
15 | ' -r, --raw Displays the raw Dollphie source',
|
16 | ' -h, --help Displays this screen',
|
17 | ' -v, --version Displays the version number',
|
18 | '',
|
19 | '#Transformations:',
|
20 | ' --input=<FORMAT> Convert from the given input format (see --list-input)',
|
21 | ' --output=<FORMAT> The output format for the docs (see --list-output)',
|
22 | ' --formatter=<TEXT> The format used for the text (see --list-formatters)',
|
23 | ' --json Serialises the output to JSON',
|
24 | '',
|
25 | '#Specific help topics:',
|
26 | ' --list-formatters Lists available text formatters',
|
27 | ' --list-output Lists available output formats',
|
28 | ' --list-input Lists available input formats'
|
29 | ].join('\n');
|
30 |
|
31 | var docopt = require('docopt').docopt;
|
32 | var inspect = require('core.inspect');
|
33 | var pkg = require('../package.json');
|
34 | var fs = require('fs');
|
35 | var __ref = require('./language/');
|
36 | var parse = __ref.parse;
|
37 | var prelude = __ref.prelude;
|
38 | var evaluate = __ref.evaluate;
|
39 | var formatters = require('./post-processing/text');
|
40 | var outputters = require('./post-processing/output');
|
41 | var inputters = require('./pre-processing/input-conversion');
|
42 | var aggregate = require('./post-processing/aggregate');
|
43 |
|
44 | var log = console.log.bind(console);
|
45 | var maybeFn = function (f) {
|
46 | return function (a) {
|
47 | return a == null ? null : f(a);
|
48 | };
|
49 | };
|
50 | var show = maybeFn(function (a) {
|
51 | return log(inspectComplex(a));
|
52 | });
|
53 | var json = maybeFn(function (a) {
|
54 | return JSON.stringify(a, null, 2);
|
55 | });
|
56 | var read = function (a) {
|
57 | return fs.readFileSync(a, 'utf-8');
|
58 | };
|
59 | var run = function (a) {
|
60 | return evaluate(prelude())(parse(a));
|
61 | };
|
62 | function inspectComplex(a0) {
|
63 | if (typeof a0 === 'string' || Object.prototype.toString.call(a0) === '[object String]') {
|
64 | var a = a0;
|
65 | return a;
|
66 | }
|
67 | var any = a0;
|
68 | return inspect(any);
|
69 | }
|
70 | function list(transforms) {
|
71 | return Object.keys(transforms).map(function (transform) {
|
72 | return '- ' + transform + '\n ' + transforms[transform].description;
|
73 | }).join('\n');
|
74 | }
|
75 | function select(desc, what) {
|
76 | return function (key) {
|
77 | if (!(key in what)) {
|
78 | throw new ReferenceError('Unknown ' + desc + ': ' + key);
|
79 | }
|
80 | return what[key].transformation;
|
81 | };
|
82 | }
|
83 | var normalise = aggregate;
|
84 | var postProcessing = [
|
85 | [
|
86 | '--formatter',
|
87 | select('formatter', formatters)
|
88 | ],
|
89 | [
|
90 | '--output',
|
91 | select('output format', outputters)
|
92 | ],
|
93 | [
|
94 | '--json',
|
95 | function () {
|
96 | return json;
|
97 | }
|
98 | ]
|
99 | ];
|
100 | var preProcessing = [[
|
101 | '--input',
|
102 | select('input format', inputters)
|
103 | ]];
|
104 | function transformationsFor(transformations, args) {
|
105 | return transformations.map(function (t) {
|
106 | return args[t[0]] ? t[1](args[t[0]]) : null;
|
107 | }).filter(Boolean).reduce(function (a, b) {
|
108 | return function (a$2) {
|
109 | return b(a(a$2));
|
110 | };
|
111 | }, function (a) {
|
112 | return a;
|
113 | });
|
114 | }
|
115 |
|
116 | module.exports = function Main() {
|
117 | var args = docopt(doc.replace(/^#[^\r\n]*/gm, ''), { help: false });
|
118 | var postProcess = transformationsFor(postProcessing, args);
|
119 | var preProcess = transformationsFor(preProcessing, args);
|
120 | var process = function (a) {
|
121 | return function (a$2) {
|
122 | return function (a$3) {
|
123 | return function (a$4) {
|
124 | return postProcess(normalise(a$4));
|
125 | }(run(a$3));
|
126 | }(preProcess(a$2));
|
127 | }(read(a));
|
128 | };
|
129 | var ast = function (a) {
|
130 | return function (a$2) {
|
131 | return function (a$3) {
|
132 | return postProcess(parse(a$3));
|
133 | }(preProcess(a$2));
|
134 | }(read(a));
|
135 | };
|
136 | var raw = function (a) {
|
137 | return preProcess(read(a));
|
138 | };
|
139 | ;
|
140 | args['--help'] ? log(doc.replace(/^#/gm, '')) : args['--version'] ? log('Dollphie version ' + pkg.version) : args['--list-formatters'] ? log(list(formatters)) : args['--list-output'] ? log(list(outputters)) : args['--list-input'] ? log(list(inputters)) : args['--raw'] ? show(raw(args['<file>'])) : args['--ast'] ? show(ast(args['<file>'])) : show(process(args['<file>']));
|
141 | }; |
\ | No newline at end of file |