为什么做这个: 把原来博客的文章从Hexo转移至Hugo,但两者在MD文件头部处理不一致。
- Hexo格式
title: 'Http-Header'
tags:
- API测试
- SuperTest
categories:
- Tool
date: 2016-02-29 20:14:00
thumbnail: /img/a.png
---
- Hugo格式
---
title: 'Http-Header'
tags:
- API测试
- SuperTest
categories:
- Tool
date: 2016-02-29 20:14:00
---
暂时发现,需要处理的内容有两处:
- 在文件首行添加
---
- 删除MD文件Header中的
thumbnail
字段(nice to have)
- Read MD file content
- Create source MD file
- Create covert file
- Read MD file
- Save the change to MD file
- Make Change to the MD file
- Save the change to
New
file
- Add
---
to the first line of the MD file - Delete
thumbnail
in the file format- Find
thumbnail
line - Delete the line
- Find
- Batch to update MD files in the
folder
- read the list in the
folder
- make the convert to the file in the list
- save the new file to the new
folder
- read the list in the
- Set source md file,
source.md
- Create
convert.py
as convert controller - Use
open(file, "r").read()
to get MD file content
- Make Change and save to new MD file
- Add
---
to new MD file - Delete the line with
thumbnail
lines = (i for i in sourceFile if 'thumbnail' not in i )
targetFile.writelines(lines)
- Use
readlines
to convert file:list.insert(index,obj)
for add---
andlist.remove(obj)
for deletethumbnail
sourceFileList = sourceFile.readlines()
sourceFileList.insert(0,"---\n")
for line in sourceFileList:
print(line)
if "thumbnail" in line:
sourceFileList.remove(line)
targetFile.writelines(sourceFileList)
- Refactor the code
def convert(sourceFile, targetFile):
sourceFile = open(source, "r")
targetFile = open(target,"w")
sourceFileList = sourceFile.readlines()
sourceFileList.insert(0,"---\n")
for line in sourceFileList:
# print(line)
if "thumbnail" in line:
sourceFileList.remove(line)
targetFile.writelines(sourceFileList)
targetFile.close()
sourceFile.close()
- Get the list in the folder
import os
sourceFileList = os.listdir(sourcePath)
- Convert the files
for fileName in sourceFileList:
print("Convert file: " + fileName)
target = "./target.md"
convert(sourcePath + fileName, targetPath + fileName)