Creating Jekyll Posts

Being a good programmer, I am told, is all about being lazy. So, I got bored with creating posts by hand and wrote a little console application to do it for me.

It accepts up to 3 arguments, the post title, a date in ISO 8601 format (today's date is assumed), and a list of tags.

Many of the refinements are based on my own setup, so if it's run from the main jekyll folder, with a _posts subfolder, the post will be created there.


class Program
{
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("post <title> <optional-date> <optional-tags>");
            return;
        }

        string title = args[0];

        string outputFolder = Directory.GetCurrentDirectory();

        string postSubFolder = Path.Combine(outputFolder, "_posts");

        if (Directory.Exists(postSubFolder))
            outputFolder = postSubFolder;

        string formattedDate = string.Empty;

        if (args.Length > 1)
        {
            formattedDate = args[1];
        }
        else
        {
            DateTime date = DateTime.Now;
            formattedDate = date.ToString("yyyy-MM-dd");
        }

        string suggestedTags = string.Empty;

        if (args.Length > 2)
        {
            suggestedTags = args[2];
        }

        string fileName = string.Format("{0}-{1}.markdown", formattedDate, title.Replace(" ", "-").ToLower());
        string path = Path.Combine(outputFolder, fileName);

        var utf8WithoutBom = new System.Text.UTF8Encoding(false);

        const bool AppendToFile = false;

        using (var writer = new StreamWriter(path, AppendToFile, utf8WithoutBom))
        {
            writer.WriteLine("---");
            writer.WriteLine("layout: post");
            writer.WriteLine("title: {0}", title);
            writer.WriteLine("published: true");
            writer.Write("tags: [ ");

            if (!String.IsNullOrEmpty(suggestedTags))
            {
                string[] tags = suggestedTags.Split(new char[] { ' ', ';' });
                writer.Write(String.Join(", ", tags));
            }

            writer.WriteLine(" ]");
            writer.WriteLine("---");
            writer.WriteLine();
            writer.WriteLine(title + " - post goes here");
            writer.WriteLine();
        }
    }
}

One important feature to note is jekyll really, really doesn't like Unicode with BOM, so you have to be careful to explicitly create a UTF8Encoding object (with BOM turned off) when writing out the file content (see line 42).