Last active
December 28, 2020 05:24
-
-
Save shotasenga/57b72a0a13f8467e44bd8d89d7afaf27 to your computer and use it in GitHub Desktop.
internationalize Hugo content files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from os import path | |
import sys | |
import frontmatter | |
import re | |
from datetime import datetime | |
def process_dir(dirpath): | |
for fname in os.listdir(dirpath): | |
fpath = path.join(dirpath, fname) | |
if not path.isfile(fpath): | |
continue | |
add_alias(fpath) | |
fpath = rename_for_lang(fpath, 'ja') | |
def add_alias(mdpath): | |
post = frontmatter.load(mdpath) | |
if type(post['date']) == str: | |
post['date'] = datetime.fromisoformat(post['date']) | |
# blog: "/:section/:year-:month-:day/:slug/" | |
dates = post['date'].strftime('%Y-%m-%d') | |
alias = f'/blog/{dates}/{post["slug"]}/' | |
if 'aliases' in post: | |
post['aliases'].append(alias) | |
else: | |
post['aliases'] = [alias] | |
with open(mdpath, 'w') as f: | |
f.write(frontmatter.dumps(post)) | |
def rename_for_lang(mdpath, lang): | |
newpath = re.sub(r'(content/blog/.*)\.md', '\\1/index.ja.md', mdpath) | |
os.renames(mdpath, newpath) | |
return newpath | |
def main(): | |
cwd = path.dirname(__file__) | |
blog_content_dir = path.join(cwd, 'content/blog') | |
process_dir(blog_content_dir) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
name = "pypi" | |
url = "https://pypi.org/simple" | |
verify_ssl = true | |
[dev-packages] | |
[packages] | |
python-frontmatter = "*" | |
[requires] | |
python_version = "3.7" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment