1#!/usr/bin/env python
2# texi-langutils.py
3#
4# This file is part of LilyPond, the GNU music typesetter.
5#
6# Copyright (C) 2006--2021 Jan Nieuwenhuizen <janneke@gnu.org>
7#
8# LilyPond is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# LilyPond is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20
21
22# WARNING: this script can't find files included in a different directory
23
24import sys
25import re
26import getopt
27import os
28
29
30def read_pipe(command):
31    print(command)
32    pipe = os.popen(command)
33    output = pipe.read()
34    if pipe.close():
35        print("pipe failed: %(command)s" % locals())
36    return output
37
38
39optlist, texi_files = getopt.getopt(sys.argv[1:], 'no:d:b:i:l:', [
40                                    'skeleton', 'gettext', 'head-only'])
41# -n   don't process @include's in texinfo files
42process_includes = not ('-n', '') in optlist
43
44# --gettext    generate a node list from a Texinfo source
45make_gettext = ('--gettext', '') in optlist
46# --skeleton   extract the node tree from a Texinfo source
47make_skeleton = ('--skeleton', '') in optlist
48# --head-only  only write first node in included Texinfo skeletons
49head_only = ('--head-only', '') in optlist
50
51output_name = 'doc.pot'
52
53# @untranslated should be defined as a macro in Texinfo source
54node_blurb = '''@untranslated
55'''
56doclang = ''
57head_committish = read_pipe('git rev-parse HEAD')
58intro_blurb = '''\\input texinfo @c -*- coding: utf-8; mode: texinfo%(doclang)s -*-
59@c This file is part of %(topfile)s
60@ignore
61    Translation of GIT committish: %(head_committish)s
62    When revising a translation, copy the HEAD committish of the
63    version that you are working on.  See TRANSLATION for details.
64@end ignore
65'''
66
67end_blurb = """
68@c -- SKELETON FILE --
69"""
70
71for x in optlist:
72    if x[0] == '-o':  # -o NAME   set PO output file name to NAME
73        output_name = x[1]
74    elif x[0] == '-d':  # -d DIR    set working directory to DIR
75        print('FIXME: this is evil.  use cd DIR && texi-langutils ...')
76        # even better, add a sane -o option
77        os.chdir(x[1])
78    elif x[0] == '-b':  # -b BLURB  set blurb written at each node to BLURB
79        node_blurb = x[1]
80    elif x[0] == '-i':  # -i BLURB  set blurb written at beginning of each file to BLURB
81        intro_blurb = x[1]
82    elif x[0] == '-l':  # -l ISOLANG  set documentlanguage to ISOLANG
83        doclang = '; documentlanguage: ' + x[1]
84
85texinfo_with_menus_re = re.compile(
86    r"^(\*) +([^:\n]+)::.*?$|^@(include|menu|end menu|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *(.*?)$|@(rglos){(.+?)}", re.M)
87
88texinfo_re = re.compile(
89    r"^@(include|node|(?:unnumbered|appendix)(?:(?:sub){0,2}sec)?|top|chapter|(?:sub){0,2}section|(?:major|chap|(?:sub){0,2})heading) *(.+?)$|@(rglos){(.+?)}", re.M)
90
91ly_string_re = re.compile(
92    r'^([a-zA-Z]+)[\t ]*=|%+[\t ]*(.*)$|\\(?:new|context)\s+(?:[a-zA-Z]*?(?:Staff(?:Group)?|Voice|FiguredBass|FretBoards|Names|Devnull))\s+=\s+"?([a-zA-Z]+)"?\s+')
93lsr_verbatim_ly_re = re.compile(r'% begin verbatim$')
94texinfo_verbatim_ly_re = re.compile(r'^@lilypond\[.*?verbatim')
95
96
97def process_texi(texifilename, i_blurb, n_blurb, write_skeleton, topfile,
98                 output_file=None, scan_ly=False, inclusion_level=0):
99    try:
100        f = open(texifilename, 'r', encoding='utf8')
101        texifile = f.read()
102        f.close()
103        printedfilename = texifilename.replace('../', '')
104        includes = []
105
106        # process ly var names and comments
107        if output_file and (scan_ly or texifilename.endswith('.ly')):
108            lines = texifile.splitlines()
109            i = 0
110            in_verb_ly_block = False
111            if texifilename.endswith('.ly'):
112                verbatim_ly_re = lsr_verbatim_ly_re
113            else:
114                verbatim_ly_re = texinfo_verbatim_ly_re
115            for i in range(len(lines)):
116                if verbatim_ly_re.search(lines[i]):
117                    in_verb_ly_block = True
118                elif lines[i].startswith('@end lilypond'):
119                    in_verb_ly_block = False
120                elif in_verb_ly_block:
121                    for (var, comment, context_id) in ly_string_re.findall(lines[i]):
122                        if var:
123                            output_file.write('# ' + printedfilename + ':' +
124                                              str(i + 1) + ' (variable)\n_(r"' + var + '")\n')
125                        elif comment:
126                            output_file.write('# ' + printedfilename + ':' +
127                                              str(i + 1) + ' (comment)\n_(r"' +
128                                              comment.replace('"', '\\"') + '")\n')
129                        elif context_id:
130                            output_file.write('# ' + printedfilename + ':' +
131                                              str(i + 1) + ' (context id)\n_(r"' +
132                                              context_id + '")\n')
133
134        # process Texinfo node names and section titles
135        if write_skeleton:
136            g = open(os.path.basename(texifilename), 'w', encoding='utf8')
137            subst = globals()
138            subst.update(locals())
139            g.write(i_blurb % subst)
140            tutu = texinfo_with_menus_re.findall(texifile)
141            node_just_defined = ''
142            for item in tutu:
143                if item[0] == '*':
144                    g.write('* ' + item[1] + '::\n')
145                elif output_file and item[4] == 'rglos':
146                    output_file.write(
147                        '_(r"' + item[5] + '") # @rglos in ' + printedfilename + '\n')
148                elif item[2] == 'menu':
149                    g.write('@menu\n')
150                elif item[2] == 'end menu':
151                    g.write('@end menu\n\n')
152                elif item[2] == 'documentlanguage':
153                    g.write('@documentlanguage ' + doclang + '\n')
154                else:
155                    space = ' '
156                    if item[3].startswith('{') or not item[3].strip():
157                        space = ''
158                    g.write('@' + item[2] + space + item[3] + '\n')
159                    if node_just_defined:
160                        g.write('@translationof ' + node_just_defined + '\n')
161                        g.write(n_blurb)
162                        node_just_defined = ''
163                        if head_only and inclusion_level == 1:
164                            break
165                    elif item[2] == 'include':
166                        includes.append(item[3])
167                    else:
168                        if output_file:
169                            output_file.write('# @' + item[2] + ' in ' +
170                                              printedfilename + '\n_(r"' + item[3].strip() + '")\n')
171                        if item[2] == 'node':
172                            node_just_defined = item[3].strip()
173            if not head_only:
174                g.write(end_blurb)
175            g.close()
176
177        elif output_file and scan_ly:
178            toto = texinfo_re.findall(texifile)
179            for item in toto:
180                if item[0] == 'include':
181                    includes.append(item[1])
182                elif item[2] == 'rglos':
183                    output_file.write(
184                        '# @rglos in ' + printedfilename + '\n_(r"' + item[3] + '")\n')
185                else:
186                    output_file.write('# @' + item[0] + ' in ' + printedfilename +
187                                      '\n_(r"' + item[1].strip().replace('\\', r'\\') + '")\n')
188
189        if process_includes and (not head_only or inclusion_level < 1):
190            dir = os.path.dirname(texifilename)
191            for item in includes:
192                process_texi(os.path.join(dir, item.strip()), i_blurb, n_blurb,
193                             write_skeleton, topfile, output_file, scan_ly, inclusion_level + 1)
194    except IOError as xxx_todo_changeme:
195        (errno, strerror) = xxx_todo_changeme.args
196        sys.stderr.write("I/O error(%s): %s: %s\n" %
197                         (errno, texifilename, strerror))
198
199
200if intro_blurb != '':
201    intro_blurb += '\n\n'
202if node_blurb != '':
203    node_blurb = '\n' + node_blurb + '\n\n'
204if make_gettext:
205    node_list_filename = 'node_list'
206    node_list = open(node_list_filename, 'w', encoding='utf8')
207    node_list.write('# -*- coding: utf-8 -*-\n')
208    for texi_file in texi_files:
209        # Urgly: scan ly comments and variable names only in English doco
210        is_english_doc = (
211            True
212            and not 'Documentation/ca/' in texi_file
213            and not 'Documentation/cs/' in texi_file
214            and not 'Documentation/de/' in texi_file
215            and not 'Documentation/es/' in texi_file
216            and not 'Documentation/fr/' in texi_file
217            and not 'Documentation/hu/' in texi_file
218            and not 'Documentation/ja/' in texi_file
219            and not 'Documentation/it/' in texi_file
220            and not 'Documentation/nl/' in texi_file
221            and not 'Documentation/po/' in texi_file
222            and not 'Documentation/pt/' in texi_file
223            and not 'Documentation/zh/' in texi_file
224        )
225        process_texi(texi_file, intro_blurb, node_blurb, make_skeleton,
226                     os.path.basename(texi_file), node_list,
227                     scan_ly=is_english_doc)
228    for word in ('Up:', 'Next:', 'Previous:', 'Appendix ', 'Footnotes', 'Table of Contents'):
229        node_list.write('_(r"' + word + '")\n')
230    node_list.close()
231    os.system('xgettext --keyword=_doc -c -L Python --no-location -o ' +
232              output_name + ' ' + node_list_filename)
233else:
234    for texi_file in texi_files:
235        process_texi(texi_file, intro_blurb, node_blurb, make_skeleton,
236                     os.path.basename(texi_file))
237