Github源代码地址:https://github.com/aimer1124/python-format-md
为什么做这个:
把原来博客的文章从Hexo转移至Hugo,但两者在MD文件头部处理不一致。
1
2
3
4
5
6
7
8
9
  | title: 'Http-Header'
tags:
  - API测试
  - SuperTest
categories:
  - Tool
date: 2016-02-29 20:14:00
thumbnail: /img/a.png
---
  | 
1
2
3
4
5
6
7
8
9
  | ---
title: 'Http-Header'
tags:
  - API测试
  - SuperTest
categories:
  - Tool
date: 2016-02-29 20:14:00
---
  | 
暂时发现,需要处理的内容有两处:
- 在文件首行添加
--- - 删除MD文件Header中的
thumbnail字段(nice to have) 
Function#
Day by Day#
Day 1#
- Set source md file, 
source.md - Create 
convert.py as convert controller - Use 
open(file, "r").read() to get MD file content 
Day 2#
- Make Change and save to new MD file
 - Add 
--- to new MD file - Delete the line with 
thumbnail 
1
2
  | 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 --- and list.remove(obj) for delete thumbnail 
1
2
3
4
5
6
7
8
9
  | sourceFileList = sourceFile.readlines()
sourceFileList.insert(0,"---\n")
for line in sourceFileList:
    print(line)
    if "thumbnail" in line:
        sourceFileList.remove(line)
targetFile.writelines(sourceFileList)
  | 
Day 3#
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  | 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
 
1
2
  | import os
sourceFileList = os.listdir(sourcePath)
  | 
1
2
3
4
5
  | for fileName in sourceFileList:
    print("Convert file: " + fileName)
    target = "./target.md"
    convert(sourcePath + fileName, targetPath + fileName)
  |