python 帮我创建hexo的草稿。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 帮我创建hexo的草稿。相关的知识,希望对你有一定的参考价值。

#!/bin/python3

import sys
import os
import time
import subprocess

def write(layout, title, current, category, target):
    yaml = '''\
title: {1}
layout: {0}
date: {2}
categories: {3}
---

    '''.format(layout, title, current, category)
    f = open(target, 'w', encoding='utf-8')
    f.write(yaml)
    f.close()
    print('\nFile saved at {0}\n'.format(target))
    print('\nDo you want to open it now? (y/n)')
    yes_no = input('> ')
    if (yes_no == 'y' or yes_no == 'Y'):
        subprocess.call(["xdg-open", target])


# Your hexo root folder
HEXO = '/home/ehome/Documents/blog'

# File name
print('Enter the file name. (Default is untitled)')
name = input('> ')
if name == '':
    name = 'untitled'
print("Draft name is {0}".format(name), flush=True)
draft_name = name.replace(' ', '-')

# Draft title
print('\nEnter the draft title. (Default is "{0}")'.format(name.title()))
title = input('> ')
if title == '':
    title = name.title()
print("Draft title is {0}".format(title), flush=True)

# Layout
print("\nEnter the layout name. (Default is post)")
layout = input('> ')

if layout == '':
    layout = 'post'

# Category
print("\nEnter the draft's category. (Default is post)")
category = input('> ')

print("Layout name is {0}".format(layout), flush=True)

if layout == 'post':
    draft_layout = '_posts'
elif layout == 'draft':
    draft_layout = '_drafts'
else:
    draft_layout = layout

# If the layout folder doesn't exists, create it.
path = os.path.join(HEXO, 'source', draft_layout)
if not os.path.exists(path):
    print("{0} doesn't exists. Created.".format(path), flush=True)
    os.mkdir(path)
    
current = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

target = os.path.join(path, draft_name) + '.md'

if os.path.exists(target):
    print("{0} already exists. Do you want to override it? (y/n)".format(target))
    yes_no = input('> ')
    if (yes_no == 'y' or yes_no == 'Y'):
        write(layout, title, current, category, target)
    else:
        print('\nMission Cancelled.\n')
else:
    write(layout, title, current, category, target)

以上是关于python 帮我创建hexo的草稿。的主要内容,如果未能解决你的问题,请参考以下文章

hexo文章编写部分语法总结以及hexo使用

从jekyll转向hexo

007 python接口 参数关联

以编程方式发布包含用户指定的 HTML 和纯文本正文的 Gmail 草稿

Python部分草稿

hexo创建各种页面问题