From f1baad53472c8523ffb2081e87c03bf1cdcd45f0 Mon Sep 17 00:00:00 2001 From: Neelix57 Date: Wed, 23 Sep 2020 10:52:48 +0200 Subject: [PATCH] gen-html.py translation --- .../gen-html.py | 266 ++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100755 contrib/help-pages-translation-to-french/gen-html.py diff --git a/contrib/help-pages-translation-to-french/gen-html.py b/contrib/help-pages-translation-to-french/gen-html.py new file mode 100755 index 00000000..d74927e7 --- /dev/null +++ b/contrib/help-pages-translation-to-french/gen-html.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python3 + +import builtins +import os +import sys + +from pysollib.gamedb import GAME_DB +from pysollib.gamedb import GI +from pysollib.mfxutil import latin1_normalize +from pysollib.mygettext import fix_gettext +# outdir = '../html' +pysollib_dir = '../' + + +builtins._ = lambda x: x +builtins.n_ = lambda x: x + +import pysollib.games # noqa: E402,F402,I100,I202 +import pysollib.games.mahjongg # noqa: E402,F402 +import pysollib.games.special # noqa: E402,F401,F402 +import pysollib.games.ultra # noqa: E402,F401,F402 + +try: + os.mkdir('html') +except Exception: + pass + +try: + os.mkdir('html/rules') +except Exception: + pass + + +def merge_dicts(x, y): + ret = x.copy() + ret.update(y) + return ret + + +pysollib_path = os.path.join(sys.path[0], pysollib_dir) +sys.path[0] = os.path.normpath(pysollib_path) +# print sys.path + +fix_gettext() + +files = [ + ('credits.html', 'PySol Credits'), + ('ganjifa.html', 'PySol - Règles générales du Ganjifa'), + ('general_rules.html', 'PySol - Règles générales'), + ('glossary.html', 'PySol - Glossaire'), + ('hanafuda.html', 'PySol - Règles générales des jeux de cartes à fleurs'), + ('hexadeck.html', 'PySol - Règles générales du jeu Hex A Deck'), + ('howtoplay.html', 'Comment jouer à PySol'), + ('index.html', 'PySol - une collection de jeux de solitaire'), + ('install.html', 'PySol - Installation'), + ('intro.html', 'PySol - Introduction'), + ('license.html', 'Licence de PySol'), + ('news.html', 'PySol - une collection de jeux de solitaire'), + # ('rules_alternate.html', 'PySol - a Solitaire Game Collection'), + # ('rules.html', 'PySol - a Solitaire Game Collection'), + ] + +rules_files = [ + # ('hanoipuzzle.html', ), + ('mahjongg.html', 'PySol - Règles du Mahjongg'), + ('matrix.html', 'PySol - Règles du Matrix'), + ('pegged.html', 'PySol - Règles du Pegged'), + ('shisensho.html', 'PySol - Règles du Shisen-Sho'), + ('spider.html', 'PySol - Règles du Spider'), + ('freecell.html', 'PySol - Règles du FreeCell'), + ] +wikipedia_files = [ + ('houseinthewood.html', 'PySol - Règles de House in the Woods'), + ('fourseasons.html', 'PySol - Règles du Four Seasons'), + ] + + +def _fmt(fmt, params): + return fmt % merge_dicts(params, {'logo_url': "images/pysollogo03.png", + 'logo_alt': "PySol FC Logo"}) + + +main_header = ''' + + +%(title)s + + + + +%(logo_alt)s +
+''' +main_footer = ''' +

+
+%(back_to_index_link)s + +''' + +rules_header = ''' + + +%(title)s + + + + +%(logo_alt)s +
+''' +rules_footer = ''' +

+%(footer)s +
+Glossaire +
+Règles générales + +

+Sommaire + +''' + +wikipedia_header = ''' + + +%(title)s + + + + +%(logo_alt)s +
+''' + + +def getGameRulesFilename(n): + if n.startswith('Mahjongg'): + return 'mahjongg.html' + return latin1_normalize(n) + '.html' + + +def gen_main_html(): + for infile, title in files: + outfile = open(os.path.join('html', infile), 'w') + print(_fmt(main_header, {'title': title}), file=outfile) + with open(infile, 'r') as file: + print(file.read(), file=outfile) + s = 'Sommaire' + if infile == 'index.html': + s = '' + print(_fmt(main_footer, {'back_to_index_link': s}), file=outfile) + outfile.close() + + +def gen_rules_html(): + # ls = glob(os.path.join('rules', '*.html')) + rules_ls = os.listdir('rules') + rules_ls.sort() + wikipedia_ls = os.listdir('wikipedia') + wikipedia_ls.sort() + + games = GAME_DB.getGamesIdSortedByName() + rules_list = [] + files_list = [] + for fn, tt in rules_files: + rules_list.append(('rules', fn, tt, '')) + files_list.append(fn) + for fn, tt in wikipedia_files: + rules_list.append(('wikipedia', fn, tt, '')) + files_list.append(fn) + altnames = [] + + # open file of list of all rules + out_rules = open(os.path.join('html', 'rules.html'), 'w') + print(_fmt(main_header, {'title': 'PySol - une collection de jeux de solitaire'}), + file=out_rules) + with open('rules.html', 'r') as file: + print(file.read(), file=out_rules) + + for id in games: + # create list of rules + + gi = GAME_DB.get(id) + + rules_fn = gi.rules_filename + if not rules_fn: + rules_fn = getGameRulesFilename(gi.name) + + if rules_fn in files_list: + continue + + if rules_fn in rules_ls: + rules_dir = 'rules' + elif rules_fn in wikipedia_ls: + rules_dir = 'wikipedia' + else: + print('missing rules for %s (file: %s)' + % (gi.name, rules_fn)) + continue + + # print '>>>', rules_fn + + title = 'PySol - Règles pour ' + gi.name + s = '' + if gi.si.game_type == GI.GT_HANAFUDA: + s = 'Règles générales des jeux de cartes à fleurs' + elif gi.si.game_type == GI.GT_DASHAVATARA_GANJIFA: + s = 'A-propos du Ganjifa' + elif gi.si.game_type == GI.GT_HEXADECK: + s = 'Règles générales du jeu Hex A Deck' + elif gi.si.game_type == GI.GT_MUGHAL_GANJIFA: + s = 'A-propos du Ganjifa' + # print '***', gi.name, '***' + + rules_list.append((rules_dir, rules_fn, title, s)) + files_list.append(rules_fn) + # rules_list.append((rules_fn, gi.name)) + print('

  • %s' + % (rules_fn, gi.name), file=out_rules) + for n in gi.altnames: + altnames.append((n, rules_fn)) + + print('\n' + _fmt(main_footer, + {'back_to_index_link': + 'Sommaire'}), + file=out_rules) + + out_rules.close() + + # create file of altnames + out_rules_alt = open(os.path.join('html', 'rules_alternate.html'), 'w') + print(_fmt(main_header, {'title': 'PySol - une collection de jeux de solitaire'}), + file=out_rules_alt) + with open('rules_alternate.html', 'r') as file: + print(file.read(), file=out_rules_alt) + altnames.sort() + for name, fn in altnames: + print('
  • %s' + % (fn, name), file=out_rules_alt) + print('\n' + _fmt(main_footer, + {'back_to_index_link': + 'Sommaire'}), + file=out_rules_alt) + out_rules_alt.close() + + # create rules + for dir, filename, title, footer in rules_list: + outfile = open( + os.path.join('html', 'rules', filename), 'w', encoding='utf-8') + if dir == 'rules': + print(_fmt(rules_header, {'title': title}), file=outfile) + else: # d == 'wikipedia' + print(_fmt(wikipedia_header, {'title': title}), file=outfile) + with open(os.path.join(dir, filename), 'r', encoding='utf-8') as file: + print(file.read(), file=outfile) + print(_fmt(rules_footer, {'footer': footer}), file=outfile) + outfile.close() + + +gen_main_html() +gen_rules_html()