关于:

  • Hexo 文件夹系统(已失效)
  • Blog 在 Gitee 上自动更新

Hexo文件夹系统

注意,该POST已过时

在某一个Hexo的版本更新时,其原生地支持了子文件夹的读取,所以本节所表述的方法已不在有必要。

因为最近累积的笔记越来越多了,而 Hexo 原生似乎不支持文件夹操作,故决定建立文件夹系统。它的思路很简单,就是在一个别处的文件夹下完成笔记的记录,在需要更新的时候,使用脚本递归地搜索这些文件夹,将所有的 md 文件全部收集到对应的_posts文件夹下。具体脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import os
import shutil

ori_path = '../files'
dst_path = '../source/_posts/'

def get_file(path, name, cat):
if os.path.isdir(path):
cat_c = cat.copy()
cat_c.append(name)
files = os.listdir(path)
for file in files:
get_file(path + '/' + file, file, cat_c)
else:
if '.md' in name:
route = '-'.join(cat[1:])
shutil.copyfile(path, dst_path + route + "-" + name)

get_file(ori_path, '', [])
print('Done!')

自动更新Gitee

由于 Gitee 每一次更新 Blog 后需要访问 Gitee 手动完成 deploy。这个过程是在令人不爽。所以添加一个脚本用来自动完成这一步。具体依赖 selenium。

安装selenium

进入谷歌商店,进入拓展商店下载 selenium。录制结束后导出为 py 文件。注意在这个过程中,需要将部分路径改为xpath。此外可能有 xpath 不能生效的情况,此时需要在该语句前添加 flush()。最后,由于 deploy 需要一些时间,所以需要使用 time 函数手动停止一些时间以完成 deploy。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestBlog():
def setup_method(self):
options = webdriver.ChromeOptions()
self.driver = webdriver.Chrome()
self.vars = {}

def teardown_method(self):
self.driver.quit()

def wait_for_window(self, timeout = 2):
time.sleep(round(timeout / 1000))
wh_now = self.driver.window_handles
wh_then = self.vars["window_handles"]
if len(wh_now) > len(wh_then):
return set(wh_now).difference(set(wh_then)).pop()

def test_blog(self):
self.driver.get("https://gitee.com/yoursite/pages")
self.driver.set_window_size(1514, 984)
self.driver.find_element(By.LINK_TEXT, "Sign in").click()
self.driver.find_element(By.ID, "user_login").click()
self.driver.find_element(By.ID, "user_login").send_keys("email@foobar.com")
self.driver.find_element(By.ID, "user_password").send_keys("password")
self.driver.find_element(By.CSS_SELECTOR, ".two:nth-child(3) label").click()
self.driver.find_element(By.NAME, "commit").click()
self.driver.refresh()
self.driver.find_element(By.CSS_SELECTOR, ".redeploy-button").click()
assert self.driver.switch_to.alert.text == "Are you sure to redeploy Gitee Pages?"
self.driver.switch_to.alert.accept()
time.sleep(60)

test = TestBlog()
test.setup_method()
test.test_blog()
test.teardown_method()

完成上述步骤后,结合之前的自动添加空格以及基本的 hexo 操作,我们就可以全自动地完成文件夹整理及 deploy 操作了。如下:

1
2
3
4
5
6
@echo off
C:
cd C:\Users\13808\Documents\Joseph\Blog\update
python update_file.py
cd C:\Users\13808\Documents\Joseph\Blog
textlint --fix source/_posts/*.md && hexo clean && hexo g && hexo d && cd C:\Users\13808\Documents\Joseph\Blog\update && python gitee.py