-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·145 lines (127 loc) · 3.57 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
#! /usr/bin/env node
var fs = require('fs')
var chalk = require('chalk')
var minimist = require('minimist')
var flatten = require('lodash.flatten')
var findindex = require('lodash.findindex')
var argv = minimist(process.argv.slice(2), {
alias: {
c: 'classname',
o: 'output',
t: 'title',
s: 'sort',
h: 'help'
},
default: {
sort: 'true'
}
})
var usage = `
usage:
myops <source> -o <output> -c <classname>
options:
--classname, -c name of class
--output, -o name of output file
--title, -t title for result
--sort, -s sort functions by name [true]
--help, -h show this help message
`
if (argv.help) {
console.log(usage)
process.exit()
}
var source = argv._[0]
if (!source) {
console.log(usage)
process.exit()
}
var contents = String(fs.readFileSync(source)).split('\n')
var classname = argv.classname
var title = argv.title ? argv.title : null
var output = argv.output
var sort = argv.sort === 'true'
var ind, tab, indent
if (classname) {
ind = findindex(contents, function (line) {
return line.indexOf('class ' + classname) > -1
})
indent = 2
tab = ' '
} else {
ind = 0
indent = 1
tab = ''
}
var results = []
var start, line, previous, cond1, cond2, signature, docstring, i, j, k
if (ind < 0) {
warning('no content parsed, try a different class name?')
process.exit()
}
for (i = ind; i < contents.length; i++) {
line = contents[i]
previous = (i > 0) ? contents[i - 1] : contents[i]
cond1 = line.indexOf(tab + 'def') === 0
cond2 = line.indexOf(tab + 'def _') === -1 && previous.indexOf(tab + '@') === -1
if (cond1 && cond2) {
signature = line.slice(indent * 4, line.length)
signature = signature.slice(0, signature.length - 1)
if (classname) {
signature = signature.replace('self, ', '')
signature = signature.replace('self', '')
}
for (j = i + 2; j < contents.length; j++) {
if (contents[j].indexOf(tab + tab + '"""') > -1) break
}
docstring = contents.slice(i + 2, j)
docstring = docstring.map(function (line) {
return line.slice(indent * 4, line.length)
}).filter(function (line) {
return (line !== '----------' && line !== 'Parameters')
}).map(function (line) {
if (line.indexOf(' ') === 0) return line.slice(4, line.length)
else return line
}).map(function (line) {
if (line.indexOf(' : ') > -1) {
start = line.indexOf(' : ')
line = '- **`' + line.slice(0, start) + '`**' + ' ' + '`' +
line.slice(start + 1, line.length).split(', ').join('` `') + '`' + '\n'
line = line.replace('`: ', '`')
return line
} else {
return line
}
})
for (k = 0; k < docstring.length; k++) {
if (docstring[k].indexOf('-') === 0) {
docstring[k + 1] = ' ' + docstring[k + 1]
}
}
results.push({
signature: signature,
docstring: docstring
})
}
}
if (sort) {
results = results.sort(function (a, b) {
return a.signature.localeCompare(b.signature)
})
}
if (results.length === 0) {
warning('no content parsed, maybe you need a class name?')
process.exit()
}
var final = []
results.forEach(function (func) {
final.push(['', '#### `' + func.signature + '`', ''])
final.push(func.docstring)
})
var flattened = flatten(final)
flattened = flattened.slice(1, flattened.length)
if (title) flattened = ['## ' + title, ''].concat(flattened)
if (output) fs.writeFileSync(output, flattened.join('\n'))
else console.log(flattened.join('\n'))
function warning (msg) {
console.log('[' + chalk.yellow('warning') + '] ' + msg)
}