-
Notifications
You must be signed in to change notification settings - Fork 2
/
SKU_generator_wiki_CN_MD.py
73 lines (61 loc) · 2.43 KB
/
SKU_generator_wiki_CN_MD.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
#coding:utf-8
import os
import re
class MarkdownFolder():
def __init__(self, folder_path):
self._folder_path = folder_path
self._folder_file_list = os.listdir(self._folder_path)
self._mdfile_dict = {}
self._mdfile_num = self.getMdfileNum()
self._file_num = self.getFileNum()
self._index = "<!DOCTYPE html>\r\n\
<html>\r\n\
<head>\r\n\
<link rel=\"canonical\" href=\"http://wiki.seeedstudio.com/cn/%s/\"/>\r\n\
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\r\n\
<meta http-equiv=\"refresh\" content=\"0;url=http://wiki.seeedstudio.com/cn/%s/\"/>\r\n\
</head>\r\n\
</html>"
def getMdfileNum(self):
num = 0
for filename in self._folder_file_list:
if '.md' in filename:
num += 1
self._mdfile_dict.setdefault(filename,'')
return num
def getFileNum(self):
return self._folder_file_list.__len__()
def getMdfileSku(self):
pattern = re.compile(r'\d{9}')
for filename in self._mdfile_dict:
with open((self._folder_path + '/' + filename), 'r') as f:
for eachLine in f:
if eachLine.find('sku:') != -1:
sku=pattern.search(eachLine, pos=4)
if sku:
t = sku.group()
self._mdfile_dict[filename] = t
def printDict(self):
for filename in self._mdfile_dict:
print("{} \t {}".format(filename, self._mdfile_dict[filename]))
# 1. make a folder called "sku"
# 2. read _mdfile_dict and make sub folders of "sku" with the value of _mdfile_dict
# 3. write a index file
def makeSkuIndex(self):
for mdfile in self._mdfile_dict:
sku = self._mdfile_dict[mdfile]
board_name = mdfile.split('.md')
if sku is not '':
try:
with open(PATH + "/%s.md" % sku, 'w') as fd:
fd.write((self._index % (board_name[0], board_name[0])))
#if boards sku repeat, how to do?
except OSError as e:
print(e)
print("sku重复: {0:_<40}{1}".format(board_name[0], sku))
if __name__ == '__main__':
PATH = os.getcwd() + "\\docs"
md = MarkdownFolder(PATH)
md.getMdfileSku()
md.makeSkuIndex()
# md.printDict()