当前位置:网站首页>长markdown文档的拆分与合并
长markdown文档的拆分与合并
2022-08-10 16:52:00 【zdlwhereyougo】
目的
使用vscode写长markdown文档时,实时渲染会卡顿,不方便检查,拆分成每一部分比较好检查。
在导出全文的时候,需要保证交叉引用不会出问题,最好再合并成一个文档。万能python实现了这个功能。这个功能不难,参考了站内【羊城迷鹿】的文章,对他的函数进行了改动,更符合我的要求。
from os.path import basename
import re
import os
from urllib.parse import quote
import warnings
warnings.filterwarnings('ignore')
def get_subtitle(content):
# 从md文件中获取h1标题
# 返回日期和索引
pattern = re.compile(r'#{1}(.*?)\n')#正则表达
heads = pattern.findall(content)
# print(heads)
# 防止出现空h1标题
subtitle = [h[1:] for h in heads if h[0] == ' ' and len(h) > 2]#以井号开头
indexes = [content.find(title+'\n')-2 for title in subtitle]
indexes.append(len(content))
return subtitle, indexes
def save_md(path, article):
# 保存分割后的文件
with 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):
continue
article = content[indexes[i]:indexes[i+1]]
save_md(article_path, article)
# 再定义一个合并文件的
# 合并的代码
def combine_md(filepath,savepath):
md_list = os.listdir(savepath)#列出保存路径下所有的md文件
file_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 + '\\' + md
with open(md_file, 'r', encoding='utf-8') as file:
contents.append(file.read() + "\n")
#构造输出路径
output_path=savepath+'\\'+fname+'.md'
with open(output_path,"w", encoding='utf-8') as file:
file.writelines(contents)
#拆分成小部分,更好查看,原始文件所在目录
filepath=r'D:\BaiduNetdiskWorkspace\V6.md'
#拆分后文件所在目录,为了方便,需要新建一个文件夹
savepath=r'D:\BaiduNetdiskWorkspace\newV6t'
#执行拆分命令
split_and_save_md(filepath,savepath)
#执行合并命令
# combine_md(filepath,savepath)
边栏推荐
猜你喜欢
cube-studio配置镜像仓库并允许
3 年 CRUD 从 8K 涨到 28K,谁知道这4个月我到底经历了什么?
Redis下载安装教程 (windows)
sprintboot验证码kaptcha 自定义图片样式
v-bind指令:设置元素的属性
阿里工作7年,肝到P8就剩这份学习笔记了,已助朋友拿到10个Offer
HTTP学习——协议与术语、HTTP、缓存、Cookie
Annual salary of 600,000+?This 100,000-word interview assault book covers all technology stacks from Ali P5 engineers to P7
Yi Gene|In-depth review: epigenetic regulation of m6A RNA methylation in brain development and disease
从宠爱到嫌弃,蒸发1500亿后,这家明星企业为何陨落?
随机推荐
剑指OfferⅡ 045.二叉树最底层最左边的值 dfs
MogDB学习笔记-从2开始(MogHA)
观测云入选 CNCF 云原生全景图
shell之正则表达式及三剑客awk命令
excel的lookup函数查找匹配关键字的坑
一张图快速了解 Istio 的 EnvoyFilter
MySQL数据库完全备份与增量备份怎么实现
一种新的测试方法:视觉感知测试
matsuri.icu 筛选单场直播中 指定用户的弹幕
ROBOTSTXT_OBEY[通俗易懂]
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)【6节点 9节点 14节点 26节点 30节点 57节点】(Matlab代码实现)
Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso「建议收藏」
RMAN-08120的处理
leetcode:1013. 将数组分成和相等的三个部分
等保2.0一个中心三重防护指的是什么?如何理解?
shell获取前n天的日期
被大厂面试官参考的Redis笔记,堪称Redis面试天花板
Knox 代理各类组件
ARM开发(三)ARM寻址方式,异常中断,异常向量表
一文带你彻底拿下a,b两点间等效电阻