Python: Gridea posts change tags

2019-12-22 Views Programming | Python329字3 min read
featureimg

Tags can classify each article in its category. But when you need to change tags in Gridea, you will find it very difficult since the maker doesn't make this function in Gridea. Thus, I write this program to solve this problem.

Before using the python program, you need to do this:

  1. Put a dict.txt in your post file, ususally in
C:\Users\User_name\Documents\Gridea\posts
  1. write you dict.txt like this:
Tag name [before] [Space] Tag name [after]
EG:
ACSL American Computer Science League
  1. Put the following python program in the same folder as dic.txt
import os
import linecache
import sys

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

files = os.listdir()
re = []
dic = {}
key = []

read = open("dict.txt","r",encoding="utf-8")
read = read.readlines()

for i in range(0,len(read)):
    temp = read[i].split()
    dic[temp[0]] = temp[1]

for file in files:
    (filename, extension) = os.path.splitext(file)
    if (extension == '.md'):
        re.append(filename+extension)

for i in range(0,len(re)):
    temp = []
    line = linecache.getline(re[i], 4)
    q = line.find("[")
    h = line.find("]")
    if h - q == 1:
        key.append(temp)
        continue

    comma = line.find(",")

    if comma == -1:
        substring = line[q+1:h]
        print()
        temp.append(dic[substring])
    else:
        while comma != -1:
            substring = line[q+1:comma]
            temp.append(dic[substring])
            q = comma
            comma = line.find(",",comma+1)
        substring = line[q+1:h]
        temp.append(dic[substring])

    key.append(temp)

for i in range(0,len(key)):
    file = open(re[i],"r",encoding="utf-8")
    t = file.readlines()
    file = open(re[i],"w",encoding="utf-8")
    for k in range(0,len(t)):
        if k == 3:
            file.writelines("tags: " + str(key[i])+"\n")
            continue
        file.writelines(str(t[k]).translate(non_bmp_map))
    file.close()

Defects:
This program can not be used to process emoji.


EOF
本文章采用CC BY-NC-SA 4.0进行许可。转载请注明出处!
上一篇

JS automatic references generator


下一篇

Python: A program help you to recite