从一个私有文字托管系统打包所有写的文字出来,批量生成指定格式文档。
摘要: 在牛逼的欢爷手把手传授下,写出批量生成 github
博文脚本
直接上代码啦:
# coding: utf-8
import os
import uuid
import random
import shutil
root = 'C:\\Users\\123\\Desktop\\Cmd-Markdowns-2016-09-12-19-01'
def createDate():
year = range(2015,2017)[random.randint(0,1)]
month = range(1,13)[random.randint(0,11)]
day = range(1,31)[random.randint(0,29)]
date = '%s-%02d-%02d' %(year,month,day)
return date
def createFilename():
return str(uuid.uuid4())
def createCate(c):
#print c[2][26:]
return c[2][27:].split()[0]
def createTags(c):
return c[2][27:-1]
def createTitle(c):
return c[0][2:-1]
def createAbst(c):
return ''.join(c[10:14])
def createNewContent(c):
nc = []
for line in c:
if line.startswith('###') and line[3] != ' ':
new_line = line[:3] + ' ' + line[3:]
elif line.startswith('##') and line[2] != ' ':
new_line = line[:2] + ' ' + line[2:]
elif line.startswith('#') and line[1] != ' ':
new_line = line[:1] + ' ' + line[1:]
else:
new_line = line
nc.append(new_line)
return nc
dir_list = os.listdir(root)
# print dir_list
if os.path.isdir('F:\\new-dir'):
shutil.rmtree('F:\\new-dir')
os.mkdir('F:\\new-dir')
for i in dir_list:
print i
sec_dir = os.listdir(root+ '\\' +i)
#print sec_dir
for j in sec_dir:
print j
content = open(root+ '\\' +i + '\\'+ j).readlines()
content = createNewContent(content)
title = createTitle(content)
date = createDate()
cate = createCate(content)
tags = createTags(content)
abst = createAbst(content)
f = open('F:\\new-dir' + '\\' + date + '-' + createFilename()+ '.md', 'w')
header = '''---
layout: post
title: %s
date: %s
categories: %s
tags: %s
---
* content
{:toc}
%s
'''
header_content = header %(title,date,cate,tags,abst)
f.write(header_content)
f.write(''.join(content))
f.close()
建议使用 notepad++
编辑器: 设置
——>首选项
——>新建
——>unix
——>UTF-8(无BOM)
,勾上应用于打开ANSI文件。