CSDN博客自动美化工具(支持HDU,POJ,Codeforces)

Github传送门 ~~

本文目前只针对Codeforces,HDOJ和POJ题面(POJ和HDU其实是一样的)到CSDN博客的美化有效(后期可能会追加其他各类OJ的处理工具)

使用方法

创建一个文件夹,里面放上代码也就是xx.py文件,再创建一个in.txt文件,使用时把题目页面的内容(题目标题,示例输入输出,以及note)整个复制到in.txt文件,
然后点击xx.py(github上直接下载exe文件运行也可以)文件运行程序,之后就会在文件夹里生成一个out.txt文件,就是处理后的文本,把整个文本复制到CSDN编辑页面就行了。
效果如图:
在这里插入图片描述

Python代码:

# in.txt文件必须和程序放在同一个文件夹下或者在这里指定文件路径
def main():
    with open("in.txt","r",encoding='utf-8') as f:
        content = f.read().split("\n")
    print("Reading file succeeded !!!")
    mode = eval(input("Please choose a mode:\n\
1、For codeforces    2、For HDU\n"))

    content[0] = '## '+ content[0]
    content[1:5] = '\n'
    content = codeforces(content) if mode == 1 else HDU(content)
    content.append("\n## 解题思路:\n\n")
    content.append("## AC代码:\n")
    content.append('```cpp\n\n```\n')
    with open("out.txt","w",encoding='utf-8') as f:
        for i in content:
            f.write(i)
            f.write('\n')
    print("Writing file secceeded !!!")
    print("Process ended ! ^_^ ~~")
    tmp = input("Press any key to quit.")

def codeforces(content):
    flag = False
    flag2 = True
    i = 1
    while i < len(content):
        if (len(content[i]) >= 5 and ( content[i][:5] == 'input' or content[i][:5] == 'Input' )):
            if( 'Copy' in content[i]):
                content[i] = '## '+ content[i][:-4]
                if flag:
                    content.insert(i,'```')
                    content.insert(i+2,'```cpp')
                    i += 2
                else:
                    flag = True
                    content.insert(i+1,'```cpp')
                    i += 1
            else:
                content[i] = '## '+ content[i]
        elif (len(content[i]) >= 6 and ( content[i][:6] == 'output' or content[i][:6] == 'Output' )):
            if( 'Copy' in content[i]):
                content[i] = '## '+ content[i][:-4]
                content.insert(i,'```')
                content.insert(i+2,'```cpp')
                i += 2
            else:
                content[i] = '## '+ content[i]
        elif (len(content[i]) >= 8 and ( content[i][:8] == 'Examples' or content[i][:8] == 'examples' )):
            content[i] = '## '+ content[i]
        elif (len(content[i]) >= 4 and ( content[i][:4] == 'Note' or content[i][:4] == 'note')):
            flag2 = False
            content[i] = '## '+ content[i]
            content.insert(i,'```')
            i += 1
        i += 1
    if flag2:  
        content.append('```')
    return content

def HDU(content):
    flag = False
    flag2 = True
    i = 1
    while i < len(content):
        if content[i] == "Input" or content[i] == "Sample Input":
            if content[i] == "Sample Input":
                content[i] = '## '+ content[i]
                if flag:
                    content.insert(i,'```')
                    content.insert(i+2,'```cpp')
                    i += 2
                else:
                    flag = True
                    content.insert(i+1,'```cpp')
                    i += 1
            else:
                content[i] = '## '+ content[i]
        elif  content[i] == "Output" or content[i] == "Sample Output":
            if content[i] == "Sample Output":
                content[i] = '## '+ content[i]
                content.insert(i,'```')
                content.insert(i+2,'```cpp')
                i += 2
            else:
                content[i] = '## '+ content[i]
        elif content[i] == "Problem Description":
            content[i] = '## '+ content[i]
        i += 1
    if flag2:  
        content.append('```')
    return content

if __name__ == "__main__":
    main()
发布了104 篇原创文章 · 获赞 7 · 访问量 4060

猜你喜欢

转载自blog.csdn.net/qq_43461168/article/details/101981825