Skip to content

Instantly share code, notes, and snippets.

@wzpan
Last active January 1, 2016 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzpan/8204508 to your computer and use it in GitHub Desktop.
Save wzpan/8204508 to your computer and use it in GitHub Desktop.
Help me create a draft for 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment