-
Notifications
You must be signed in to change notification settings - Fork 12
/
pelican_alias.py
71 lines (54 loc) · 2.19 KB
/
pelican_alias.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os.path
import logging
from urlparse import urlparse
from pelican import signals
logger = logging.getLogger(__name__)
class AliasGenerator(object):
TEMPLATE = """<!DOCTYPE html><html><head><meta charset="utf-8" />
<meta http-equiv="refresh" content="0;url={destination}" />
</head></html>"""
def __init__(self, context, settings, path, theme, output_path, *args):
self.output_path = output_path
self.context = context
self.alias_delimiter = settings.get('ALIAS_DELIMITER', ',')
def create_alias(self, page, alias):
# If path starts with a /, remove it
if alias[0] == '/':
relative_alias = alias[1:]
else:
relative_alias = alias
path = os.path.join(self.output_path, relative_alias)
directory, filename = os.path.split(path)
try:
os.makedirs(directory)
except OSError:
pass
if filename == '':
path = os.path.join(path, 'index.html')
logger.info('[alias] Writing to alias file %s' % path)
with open(path, 'w') as fd:
destination = page.url
# if schema is empty then we are working with a local path
if not urlparse(destination).scheme:
# if local path is missing a leading slash then add it
if not destination.startswith('/'):
destination = '/{0}'.format(destination)
fd.write(self.TEMPLATE.format(destination=destination))
def generate_output(self, writer):
pages = (
self.context['pages'] + self.context['articles'] +
self.context.get('hidden_pages', []))
for page in pages:
aliases = page.metadata.get('alias', [])
if type(aliases) != list:
aliases = aliases.split(self.alias_delimiter)
for alias in aliases:
alias = alias.strip()
logger.info('[alias] Processing alias %s' % alias)
self.create_alias(page, alias)
def get_generators(generators):
return AliasGenerator
def register():
signals.get_generators.connect(get_generators)