You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.0 KiB
57 lines
2.0 KiB
#!/usr/bin/python3.8
|
|
# (requires python3.8)
|
|
|
|
import translate
|
|
import sys
|
|
import os
|
|
from shutil import copyfile
|
|
|
|
|
|
RECURSIVE = True
|
|
|
|
def careful_translate(old_file, new_file):
|
|
try:
|
|
translate.write_translated(old_file, new_file)
|
|
except translate.UnknownKey as e:
|
|
print(f"ERROR: unknow key {e.args[0]} in file {old_file}")
|
|
except:
|
|
print(f"ERROR in file {old_file}")
|
|
raise
|
|
|
|
def batch_translate(source_folder, dest_folder):
|
|
source_files = os.listdir(source_folder)
|
|
for f in source_files:
|
|
old_file = os.path.join(source_folder, f)
|
|
new_file = os.path.join(dest_folder, f)
|
|
|
|
if os.path.isfile(old_file): # === file ===
|
|
if f.endswith(".md"): # --- markdown file to convert
|
|
start = f.rstrip(".md") # pure name
|
|
old_start = os.path.join(source_folder, start)
|
|
new_dest = os.path.join(dest_folder, start)
|
|
if os.path.isdir(old_start): # if a folder with same name exists, this page is an "index page"
|
|
os.makedirs(new_dest, exist_ok=True) # create dir now
|
|
new_file = os.path.join(new_dest, "_index.md") # in zola, index must be inside folder
|
|
# keep going on with translation, but with new location
|
|
careful_translate(old_file, new_file)
|
|
else: # --- non markdown files are simply copied
|
|
copyfile(old_file, new_file)
|
|
|
|
elif os.path.isdir(old_file): # === folder ===
|
|
if not RECURSIVE:
|
|
print(f"ignoring directory {old_file}")
|
|
else:
|
|
os.makedirs(new_file, exist_ok=True)
|
|
batch_translate(old_file, new_file)
|
|
|
|
else: # === link, or other ===
|
|
print(f"file type not managed: {old_file}")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
source_folder = sys.argv[1]
|
|
dest_folder = sys.argv[2]
|
|
os.makedirs(dest_folder, exist_ok=True)
|
|
batch_translate(source_folder, dest_folder) |