This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
forked from linuxdeepin/developer-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·125 lines (109 loc) · 3.81 KB
/
build.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import markdown
import codecs
from collections import defaultdict
if __name__ == "__main__":
print("Genarator mkdocs.yml")
shutil.copyfile("mkdocs.yml.base","mkdocs.yml")
if not os.path.exists("docs/index"):
os.makedirs("docs/index")
fpmkdocs = codecs.open("mkdocs.yml", mode="a+", encoding="utf-8")
directory = u"docs"
toc = defaultdict(list)
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.md'):
path = root + "/" + file
with codecs.open(path, mode="r", encoding="utf-8") as fin:
data = fin.read().splitlines(True)
data = data[1:]
text = ""
for l in data:
text += l
md = markdown.Markdown(extensions = ['meta'])
md.convert(text)
if not md.Meta.has_key("category"):
continue
category = md.Meta["category"]
title = md.Meta["title"]
if category[0] == u"index":
continue
path = path.replace(directory+"/", "", 1)
print(path, category, title)
page_item = u" - ['" + unicode(path) + u"', '"+ category[0] + u"', '" + title[0] + u"']"
print("Add:", page_item)
list_item = (title[0], path)
toc[category[0]].append(list_item)
fpmkdocs.write(page_item+"\n")
index = u'''<!--Meta
title:
category:首页
DO NOT Delete Meta Above-->
<div class="minecraft clearfix">\n
'''
for k, v in toc.items():
item_count = 0
cate = u'<div class="section section-api">'
head = '\t<h3>' + k + '</h3>\n'
cate += head
cate += '\t<ul class="list-unstyled" style="line-height: 120%;">\n'
for item in v:
if item_count >= 4:
break
if 0 == len(item[0]):
continue
item_count += 1
cate += '\t\t<li><a href="' + item[1][:-3]+ '">' + item[0]+ '</a></li>\n'
if item_count >= 4:
cate += '\t\t<li><a href="/index/'+ k +'">More>></li>'
cate += '\t</ul>\n'
cate += '\t</div>\n'
if 0 == item_count:
cate = u''
index += cate
index += '</div>\n'
fp = codecs.open("docs/index.md", mode="w", encoding="utf-8")
fp.write(index)
fp.close()
#create index.md of catory
for k, v in toc.items():
if k == u'首页':
page_item = " - ['" + "index.md" + "', '"+ "index" + "', '" + k + "']"
print("Add:", page_item)
fpmkdocs.write(page_item+"\n")
continue
indexfile="docs/index/"+k+".md"
relativepath ="index/"+k+".md"
index = u'''<!--Meta
title:{}
category:index
DO NOT Delete Meta Above-->
<div class="minecraft clearfix">\n
'''
index = index.format(k)
cate = '<div>\n'
head = '\t<h2>' + k + '</h2>\n'
cate += head
cate += '\t<ul class="list-unstyled" style="line-height: 200%; font-size: 150%">\n'
for item in v:
if 0 == len(item[0]):
continue
cate += '\t\t<li><a href="/' + item[1][:-3]+ '">' + item[0]+ '</a></li>\n'
item_count += 1
cate += '\t</ul>\n'
cate += '\t</div>\n'
if 0 == item_count:
cate = u''
index += cate
index += '</div>\n'
index += '</div>\n'
page_item = " - ['" + relativepath + "', '"+ "index" + "', '" + k + "']"
print("Add:", page_item)
fpmkdocs.write(page_item+"\n")
fp = codecs.open(indexfile, mode="w", encoding="utf-8")
fp.write(index)
fp.close()
fp.close()