当前位置:网站首页>Splitting and merging long markdown documents

Splitting and merging long markdown documents

2022-08-10 17:18:00 zdlwhereyougo

Purpose

When using vscode to write a long markdown document, the real-time rendering will be stuck, which is inconvenient for inspection. It is better to split it into each part for inspection.
When exporting the full text, it is necessary to ensure that there will be no problems with cross-references, and it is best to combine them into one document.Universal python implements this function.This function is not difficult. With reference to the article [Yangcheng Lost Deer] in the station, his function has been changed, which is more in line with my requirements.

from os.path import basenameimport reimport osfrom urllib.parse import quoteimport warningswarnings.filterwarnings('ignore')def get_subtitle(content):# Get the h1 title from the md file# return date and indexpattern = re.compile(r'#{1}(.*?)\n')#Regular expressionheads = pattern.findall(content)# print(heads)# prevent empty h1 headerssubtitle = [h[1:] for h in heads if h[0] == ' ' and len(h) > 2]# start with a pound signindexes = [content.find(title+'\n')-2 for title in subtitle]indexes.append(len(content))return subtitle, indexesdef save_md(path, article):# save the split filewith open(path, 'w+', encoding='utf8') as f:f.write(article)def split_and_save_md(filepath,savepath):file_path,fullflname = os.path.split(filepath)fname,ext = os.path.splitext(fullflname)with open(filepath, 'r+', encoding='utf8') as f:content = f.read()if not os.path.exists(savepath):os.mkdir(savepath)sub_title, indexes = get_subtitle(content)for i in range(len(sub_title)):article_path = savepath+'/'+fname+'_'+sub_title[i]+'.md'if os.path.exists(article_path):continuearticle = content[indexes[i]:indexes[i+1]]save_md(article_path, article)# define another merged file# merged codedef combine_md(filepath, savepath):md_list = os.listdir(savepath)#List all md files in the save pathfile_path,fullflname = os.path.split(filepath)fname,ext = os.path.splitext(fullflname)fname_split=fname+'_'contents = []for md in md_list:if fname_split in md:md_file = savepath + '\\' + mdwith open(md_file, 'r', encoding='utf-8') as file:contents.append(file.read() + "\n")#Construct output pathoutput_path=savepath+'\\'+fname+'.md'with open(output_path,"w", encoding='utf-8') as file:file.writelines(contents)#Split into small parts for better viewing, the directory where the original file is locatedfilepath=r'D:\BaiduNetdiskWorkspace\V6.md'#The directory where the file is located after splitting, for convenience, you need to create a new foldersavepath=r'D:\BaiduNetdiskWorkspace\newV6t'#Execute split commandsplit_and_save_md(filepath, savepath)#Execute the merge command# combine_md(filepath, savepath)
原网站

版权声明
本文为[zdlwhereyougo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101652363077.html