-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlangcategory.py
73 lines (60 loc) · 2.34 KB
/
langcategory.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
#coding: utf-8
import logging
from operator import attrgetter, itemgetter
from collections import defaultdict
from pelican import signals
from pelican.urlwrappers import URLWrapper
class Language(URLWrapper):
def __init__(self, name, *args, **kwargs):
super(Language, self).__init__(name.strip(), *args, **kwargs)
self.lang = self.name
def generate_lang_as_category(generator, writer):
self = generator
self.languages = defaultdict(list)
try:
template = self.get_template('language')
except TemplateNotFound:
template = self.get_template('category')
if not self.settings['LANGUAGE_SAVE_AS']:
return
# If LANGUAGE_URL has a trailing slash, remove it and provide a warning
logger = logging.getLogger(__name__)
langurl = self.settings['LANGUAGE_URL']
if (langurl.endswith('/')):
self.settings['LANGUAGE_URL'] = langurl[:-1]
logger.warning("Removed extraneous trailing slash from LANGUAGE_URL.")
for article in self.articles + self.translations:
self.languages[article.lang].append(article)
for lang, items in self.languages.items():
lang_wrapper = Language(lang, self.settings)
items.sort(key=attrgetter('date'), reverse=True)
dates = [article for article in self.dates if article in items]
# Override Default Language Setting
self.settings['DEFAULT_LANG'] = lang
override_in_default_lang_items = []
for i in items:
i.lang = lang
i.in_default_lang = True
override_in_default_lang_items.append(i)
items = override_in_default_lang_items
try:
import pycountry
lang_obj = pycountry.languages.get(alpha_2=str(lang))
language_name = lang_obj.name if lang_obj else lang
except ImportError:
language_name = lang
except KeyError:
language_name = lang
writer.write_file(lang_wrapper.save_as,
template,
self.context,
articles=items,
dates=dates,
paginated={'articles': items, 'dates': dates},
page_name=lang_wrapper.page_name,
language=lang,
language_name=language_name,
all_articles=items
)
def register():
signals.article_writer_finalized.connect(generate_lang_as_category)