Skip to content

Instantly share code, notes, and snippets.

@drscotthawley
Last active Jul 28, 2020
Embed
What would you like to do?
Little script I use to easily start a new Fastpages/Jekyll/Markdown blog entry
#! /usr/bin/env python
# Little script I use to start a new Markdown blog entry.
# Run from the parent directory above the blog directory,
# or supply a full path to run from anywhere.
#
# Usage: newpost.py <title>
# It automatically figures out what the current date is to
# create a new entry, and supplies a default header with a title
# and a bibliography placement
import datetime
import sys
import os
from pathlib import Path
# local directory name, or give full path to run from anywhere on system
blog_dir = "myblog"
assert len(sys.argv) > 1 # make sure the entry will have a title
# pack the command line args with dashes instead of spaces
title_str = '-'.join(sys.argv[1:])
# create the text file with the heading
filename = f'{blog_dir}/_posts/{datetime.date.today()}-{title_str}.md'
outfile = open(filename,"w")
title = ' '.join(sys.argv[1:])
home = str(Path.home()) # handy for helping typora display images
header = f'''---
title: "{title}"
description:
layout: post
toc: true
comments: true
image:
date: {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
hide: false
search_exclude: false
typora-root-url: {home}
---
'''
outfile.write(header+'\n')
# Some optional info I like to keep track of, but you can delete
abspath = os.path.abspath(filename)
orig_link = f'[Direct Link to File](file://{abspath})'
words = '{{ page.content | number_of_words | minus: 26}} words, {{ page.content | number_of_words | divided_by: 180 | append: " minute read" }}'
pages = ', {{ page.content | number_of_words | divided_by: 275 | append: " paperback pages" }}'
new_header = orig_link + '. ' + words + pages
outfile.write(new_header)
refs = '\n\n\n\n## References\n'
outfile.write(refs)
outfile.close()
# Immediately open the new file for editing in Typora
os.system(f'/usr/bin/typora {abspath}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment