annotate build_website.py @ 63:ee1df80a1715 default tip

Nicer-looking input form
author mathieui
date Sun, 24 May 2020 14:19:29 +0200
parents 46ca1aa08146
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
d3b943ac148f Add translations
mathieui
parents:
diff changeset
1 #!/usr/bin/env python3
d3b943ac148f Add translations
mathieui
parents:
diff changeset
2 '''Locale build script for XAM'''
d3b943ac148f Add translations
mathieui
parents:
diff changeset
3
60
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
4 # SPDX-License-Identifier: AGPL-3.0-only
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
5
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
6 # Copyright © 2017-2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
7 # Copyright © 2020 Mathieu Pasquet <mathieui@mathieui.net>
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
8 #
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
9 # Released under GNU AGPL v3 only, read the file 'LICENSE' for more information.
6d861d881b96 Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 59
diff changeset
10
49
d3b943ac148f Add translations
mathieui
parents:
diff changeset
11 import sys
d3b943ac148f Add translations
mathieui
parents:
diff changeset
12 from os import makedirs, path
d3b943ac148f Add translations
mathieui
parents:
diff changeset
13 from django.conf import settings
d3b943ac148f Add translations
mathieui
parents:
diff changeset
14 from shutil import copytree, copy
d3b943ac148f Add translations
mathieui
parents:
diff changeset
15
d3b943ac148f Add translations
mathieui
parents:
diff changeset
16 AVAILABLE_LANGUAGES = ['en', 'fr']
d3b943ac148f Add translations
mathieui
parents:
diff changeset
17
d3b943ac148f Add translations
mathieui
parents:
diff changeset
18
d3b943ac148f Add translations
mathieui
parents:
diff changeset
19 def make_messages():
d3b943ac148f Add translations
mathieui
parents:
diff changeset
20 '''Extract translatable strings from the template.'''
d3b943ac148f Add translations
mathieui
parents:
diff changeset
21
d3b943ac148f Add translations
mathieui
parents:
diff changeset
22 from django.core.management.commands import makemessages
d3b943ac148f Add translations
mathieui
parents:
diff changeset
23
d3b943ac148f Add translations
mathieui
parents:
diff changeset
24 command = makemessages.Command()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
25 command.stdout = sys.stdout
d3b943ac148f Add translations
mathieui
parents:
diff changeset
26 command.handle(
d3b943ac148f Add translations
mathieui
parents:
diff changeset
27 locale=AVAILABLE_LANGUAGES, exclude=[], domain='django', verbosity=True,
d3b943ac148f Add translations
mathieui
parents:
diff changeset
28 all=True, extensions=['xhtml', 'js'], symlinks=False, ignore_patterns=[],
d3b943ac148f Add translations
mathieui
parents:
diff changeset
29 use_default_ignore_patterns=True, no_wrap=True, no_location=True,
d3b943ac148f Add translations
mathieui
parents:
diff changeset
30 no_obsolete=True, keep_pot=True, add_location=False
d3b943ac148f Add translations
mathieui
parents:
diff changeset
31 )
d3b943ac148f Add translations
mathieui
parents:
diff changeset
32
d3b943ac148f Add translations
mathieui
parents:
diff changeset
33
d3b943ac148f Add translations
mathieui
parents:
diff changeset
34 def compile_messages():
d3b943ac148f Add translations
mathieui
parents:
diff changeset
35 '''For each locale we support, compile the po file into a mo.'''
d3b943ac148f Add translations
mathieui
parents:
diff changeset
36
d3b943ac148f Add translations
mathieui
parents:
diff changeset
37 from django.core.management.commands import compilemessages
d3b943ac148f Add translations
mathieui
parents:
diff changeset
38
d3b943ac148f Add translations
mathieui
parents:
diff changeset
39 command = compilemessages.Command()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
40 command.verbosity = 1
d3b943ac148f Add translations
mathieui
parents:
diff changeset
41 command.stdout = sys.stdout
d3b943ac148f Add translations
mathieui
parents:
diff changeset
42 command.compile_messages(('locale/%s/LC_MESSAGES' % lang, 'django.po')
d3b943ac148f Add translations
mathieui
parents:
diff changeset
43 for lang in AVAILABLE_LANGUAGES)
d3b943ac148f Add translations
mathieui
parents:
diff changeset
44
d3b943ac148f Add translations
mathieui
parents:
diff changeset
45
d3b943ac148f Add translations
mathieui
parents:
diff changeset
46 def main():
d3b943ac148f Add translations
mathieui
parents:
diff changeset
47 '''Call everything in the correct order.'''
d3b943ac148f Add translations
mathieui
parents:
diff changeset
48
d3b943ac148f Add translations
mathieui
parents:
diff changeset
49 from subprocess import call
d3b943ac148f Add translations
mathieui
parents:
diff changeset
50
d3b943ac148f Add translations
mathieui
parents:
diff changeset
51 settings.configure()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
52 make_messages()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
53 compile_messages()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
54
d3b943ac148f Add translations
mathieui
parents:
diff changeset
55 for language in AVAILABLE_LANGUAGES:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
56 call([sys.argv[0], language])
d3b943ac148f Add translations
mathieui
parents:
diff changeset
57
d3b943ac148f Add translations
mathieui
parents:
diff changeset
58
d3b943ac148f Add translations
mathieui
parents:
diff changeset
59 def generate_pages(language):
d3b943ac148f Add translations
mathieui
parents:
diff changeset
60 '''Write a single page, and gzip it for lower network usage.'''
d3b943ac148f Add translations
mathieui
parents:
diff changeset
61
d3b943ac148f Add translations
mathieui
parents:
diff changeset
62 assert language in AVAILABLE_LANGUAGES
d3b943ac148f Add translations
mathieui
parents:
diff changeset
63 target_dir = path.join('.', 'build', language)
d3b943ac148f Add translations
mathieui
parents:
diff changeset
64 makedirs(target_dir, exist_ok=True)
d3b943ac148f Add translations
mathieui
parents:
diff changeset
65
d3b943ac148f Add translations
mathieui
parents:
diff changeset
66 import django
d3b943ac148f Add translations
mathieui
parents:
diff changeset
67 from django.template import Template, Context
d3b943ac148f Add translations
mathieui
parents:
diff changeset
68 import gzip
d3b943ac148f Add translations
mathieui
parents:
diff changeset
69
d3b943ac148f Add translations
mathieui
parents:
diff changeset
70 settings.configure(LANGUAGE_CODE=language, LOCALE_PATHS=['locale'], TEMPLATES=[{'BACKEND': 'django.template.backends.django.DjangoTemplates'}])
d3b943ac148f Add translations
mathieui
parents:
diff changeset
71
d3b943ac148f Add translations
mathieui
parents:
diff changeset
72 django.setup()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
73
62
46ca1aa08146 Rename prosody.css to style.css, and remove Prosody specifics.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 60
diff changeset
74 static = ['share', 'style.css', 'spinner.svg', 'ok.svg', 'error.svg']
49
d3b943ac148f Add translations
mathieui
parents:
diff changeset
75 files = [
d3b943ac148f Add translations
mathieui
parents:
diff changeset
76 'index.xhtml', 'client.js', 'roster.js', 'avatar.js', 'client.js', 'index.xhtml',
d3b943ac148f Add translations
mathieui
parents:
diff changeset
77 'mam.js', 'nickname.js', 'pep.js', 'roster.js', 'strophe.js', 'strophe.mam.js',
d3b943ac148f Add translations
mathieui
parents:
diff changeset
78 'strophe.rsm.js', 'util.js', 'vcard.js'
d3b943ac148f Add translations
mathieui
parents:
diff changeset
79 ]
d3b943ac148f Add translations
mathieui
parents:
diff changeset
80 for file_to_localize in files:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
81 with open(file_to_localize) as template_file:
56
010b905a74d4 Update translation strings
mathieui
parents: 50
diff changeset
82 template = Template('{% load i18n %}' + template_file.read())
49
d3b943ac148f Add translations
mathieui
parents:
diff changeset
83 rendered = template.render(Context({'AVAILABLE_LANGUAGES':
d3b943ac148f Add translations
mathieui
parents:
diff changeset
84 AVAILABLE_LANGUAGES}))
d3b943ac148f Add translations
mathieui
parents:
diff changeset
85 filename = path.join(target_dir, file_to_localize)
d3b943ac148f Add translations
mathieui
parents:
diff changeset
86 with open(filename, 'w') as out:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
87 out.write(rendered)
d3b943ac148f Add translations
mathieui
parents:
diff changeset
88 for static_resource in static:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
89 if path.isdir(static_resource):
50
78f3f79c911b mathieui: Add the CSS files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 49
diff changeset
90 copytree(static_resource, path.join(target_dir, static_resource), dirs_exist_ok=True)
49
d3b943ac148f Add translations
mathieui
parents:
diff changeset
91 else:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
92 copy(static_resource, target_dir)
d3b943ac148f Add translations
mathieui
parents:
diff changeset
93
d3b943ac148f Add translations
mathieui
parents:
diff changeset
94 if __name__ == '__main__':
d3b943ac148f Add translations
mathieui
parents:
diff changeset
95 if len(sys.argv) == 1:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
96 main()
d3b943ac148f Add translations
mathieui
parents:
diff changeset
97 else:
d3b943ac148f Add translations
mathieui
parents:
diff changeset
98 generate_pages(sys.argv[1])