forked from jdan/cleaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleaver
executable file
·157 lines (136 loc) · 4.52 KB
/
cleaver
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var program = require('commander');
var tinylr = require('tiny-lr');
var pluck = require('../lib/pluck');
var Cleaver = require('../');
/**
* Helper function to use the cleaver API to create and save a new
* presentation
*/
function createAndSave(files, options, done) {
files.forEach(function (file) {
fs.readFile(file, 'utf-8', function (err, contents) {
if (err) {
throw err;
}
var presentation = new Cleaver(contents, options, path.resolve(path.dirname(file)));
presentation.run()
.then(function (product) {
var outputFile = presentation.options.output || path.basename(file, '.md') + '-cleaver.html';
fs.writeFile(outputFile, product, done);
})
.fail(function (err) {
process.stderr.write('!! ' + err.message + '\n');
if (program.debug) {
process.stderr.write(err.stack);
}
process.exit(1);
});
});
});
}
/**
* Exposes the options sent to the program as an object
*/
function getOptions() {
return pluck(program, [
'title', 'theme', 'style', 'output', 'controls', 'progress', 'encoding',
'template', 'layout', 'debug'
]);
}
program
.version(require('../package.json').version)
.usage('[command] [file]');
/**
* Command to watch for changes on a given file
* This command only uses the first argument passed in, meaning you cannot
* watch multiple files.
*
* Usage:
* cleaver watch file --some-options
*/
program
.command('watch')
.description('Watch for changes on markdown file')
.action(function () {
var file = program.args[0];
var options = getOptions();
options.watch = true;
createAndSave([file], options, function() {});
var port = 35729;
tinylr().listen(port, function() {
console.log('Live reload listening on %s', port);
});
console.log('Watching for changes on ' + file + '. Ctrl-C to abort.');
fs.watchFile(file, { persistent: true, interval: 100 }, function () {
console.log('Rebuilding: ' + new Date());
createAndSave([file], options, function() {
tinylr.changed(file);
});
});
// Also watch on stylesheet if included via command prompt
if (options.style) {
console.log('Also watching for changes on ' + options.style);
fs.watchFile(options.style, { persistent: true, interval: 100 }, function () {
console.log('Rebuilding: ' + new Date());
createAndSave([file], options, function() {
tinylr.changed(file);
});
});
}
process.on('SIGINT', function () {
console.log('\nRebuilding without watch mode: ' + new Date());
options.watch = false;
createAndSave([file], options, function() {
process.exit();
});
});
});
/**
* The default command, running cleaver on one or more files.
*
* Usage:
* cleaver file1 file2 --some-options
*/
program
.command('*')
.description('')
.action(function () {
/**
* Extract the files out of `program.args`. This is a total workaround for
* commander.
*/
var files = program.args.filter(function (arg) {
return typeof arg === 'string';
});
// Reload cleaver with the new ENV for debugging
// TODO: This seems a little janky, maybe handle this in lib/
Cleaver = require('../');
createAndSave(files, getOptions(), function() {});
});
program
.option('--title <title>', 'The title of the rendered document ' +
'(default: Untitled')
// TODO: author options
.option('--theme <theme>', 'An theme to load')
.option('--style <css>', 'A particular stylesheet to load')
.option('--output <filename>', 'The filename of the rendered document ' +
'(default: [INPUT]-cleaver.html)')
.option('--controls', 'Whether or not to include simple navigation ' +
'controls in your presentation (default: true)')
.option('--progress', 'Option to display a small progress bar at the top ' +
'of your presentation (default: true)')
.option('--encoding <encoding>', 'Content encoding used for the rendered ' +
'document (default: utf-8)')
.option('--template <path>', 'URL or path to a mustache template used for ' +
'individual slides')
.option('--layout <path>', 'URL or path to a mustache template used to ' +
'render the entire presentation')
.option('--debug', 'Enable debug output');
program.parse(process.argv);
if (!program.args.length) {
// TODO: custom help screen
program.help();
}