-
Notifications
You must be signed in to change notification settings - Fork 49
/
godoc2md.py
executable file
·106 lines (83 loc) · 2.4 KB
/
godoc2md.py
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
#!/usr/bin/env python
#coding=utf-8
"""
根据翻译的格式约定
这个脚本用于半自动化生成待翻译的文档
例如要翻译bufio包
python godoc2md.py bufio > bufio.md
开始编辑翻译bufio.md吧!
by getwe
TODO:
还没搞定在哪个地方插入 "##内容".暂时先手动加一下
"""
import os
import sys
import string
import re
def getPackage(package):
cmdStr = "cd $GOROOT/src/pkg; godoc " + package
handle = os.popen(cmdStr)
# 结构体定义
structPattern = re.compile("^type\s(.*?)\s\w+\s{")
# 普通函数
funcPattern = re.compile("^func\s(\w+)\(.*?\)")
# 方法
methodPattern = re.compile("^func\s\(\w.*?\s(.*?)\)\s(\w+)")
print "#%s"%package
print ""
print "import \"%s\""%package
print "\n##简介\n\n##概览"
for line in handle.readlines():
line = line.strip('\n')
if line == "CONSTANTS":
print "###常量"
continue
if line == "VARIABLES":
print "###变量"
continue
# 结构体
match = structPattern.match(line)
if match:
name = match.group(1)
print "###type %s"%(name)
print "```go"
print line
#结构体定义有可能是多行,还不能加下面这行
#print "```"
continue
# 尝试匹配普通函数
match = funcPattern.match(line)
if match:
className = match.group(1)
print "###func %s"%(className)
print "```go"
print line
print "```"
continue
# 尝试匹配类方法
match = methodPattern.match(line)
if match:
className = match.group(1)
methodName = match.group(2)
print "###func (%s) %s"%(className,methodName)
print "```go"
print line
print "```"
continue
# 常量变量
if line == "const (" or line == "var (":
print "```go"
print line
continue
# 多行定义的结束
if line == ")" or line == "}":
print line
print "```"
continue
# 无法识别的行直接输出
print line
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage %s packageName"%sys.argv[0]
sys.exit(1)
getPackage(sys.argv[1])