import os from pickle import FALSE, TRUE from secrets import choice import shutil import os import re import time BlogMdPath = os.path.dirname(os.getcwd()) + "\myblog\content\post" BlogPicPath = os.path.dirname(os.getcwd()) + "\myblog\static" NotePath = os.getcwd().replace('\\','/') + str("\笔记文件\\2.笔记") PicPath = os.getcwd().replace('\\','/') + str("\笔记文件\\附件") outStr = open(r'out.md','r',encoding="utf-8") outStrData = outStr.read() inStr= open(r'in.md','r',encoding="utf-8") inStrData = inStr.read() usualStr = open(r'usual.md','r',encoding="utf-8") usualStrData = usualStr.read() currentPath = os.getcwd().replace('\\','/') # 获取当前路径 fileList = [] #文本列表容器 dirPath = [] allMdPath = [] allPicPath = [] blogOutPath = [] pattern = re.compile(r'\[\[.*?\]\]') def ChangeDir(path): global currentPath os.chdir(path) currentPath = os.getcwd().replace('\\','/') dataNames = os.listdir(currentPath) for fileObj in dataNames: name = str(fileObj) if(".md" in name): file = open(name,'r',encoding= "utf-8") fileList =file.readlines() for i in range(len(fileList)): if('#unity/教程' in fileList[i]): dirPath.append(name) if(name not in allMdPath): allMdPath.append(name) blogOutPath.append(name.replace(' ','')) for dirfile in dirPath: file = open(dirfile,'r',encoding= "utf-8") fileList =file.readlines() for i in range(len(fileList)): if(r'[[' in fileList[i] and r']]' in fileList[i]): if("|" in fileList[i]): name = str(fileList[i].replace(r'[[','').replace(']]','').replace("\n","").split('|')[0]) else: name = str(fileList[i].replace(r'[[','').replace(']]','').replace("\n","")) CheckLink(name) def CheckLink(fileName): if(r'.png' not in fileName and r'.jpg' not in fileName): newName = str(fileName.replace(r'[[','').replace(r']]','')+".md") if(newName not in allMdPath): allMdPath.append(newName) file = open(newName,'r',encoding= "utf-8") fileList =file.readlines() for i in range(len(fileList)): matches = pattern.findall(fileList[i]) for match in matches: CheckLink(str(match.replace(r'[[','').replace(']]','').split('|')[0])) def ChangeTxt(fileObj): global outStrData global inStrData global usualStrData trueName = fileObj.split('.')[0] modify_time = os.path.getmtime(fileObj) modify_time_local = time.localtime(modify_time) file = open(fileObj,'r',encoding= "utf-8") filedata = file.read() timeStr = str(time.strftime('%Y-%m-%d', modify_time_local)).replace("2024-04-24","2024-04-22").replace("2024-04-23","2024-04-22") if(fileObj in blogOutPath): newhearstrdata = outStrData.replace(r'"0"','"' + trueName + '"').replace(r'"1"','"'+ timeStr + '"') else: if(r'#unity/日常积累' in filedata): newhearstrdata = usualStrData.replace(r'"0"','"' + trueName + '"').replace(r'"1"','"'+ timeStr + '"') else: newhearstrdata = inStrData.replace(r'"0"','"' + trueName + '"').replace(r'"1"','"'+ timeStr + '"') togetherTxt = newhearstrdata + filedata matches = pattern.findall(togetherTxt) allnewStrDic = {} for match in matches: if(r'.png' not in match): newName = str(match.replace(r'[[','').replace(r']]','').split('|')[0]) newStr = r'[' + newName + r']({{< ref "/post/' + newName.replace(' ','') + r'.md"' + r" >}})" elif(r'.png' in match): newName = str(match.replace(r'[[','').replace(r']]','')) allPicPath.append(newName) newStr = r'[](/'+newName.replace(' ','')+r')' allnewStrDic[match] = newStr for key,value in allnewStrDic.items(): togetherTxt = togetherTxt.replace(key,value) return togetherTxt def CopyMdFile(): for mdName in allMdPath: name = str(mdName) shutil.copy2(currentPath + "/" + name,BlogMdPath + "/" + name.strip().replace(' ','')) def CopyPicFile(): for picName in allPicPath: name = str(picName) shutil.copy2(PicPath + "/" + name,BlogPicPath + "/" + name.strip().replace(' ','')) def ChangeMdFile(): os.chdir(BlogMdPath) currentPath = os.getcwd().replace('\\','/') dataNames = os.listdir(currentPath) for fileObj in dataNames: name = str(fileObj) newTxt = ChangeTxt(fileObj) with open(name,'w',encoding= "utf-8") as file: file.write(newTxt) def delete_files_in_folder(folder_path): for filename in os.listdir(folder_path): file_path = os.path.join(folder_path, filename) try: if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print(f'Failed to delete {file_path}. Reason: {e}') ChangeDir(NotePath) delete_files_in_folder(BlogMdPath) CopyMdFile() ChangeMdFile() delete_files_in_folder(BlogPicPath) CopyPicFile()