Search code examples
pelican

How to load lists in pelilcanconf.py from external file


There are different lists available in pelicanconf.py such as

SOCIAL = (('Facebook','www.facebook.com'),)
LINKS = 

etc.

I want to manage these content and create my own lists by loading these values from an external file which can be edited independently. I tried importing data as a text file using python but it doesn't work. Is there any other way?


Solution

  • What exactly did not work? Can you provide code?

    You can execute arbitrary python code in your pelicanconf.py.

    Example for a very simple CSV reader:

    # in pelicanconf.py
    def fn_to_list(fn):
        with open(fn, 'r') as res:
            return tuple(map(lambda line: tuple(line[:-1].split(';')), res.readlines()))
    
    print(fn_to_list("data"))
    

    CSV file data:

    A;1
    B;2
    C;3
    D;4
    E;5
    F;6
    

    Together, this yields the following when running pelican:

    # ...
    ((u'A', u'1'), (u'B', u'2'), (u'C', u'3'), (u'D', u'4'), (u'E', u'5'), (u'F', u'6'))
    # ...
    

    Instead of printing you can also assign this list to a variable, say LINKS.