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

电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)【6节点 9节点 14节点 26节点 30节点 57节点】(Matlab代码实现)

数据库注入提权总结(二)

电力系统潮流计算与PowerWorld仿真(牛顿拉夫逊法和高斯赛德尔法)(Matlab实现)

电力系统潮流【牛顿-拉夫逊法】(4节点、5节点、6节点、9节点)(Matlab代码实现)

How to use bitwise operators in C language

Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers

Meaning of CDF graph

v-model指令:获取和设置表单元素的值

Pytorch GPU模型推理时间探讨

kuangbin专题一 简单搜索
随机推荐
Knox 代理各类组件
险资又做LP,一出手40亿
ahx文件转mav文件 工具分享及说明
重庆新壹汽与一汽集团达成新能源项目战略合作,赋能“碳中和”创造“碳财富”
glut库更新旧程序无法完成编译问题描述
leetcode:337. 打家劫舍 III
【荣耀智慧服务】快捷服务开发指南
pytorch 模型GPU推理时间探讨3——正确计算模型推理时间
【QT VS项目名称修改】
【科研】常见火灾数据集
蓝桥ROS之 cmake gcc g++ 默认版本和升级
JWT 实现登录认证 + Token 自动续期方案
PC软件问题二[Win10系统将UltraEdit添加到右键菜单的方法]
招聘分析2020.6.1
等保2.0一个中心三重防护指的是什么?如何理解?
神经网络如何提高准确率,神经网络的求解方式
C专家编程 第10章 再论指针 10.8 轻松一下---程序检验的限制
个人博客部署注意的几点问题
植物肉,为何在中国没法“真香”?
HTTP学习——协议与术语、HTTP、缓存、Cookie