花了一下午的事件做的程序,累死了
/*
*/
import json
import re
import os
import numpy as np
md_directory = r"D:\my-note\A_Ruan_Es6\B-javascript-tutorial-master\docs"
rules = ['arr', 'md']
layerStart=1
listDir=''
def getDir():
return listDir
def setDir(dirName):
global listDir
listDir=dirName
print("Genarating...")
order=-1
def getOrder():
# return ''
global order
order=order+1
if(order<9):
return '0'+str(order)
else:
return str(order)
idx = 1713753104951
id = 1
layer={
"1":"#",
"2":"##",
"3":"###",
"4":"####",
"5":"#####"
}
def getPatter():
pattern = f'^{rules[0]}.*\.{rules[1]}$'
return re.compile(pattern)
def getIdx():
global idx # 声明 idx 是全局变量
idx = idx + 1
return idx
def getId():
global id
id = id + 1
return id
def getMd():
pattern = getPatter()
files = os.listdir(listDir)
matched_files = [file for file in files if re.match(pattern, file)]
return matched_files[0]
def getTitle():
md_path = os.path.join(listDir, getMd())
with open(md_path, 'r', encoding='utf-8') as srt_file:
lines = srt_file.readlines()
titles_array = np.array(lines)
Title = titles_array.tolist()
Title = [title.replace("[^](#Head)", "") for title in Title]
res=[]
for title in Title:
if '#' in title:
res.append(title);
return res
rootName='root'
rootNode={}
finRoot={}
def rootNodeInit():
global rootNode
rootNode = {
"data": {
"id": "d0qa84inhg00",
"created": 1713751556670,
"text": "root"
},
"children": []
}
def finRootInit():
global finRoot
finRoot = {
"root": {
},
"template": "default"
, "theme": "fresh-blue",
"version": "1.4.43"
}
def createNode(nodeName):
newNode = {
"data": {
"id": "d0qarvse1p",
"created": 1713753104951,
"text": "\u81ea\u589e\u81ea\u51cf"
},
"children": []
}
newNode["data"]["text"] = nodeName
newNode["data"]["created"] = getIdx()
newNode["data"]["id"] += str(getId())
return newNode
def addChildren(root, child):
traverse_childre(rootNode, root, child)
def traverse_childre(node, root, childName):
if node["data"]["text"] == root:
node["children"].append(createNode(childName))
return
else:
for child in node["children"]:
traverse_childre(child, root, childName)
def generate_tree():
tag=False
curNode = 'root'
lev = layerStart#2
mid=layerStart+1
depest=layerStart+2
for line in getTitle():
if layer[str(depest)] in line:
newline = line.strip().replace(layer[str(depest)], '').replace(" ", "")
finline=getOrder()+newline
if lev == mid or lev == depest:
addChildren(curNode, finline)
lev = depest
elif layer[str(mid)] in line:
newline = line.strip().replace(layer[str(mid)], '').replace(" ", "")
finline=getOrder()+newline
if lev == depest or lev == mid:
curNode = rootName
addChildren(curNode,finline)
curNode = finline
lev = mid
elif layer[str(layerStart)] in line and tag==False:
newline = line.strip().replace(layer[str(layerStart)], '').replace(" ", "")
rootNode["data"]["text"]=newline
rootName=newline
tag=True
lev=layerStart
def debug(namehh):
finRoot["root"] = rootNode
print(json.dumps(finRoot, ensure_ascii=False, indent=4))
print(namehh)
def output_file():
finRoot["root"] = rootNode
km_path = os.path.join(listDir, getMd().replace('md', 'km'))
# print(str(finRoot).replace('\'', '\"'))
with open(km_path, 'w', encoding='utf-8') as km_file:
km_file.write(str(finRoot).replace('\'', '\"'))
print("Fine")
def generate(name,list):
rootNodeInit()
finRootInit()
global listDir
rules[0]=name
listDir=list
generate_tree()
# debug(getTitle())
output_file()
def run():
for listDir in [f for f in os.listdir(md_directory) if os.path.isdir(os.path.join(md_directory, f))]:
md_files = [f for f in os.listdir(os.path.join(md_directory,listDir)) if f.endswith('.md')]
for file in md_files:
id=1
order=-1
generate(file.split('.md')[0],os.path.join(md_directory,listDir))
run()