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)
56 # Concatenates old and new in an intelligent way:
57 # If old is wrapped in ", they are stripped. The result is wrapped in ".
58 def concatenate_label(old, new):
59 # Don't use strip as long as we support python 1.5.2
61 return old[0:-1] + new + '"'
63 return '"' + old + new + '"'
67 " Convert to new format."
68 re_Comment = re.compile(r'^(\s*)#')
69 re_Empty = re.compile(r'^(\s*)$')
70 re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
71 re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
72 re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
73 re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
74 re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
75 re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
76 re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
77 re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
78 re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
79 re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
81 # counters for sectioning styles (hardcoded in 1.3)
82 counters = {"part" : "\\Roman{part}",
83 "chapter" : "\\arabic{chapter}",
84 "section" : "\\arabic{section}",
85 "subsection" : "@Section@.\\arabic{subsection}",
86 "subsubsection" : "@Subsection@.\\arabic{subsubsection}",
87 "paragraph" : "@Subsubsection@.\\arabic{paragraph}",
88 "subparagraph" : "@Paragraph@.\\arabic{subparagraph}"}
90 # counters for sectioning styles in appendix (hardcoded in 1.3)
91 appendixcounters = {"chapter" : "\\Alph{chapter}",
92 "section" : "\\Alph{section}",
93 "subsection" : "@Section@.\\arabic{subsection}",
94 "subsubsection" : "@Subsection@.\\arabic{subsubsection}",
95 "paragraph" : "@Subsubsection@.\\arabic{paragraph}",
96 "subparagraph" : "@Paragraph@.\\arabic{subparagraph}"}
98 # Value of TocLevel for sectioning styles
99 toclevels = {"part" : 0,
112 labelstringappendix = ""
114 labelstring_line = -1
115 labelstringappendix_line = -1
120 while i < len(lines):
122 # Skip comments and empty lines
123 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
127 # insert file format if not already there
129 match = re_Format.match(lines[i])
131 format = match.group(4)
135 error('Cannot convert file format %s' % format)
137 lines.insert(i, "Format 2")
141 # Don't get confused by LaTeX code
142 if re_Preamble.match(lines[i]):
144 while i < len(lines) and not re_EndPreamble.match(lines[i]):
148 # Delete MaxCounter and remember the value of it
149 match = re_MaxCounter.match(lines[i])
151 level = match.group(4)
152 if string.lower(level) == "counter_chapter":
154 elif string.lower(level) == "counter_section":
156 elif string.lower(level) == "counter_subsection":
158 elif string.lower(level) == "counter_subsubsection":
160 elif string.lower(level) == "counter_paragraph":
162 elif string.lower(level) == "counter_subparagraph":
164 elif string.lower(level) == "counter_enumi":
166 elif string.lower(level) == "counter_enumii":
168 elif string.lower(level) == "counter_enumiii":
175 # LabelType Counter_EnumI
182 match = re_LabelType.match(lines[i])
184 label = match.group(4)
185 # Remember indenting space for later reuse in added lines
186 space1 = match.group(1)
187 # Remember the line for adding the LabelCounter later.
188 # We can't do it here because it could shift latextype_line etc.
190 if string.lower(label[:8]) == "counter_":
191 counter = string.lower(label[8:])
192 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
194 # Remember the LabelString line
195 match = re_LabelString.match(lines[i])
197 labelstring = match.group(4)
200 # Remember the LabelStringAppendix line
201 match = re_LabelStringAppendix.match(lines[i])
203 labelstringappendix = match.group(4)
204 labelstringappendix_line = i
206 # Remember the LatexType line
207 match = re_LatexType.match(lines[i])
211 # Reset variables at the beginning of a style definition
212 match = re_Style.match(lines[i])
214 style = string.lower(match.group(4))
219 labelstringappendix = ""
220 labelstring_line = -1
221 labelstringappendix_line = -1
225 if re_End.match(lines[i]):
227 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
228 # (or change the existing LatexType)
229 if string.lower(label) == "bibliography":
230 if (latextype_line < 0):
231 lines.insert(i, "%sLatexType Bib_Environment" % space1)
234 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
238 # LabelString "Chapter"
242 # LabelString "Chapter \arabic{chapter}"
244 # if this style has a counter. Ditto for LabelStringAppendix.
245 # This emulates the hardcoded article style numbering of 1.3
248 if counters.has_key(style):
249 if labelstring_line < 0:
250 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
253 new_labelstring = concatenate_label(labelstring, counters[style])
254 lines[labelstring_line] = re_LabelString.sub(
255 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
256 lines[labelstring_line])
257 if appendixcounters.has_key(style):
258 if labelstringappendix_line < 0:
259 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
262 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
263 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
264 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
265 lines[labelstringappendix_line])
267 # Now we can safely add the LabelCounter line
268 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
271 # Add the TocLevel setting for sectioning styles
272 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
273 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
286 input = open(argv[1], 'rb')
287 output = open(argv[2], 'wb')
289 error(usage(argv[0]))
304 if __name__ == "__main__":