Skip to content

Commit 2b2fd6a

Browse files
committed
wip, compile step
1 parent cdddefd commit 2b2fd6a

File tree

8 files changed

+132
-322
lines changed

8 files changed

+132
-322
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ Each param is associaated with the last function or method.
4545
#### comment `text...`
4646
Each comment is associated with the last keyword.
4747

48+
# CLI
49+
Hyper-docs cli will output an AST. It will also accept an AST as input
50+
and compile to either html (`--html`) or markdown (`--md`) pending on the
51+
specified flag.
52+
53+
```bash
54+
hyper-docs /path/to/projects | ./bin/docs --html
55+
```
4856

4957
# PARSER
5058
Outputs an AST. See [this][1] example.

bin/docs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('..')

compile.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const marked = require('marked')
2+
const hl = require('highlight.js')
3+
4+
const templateOperators = members => {
5+
const operators = members.filter(m => m.type === 'operator')
6+
7+
let table = `|Operator|Description|\n`
8+
table += `|:-------|:----------|\n`
9+
10+
table += operators.map(operator => {
11+
return `|${operator.name}|${operator.comment}|`
12+
}).join('\n')
13+
14+
return table
15+
}
16+
17+
const templateClass = (key, type) => `
18+
### \`${key}\`
19+
${type.comment}
20+
21+
${templateOperators(type.members)}
22+
`
23+
24+
const templateFunction = (key, type) => `
25+
### \`${key}\`
26+
${type.comment}
27+
`
28+
29+
const template = ast => `
30+
# [${ast.namespace}](${ast.repo})
31+
32+
## TYPES
33+
${
34+
Object.keys(ast.types).map(key => {
35+
const type = ast.types[key]
36+
37+
return type.members
38+
? templateClass(key, type)
39+
: templateFunction(key, type)
40+
}).join('\n')
41+
}
42+
`
43+
44+
const createHTML = s => {
45+
marked.setOptions({
46+
renderer: new marked.Renderer(),
47+
highlight: code => hl.highlightAuto(code).value
48+
})
49+
50+
return marked(s)
51+
}
52+
53+
const createMD = ast => {
54+
if (!ast.namespace) {
55+
return ''
56+
} else {
57+
ast.namespace = ast.namespace.join('::')
58+
}
59+
60+
return template(ast)
61+
}
62+
63+
module.exports = (buffers, type) => {
64+
buffers.forEach(ast => {
65+
const output = type === 'html'
66+
? createHTML(createMD(ast))
67+
: createMD(ast)
68+
69+
process.stdout.write(output)
70+
})
71+
}

index.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const fs = require('fs')
2+
const os = require('os')
23
const path = require('path')
34

45
const parse = require('./parse')
6+
const compile = require('./compile')
57

68
//
79
// Find all header files except in ./deps or hidden dirs
@@ -16,9 +18,13 @@ const read = d => {
1618
const p = paths[i]
1719
const base = path.basename(p)
1820

19-
if (base[0] === '.' || (base === 'deps')) {
20-
continue
21-
}
21+
const dontParse = (
22+
(base[0] === '.') ||
23+
(base === 'deps') ||
24+
(base === 'hyper-docs')
25+
)
26+
27+
if (dontParse) continue
2228

2329
const stat = fs.statSync(p)
2430

@@ -52,13 +58,31 @@ const parseUrl = s => {
5258
let url = s.split('//')[1]
5359
if (!url) url = s.split('@')[1]
5460

55-
return url.replace(':', '/').replace('.git', '')
61+
return 'https://' + url.replace(':', '/').replace('.git', '')
5662
}
5763

5864
//
5965
// Read all files from a path and parse their headers for docs
6066
//
6167
function main (argv) {
68+
if (argv[0] === '--md' || argv[0] === '--html') {
69+
let buffers = fs
70+
.readFileSync(0, 'utf8')
71+
.split(os.EOL)
72+
.filter(Boolean)
73+
74+
try {
75+
buffers = buffers.map(JSON.parse)
76+
} catch (err) {
77+
console.error(err.message)
78+
process.exit(1)
79+
}
80+
81+
const type = argv[0].slice(2)
82+
compile(buffers, type)
83+
return
84+
}
85+
6286
const files = read(argv[0])
6387

6488
for (const file of files) {
@@ -73,7 +97,7 @@ function main (argv) {
7397
const output = parse(s)
7498

7599
output.repo = parseUrl(pkg.repository.url)
76-
console.log(JSON.stringify(output, 2, 2))
100+
process.stdout.write(JSON.stringify(output) + '\n')
77101
}
78102
}
79103

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"homepage": "https://github.com/datcxx/hyper-docs#readme",
2020
"dependencies": {
21+
"highlight.js": "^9.15.6",
2122
"marked": "^0.6.2"
2223
}
2324
}

parse.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//
2-
// Parse each line
3-
//
41
module.exports = source => {
52
const DOC_RE = /\/\/\/(.*)[\n|\r]/g
63

@@ -25,15 +22,11 @@ module.exports = source => {
2522
functions: [],
2623
}
2724

28-
let lastClass = null
25+
let lastType = null
2926
let lastMethod = null
3027
let hasConstructor = false
3128
let last = null
3229

33-
//
34-
// Parses all lines of the file, the rule for docs is
35-
// last-one-in-wins like css.
36-
//
3730
const parseLine = line => {
3831
const words = line.split(' ')
3932
const keyword = words.shift()
@@ -53,18 +46,27 @@ module.exports = source => {
5346
break
5447
}
5548

49+
case 'function': {
50+
lastType = words[0].match(/\w+/)[0]
51+
last = lastMethod = output.types[lastType] = {}
52+
break
53+
}
54+
5655
case 'struct':
5756
case 'class': {
58-
lastClass = words.shift()
59-
last = output.types[lastClass] = []
57+
lastType = words.shift()
58+
last = output.types[lastType] = { members: [] }
6059
break
6160
}
6261

62+
case 'property':
6363
case 'operator': {
6464
last = {
65-
operator: words.join(' ')
65+
type: keyword,
66+
name: words.join(' ')
6667
}
67-
output.types[lastClass].push(last)
68+
69+
output.types[lastType].members.push(last)
6870
break
6971
}
7072

@@ -93,15 +95,6 @@ module.exports = source => {
9395
break
9496
}
9597

96-
case 'property': {
97-
last = {
98-
property: words.join(' ')
99-
}
100-
101-
output.types[lastClass].push(last)
102-
break
103-
}
104-
10598
case 'overload':
10699
case 'constructor':
107100
case 'method': {
@@ -134,7 +127,7 @@ module.exports = source => {
134127
hasConstructor = true
135128
}
136129

137-
output.types[lastClass].push(lastMethod)
130+
output.types[lastType].members.push(lastMethod)
138131

139132
break
140133
}

0 commit comments

Comments
 (0)