Skip to content

Change the hexo site to hugo site. Use python to format MD file.

Notifications You must be signed in to change notification settings

aimer1124/python-format-md

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-format-md

为什么做这个: 把原来博客的文章从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)

ToDo

  • 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
  • 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

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
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
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

  • 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)

参考

About

Change the hexo site to hugo site. Use python to format MD file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages