-
Notifications
You must be signed in to change notification settings - Fork 844
/
Copy pathpermalinks.py
156 lines (124 loc) · 4.18 KB
/
permalinks.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
"""
This plugin enables a kind of permalink which can be used to refer to a piece
of content which is resistant to the file being moved or renamed.
"""
import itertools
import logging
import os
import os.path
from pelican import signals
from pelican.generators import Generator
from pelican.utils import clean_output_dir
from pelican.utils import mkdir_p
logger = logging.getLogger(__name__)
def article_url(content):
'''
Get the URL for an item of content
'''
return '{content.settings[SITEURL]}/{content.url}'.format(
content=content)
REDIRECT_STRING = '''
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0;url={url}">
<script type="text/javascript">
window.location.href = "{url}"
</script>
<title>Page Redirection to {title}</title>
</head>
<body>
If you are not redirected automatically, follow the
<a href='{url}'>link to {title}</a>
</body>
</html>
'''
class PermalinkGenerator(Generator):
'''
Generate a redirect page for every item of content with a
permalink_id metadata
'''
def generate_context(self):
'''
Setup context
'''
self.permalink_output_path = os.path.join(
self.output_path, self.settings['PERMALINK_PATH'])
self.permalink_id_metadata_key = (
self.settings['PERMALINK_ID_METADATA_KEY'])
def generate_output(self, writer=None):
'''
Generate redirect files
'''
logger.info(
'Generating permalink files in %r', self.permalink_output_path)
clean_output_dir(self.permalink_output_path, [])
mkdir_p(self.permalink_output_path)
for content in itertools.chain(
self.context['articles'], self.context['pages']):
for permalink_id in content.get_permalink_ids_iter():
permalink_path = os.path.join(
self.permalink_output_path, permalink_id) + '.html'
redirect_string = REDIRECT_STRING.format(
url=article_url(content),
title=content.title)
open(permalink_path, 'w').write(redirect_string)
def get_permalink_ids_iter(self):
'''
Method to get permalink ids from content. To be bound to the class last
thing.
'''
permalink_id_key = self.settings['PERMALINK_ID_METADATA_KEY']
permalink_ids = self.metadata.get(permalink_id_key, '')
for permalink_id in permalink_ids.split(','):
if permalink_id:
yield permalink_id.strip()
def get_permalink_ids(self):
'''
Method to get permalink ids from content. To be bound to the class last
thing.
'''
return list(self.get_permalink_ids_iter())
def get_permalink_path(self):
"""Get just path component of permalink."""
try:
first_permalink_id = next(self.get_permalink_ids_iter())
except StopIteration:
return None
return '/{settings[PERMALINK_PATH]}/{first_permalink}.html'.format(
settings=self.settings, first_permalink=first_permalink_id)
def get_permalink_url(self):
'''
Get a permalink URL
'''
return "/".join((self.settings['SITEURL'], self.get_permalink_path()))
PERMALINK_METHODS = (
get_permalink_ids_iter,
get_permalink_ids,
get_permalink_url,
get_permalink_path,
)
def add_permalink_methods(content_inst):
'''
Add permalink methods to object
'''
for permalink_method in PERMALINK_METHODS:
setattr(
content_inst,
permalink_method.__name__,
permalink_method.__get__(content_inst, content_inst.__class__))
def add_permalink_option_defaults(pelicon_inst):
'''
Add perlican defaults
'''
pelicon_inst.settings.setdefault('PERMALINK_PATH', 'permalinks')
pelicon_inst.settings.setdefault(
'PERMALINK_ID_METADATA_KEY', 'permalink_id')
def get_generators(_pelican_object):
return PermalinkGenerator
def register():
signals.get_generators.connect(get_generators)
signals.content_object_init.connect(add_permalink_methods)
signals.initialized.connect(add_permalink_option_defaults)