Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translation support #26

Merged
merged 12 commits into from
Feb 25, 2020
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pelican-themes -i theme
python get_posts_from_notion.py

# Build the site
make html
make publish
Empty file.
44 changes: 0 additions & 44 deletions content/pages/about.md

This file was deleted.

35 changes: 0 additions & 35 deletions content/pages/signup.html

This file was deleted.

97 changes: 49 additions & 48 deletions get_posts_from_notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,60 @@
from notion.client import NotionClient

CLIENT = NotionClient(token_v2=os.environ["NOTION_TOKEN"])
BLOG_HOME = CLIENT.get_block(os.environ["NOTION_BLOG_POSTS_PAGE"])
NOTION_BLOG_POSTS = CLIENT.get_block(os.environ["NOTION_BLOG_POSTS"])
NOTION_PAGES = CLIENT.get_block(os.environ["NOTION_PAGES"])

# Main Loop
for post in BLOG_HOME.children:
if not post.type == 'page':
print("Skipping", post)
continue
def get_pages(table, outdir):
# Main Loop
for page in table.collection.get_rows():
print(page)
if not page.type == 'page':
print("Skipping", page)
continue

# Handle Frontmatter
text = """---
# Handle Frontmatter
text = """---
title: %s
date: %s
description:
summary: %s
slug: %s
lang: %s
---
""" % (post.title, datetime.datetime.now())
# Handle Title
text = text + '\n\n' + '# ' + post.title + '\n\n'
for content in post.children:
# Handles H1
if content.type == 'header':
text = text + '# ' + content.title + '\n\n'
# Handles H2
elif content.type == 'sub_header':
text = text + '## ' + content.title + '\n\n'
# Handles H3
elif content.type == 'sub_sub_header':
text = text + '### ' + content.title + '\n\n'
# Handles Code Blocks
elif content.type == 'code':
text = text + '```\n' + content.title + '\n```\n\n'
# Handles Images
elif content.type == 'image':
text = text + '![' + content.id + '](' + content.source + ')\n\n'
# Handles Bullets
elif content.type == 'bulleted_list':
text = text + '* ' + content.title + '\n'
# Handles Dividers
elif content.type == 'divider':
text = text + '---' + '\n\n'
# Handles Basic Text, Links, Single Line Code
elif content.type == 'text':
text = text + content.title + '\n\n'
else:
print("Ignoring unknown block", content)
""" % (page.title, page.created, page.summary, page.slug, page.language)
Comment on lines +23 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be fstring too if you like. IMO, it would be a bit cleaner as there is no chain anymore.

Copy link
Contributor Author

@jean jean Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Netlify only supports 3.6 which doesn't have fstrings yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From PEP 498, the fstring is working since 3.6 isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All I know is that it blew up when I pushed it ... I might have fumbled something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... what's the status of this now by the way?

# Handle Title
text = text + '\n\n' + '# ' + page.title + '\n\n'
for content in page.children:
# Handles H1
if content.type == 'header':
text = text + '# ' + content.title + '\n\n'
# Handles H2
elif content.type == 'sub_header':
text = text + '## ' + content.title + '\n\n'
# Handles H3
elif content.type == 'sub_sub_header':
text = text + '### ' + content.title + '\n\n'
# Handles Code Blocks
elif content.type == 'code':
text = text + '```\n' + content.title + '\n```\n\n'
# Handles Images
elif content.type == 'image':
text = text + '![' + content.id + '](' + content.source + ')\n\n'
# Handles Bullets
elif content.type == 'bulleted_list':
text = text + '* ' + content.title + '\n'
# Handles Dividers
elif content.type == 'divider':
text = text + '---' + '\n\n'
# Handles Basic Text, Links, Single Line Code
elif content.type == 'text':
text = text + content.title + '\n\n'
else:
print("Ignoring unknown block", content)

with open('./content/%s/%s%s.md' % (outdir, page.slug, '-%s'%page.language if page.language else ''), 'w') as page:
page.write(text)
print('Wrote A New Page')

title = post.title.replace(' ', '-')
title = title.replace(',', '')
title = title.replace(':', '')
title = title.replace(';', '')
title = title.lower()

with open('./content/blog/' + title + '.md', 'w') as post:
post.write(text)
print('Wrote A New Page')
print(text)
get_pages(NOTION_PAGES, 'pages')
get_pages(NOTION_BLOG_POSTS, 'blog')
5 changes: 5 additions & 0 deletions pelicanconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

DEFAULT_LANG = 'en'

LANGUAGE_CODES = {
'en': 'English',
'th': 'ไทย',
}

THEME = 'theme'

# Feed generation is usually not desired when developing
Expand Down
12 changes: 10 additions & 2 deletions theme/templates/_includes/translations.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{% macro entry_hreflang(entry) %}
{% if entry.translations %}
{% for translation in entry.translations %}
<link rel="alternate" hreflang="{{ translation.lang }}" href="{{ SITEURL }}/{{ translation.url }}">
{% endfor %}
{% endif %}
{% endmacro %}

{% macro translations_for(article) %}
{% if article.translations %}
<p id="list-of-translations">
This post is also available in:
This content is also available in:
{% for translation in article.translations|sort(attribute = 'lang') %}
<a href="{{ SITEURL }}/{{ translation.url }}">{{ translation.lang }}</a>{% if loop.index0 + 2 == loop.length %} and{% elif not loop.last %}, {% endif %}
<a href="{{ SITEURL }}/{{ translation.url }}">{{ LANGUAGE_CODES[translation.lang] }}</a>{% if loop.index0 + 2 == loop.length %} and{% elif not loop.last %}, {% endif %}
{% endfor %}
</p>
{% endif %}
Expand Down
5 changes: 5 additions & 0 deletions theme/templates/article.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
{% block head_links %}
{{ super() }}
{% include '_includes/photos_header.html' %}

{% import 'translations.html' as translations with context %}
{% if translations.entry_hreflang(article) %}
{{ translations.entry_hreflang(article) }}
{% endif %}
{% endblock head_links %}

{% block content %}
Expand Down
10 changes: 10 additions & 0 deletions theme/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
{{ smo_metadata(page) }}
{% endblock meta_tags_in_head %}

{% block head_links %}
{{ super() }}
{% import 'translations.html' as translations with context %}
{% if translations.entry_hreflang(page) %}
{{ translations.entry_hreflang(page) }}
{% endif %}
{% endblock head_links %}

{% block content %}
<article>
<div class="row-fluid">
Expand All @@ -41,6 +49,8 @@ <h4>Contents</h4>
<div class="span8 offset2 article-content">
{% endif %}

{% import '_includes/translations.html' as translations with context %}
{{ translations.translations_for(page) }}
{{ page.content }}
{% from '_includes/comments.html' import comments_section with context %}
{{ comments_section(page) }}
Expand Down