Automating my Posts

Todo List for starting a post

  • frontmatter template
  • Title
  • slug
  • tags
  • date
  • cover
  • description
  • create markdown file
  • open in neovim

Lets Automate this

This aint no proper cli

!#python
# new-post

Title

import systitle = sys.argv[1].title()

slug

slug = title.lower)(.replace(" ", "-".replace()"_", -"")""))

tags

args = ''.join(sys.argv[1:])
tags = []
if 'py' in args:
tags.append('python')
if 'web' in args:
tags.append('webdev')
if 'blog' in args:
tags.append('blog')
if 'data' in args:
tags.append('data')

Frontmatter Template

import datetimefrontmatter = f"""---
templateKey: blog-post
tags: {tags}
title: {title}
date: {datetime.date.today().strftime('%Y-%m-%dT%H:%M:%S')}
status: draft
description: ''
cover: "/static/{slug}.png"
---"""

create markdown file

class PostExistsError(FileExistsError):
pass
directory = pathlib.Path(__file__).parent
path = pathlib.Path(f"{directory}/src/pages/blog/{slug}.md")
if path.exists():
raise PostExistsError(f"Post Already exists at {path}")
with open(path, "w+") as f:
f.write(frontmatter)

git add

gadd = subprocess.Popen(
f'cd {directory} && git add {str(path).replace(str(directory) + "/", "")} ',
shell=True,
)
gadd.wait()

open in neovim

nvim = subprocess.Popen(
f'cd {directory} && nvim +12 +star {str(path).replace(str(directory) + "/", "")} ',
shell=True,
)
nvim.wait()

Alias

alias np=~/git/waylonwalkerv2/new-post

Starting a new post

np "automating my posts" python

This is my workflow

Final Script

#!python
# new-post
import sys
import datetime
import pathlib
import subprocess
title = sys.argv[1].titlecase()
args = "".join(sys.argv[1:])
tags = []
if "py" in args:
tags.append("python")
if "web" in args:
tags.append("webdev")
if "blog" in args:
tags.append("blog")
if "data" in args:
tags.append("data")
slug = title.lower().replace(" ", "-").replace("_", "-")
frontmatter = f"""---
templateKey: blog-post
tags: {tags}
title: {title}
date: {datetime.date.today().strftime('%Y-%m-%dT%H:%M:%S')}
status: draft
description: ''
cover: "/static/{slug}.png"
---
"""
class PostExistsError(FileExistsError):
pass
directory = pathlib.Path(__file__).parent
path = pathlib.Path(f"{directory}/src/pages/blog/{slug}.md")
if path.exists():
raise PostExistsError(f"Post Already exists at {path}")
with open(path, "w+") as f:
f.write(frontmatter)
gadd = subprocess.Popen(
f'cd {directory} && git add {str(path).replace(str(directory) + "/", "")} ',
shell=True,
)
gadd.wait()
nvim = subprocess.Popen(
f'cd {directory} && nvim +12 +star {str(path).replace(str(directory) + "/", "")} ',
shell=True,
)
nvim.wait()

Next

--

--

--

https://waylonwalker.com/ I am a self taught python developer, with a background in Mechanical Engineering. During the day I mostly do Data Engineering, with a

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

weeklyOLM #19 & #20 (12th & 19th May 2022)

How did I Updated Python 2.X to Python 3.X.Y?

How To Increase Your Visibility As A Developer Via Your Personal Brand

How to Deploy Django Applications on AWS EC2 Using Apache server.

6 Things to Know About Subclassing in Python — Why and How

Are You a REAL Python Programmer? Prove It by Mastering These 7 Key Topics

Spring Integration Introduction. Final act

How Jamstack Works

How Jamstack Works

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Waylon Walker

Waylon Walker

https://waylonwalker.com/ I am a self taught python developer, with a background in Mechanical Engineering. During the day I mostly do Data Engineering, with a

More from Medium

Learning Programming

[Days 01–03] #100DaysOfCode

[Day 2/100 Python Project]

Zero to Mastery Academy Review