]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
48697693641c92011a80f53bf2582977a59ee916
[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:] == '\r' or line[-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     re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
66     re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
67     re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
68
69     i = 0
70     only_comment = 1
71     label = ""
72     space1 = ""
73     latextype_line = -1
74     style = ""
75     while i < len(lines):
76
77         # Skip comments and empty lines
78         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
79             i = i + 1
80             continue
81
82         # insert file format if not already there
83         if (only_comment):
84                 match = re_Format.match(lines[i])
85                 if match:
86                         format = match.group(4)
87                         if format == '2':
88                                 # nothing to do
89                                 return
90                         error('Cannot convert file format %s' % format)
91                 else:
92                         lines.insert(i, "Format 2")
93                         only_comment = 0
94                         continue
95
96         # Don't get confused by LaTeX code
97         if re_Preamble.match(lines[i]):
98             i = i + 1
99             while i < len(lines) and not re_EndPreamble.match(lines[i]):
100                 i = i + 1
101             continue
102
103         # Delete MaxCounter
104         if re_MaxCounter.match(lines[i]):
105             del lines[i]
106             continue
107
108         # Replace line
109         #
110         # LabelType Counter_EnumI
111         #
112         # with two lines
113         #
114         # LabelType Counter
115         # LabelCounter EnumI
116         #
117         match = re_LabelType.match(lines[i])
118         if match:
119             label = match.group(4)
120             space1 = match.group(1)
121             if string.lower(label[:8]) == "counter_":
122                 counter = label[8:]
123                 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
124                 # use the same indentation
125                 lines.insert(i + 1, "%sLabelCounter %s" % (space1, counter))
126
127         # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
128         # (or change the existing LatexType)
129         match = re_LatexType.match(lines[i])
130         if match:
131             latextype_line = i
132         match = re_Style.match(lines[i])
133         if match:
134             style = match.group(4)
135             label = ""
136             space1 = ""
137             latextype_line = -1
138         if re_End.match(lines[i]) and string.lower(label) == "bibliography":
139             if (latextype_line < 0):
140                 lines.insert(i, "%sLatexType Bib_Environment" % space1)
141                 i = i + 1
142             else:
143                 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
144
145         i = i + 1
146
147
148 def main(argv):
149
150     # Open files
151     if len(argv) == 1:
152         input = sys.stdin
153         output = sys.stdout
154     elif len(argv) == 3:
155         input = open(argv[1], 'rb')
156         output = open(argv[2], 'wb')
157     else:
158         error(usage(argv[0]))
159
160     # Do the real work
161     lines = read(input)
162     convert(lines)
163     write(output, lines)
164
165     # Close files
166     if len(argv) == 3:
167         input.close()
168         output.close()
169
170     return 0
171
172
173 if __name__ == "__main__":
174     main(sys.argv)