]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
layout file converter for layout files in old format
[lyx.git] / lib / scripts / layout2layout.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3
4 # file layout2layout.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 will update a .layout file to format 2
13
14
15 import os, re, string, sys
16
17
18 def usage(prog_name):
19     return ("Usage: %s inputfile outputfile\n" % prog_name +
20             "or     %s <inputfile >outputfile" % prog_name)
21
22
23 def error(message):
24     sys.stderr.write(message + '\n')
25     sys.exit(1)
26
27
28 def trim_eol(line):
29     " Remove end of line char(s)."
30     if line[-2:-1] == '\r':
31         return line[:-2]
32     elif line[-1:-1] == '\r' or line[-1:-1] == '\n':
33         return line[:-1]
34     else:
35         # file with no EOL in last line
36         return line
37
38
39 def read(input):
40     " Read input file and strip lineendings."
41     lines = list()
42     while 1:
43         line = input.readline()
44         if not line:
45             break
46         lines.append(trim_eol(line))
47     return lines
48
49
50 def write(output, lines):
51     " Write output file with native lineendings."
52     for line in lines:
53         output.write(line + os.linesep)
54
55
56 def convert(lines):
57     " Convert to new format."
58     re_Comment = re.compile(r'^(\s*)#')
59     re_Empty = re.compile(r'^(\s*)$')
60     re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
61     re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
62     re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
63     re_MaxCounter = re.compile(r'^\s*MaxCounter', re.IGNORECASE)
64     re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
65
66     i = 0
67     only_comment = 1
68     while i < len(lines):
69
70         # Skip comments and empty lines
71         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
72             i = i + 1
73             continue
74
75         # insert file format if not already there
76         if (only_comment):
77                 match = re_Format.match(lines[i])
78                 if match:
79                         format = match.group(4)
80                         if format == '2':
81                                 # nothing to do
82                                 return
83                         error('Cannot convert file format %s' % format)
84                 else:
85                         lines.insert(i, "Format 2")
86                         only_comment = 0
87                         continue
88
89         # Don't get confused by LaTeX code
90         if re_Preamble.match(lines[i]):
91             i = i + 1
92             while i < len(lines) and not re_EndPreamble.match(lines[i]):
93                 i = i + 1
94             continue
95
96         # Delete MaxCounter
97         if re_MaxCounter.match(lines[i]):
98             del lines[i]
99             continue
100
101         # Replace line
102         #
103         # LabelType Counter_EnumI
104         #
105         # with two lines
106         #
107         # LabelType Counter
108         # LabelCounter EnumI
109         #
110         match = re_LabelType.match(lines[i])
111         if match:
112             label = match.group(4)
113             if string.lower(label[:8]) == "counter_":
114                 counter = label[8:]
115                 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
116                 # use the same indentation
117                 space1 = match.group(1)
118                 lines.insert(i + 1, "%sLabelCounter %s" % (space1, counter))
119
120         i = i + 1
121
122
123 def main(argv):
124
125     # Open files
126     if len(argv) == 1:
127         input = sys.stdin
128         output = sys.stdout
129     elif len(argv) == 3:
130         input = open(argv[1], 'rb')
131         output = open(argv[2], 'wb')
132     else:
133         error(usage(argv[0]))
134
135     # Do the real work
136     lines = read(input)
137     convert(lines)
138     write(output, lines)
139
140     # Close files
141     if len(argv) == 3:
142         input.close()
143         output.close()
144
145     return 0
146
147
148 if __name__ == "__main__":
149     main(sys.argv)