JOL/lib/mix/tasks/gen_post.ex

42 lines
1 KiB
Elixir

defmodule Mix.Tasks.GenPost do
@moduledoc "gen_post: Generate a new blog post."
@shortdoc "Generates a new blog post. Takes the title, and generates the rest."
use Mix.Task
@impl Mix.Task
def run([title]) do
dir = Application.app_dir(:jol, "priv/posts/blog/")
slug = title
|> String.downcase()
|> String.replace(" ", "-")
|> String.replace(~r/[^a-zA-Z0-9 -]/, "")
pubdate = DateTime.now!("America/New_York")
filename = "#{slug}.md"
filepath = "#{dir}/#{filename}"
if File.exists?(filepath) do
IO.puts(:stderr, "Already exists: priv/posts/blog/#{filename}")
IO.puts(:stderr, "Stop reusing blog titles!")
else
content = """
+++
slug = "#{slug}"
title = "#{title}"
date = #{DateTime.to_iso8601(pubdate)}
draft = false
[taxonomies]
# An array of quoted strings
tags = []
+++
"""
File.write(filepath, content)
IO.puts "Created: #{dir}#{filename}"
end
end
end