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:] == '\r' or line[-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)
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)
77 # Skip comments and empty lines
78 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
82 # insert file format if not already there
84 match = re_Format.match(lines[i])
86 format = match.group(4)
90 error('Cannot convert file format %s' % format)
92 lines.insert(i, "Format 2")
96 # Don't get confused by LaTeX code
97 if re_Preamble.match(lines[i]):
99 while i < len(lines) and not re_EndPreamble.match(lines[i]):
104 if re_MaxCounter.match(lines[i]):
110 # LabelType Counter_EnumI
117 match = re_LabelType.match(lines[i])
119 label = match.group(4)
120 space1 = match.group(1)
121 if string.lower(label[:8]) == "counter_":
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))
127 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
128 # (or change the existing LatexType)
129 match = re_LatexType.match(lines[i])
131 latextype = match.group(4)
132 lines[i] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[i])
133 match = re_Style.match(lines[i])
135 style = match.group(4)
139 if re_End.match(lines[i]) and string.lower(label) == "bibliography":
140 if (latextype == ""):
141 lines.insert(i, "%sLatexType Bib_Environment" % space1)
154 input = open(argv[1], 'rb')
155 output = open(argv[2], 'wb')
157 error(usage(argv[0]))
172 if __name__ == "__main__":