]> git.lyx.org Git - lyx.git/blob - development/tools/unicodesymbols.py
ctests: Sort dedicated test documents for language support.
[lyx.git] / development / tools / unicodesymbols.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # file unciodesymbols.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Georg Baum
9
10 # Full author contact details are available in file CREDITS
11
12 # This script reads a unicode symbol file and completes it in the given range
13
14
15 from __future__ import print_function
16 import os, re, string, sys, unicodedata
17 import io
18
19 def usage(prog_name):
20     return ("Usage: %s start stop inputfile outputfile\n" % prog_name +
21             "or     %s start stop <inputfile >outputfile" % prog_name)
22
23
24 def error(message):
25     sys.stderr.write(message + '\n')
26     sys.exit(1)
27
28
29 def trim_eol(line):
30     " Remove end of line char(s)."
31     if line[-1:] == '\n':
32         return line[:-1]
33     else:
34         # file with no EOL in last line
35         return line
36
37
38 def read(input):
39     " Read input file and strip lineendings."
40     lines = list()
41     while 1:
42         line = input.readline()
43         if not line:
44             break
45         line = trim_eol(line)
46         tokens = line.split()
47         char = -1
48         if len(tokens) > 0:
49             if tokens[0][0:2] == "0x":
50                 char = int(tokens[0][2:], 16)
51             elif tokens[0][0:3] == "#0x":
52                 char = int(tokens[0][3:], 16)
53         lines.append([char, line])
54     return lines
55
56
57 def write(output, lines):
58     " Write output file."
59     for line in lines:
60         output.write(line[1] + '\n')
61
62
63 def complete(lines, start, stop):
64     l = 0
65     for i in range(start, stop):
66         # This catches both comments (lines[l][0] == -1) and code points less than i
67         while l < len(lines) and lines[l][0] < i:
68 #            print(lines[l])
69             l = l + 1
70             continue
71         if l >= len(lines) or lines[l][0] != i:
72             if sys.version_info[0] < 3:
73                 c = unichr(i)
74             else:
75                 c = chr(i)
76             name = unicodedata.name(c, "")
77             if name != "":
78                 if unicodedata.combining(c):
79                     combining = "combining"
80                 else:
81                     combining = ""
82                 line = [i, '#0x%04x ""                         "" "%s" "" "" # %s' % (i, combining, name)]
83                 lines.insert(l, line)
84 #                print(lines[l])
85                 l = l + 1
86
87
88 def main(argv):
89
90     # Open files
91     if len(argv) == 3:
92         input = sys.stdin
93         output = sys.stdout
94     elif len(argv) == 5:
95         input = io.open(argv[3], 'r', encoding='utf_8')
96         output = io.open(argv[4], 'w', encoding='utf_8')
97     else:
98         error(usage(argv[0]))
99     if argv[1][:2] == "0x":
100         start = int(argv[1][2:], 16)
101     else:
102         start = int(argv[1])
103     if argv[2][:2] == "0x":
104         stop = int(argv[2][2:], 16)
105     else:
106         stop = int(argv[2])
107
108     # Do the real work
109     lines = read(input)
110     complete(lines, start, stop)
111     write(output, lines)
112
113     # Close files
114     if len(argv) == 3:
115         input.close()
116         output.close()
117
118     return 0
119
120
121 if __name__ == "__main__":
122     main(sys.argv)