-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_content.py
executable file
·54 lines (42 loc) · 1.41 KB
/
import_content.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
#!/usr/bin/env python3
import os
import json
import fnmatch
SOURCE_DIR = 'content'
LANGUAGES = ['de', 'fr', 'it']
OUTPUT = 'src/assets/articles.json'
def load():
""" Load articales into dictionary """
articles = {lang: [] for lang in LANGUAGES}
for lang in LANGUAGES:
files = os.listdir(os.path.join(SOURCE_DIR, lang))
markdown_files = [f for f in files if fnmatch.fnmatch(f, '*.md')]
markdown_files.sort()
for article in markdown_files:
with open(os.path.join(SOURCE_DIR, lang, article)) as md_file:
content = md_file.read()
title = content.splitlines()[0]
article_id = article[0:2]
articles[lang].append({
"id": article_id,
"title": title,
"content": content,
})
return articles
def overview(articles):
""" Show number of imported articles per language """
print()
print('IMPORTIERTE ARTIKEL')
print('###################')
print()
for (lang, articles) in articles.items():
print(f'{lang}: {len(articles)}')
print()
def export(articles):
""" Update articles.json in PWA src """
with open(OUTPUT, 'w') as output:
json.dump(articles, output, indent=4, ensure_ascii=False)
if __name__ == '__main__':
all_articles = load()
overview(all_articles)
export(all_articles)