当前位置:网站首页>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)边栏推荐
猜你喜欢

Annual salary of 600,000+?This 100,000-word interview assault book covers all technology stacks from Ali P5 engineers to P7

百日刷题挑战--错题01day

Redis下载安装教程 (windows)

CDF 图的含义

#夏日挑战赛#【ELT.ZIP】啃论文俱乐部——学术科研方法论沉淀辑

数学基础(五)最优化理论(最优化,无约束,有约束,拉格朗日乘子的意义,KKT条件)

DeamNet代码学习||网络框架核心代码 逐句查找学习

网易云信亮相LiveVideoStackCon2022,解构基于WebRTC的开源低延时播放器实践

如何构建一个自己的代理ip池

植物肉,为何在中国没法“真香”?
随机推荐
node环境变量配置,npm环境变量配置
【无标题】
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)【6节点 9节点 14节点 26节点 30节点 57节点】(Matlab代码实现)
LabView---双通道示波器(内含信号发生器)
ahx文件转mav文件 工具分享及说明
shell获取前n天的日期
【云原生| Docker】 部署 Django & mysql 项目
一文带你彻底拿下a,b两点间等效电阻
李斌带不动的长安新能源高端梦,华为和“宁王”能救吗?
数学基础(五)最优化理论(最优化,无约束,有约束,拉格朗日乘子的意义,KKT条件)
需求骤降,成本激增,PC行业再次入冬
软件工程基础知识--需求分析
一文带你拿下信号卷积—常见信号卷积
promise笔记(二)
华为-坐标移动
Qt 绘图和绘图设备
取Json中的数组进行遍历
观测云入选 CNCF 云原生全景图
险资又做LP,一出手40亿
Annual salary of 600,000+?This 100,000-word interview assault book covers all technology stacks from Ali P5 engineers to P7