-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
170 lines (124 loc) · 3.12 KB
/
index.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
/* global module, require, __dirname */
var isEmptyObject = require('is-empty-object');
var JSONStream = require('JSONStream');
var assign = require('object-assign');
var traverse = require('traverse');
var mkdirp = require('mkdirp');
var pretty = require('pretty');
var path = require('path');
var fs = require('fs');
/**
* Create markdown file from
* pug-doc stream or json file
*/
function PugDocMarkdown(options){
if(typeof options.output === 'undefined'){
throw new Error('Pug doc markdown requires settings.output to be set.');
}
// options
options = assign({
input: null,
output: null
}, options);
// create output file
mkdirp.sync(path.dirname(options.output));
var output = fs.createWriteStream(options.output);
output.on('close', function(){
stream.emit('complete');
}.bind(this));
output.write('# Pug Documentation \n\n');
/**
* Create markdown snippet
*/
function createSnippet(obj){
var lines = [];
obj = JSON.parse(JSON.stringify(obj));
// push name to markdown output
lines.push('## '+ obj.meta.name);
lines.push(obj.meta.description);
lines.push('\n');
// traverse all arguments
// and indent according to level
var spaces;
var arg;
traverse(obj.meta).forEach(function(x){
// check for empty object
if(isEmptyObject(x)){
return;
}
if(this.key === 'name'){
return;
}
if(this.key === 'description'){
return;
}
if(typeof this.key === 'undefined'){
return;
}
// set indentation
spaces = new Array(this.level).join(' ');
arg = [];
arg.push(spaces);
arg.push(this.key);
if(typeof x !== 'object'){
arg.push(': ');
arg.push(x);
}
lines.push(arg.join(''));
});
lines.push('');
// push jade snippet
lines.push('```jade');
lines.push(obj.source);
lines.push('```');
lines.push('');
// push html snippet
lines.push('```html');
lines.push(pretty(obj.output));
lines.push('```');
// whitespace
lines.push('');
lines.push('');
lines.push('---');
lines.push('');
lines.push('');
lines.push('');
return lines.join('\n');
}
/**
* Output stream
*/
var stream = JSONStream.parse('*');
stream.on('data', function(data){
// create code snippet
var snippet = createSnippet(data);
// push lines
output.write(snippet);
});
stream.on('end', function(){
output.end();
});
/**
* Input from file
*/
if(typeof options.input !== 'undefined'){
// read input json
var input = fs.createReadStream(__dirname +'/'+ options.input);
input.on('data', function(data){
var json = JSON.parse(data.toString());
var snippet;
json.forEach(function(obj){
// create code snippet
snippet = createSnippet(obj);
// append json data to template
output.write(snippet);
});
// end stream
stream.push(null);
stream.end();
}.bind(this));
}
return stream;
}
module.exports = PugDocMarkdown;