forked from dracupid/nodoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffee.coffee
78 lines (66 loc) · 2 KB
/
coffee.coffee
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
# Inspired by nokit: https://github.com/ysmood/nokit
_ = require 'lodash'
rule =
commentReg: /###\*([\s\S]+?)###\s+([\w\.]+)/g
splitReg: /^\s+\* ?@/m
tagNameReg: /^([\w\.]+)\s*/
typeReg: /^\{(.+?)\}\s*/
nameReg: /^(\w+)\s*/
nameTags: ['param', 'property']
descriptionReg: /^([\s\S]*)/
parseContent = (content, r) ->
# Unescape '\/'
content = content.replace /\\\//g, '/'
# Clean the prefix '*'
arr = content.split(r.splitReg).map (el) ->
el.replace(/^[ \t]+\*[ \t]?/mg, '').trim()
description: arr[0] or ''
tags: arr[1..].map (el)->
parseTag = (reg) ->
m = el.match reg
if m and m[1]
el = el[m[0].length..]
m[1]
else
null
tag = {}
tag.tagName = parseTag r.tagNameReg
type = parseTag r.typeReg
if type
tag.type = type.trim()
if tag.tagName in r.nameTags
tag.name = parseTag r.nameReg
tag.description = parseTag(r.descriptionReg) or ''
tag
###*
* Parse comment from source code
* @param {string} source source code
* @param {Object=} localRule optional, custom rule object, use once
* @return {Array} parsed comments object array
###
parse = (source, localRule = {})->
r = _.defaults localRule, rule
comments = []
while m = r.commentReg.exec source
content = parseContent m[1], r
lastIndex = r.commentReg.lastIndex
comments.push
name: m[2]
description: content.description
tags: content.tags
lineNum: source[...lastIndex].split('\n').length
comments
module.exports =
parse: parse
###*
* Set the rule of the parser
* @param {Object} ruleObj rule object
###
setRule: (ruleObj)->
_.assign rule, ruleObj
###*
* Hmm..., I'd like to use this to generate document.
* @return {Object} rule object
###
getRule: ()->
rule