view build_website.py @ 62:46ca1aa08146

Rename prosody.css to style.css, and remove Prosody specifics.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 24 May 2020 13:15:01 +0200
parents 6d861d881b96
children
line wrap: on
line source

#!/usr/bin/env python3
'''Locale build script for XAM'''

# SPDX-License-Identifier: AGPL-3.0-only

# Copyright © 2017-2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
# Copyright © 2020 Mathieu Pasquet <mathieui@mathieui.net>
#
# Released under GNU AGPL v3 only, read the file 'LICENSE' for more information.

import sys
from os import makedirs, path
from django.conf import settings
from shutil import copytree, copy

AVAILABLE_LANGUAGES = ['en', 'fr']


def make_messages():
    '''Extract translatable strings from the template.'''

    from django.core.management.commands import makemessages

    command = makemessages.Command()
    command.stdout = sys.stdout
    command.handle(
        locale=AVAILABLE_LANGUAGES, exclude=[], domain='django', verbosity=True,
        all=True, extensions=['xhtml', 'js'], symlinks=False, ignore_patterns=[],
        use_default_ignore_patterns=True, no_wrap=True, no_location=True,
        no_obsolete=True, keep_pot=True, add_location=False
    )


def compile_messages():
    '''For each locale we support, compile the po file into a mo.'''

    from django.core.management.commands import compilemessages

    command = compilemessages.Command()
    command.verbosity = 1
    command.stdout = sys.stdout
    command.compile_messages(('locale/%s/LC_MESSAGES' % lang, 'django.po')
                             for lang in AVAILABLE_LANGUAGES)


def main():
    '''Call everything in the correct order.'''

    from subprocess import call

    settings.configure()
    make_messages()
    compile_messages()

    for language in AVAILABLE_LANGUAGES:
        call([sys.argv[0], language])


def generate_pages(language):
    '''Write a single page, and gzip it for lower network usage.'''

    assert language in AVAILABLE_LANGUAGES
    target_dir = path.join('.', 'build', language)
    makedirs(target_dir, exist_ok=True)

    import django
    from django.template import Template, Context
    import gzip

    settings.configure(LANGUAGE_CODE=language, LOCALE_PATHS=['locale'], TEMPLATES=[{'BACKEND': 'django.template.backends.django.DjangoTemplates'}])

    django.setup()

    static = ['share', 'style.css', 'spinner.svg', 'ok.svg', 'error.svg']
    files = [
        'index.xhtml', 'client.js', 'roster.js', 'avatar.js', 'client.js', 'index.xhtml',
        'mam.js', 'nickname.js', 'pep.js', 'roster.js', 'strophe.js', 'strophe.mam.js',
        'strophe.rsm.js', 'util.js', 'vcard.js'
    ]
    for file_to_localize in files:
        with open(file_to_localize) as template_file:
            template = Template('{% load i18n %}' + template_file.read())
        rendered = template.render(Context({'AVAILABLE_LANGUAGES':
                                            AVAILABLE_LANGUAGES}))
        filename = path.join(target_dir, file_to_localize)
        with open(filename, 'w') as out:
            out.write(rendered)
    for static_resource in static:
        if path.isdir(static_resource):
            copytree(static_resource, path.join(target_dir, static_resource), dirs_exist_ok=True)
        else:
            copy(static_resource, target_dir)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        main()
    else:
        generate_pages(sys.argv[1])