Patching the Pelican Makefile

Its tedious to generate new entries from scratch all the time.

I found an entry on some site where somebody had created some bits to modify the Makefile. This was a good starting place. It offered an example of how to create a new page.

Changes to the Makefile

I’ve added to it a bit, and duplicated the page section so I can create both pages and articles.

Things I’ve added or tweaked:

  • Variables to define things like location for pages/articles/filenames
  • Headers that get dumped in to new articles/pages

I (currently) store articles in content/articles. I don’t want them sorting by slug, so I have the date prepended to the slug. I want the template to respect this.

Also potentially noteworthy, some of the bits in the template need plugins to function, and may in fact cause errors if they’re not present. Examples include Pin, Image, and TOC.

Here are the additions I’ve made to the makefile:

PAGESDIR=$(INPUTDIR)/pages
ARTICLESDIR=$(INPUTDIR)/articles
DATE := $(shell date +'%Y-%m-%d %H:%M:%S')
SLUG := $(shell echo '${NAME}' | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' |  tr A-Z a-z)
FILENAME := $(shell date +'%Y_%m_%d')-$(SLUG)
EXT ?= md

newarticle:
ifdef NAME
        echo "+++
title = " $(NAME)" > $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "date = $(DATE)" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "tags= = [
  - " >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "categories = [
  - " >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "slug = " $(SLUG)" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "Image: {photo}/icons/foo.jpg" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "Pin: false" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "Status: draft" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "[TOC]" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "## **Overview** ##" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
        $(EDITOR) ${ARTICLESDIR}/${FILENAME}.${EXT}
else
        @echo 'Variable NAME is not defined.'
        @echo 'Do make newarticle NAME='"'"'Post Name'"'"
endif

editarticle:
ifdef NAME
        $(EDITOR) ${ARTICLESDIR}/${FILENAME}.${EXT}
else
        @echo 'Variable NAME is not defined.'
        @echo 'Do make editarticle NAME='"'"'Article Name'"'"
endif

newpage:
ifdef NAME
        echo "+++
title = " $(NAME)" > $(PAGESDIR)/$(SLUG).$(EXT)
        echo "date = $(DATE)" >> $(PAGESDIR)/$(SLUG).$(EXT)
        echo "Cagetory: " >> $(PAGESDIR)/$(SLUG).$(EXT)
        echo "slug = " $(SLUG)" >> $(PAGESDIR)/$(SLUG).$(EXT)
        echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
        echo "**Overview**" >> $(PAGESDIR)/$(SLUG).$(EXT)
        echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
        echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
        $(EDITOR) ${PAGESDIR}/${SLUG}.${EXT}
else
        @echo 'Variable NAME is not defined.'
        @echo 'Do make newpage NAME='"'"'Page Name'"'"
endif

editpage:
ifdef NAME
        $(EDITOR) ${PAGESDIR}/${SLUG}.${EXT}
else
        @echo 'Variable NAME is not defined.'
        @echo 'Do make editpage NAME='"'"'Page Name'"'"
endif

Lastly, add your new make targets to the end of the ‘.PHONY’ target.

How to use it

In your pelican directory, type in the following: make newarticle NAME='New Article Name' and it will create a new file, and open it in your editor (what ever is defined in your environment as $EDITOR). You can also type: make editarticle NAME='New Article Name' and it will open up the article in your editor. Both of these commands work the same way for pages and articles, just substitute page for article.