Implement handling of one post in multiple languages.
This commit is contained in:
parent
12b5e31e49
commit
21a857ea41
@ -83,6 +83,7 @@ MONITORED_EVENT_TYPES = (
|
||||
'IN_DELETE_SELF',
|
||||
'IN_MOVE_SELF',
|
||||
)
|
||||
LANG_PRIORITY = ('en', 'es', 'gl')
|
||||
|
||||
|
||||
def parse_nikola_date(match):
|
||||
@ -161,6 +162,8 @@ def get_out_path(front_matter):
|
||||
front_matter['slug'] = slugify(front_matter['title'])
|
||||
|
||||
out_path = os.path.join(str(front_matter['date'].year), front_matter['slug'])
|
||||
if front_matter.get('lang', LANG_PRIORITY[0]) != LANG_PRIORITY[0]:
|
||||
out_path = os.path.join(str(front_matter['date'].year), front_matter['lang'], front_matter['slug'])
|
||||
return out_path
|
||||
|
||||
|
||||
@ -192,12 +195,16 @@ def load_doc(filepath):
|
||||
return (doc, front_matter, out_path)
|
||||
|
||||
|
||||
def render_article(doc, front_matter, f):
|
||||
def render_article(doc, front_matter, f, out_path):
|
||||
extsep = '/' if '/' in out_path else '\\'
|
||||
subdirs = len(out_path.split(extsep))
|
||||
base_path = os.path.join(*(['..'] * subdirs))
|
||||
result = ARTICLE_TEMPLATE.render(
|
||||
content=doc,
|
||||
title=front_matter['title'],
|
||||
post_publication_date=front_matter['date'],
|
||||
post_tags=split_tags(front_matter['tags']),
|
||||
base_path=base_path,
|
||||
)
|
||||
f.write(result)
|
||||
|
||||
@ -286,7 +293,26 @@ def summarize(doc):
|
||||
return result
|
||||
|
||||
def render_index(docs, dest_top):
|
||||
docs = sorted(docs.values(), key=lambda x: x[1]['date'], reverse=True)
|
||||
# Collect all languages accepted for all docs
|
||||
docs_by_slug = {}
|
||||
for (doc, front_matter, out_path) in docs.values():
|
||||
if front_matter['slug'] not in docs_by_slug:
|
||||
docs_by_slug[front_matter['slug']] = {}
|
||||
docs_by_slug[front_matter['slug']][front_matter.get('lang', LANG_PRIORITY[0])] = (doc, front_matter, out_path)
|
||||
|
||||
# Remove duplicated for langs with less priority
|
||||
selected_docs = []
|
||||
for (doc, front_matter, out_path) in docs.values():
|
||||
langs = docs_by_slug[front_matter['slug']]
|
||||
lang_priority = LANG_PRIORITY.index(front_matter.get('lang', LANG_PRIORITY[0]))
|
||||
min_lang_priority = min([
|
||||
LANG_PRIORITY.index(lang)
|
||||
for lang in langs.keys()
|
||||
])
|
||||
if lang_priority == min_lang_priority:
|
||||
selected_docs.append((doc, front_matter, out_path, langs))
|
||||
|
||||
docs = sorted(selected_docs, key=lambda x: x[1]['date'], reverse=True)
|
||||
|
||||
index_ranges = range(0, len(docs), BLOG_INDEX_PAGE_SIZE)
|
||||
|
||||
@ -302,7 +328,7 @@ def render_index(docs, dest_top):
|
||||
"summary": summarize(doc),
|
||||
"link": out_path.rstrip('/') + '/',
|
||||
}
|
||||
for (doc, front_matter, out_path) in page
|
||||
for (doc, front_matter, out_path, _alternatives) in page
|
||||
]
|
||||
|
||||
prev_index_num = None
|
||||
@ -390,11 +416,11 @@ def regen_all(source_top, dest_top, docs=None):
|
||||
doc_full_path = os.path.join(dest_top, out_path)
|
||||
os.makedirs(os.path.dirname(doc_full_path), exist_ok=True)
|
||||
# print("==", doc_full_path)
|
||||
out_path = doc_full_path + '/index.html'
|
||||
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
||||
with open(out_path, 'wt') as f:
|
||||
full_out_path = doc_full_path + '/index.html'
|
||||
os.makedirs(os.path.dirname(full_out_path), exist_ok=True)
|
||||
with open(full_out_path, 'wt') as f:
|
||||
try:
|
||||
render_article(doc, front_matter, f)
|
||||
render_article(doc, front_matter, f, out_path)
|
||||
except:
|
||||
logging.error(traceback.format_exc())
|
||||
logging.error("Rendering failed 😿")
|
||||
@ -493,7 +519,7 @@ def main(source_top, dest_top):
|
||||
# print("==", doc_full_path)
|
||||
with open(doc_full_path + '/index.html', 'wt') as f:
|
||||
try:
|
||||
render_article(doc, front_matter, f)
|
||||
render_article(doc, front_matter, f, out_path)
|
||||
except:
|
||||
logging.error(traceback.format_exc())
|
||||
logging.error("Rendering failed 😿")
|
||||
|
@ -4,19 +4,19 @@
|
||||
<meta charset="utf-8">
|
||||
<title>{{ title }} @ Código para llevar [blog]</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="../../css/style.css" />
|
||||
<link rel="stylesheet" href="../../css/light-syntax.css" />
|
||||
<link rel="stylesheet" href="../../css/dark-syntax.css" />
|
||||
<link rel="stylesheet" href="{{ base_path }}/css/style.css" />
|
||||
<link rel="stylesheet" href="{{ base_path }}/css/light-syntax.css" />
|
||||
<link rel="stylesheet" href="{{ base_path }}/css/dark-syntax.css" />
|
||||
</head>
|
||||
<body class="blog">
|
||||
<div class="site-header">
|
||||
<h1 class="site-name"><a href="../../">Codigo para llevar [blog]</a></h1>
|
||||
<h1 class="site-name"><a href="{{ base_path }}/">Codigo para llevar [blog]</a></h1>
|
||||
<nav class="site-links">
|
||||
<span class="fancy-link">
|
||||
<a href="../../../">Home</a>
|
||||
<a href="{{ base_path }}/../">Home</a>
|
||||
</span>
|
||||
<span class="fancy-link">
|
||||
<a href="../../../notes/">Notes</a>
|
||||
<a href="{{ base_path }}/../notes/">Notes</a>
|
||||
</span>
|
||||
<span class="fancy-link">
|
||||
<a href="https://github.com/kenkeiras">GitHub</a>
|
||||
@ -38,7 +38,7 @@
|
||||
</time>
|
||||
<ul class="post-tags">
|
||||
{% for post_tag in post_tags %}
|
||||
<li class="post-tag"><a href="../../tags/{{ post_tag }}/"</a>{{ post_tag }}</a></li>
|
||||
<li class="post-tag"><a href="{{ base_path }}/tags/{{ post_tag }}/"</a>{{ post_tag }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user