diff --git a/scripts/blog.py b/scripts/blog.py index 3f7d5eb..b059065 100644 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -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 😿") diff --git a/static/article.tmpl.html b/static/article.tmpl.html index cd23b7c..6383710 100644 --- a/static/article.tmpl.html +++ b/static/article.tmpl.html @@ -4,19 +4,19 @@