今天放代码的时候,突然觉得代码太烂太长,放在那边就扫了大家浏览的兴致,所以准备给所有的代码段加个折叠块,但是加折叠块必须要每个文件修改,很蛋疼,所以就写了个小工具去自动添加
我这个只能添加一次,运行两次就炸了~
fold.py
import os if __name__ == "__main__": path = '/media/zqh/文档/Blog/gitio/source/_posts' files = os.listdir(path) files = [it for it in files if '.md' in it] startflag = True cnt = 1 for i in range(len(files)): with open(path+'/'+files[i], 'r+') as f: lines = f.readlines() for i in range(len(lines)): if startflag: if ('```c' in lines[i]) or ('```cpp' in lines[i]) or\ ('```python' in lines[i]): pos = lines[i].find('`') strl = list(lines[i]) strl.insert(pos, '{% fold 点击显示内容 %}\n'+pos*' ') lines[i] = ''.join(strl) startflag = False else: if '```' in lines[i]: pos = lines[i].find('`') strl = list(lines[i]) strl.insert(-1, '\n'+pos*' '+'{% endfold %}\n') lines[i] = ''.join(strl) print('修改了{}处'.format(cnt)) cnt += 1 startflag = True f.seek(0) f.truncate() text = ''.join(lines) f.write(text)
|