当前位置:网站首页>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)边栏推荐
猜你喜欢
随机推荐
深度学习培训二笔记
长markdown文档的拆分与合并
浅析端口扫描原理
requests库访问接口
leetcode:281. 锯齿迭代器
How to use bitwise operators in C language
挑战用五行代码轻松集成登录系统,实现单点登录
招聘分析2020.6.1
华为-坐标移动
最详解决:jupyter notebook不会自动打开浏览器问题
为什么某互联网企业开200w年薪大家都支持,但是中金开100w年薪大家都在骂?...
kuangbin专题一 简单搜索
LabView---双通道示波器(内含信号发生器)
聊聊云原生数据平台
一文带你彻底拿下a,b两点间等效电阻
Annual salary of 600,000+?This 100,000-word interview assault book covers all technology stacks from Ali P5 engineers to P7
8.9模拟赛总结
C专家编程 第10章 再论指针 10.8 轻松一下---程序检验的限制
超宽带uwb精准定位,厘米级室内定位技术,实时高精度方案应用
Qt 绘图和绘图设备









