当前位置:网站首页>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)
边栏推荐
猜你喜欢
随机推荐
Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers
C专家编程 第10章 再论指针 10.3 在锯齿状数组上使用指针
等保2.0一个中心三重防护指的是什么?如何理解?
程序员兼职踩到的坑,本人亲身经历
【随笔】自己看的... 保存
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
leetcode:281. 锯齿迭代器
年薪60万+?这份10万字的面试突击宝典涵盖阿里 P5 工程师~P7 所有技术栈
BalsnCTF2021
aliexpress API 接入说明
Annual salary of 600,000+?This 100,000-word interview assault book covers all technology stacks from Ali P5 engineers to P7
从宠爱到嫌弃,蒸发1500亿后,这家明星企业为何陨落?
烟雾、空气质量、温湿度...自己徒手做个环境检测设备
How to realize full backup and incremental backup of MySQL database
MogDB学习笔记-从2开始(MogHA)
JWT 实现登录认证 + Token 自动续期方案
Qt 绘图和绘图设备
挑战用五行代码轻松集成登录系统,实现单点登录
Pytorch GPU模型推理时间探讨
强网杯2021final