hexo升级跳坑

Posted by Mr.Be1ieVe on Tuesday, April 7, 2020

应该是从4.0升级到4.2,_config.yml的更改就不说了,主要讲讲昨天晚上都干了啥(吐血捂肝

4.2多了个友链(万恶起源)上来之后,最开始的变化就是,所有文章的日期都变成了创建时间,即便修改时间比创建时间还早0.0

image-20200407074702787

于是,写了脚本给每个文章加上date,确定最早的发布/创建时间,并且,没有这个Front-matter是会报莫名其妙的YAMLException的。

import os
import sys
import time
import datetime
fpath = os.getcwd()
path=os.path.abspath(os.path.dirname(__file__))
#时间戳变成
def shift_time(filename):
    time_stamp_create = os.path.getctime(filename)
    time_stamp_modify = os.path.getmtime(filename)
    if time_stamp_create > time_stamp_modify:
        time_stamp = time_stamp_modify
    else :
        time_stamp = time_stamp_create
    dateArray = datetime.datetime.fromtimestamp(time_stamp)  
    return dateArray.strftime("%Y/%m/%d %H:%M:%S") 

def writeback(filename,text):
    s = ''.join(text)
    with open(filename , "w" , encoding='utf-8',) as f:
        f.write(s)

#将文件创建时间或修改时间,最早的一个,写入date中,作为文章时间
for parent, dirnames, filenames in os.walk(fpath):
    for filename in filenames:         
        temp=os.path.splitext(filename)
        file,type=temp
        if type == ".md":
            headline = False
            head_exist = False
            date_exist = False
            line_num = False
            create_date = shift_time(filename) 
            for line in open(filename,encoding='utf-8'):
                line_num += 1
                if line == "---\n":
                    headline += 1
                    continue
                if "date:" in line:
                    date_exist = True
                    break
                if headline == 2:
                    head_exist = True
                    break
                if line_num == 10:
                    break
            if date_exist:
                continue
            elif head_exist:
                text = []
                for line in open(filename,encoding='utf-8'): 
                    text.append(line)
                text.insert(1 , "date: " + create_date + '\n')
                writeback(filename,text)
                continue
            else:
                text = []
                for line in open(filename,encoding='utf-8'): 
                    text.append(line)
                text.insert(0 , "---\ndate: " + create_date + '\n---\n')
                writeback(filename,text)

这部分修改好了之后呢,跑起来测试之后发现图片加载不了了,原来的hexo-assets-image的图片src地址有误,没法用了,换了hexo-image-link之后,可以是可以了,但是我原先的结构不能用了QAQ

原先是图片都放在 markdown.assets/xxx.png 这样的,现在根据 官方和 image-link 的呢,只能改成 markdown/xxx.png 这样了 于是又写了这部分代码QAQ

#先找到.assets文件夹,然后再去找md文件替换掉文件里的xxx.assets/image的.assets
for folder in os.listdir():
    if folder == ".vscode":
        continue
    if os.path.isdir(folder)==True:
        temp=os.path.splitext(folder)
        file,type=temp
        if type == ".assets":
            for parent, dirnames, filenames in os.walk(fpath):
                for filename in filenames:  
                    if filename == file + '.md':
                        text = []
                        for line in open(filename,encoding='utf-8'): 
                            text.append(line.replace(".assets",""))
                        writeback(file + '.md',text)
        new_name=folder.replace('.assets','')
        os.rename(os.path.join(path,folder),os.path.join(path,new_name))

再加上调下typora的插入图片时改为复制到指定路径即可。

这样,我本地看也没有任何问题,发布出去也不会有问题,耶✌

懒才是最强生产力(确信

所以现在就是双击Winclick.bat 跑run.py。写完就丢嘿嘿

#run.py
import os
import sys
import time
import datetime
fpath = os.getcwd()
path=os.path.abspath(os.path.dirname(__file__))


def shift_time(filename):
    time_stamp_create = os.path.getctime(filename)
    time_stamp_modify = os.path.getmtime(filename)
    if time_stamp_create > time_stamp_modify:
        time_stamp = time_stamp_modify
    else :
        time_stamp = time_stamp_create
    dateArray = datetime.datetime.fromtimestamp(time_stamp)  
    return dateArray.strftime("%Y/%m/%d %H:%M:%S") 

def writeback(filename,text):
    s = ''.join(text)
    with open(filename , "w" , encoding='utf-8',) as f:
        f.write(s)

#将文件创建时间或修改时间,最早的一个,写入date中,作为文章时间
for parent, dirnames, filenames in os.walk(fpath):
    for filename in filenames:         
        temp=os.path.splitext(filename)
        file,type=temp
        if type == ".md":
            headline = False
            head_exist = False
            date_exist = False
            line_num = False
            create_date = shift_time(filename) 
            for line in open(filename,encoding='utf-8'):
                line_num += 1
                if line == "---\n":
                    headline += 1
                    continue
                if "date:" in line:
                    date_exist = True
                    break
                if headline == 2:
                    head_exist = True
                    break
                if line_num == 10:
                    break
            if date_exist:
                continue
            elif head_exist:
                text = []
                for line in open(filename,encoding='utf-8'): 
                    text.append(line)
                text.insert(1 , "date: " + create_date + '\n')
                writeback(filename,text)
                continue
            else:
                text = []
                for line in open(filename,encoding='utf-8'): 
                    text.append(line)
                text.insert(0 , "---\ndate: " + create_date + '\n---\n')
                writeback(filename,text)
   
for folder in os.listdir():
    if folder == ".vscode":
        continue
    if os.path.isdir(folder)==True:
        temp=os.path.splitext(folder)
        file,type=temp
        if type == ".assets":
            for parent, dirnames, filenames in os.walk(fpath):
                for filename in filenames:  
                    if filename == file + '.md':
                        text = []
                        for line in open(filename,encoding='utf-8'): 
                            text.append(line.replace(".assets",""))
                        writeback(file + '.md',text)
        new_name=folder.replace('.assets','')
        os.rename(os.path.join(path,folder),os.path.join(path,new_name))

os.system(('cd {} && hexo clean && hexo gen && hexo deploy').format(path))

「真诚赞赏,手留余香」

Mr.Be1ieVe's Treasure

真诚赞赏,手留余香

使用微信扫描二维码完成支付