4

[Pelican] Create URL Based on File Path

 2 years ago
source link: http://siongui.github.io/2018/10/17/pelican-create-url-based-on-file-path/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Pelican] Create URL Based on File Path

October 17, 2018

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK