博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个简单的将Markdown二级标题进行排序的脚本
阅读量:4456 次
发布时间:2019-06-08

本文共 1642 字,大约阅读时间需要 5 分钟。

我在写博客《Linux的1000个命令》的时候,相对二级标题进行一下排序,方便阅读和查找,于是就有了这个小程序。

#! /usr/bin/env python3import osimport subprocessclass BaseMethod:    @staticmethod    def run_cmd(cmd):        retcode, output = subprocess.getstatusoutput(cmd)        return retcode, output    @staticmethod    def write_file(filename, content):        with open(filename, 'w', encoding='UTF-8')as tf:            tf.write(content)    @staticmethod    def read_file(file_name, mode="r"):        if not os.path.exists(file_name):            raise IOError("No such file or directory: %s" % file_name)        with open(file_name, mode, encoding='UTF-8')as fp:            content = fp.read()        return contentf="linux.md"class SortMD:    def __init__(self, filename):        # self.filename = filename        self.content = BaseMethod.read_file(filename)        self.data = {}    def split_now(self):        sharp2 = self.content.split('##')        for i in sharp2:            if i.startswith('#'):                self.data['sharp1'] = i            else:                if 'sharp2' in self.data.keys():                    self.data['sharp2'].append('## '+i.strip('\n'))                else:                    self.data['sharp2'] = ['##' + i.strip('\n')]        print(self.data)        self.data['sharp2'].sort()        print(self.data)    def save2file(self, name):        s1 = self.data['sharp1']        s2 = "\n\n".join(self.data['sharp2'])        data = s1 + s2        print(data)        BaseMethod.write_file(name, data)def main():    md = SortMD(f)    md.split_now()    md.save2file("new.md")if __name__ == "__main__":    main()

转载于:https://www.cnblogs.com/jkhere/p/10793100.html

你可能感兴趣的文章
接口,lambda表达式与内部类(二)
查看>>
Phabricator是什么,代码审查工具
查看>>
Java虚拟机类加载机制
查看>>
UITextView,UIWebView 直接显示html代码
查看>>
DirectX:函数可以连接任意两个filter 分类: Direct...
查看>>
Android APP开发入门教程-Button 分类: JAVA ...
查看>>
WustOJ 1575 Gingers and Mints(快速幂 + dfs )
查看>>
js中,for循环里面放ajax,ajax访问不到变量以及每次循环获取不到数据问题总结...
查看>>
算法:求从1到n这n个整数的十进制表示中1出现的次数-- python 实现
查看>>
CSU 1160 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示
查看>>
LintCode 58: Compare Strings
查看>>
[Unity插件]Lua行为树(五):装饰节点Repeater
查看>>
顺序表、链表、栈和队列
查看>>
Linux第二天(Linux常用命令2)
查看>>
MySql知识体系
查看>>
JIRA中的标记语言的语法参考
查看>>
hdu 6318 Swaps and Inversions(归并排序)
查看>>
用css在IE7、8上实现圆角
查看>>
三维绿幕标定与跟踪
查看>>
android ProgressBar自定义半圆形进度条
查看>>