[Pelican] Create URL Based on File Path


Pelican static site generator - Create URL of page or article based on the full path relative to the content source directory.

Problem

for example, the following is the path of a page:

content/pages/intro.rst

The URL of the page will be:

/intro/

The following is the path of a article:

content/articles/category/myproduct.rst

The URL of the article will be:

/category/myproduct/

Solution

We will create a custom metadata field called urlpath. The value of urlpath is extracted from article/page's path relative to the content source directory. Then the value of urlpath will be used to determine the URL of the article/page. You do not need to modify the file content of articles/pages.

The solution is to set PATH_METADATA, PAGE_URL, PAGE_SAVE_AS, ARTICLE_URL, ARTICLE_SAVE_AS in pelicanconf.py as follows:

PATH_METADATA = '(articles|pages)/(?P<urlpath>[-a-zA-Z0-9/]*)\.rst'

ARTICLE_URL = '{urlpath}/'
ARTICLE_SAVE_AS = '{urlpath}/index.html'

PAGE_URL = '{urlpath}/'
PAGE_SAVE_AS = '{urlpath}/index.html'

Tested on: Ubuntu Linux 18.04, Python 2.7.15rc1, Pelican 3.7.1.


References:

[1]Settings — Pelican 3.7.1 documentation