2 # -*- coding: iso-8859-1 -*-
4 # file layout2layout.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
12 # This script will update a .layout file to format 2
15 import os, re, string, sys
19 return ("Usage: %s inputfile outputfile\n" % prog_name +
20 "or %s <inputfile >outputfile" % prog_name)
24 sys.stderr.write(message + '\n')
29 " Remove end of line char(s)."
30 if line[-2:-1] == '\r':
32 elif line[-1:-1] == '\r' or line[-1:-1] == '\n':
35 # file with no EOL in last line
40 " Read input file and strip lineendings."
43 line = input.readline()
46 lines.append(trim_eol(line))
50 def write(output, lines):
51 " Write output file with native lineendings."
53 output.write(line + os.linesep)
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)
70 # Skip comments and empty lines
71 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
75 # insert file format if not already there
77 match = re_Format.match(lines[i])
79 format = match.group(4)
83 error('Cannot convert file format %s' % format)
85 lines.insert(i, "Format 2")
89 # Don't get confused by LaTeX code
90 if re_Preamble.match(lines[i]):
92 while i < len(lines) and not re_EndPreamble.match(lines[i]):
97 if re_MaxCounter.match(lines[i]):
103 # LabelType Counter_EnumI
110 match = re_LabelType.match(lines[i])
112 label = match.group(4)
113 if string.lower(label[:8]) == "counter_":
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))
130 input = open(argv[1], 'rb')
131 output = open(argv[2], 'wb')
133 error(usage(argv[0]))
148 if __name__ == "__main__":