File size: 2,184 Bytes
f65fe85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# makesnippets.py
#
# This file is part of LilyPond, the GNU music typesetter.
#
# Copyright (C) 2012--2020  John Mandereau <[email protected]>
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.

'''USAGE: makesnippets.py INPUT_DIR OUTPUT_DIR LANGUAGES DOC_DIR

Read all .ly files from INPUT_DIR, insert translations from .texidoc
files found in DOC_DIR/LANG/texdiocs, and write ther result to OUTPUT_DIR.'''

import codecs
import glob
import sys
import os.path
import re

(input_dir, output_dir, languages_str, doc_dir) = sys.argv[1:5]

languages = languages_str.split(' ')

texidoc_dirs = [os.path.join(doc_dir, lang, 'texidocs')
                for lang in languages if lang]

begin_header_re = re.compile(r'\\header\s*{', re.M)

for f in glob.glob(os.path.join(input_dir, '*.ly')):
    name = os.path.basename(f)
    s = codecs.open(f, 'r', 'utf-8').read()
    for path in texidoc_dirs:
        texidoc_translation_path = \
            os.path.join(path, os.path.splitext(name)[0] + '.texidoc')
        if os.path.exists(texidoc_translation_path):
            texidoc_translation = codecs.open(
                texidoc_translation_path, 'r', 'utf-8').read()
            # Since we want to insert the translations verbatim using a
            # regexp, \\ is understood as ONE escaped backslash. So we have
            # to escape those backslashes once more...
            texidoc_translation = texidoc_translation.replace('\\', '\\\\')
            s = begin_header_re.sub('\\g<0>\n' + texidoc_translation, s, 1)
    dest = os.path.join(output_dir, name)
    codecs.open(dest, 'w', 'utf-8').write(s)