| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- from os.path import join
- from pathlib import Path
- from shutil import copytree, rmtree
- from jinja2 import Environment, FileSystemLoader
- import jogai._globals as _globals
- from jogai.settings import paths, settings
- from jogai.translations import gettext as _
- from jogai.videogame import load_environment, prepare
- CLASSNAMES = {
- 'character': _('character').capitalize(),
- 'scene': _('scene').capitalize(),
- }
- DIRS = {
- 'html': Path(f'{paths.EXPORT_MAIN_DIR}'), # /{paths.EXPORT_HTML_SUBDIR}'),
- 'css': Path(f'{paths.EXPORT_MAIN_DIR}/{paths.EXPORT_CSS_SUBDIR}'),
- 'js': Path(f'{paths.EXPORT_MAIN_DIR}/{paths.EXPORT_JS_SUBDIR}'),
- }
- def create_dirs():
- for dir_path in DIRS.values():
- dir_path.mkdir(parents=True)
- copytree(paths.IMAGES_DIR, paths.EXPORT_MAIN_DIR / paths.IMAGES_DIR)
- def write_file(filename: str, content: str):
- filename = Path(filename)
- file_path = DIRS[filename.suffix.removeprefix('.')] / filename
- with file_path.open(mode='w', encoding='utf-8') as f:
- f.write(content)
- def export(environment: str):
- load_environment(environment, to_export=True)
- window_size = (settings.WINDOW_WIDTH, settings.WINDOW_HEIGHT)
- jinja_env = Environment(
- loader=FileSystemLoader(
- join(
- settings.BASE_DIR,
- paths.EXPORTER_TEMPLATES_PATH,
- )
- )
- )
- template = jinja_env.get_template('base.html')
- html_content = template.render(
- characters=_globals.vg_characters,
- classnames=CLASSNAMES,
- css_path=f'{paths.EXPORT_CSS_SUBDIR}/{environment}.css',
- current_scene=_globals.vg_scene,
- language=settings.LANGUAGE_CODE,
- js_dir=f'{paths.EXPORT_JS_SUBDIR}',
- title=_globals.vg_title,
- )
- template = jinja_env.get_template('base.css')
- css_content = template.render(
- classnames=CLASSNAMES,
- window_size=window_size,
- )
- template = jinja_env.get_template('videogame.js')
- js_videogame_content = template.render(
- characters=_globals.vg_characters,
- classnames=CLASSNAMES,
- window_size=window_size,
- )
- template = jinja_env.get_template('scene.js')
- js_scenes_content = template.render(
- classnames=CLASSNAMES,
- scene=_globals.vg_scene,
- settings=settings,
- )
- template = jinja_env.get_template('character.js')
- js_characters_content = template.render(
- classnames=CLASSNAMES,
- settings=settings,
- paths=paths,
- )
- rmtree(paths.EXPORT_MAIN_DIR)
- create_dirs()
- write_file(f'{environment}.html', html_content)
- write_file(f'{environment}.css', css_content)
- write_file('videogame.js', js_videogame_content)
- write_file('scenes.js', js_scenes_content)
- write_file('characters.js', js_characters_content)
|