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
| # 方法的固定功能说明。
def generateSumary(oriText,className):
if oriText.find('init') >= 0:
return u"类初始化。"
elif oriText.lower().find('viewdidload') >= 0:
return u"页面加载完成。"
elif oriText.lower().find('viewdidappear') >= 0:
return "页面已经显示。"
elif oriText.lower().find('viewwillappear') >= 0:
return "页面将要显示。"
elif oriText.lower().find('viewwilldisappear') >= 0:
return u"页面将要消失。"
elif oriText.lower().find('viewdiddisappear') >= 0:
return u"页面已经消失。"
else:
return u""
# 设置段落格式
def setText(doc,text,size,bold):
p1 = doc.add_paragraph()
p1.paragraph_format.first_line_indent = Cm(0.74)
p1.paragraph_format.line_spacing = 1.5 # 1.5倍行距
l1 = p1.add_run(text)
l1.font.size = size
l1.font.name = u"宋体"
l1.bold = bold
# 具体内容添加
def addDetail(doc,text1,textArr):
setText(doc,text1,Pt(14),True)
for text in textArr:
# print(text+'\n')
setText(doc,text,Pt(14),False)
# 将源文件分析完成后,输出到目标目录下
def writeToWord(sourcePath,outPath):
# 创建空文档
document = Document()
document.styles['Normal'].font.name = u'宋体'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
# 添加标题,设置级别level,0为Title,1或省略为Heading 1,0<=level<=9
# document.add_heading('Document Title', 0)
path = sourcePath+''
arrT = []
for dirpath, dirname, filenames in os.walk(path):
for filename in filenames:
if '.m' in filename or '.mm' in filename:
filePath = os.path.join(dirpath, filename)
MacroWithCountDicts = getOCFileMethdLineCountArray(filePath)
print('=====================')
print('{} 有 {} 个方法'.format(filename,len(MacroWithCountDicts)))
arrT += MacroWithCountDicts
elif '.swift' in filename:
filePath = os.path.join(dirpath, filename)
MacroWithCountDicts = getSwiftFileMethdLineCountArray(filePath)
print('=====================')
print('{} 有 {} 个方法'.format(filename,len(MacroWithCountDicts)))
arrT += MacroWithCountDicts
print("********************************")
print("一共有 {} 个方法".format(len(arrT)))
print("********************************")
for result in arrT:
fullFuncName = result[1]
allMsg = []
if fullFuncName.strip().startswith('+') or fullFuncName.strip().startswith('-'):
allMsg = anyliseOC(fullFuncName)
else:
allMsg = anyliseSwift(fullFuncName)
className = result[0]
funcName = allMsg[0]
paras = allMsg[1]
returnStr = allMsg[2]
# # 函数名设置为4级标题
document.add_heading(funcName,level=4)
# #功能设置为5级标题
document.add_heading(u"方法功能描述",level=5)
setText(document,generateSumary(funcName,className),Pt(14),False)
# 方法定义设置为5级标题
document.add_heading(u"方法定义",level=5)
addDetail(document,u"类名:",[className])
addDetail(document,u"方法名:",[funcName])
addDetail(document,u"方法入参:",paras)
addDetail(document,u"抛出异常:",[u"无"])
addDetail(document,u"方法返回值:",[returnStr])
# 方法的逻辑设置为5级标题
document.add_heading(u"方法逻辑",level=5)
# 一些固定方法填充内容。
# if 'NSCoder' in funcName:
# setText(document,u"调用fatalError方法。",Pt(14),False)
# elif 'init' in funcName:
# setText(document,u"父类初始化,",Pt(14),False)
# else:
setText(document,u"",Pt(14),False)
try:
document.save(outPath+"生成iOS方法文档.docx")
except Exception:
print(u"检查下当前目录是否存在,是否包含/")
else:
print(u"完成,请打开"+outPath+u"目录下的文件")
|