duncanlock.net

How I built this website, using Pelican: Part 3

Go through my config and explain each bit

Date based posts

ARTICLE_URL = 'blog/{date:%Y}/{date:%m}/{date:%d}/{slug}/'
ARTICLE_SAVE_AS = 'blog/{date:%Y}/{date:%m}/{date:%d}/{slug}/index.html'

YEAR_ARCHIVE_SAVE_AS = 'blog/{date:%Y}/index.html'
MONTH_ARCHIVE_SAVE_AS = 'blog/{date:%Y}/{date:%m}/index.html'
DAY_ARCHIVE_SAVE_AS = 'blog/{date:%Y}/{date:%m}/{date:%d}/index.html'

ARCHIVES_SAVE_AS = 'blog/index.html'

Gotchas

The config file is a python script

This is really great, once you get the hang of it, but for some reasons, the settings documented here aren’t in python syntax, so you can’t just copy & paste them. For example, these three settings from there :

AUTHOR
MARKUP (('rst', 'md'))
STATIC_PATHS (['images'])

need to look something like this, when you put them into your pelican config.py file:

AUTHOR = 'Duncan Lock'
MARKUP = (('rst', 'html'))
STATIC_PATHS = ["images"]

WARNING: Could not process…​’

If you see this warning, you probably have other stuff in the pelican root folder. Use ARTICLE_DIR = ('posts') & PAGE_DIR = ('pages') to tell it where to look; that these are relative to PATH = (‘.’)

Misc

  • If you’re running pelican -rs pelicanconf.py on the command line for a while and you see warnings that you don’t understand - especially if everything seems to work regardless - try restarting it.

Images

$ find . -iname "*png" -print0 | xargs -0 --max-procs=4 -n 1 pngout

Final Optimizations

Apache .htaccess

Sitemap

Use the sitemap plugin to generate an XML sitemap during compile. Add something like this to your publishing config:

#################################
#
# Setting for plugins
#
#################################

# Which extra plugins to enable when publishing
PLUGINS = PLUGINS + ['gzip_cache', 'sitemap']

# Settings for the sitemap plugin
SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 0.8,
        'indexes': 0.7,
        'pages': 0.5
    },
    'changefreqs': {
        'articles': 'weekly',
        'indexes': 'daily',
        'pages': 'monthly'
    }
}

Deployment

A separate Publish Config

Pelican can have multiple config files for different situations - and because they’re just python files they can import each other.

A simple way to take advantage of this is to have one main configuration file, containing all the settings that are common to your site during both local development and deployment - and an extra one just for publishing the live site, which imports the main one and then extands and overrides a few setting that need to be different for the live site.

So, in addition to all the settings discussed above in my main config, I have another config file called publishconf.py, which contains this:

#!/usr/bin/env python
# -*- coding: utf-8 -*- #

# This file is only used if you use `make publish` or
# explicitly specify it as your config file.

import os
import sys

# import main config
sys.path.append(os.curdir)
from pelicanconf import *

# Use real live URLs
SITEURL = 'http://duncanlock.net'
RELATIVE_URLS = False

# Generate Feeds
FEED_ATOM = 'feeds/all.atom.xml'
CATEGORY_FEED_ATOM = 'feeds/%s.atom.xml'

# Always start over
DELETE_OUTPUT_DIRECTORY = True

# Articles have comments
DISQUS_SITENAME = "duncanlocknet"

# Output Google Analytics code
GOOGLE_ANALYTICS = "UA-1493291-9"
GOOGLE_ANALYTICS_UNIVERSAL = True

#################################
#
# Setting for plugins
#
#################################

# Which extra plugins to enable when publishing
PLUGINS = PLUGINS + ['gzip_cache', 'sitemap']

# Settings for the sitemap plugin
SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 0.8,
        'indexes': 0.7,
        'pages': 0.5
    },
    'changefreqs': {
        'articles': 'weekly',
        'indexes': 'daily',
        'pages': 'monthly'
    }
}

Editing the makefile

  • moving content into a /content folder, rather than the root, or edit the makefile:
$ make ssh_upload
pelican /home/duncan/dev/duncanlock.net-pelican/content -o /home/duncan/dev/duncanlock.net-pelican/output -s /home/duncan/dev/duncanlock.net-pelican/publishconf.py
Traceback (most recent call last):
  File "/home/duncan/dev/virtualenvs/duncanlock.net-pelican/bin/pelican", line 8, in <module>
    load_entry_point('pelican==3.2', 'console_scripts', 'pelican')()
  File "/home/duncan/dev/virtualenvs/duncanlock.net-pelican/src/pelican/pelican/__init__.py", line 317, in main
    pelican = get_instance(args)
  File "/home/duncan/dev/virtualenvs/duncanlock.net-pelican/src/pelican/pelican/__init__.py", line 303, in get_instance
    settings = read_settings(args.settings, override=get_config(args))
  File "/home/duncan/dev/virtualenvs/duncanlock.net-pelican/src/pelican/pelican/settings.py", line 124, in read_settings
    return configure_settings(local_settings)
  File "/home/duncan/dev/virtualenvs/duncanlock.net-pelican/src/pelican/pelican/settings.py", line 151, in configure_settings
    raise Exception('You need to specify a path containing the content'
Exception: You need to specify a path containing the content (see pelican --help for more information)
make: *** [publish] Error 1
  • be careful with rsync_upload - quicker but will make folders match deleting anything on the server that isn’t on local

Feeds

FEED_ATOM = 'feeds/all.atom.xml'
CATEGORY_FEED_ATOM = 'feeds/%s.atom.xml'

Footnotes & References


Related Posts


Comments