site_opytex/plugins/list_files/list_files.py

41 lines
1.1 KiB
Python

from pelican import signals, contents
from os import listdir, getcwd
from os.path import isfile, join, sep, split
from copy import copy
from itertools import chain
'''
This plugin creates a URL hierarchy for articles that matches the
directory hierarchy of their sources.
'''
def lsfiles(path):
return [f for f in listdir(path) if isfile(join(path, f))]
def save_files(content_object):
if type(content_object) is not contents.Article:
return
article = content_object
path = split(article.get_relative_source_path())[0] + '/'
path = path.replace(sep, '/' )
abs_path = getcwd() + "/content/" + path
files = {i:(path+i) for i in lsfiles(abs_path)}
try:
fig_files = lsfiles(abs_path+"/fig")
except FileNotFoundError:
pass
else:
fig_files = {i:(path+"fig/"+i) for i in fig_files}
files.update(fig_files)
article.link_files = []
for i in files.items():
article.link_files.append(i)
article.link_files.sort()
def register():
signals.content_object_init.connect(save_files)