1#!/usr/local/bin/python3.8
2#
3# This file is part of the LibreOffice project.
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0. If a copy of the MPL was not distributed with this
7# file, You can obtain one at http://mozilla.org/MPL/2.0/.
8#
9
10import sys, os, getopt, signal
11
12sys.path.append(sys.path[0]+"/to-wiki")
13import wikiconv2
14import getalltitles
15
16def usage():
17    print '''
18help-to-wiki.py [params] [path to l10n]
19
20Converts .xhp files into a wiki
21
22-h, --help            - this help
23-n, --no-translations - generate only English pages
24-r, --redirects       - generate also redirect pages
25-t, --title-savefile  - save the title file
26
27Most probably, you want to generate the redirects only once when you initially
28populate the wiki, and then only update the ones that broke.\n'''
29
30def create_wiki_dirs():
31    dirs = [
32            "Common",
33            "Basic",
34            "Calc",
35            "Chart",
36            "Draw",
37            "Impress",
38            "Math",
39            "Writer",
40            "swriter",
41            "scalc",
42            "simpress",
43            "sdraw",
44            "smath",
45            "schart",
46            "sbasic",
47            "sdatabase"
48           ]
49
50    try:
51        os.mkdir( "wiki" )
52    except:
53        sys.stdout.write( "wiki already generated - the wiki/ subdir exists\n" )
54        sys.exit( 1 )
55
56    for i in dirs:
57        try:
58            os.mkdir( "wiki/" + i )
59        except:
60            pass
61
62# Langs to handle (fully translated or otherwise important)
63langs = ['', 'ast', 'bg', 'bn', 'bn-IN', 'ca', 'cs', 'da', 'de', \
64         'el', 'es', 'eu', 'fi', 'fr', 'hu', 'it', 'ja', 'km', \
65         'ko', 'nb', 'nl', 'om', 'pl', 'pt', 'pt-BR', 'ru', \
66         'sl', 'sv', 'tr', 'vi', 'zh-CN', 'zh-TW' ]
67
68# Argument handling
69try:
70    opts, args = getopt.getopt(sys.argv[1:], 'hnrt', ['help', 'no-translations', 'redirects', 'title-savefile'])
71except getopt.GetoptError:
72    usage()
73    sys.exit(1)
74
75generate_redirects = False
76title_savefile = False
77for opt, arg in opts:
78    if opt in ('-h', '--help'):
79        usage()
80        sys.exit()
81    elif opt in ('-n', '--no-translations'):
82        langs = ['']
83    elif opt in ('-r', '--redirects'):
84        generate_redirects = True
85    elif opt in ('-t', '--title-savefile'):
86        title_savefile = True
87
88def signal_handler(signal, frame):
89    sys.stderr.write( 'Exiting...\n' )
90    sys.exit(1)
91
92# Do the work
93signal.signal(signal.SIGINT, signal_handler)
94
95create_wiki_dirs()
96
97print "Generating the titles..."
98title_data = getalltitles.gettitles("source/text")
99if title_savefile:
100    with open ('alltitles.csv', 'w') as f:
101        for d in title_data:
102            f.write('%s;%s;%s\n' % (d[0], d[1], d[2]))
103
104try:
105    po_path = args[0]
106except:
107    po_path = '../translations/source'
108    sys.stderr.write('Path to the .po files not provided, using "%s"\n'% po_path)
109
110# do the work
111for lang in langs:
112    wikiconv2.convert(title_data, generate_redirects, lang, '%s/%s/helpcontent2/source'% (po_path, lang))
113
114# vim:set shiftwidth=4 softtabstop=4 expandtab:
115