栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

新建hexo文章的优化操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

新建hexo文章的优化操作

在新建hexo文章的时候需要先进入hexo的目录再新建,而且新建的文章常常不知道该给他加什么标签和分类,还得去网站上看现在有啥。这篇文章介绍了快速新建文章并且显示辅助信息的一系列操作。

显示当前有的tag和category

首先需要获取到所有的信息,由于hexo文章结构都是固定的,因此获得信息页不是很难。首先使用sed -n '/tags:/,/categories:/p' *.md > test获取到tags和categories之间的文本并且保存在test文件中。1在观察这个文件的时候我发现里边有很多不需要的内容,所以又使用了sed '/^id/d' ./test | sed '/^date/d' > test2来删除以id和date开头的行并保存在test2中。2之后再使用sort -n ./test2 | awk '{if($0!=line)print; line=$0}' > test3将数据排序去重得到粗加工产物test3.3

然后使用python来读取这个文件并且进行细分,代码如下

#!/usr/bin/env python
f = open('./test3', 'r')
words = f.read().split('n')
tags = []
categories = []
for word in words:
    if "categories" in word:
        if len(word.split(':')) > 1:
            temp = word.split(':')[1].strip()
            if temp != '' and temp != '[]':
                categories.append(temp)
    else:
        if len(word.split(':')) > 1:
            temp = word.split(':')[1].strip()
            if temp != '' and temp != '[]':
                if '[' in temp:
                    temp = temp.replace('[', '')
                    temp = temp.replace(']', '')
                    temp = temp.replace(''', '')
                    temp = temp.split(',')
                    for tag in temp:
                        tags.append(tag)
                else:
                    tags.append(temp)

print ("填写范例:")
print (
'''
单标签:        多标签:

tags:           tags:
  hexo              [easy,leetcode]
''')
print ("现有的tags:")
for i in list(set(tags)):
    print (i, end=' ')
print ("nn现有的categories:")
for i in list(set(categories)):
    print (i, end=' ')
print ("n")
f.close()

开头的一行是因为在使用脚本调用的时候括号需要转义,如果加上用环境中的python去运行的话也可以实现并且简洁不少。4

最后把他们整合到一个sh文件中:

#! /bin/bash

sed -n '/tags:/,/categories:/p' *.md > test
sed '/^id/d' ./test | sed '/^date/d'  > test2
sort -n ./test2 | awk '{if($0!=line)print; line=$0}' > test3
./test.py
rm ./test ./test2 ./test3

这个文件要放在_posts目录下运行。

现在运行new.sh就可以看到结果

快速新建文章

利用了别名的传参技巧5,具体别名实现如下:

hexo='func(){ cd ~/study/newblog/source/_posts/ && hexo $1 $2 && sh new.sh && cd ~};func'

现在在任何位置都可以使用hexo相关命令,并且创建文件后会先打开这个文章便于编辑,并且提示你当前有的所有信息,最后回到~目录下。效果如下:

参考文献:
  1. https://blog.csdn.net/ichen820/article/details/118112985 ↩︎

  2. https://os.51cto.com/article/624709.html ↩︎

  3. https://cloud.tencent.com/developer/article/1392949?from=15425 ↩︎

  4. https://www.jianshu.com/p/b1a8264c2043 ↩︎

  5. https://www.qianjinyike.com/alias-%E5%88%AB%E5%90%8D%E5%91%BD%E4%BB%A4%E6%97%B6%E5%80%99%EF%BC%8C%E5%A6%82%E4%BD%95%E6%94%AF%E6%8C%81%E4%BC%A0%E5%8F%82%EF%BC%9F/ ↩︎

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号