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:

 1PAGESDIR=$(INPUTDIR)/pages
 2ARTICLESDIR=$(INPUTDIR)/articles
 3DATE := $(shell date +'%Y-%m-%d %H:%M:%S')
 4SLUG := $(shell echo '${NAME}' | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' |  tr A-Z a-z)
 5FILENAME := $(shell date +'%Y_%m_%d')-$(SLUG)
 6EXT ?= md
 7
 8newarticle:
 9ifdef NAME
10        echo "+++
11title = " $(NAME)" > $(ARTICLESDIR)/$(FILENAME).$(EXT)
12        echo "date = $(DATE)" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
13        echo "tags= = [
14  - " >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
15        echo "categories = [
16  - " >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
17        echo "slug = " $(SLUG)" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
18        echo "Image: {photo}/icons/foo.jpg" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
19        echo "Pin: false" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
20        echo "Status: draft" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
21        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
22        echo "[TOC]" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
23        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
24        echo "## **Overview** ##" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
25        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
26        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
27        echo "" >> $(ARTICLESDIR)/$(FILENAME).$(EXT)
28        $(EDITOR) ${ARTICLESDIR}/${FILENAME}.${EXT}
29else
30        @echo 'Variable NAME is not defined.'
31        @echo 'Do make newarticle NAME='"'"'Post Name'"'"
32endif
33
34editarticle:
35ifdef NAME
36        $(EDITOR) ${ARTICLESDIR}/${FILENAME}.${EXT}
37else
38        @echo 'Variable NAME is not defined.'
39        @echo 'Do make editarticle NAME='"'"'Article Name'"'"
40endif
41
42newpage:
43ifdef NAME
44        echo "+++
45title = " $(NAME)" > $(PAGESDIR)/$(SLUG).$(EXT)
46        echo "date = $(DATE)" >> $(PAGESDIR)/$(SLUG).$(EXT)
47        echo "Cagetory: " >> $(PAGESDIR)/$(SLUG).$(EXT)
48        echo "slug = " $(SLUG)" >> $(PAGESDIR)/$(SLUG).$(EXT)
49        echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
50        echo "**Overview**" >> $(PAGESDIR)/$(SLUG).$(EXT)
51        echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
52        echo "" >> $(PAGESDIR)/$(SLUG).$(EXT)
53        $(EDITOR) ${PAGESDIR}/${SLUG}.${EXT}
54else
55        @echo 'Variable NAME is not defined.'
56        @echo 'Do make newpage NAME='"'"'Page Name'"'"
57endif
58
59editpage:
60ifdef NAME
61        $(EDITOR) ${PAGESDIR}/${SLUG}.${EXT}
62else
63        @echo 'Variable NAME is not defined.'
64        @echo 'Do make editpage NAME='"'"'Page Name'"'"
65endif

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.

Copyright

Comments