]> git.lyx.org Git - lyx.git/blob - development/cmake/doc/ReplaceValues.py
Preparation to support translations with python3
[lyx.git] / development / cmake / doc / ReplaceValues.py
1 #! /usr/bin/env python
2
3 from __future__ import print_function
4
5 # file ReplaceValues.py
6 #
7 # This file is part of LyX, the document processor.
8 # Licence details can be found in the file COPYING.
9 #
10 # author: Kornel Benko, kornel@lyx.org
11 #
12 # Syntax: ReplaceValues.py [<var1>=<Subst1> [<var2>=<Subst> ...]] <Inputfile> [<Inputfile> ...]
13
14 import sys
15 import re
16
17 Subst = {}  # map of desired substitutions
18 prog = re.compile("")
19
20 def createProg():
21     matchingS = "\\b|\\b".join(Subst.keys())
22     pattern = "".join(["(.*)(\\b", matchingS, "\\b)(.*)"])
23     return re.compile(pattern)
24
25 def SubstituteDataInLine(line):
26     result = line
27     m = prog.match(result)
28     if m:
29         return "".join([SubstituteDataInLine(m.group(1)),
30                         Subst[m.group(2)],
31                         SubstituteDataInLine(m.group(3))])
32     return line
33
34
35 def SubstituteDataInFile(InFile):
36     for line in open(InFile):
37         print(SubstituteDataInLine(line[:-1]))
38
39 ##########################################
40
41
42 args = sys.argv
43
44 del args[0] # we don't need the name ot this script
45 while len(args) > 0:
46     arg = args[0]
47     entry = args[0].split("=",1)
48     if len(entry) == 2:
49         key=entry[0]
50         val=entry[1]
51         if len(key) > 0:
52             Subst[key] = val
53     else:
54         prog = createProg()
55         SubstituteDataInFile(args[0])
56     del args[0]
57