]> git.lyx.org Git - lyx.git/blob - development/cmake/doc/ReplaceValues.py
Amend 2d48072e: Get rid of Qt resources
[lyx.git] / development / cmake / doc / ReplaceValues.py
1 #! /usr/bin/python3
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 import codecs
17
18 Subst = {}  # map of desired substitutions
19 prog = re.compile("")
20
21 def createProg():
22     matchingS = u"\\b|\\b".join(Subst.keys())
23     pattern = u"".join(["(.*)(\\b", matchingS, "\\b)(.*)"])
24     return re.compile(pattern)
25
26 def SubstituteDataInLine(line):
27     m = prog.match(line)
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 codecs.open(InFile, 'r', 'UTF-8'):
37         print(SubstituteDataInLine(line[:-1]))
38
39 ##########################################
40
41 # ensure standard output with UTF8 encoding:
42 if sys.version_info < (3,0):
43     sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
44 else:
45     sys.stdout = codecs.getwriter('UTF-8')(sys.stdout.buffer)
46
47 for arg in sys.argv[1:]:         # skip first arg (name of this script)
48     if sys.version_info < (3,0):
49         # support non-ASCII characters in arguments
50         arg = arg.decode(sys.stdin.encoding or 'UTF-8')
51     entry = arg.split("=", 1)
52     if len(entry) == 2:
53         key, val = entry
54         if len(key) > 0:
55             Subst[key] = val
56     else:
57         prog = createProg()
58         SubstituteDataInFile(arg)