forked from caixw/apidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
171 lines (144 loc) · 3.79 KB
/
input.go
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
171
// Copyright 2016 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// Package input 用于处理文件输入,过滤代码,生成 types.Doc 数据。
//
// 多行注释和单行注释在处理上会有一定区别:
// - 单行注释,风格相同且相邻的注释会被合并成一个注释块;
// - 单行注释,风格不相同且相邻的注释会被按注释风格多个注释块;
// - 多行注释,即使两个注释释块相邻也会被分成两个注释块来处理。
package input
import (
"errors"
"os"
"path/filepath"
"sync"
"time"
"github.com/tanxiaolong/apidoc/input/encoding"
"github.com/tanxiaolong/apidoc/input/syntax"
"github.com/tanxiaolong/apidoc/locale"
"github.com/tanxiaolong/apidoc/types"
"github.com/tanxiaolong/apidoc/vars"
)
// 需要解析的最小代码块,小于此值,将不作解析
// 即其长度必须大于 @api 这四个字符串的长度
const miniSize = len(vars.API) + 1
// Parse 分析源代码,获取相应的文档内容。
func Parse(options ...*Options) (*types.Doc, time.Duration) {
start := time.Now()
docs := types.NewDoc()
wg := &sync.WaitGroup{}
for _, o := range options {
wg.Add(1)
go func(o *Options) {
if err := parse(docs, o); err != nil {
o.ErrorLog.Println(err)
}
wg.Done()
}(o)
}
wg.Wait()
if len(docs.Title) == 0 {
docs.Title = vars.DefaultTitle
}
return docs, time.Now().Sub(start)
}
func parse(docs *types.Doc, o *Options) error {
blocks, found := langs[o.Lang]
if !found {
return errors.New(locale.Sprintf(locale.ErrUnsupportedInputLang, o.Lang))
}
paths, err := recursivePath(o)
if err != nil {
return errors.New(locale.Sprintf(locale.ErrUnsupportedInputLang, o.Lang))
}
wg := sync.WaitGroup{}
defer wg.Wait()
for _, path := range paths {
wg.Add(1)
go func(path string) {
parseFile(docs, path, blocks, o)
wg.Done()
}(path)
}
return nil
}
// Encodings 返回支持的编码方式
func Encodings() []string {
return encoding.Encodings()
}
// 分析 path 指向的文件,并将内容写入到 docs 中。
func parseFile(docs *types.Doc, path string, blocks []blocker, o *Options) {
docs.Title = o.Title
data, err := encoding.Transform(path, o.Encoding)
if err != nil {
if o.ErrorLog != nil {
o.ErrorLog.Println(err)
}
return
}
l := &lexer{data: data, blocks: blocks}
var block blocker
wg := sync.WaitGroup{}
defer wg.Wait()
for {
if l.atEOF() {
return
}
if block == nil {
block = l.block()
if block == nil { // 没有匹配的 block 了
return
}
}
ln := l.lineNumber() + o.StartLineNumber // 记录当前的行号,顺便调整起始行号
rs, ok := block.EndFunc(l)
if !ok {
syntax.OutputError(o.ErrorLog, path, ln, locale.ErrNotFoundEndFlag)
return // 没有找到结束标签,那肯定是到文件尾了,可以直接返回。
}
block = nil
if len(rs) < miniSize {
continue
}
wg.Add(1)
go func(rs []rune, ln int) {
i := &syntax.Input{
File: path,
Line: ln,
Data: rs,
Error: o.ErrorLog,
Warn: o.WarnLog,
}
syntax.Parse(i, docs)
wg.Done()
}(rs, ln)
} // end for
}
// 按 Options 中的规则查找所有符合条件的文件列表。
func recursivePath(o *Options) ([]string, error) {
paths := []string{}
extIsEnabled := func(ext string) bool {
for _, v := range o.Exts {
if ext == v {
return true
}
}
return false
}
walk := func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() && !o.Recursive && path != o.Dir {
return filepath.SkipDir
} else if extIsEnabled(filepath.Ext(path)) {
paths = append(paths, path)
}
return nil
}
if err := filepath.Walk(o.Dir, walk); err != nil {
return nil, err
}
return paths, nil
}