2 # -*- coding: utf-8 -*-
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 6
15 import os, re, string, sys
22 return ("Usage: %s inputfile outputfile\n" % prog_name +
23 "or %s <inputfile >outputfile" % prog_name)
27 sys.stderr.write(message + '\n')
32 " Remove end of line char(s)."
33 if line[-2:-1] == '\r':
35 elif line[-1:] == '\r' or line[-1:] == '\n':
38 # file with no EOL in last line
43 " Read input file and strip lineendings."
46 line = input.readline()
49 lines.append(trim_eol(line))
53 def write(output, lines):
54 " Write output file with native lineendings."
56 output.write(line + os.linesep)
59 # Concatenates old and new in an intelligent way:
60 # If old is wrapped in ", they are stripped. The result is wrapped in ".
61 def concatenate_label(old, new):
62 # Don't use strip as long as we support python 1.5.2
64 return old[0:-1] + new + '"'
66 return '"' + old + new + '"'
70 " Convert to new format."
71 re_Comment = re.compile(r'^(\s*)#')
72 re_Empty = re.compile(r'^(\s*)$')
73 re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
74 re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
75 re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
76 re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
77 re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
78 re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
79 re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
80 re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
81 re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
82 re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
83 re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
84 re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
85 re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
86 re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
88 # counters for sectioning styles (hardcoded in 1.3)
89 counters = {"part" : "\\Roman{part}",
90 "chapter" : "\\arabic{chapter}",
91 "section" : "\\arabic{section}",
92 "subsection" : "\\arabic{section}.\\arabic{subsection}",
93 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
94 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
95 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
97 # counters for sectioning styles in appendix (hardcoded in 1.3)
98 appendixcounters = {"chapter" : "\\Alph{chapter}",
99 "section" : "\\Alph{section}",
100 "subsection" : "\\arabic{section}.\\arabic{subsection}",
101 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
102 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
103 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
105 # Value of TocLevel for sectioning styles
106 toclevels = {"part" : 0,
119 labelstringappendix = ""
121 labelstring_line = -1
122 labelstringappendix_line = -1
129 while i < len(lines):
131 # Skip comments and empty lines
132 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
136 # insert file format if not already there
138 match = re_Format.match(lines[i])
140 format = int(match.group(4))
141 if format > 1 and format < currentFormat:
142 lines[i] = "Format %d" % (format + 1)
144 elif format == currentFormat:
148 error('Cannot convert file format %s' % format)
150 lines.insert(i, "Format 2")
154 # Don't get confused by LaTeX code
155 if re_Preamble.match(lines[i]):
157 while i < len(lines) and not re_EndPreamble.match(lines[i]):
166 # Handle conversion to long CharStyle names
167 match = re_CharStyle.match(lines[i])
169 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
171 lines.insert(i, "\tLyXType charstyle")
174 lines[i] = "\tLabelString %s" % (match.group(3))
179 # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
180 # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
182 match = re_Provides.match(lines[i])
184 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
185 match.group(3), match.group(4))
192 # delete caption styles
193 match = re_Style.match(lines[i])
195 style = string.lower(match.group(4))
196 if style == "caption":
198 while i < len(lines) and not re_End.match(lines[i]):
199 caption.append(lines[i])
202 error('Incomplete caption style.')
207 # delete undefinition of caption styles
208 match = re_NoStyle.match(lines[i])
210 style = string.lower(match.group(4))
211 if style == "caption":
215 # replace the CopyStyle statement with the definition of the real
216 # style. This may result in duplicate statements, but that is OK
217 # since the second one will overwrite the first one.
218 match = re_CopyStyle.match(lines[i])
220 style = string.lower(match.group(4))
221 if style == "caption":
223 lines[i:i+1] = caption
225 # FIXME: This style comes from an include file, we
226 # should replace the real style and not this default.
227 lines[i:i+1] = [' Margin First_Dynamic',
228 ' LatexType Command',
229 ' LatexName caption',
235 ' AlignPossible Center',
236 ' LabelType Sensitive',
237 ' LabelString "Senseless!"',
246 # Delete MaxCounter and remember the value of it
247 match = re_MaxCounter.match(lines[i])
249 level = match.group(4)
250 if string.lower(level) == "counter_chapter":
252 elif string.lower(level) == "counter_section":
254 elif string.lower(level) == "counter_subsection":
256 elif string.lower(level) == "counter_subsubsection":
258 elif string.lower(level) == "counter_paragraph":
260 elif string.lower(level) == "counter_subparagraph":
262 elif string.lower(level) == "counter_enumi":
264 elif string.lower(level) == "counter_enumii":
266 elif string.lower(level) == "counter_enumiii":
273 # LabelType Counter_EnumI
280 match = re_LabelType.match(lines[i])
282 label = match.group(4)
283 # Remember indenting space for later reuse in added lines
284 space1 = match.group(1)
285 # Remember the line for adding the LabelCounter later.
286 # We can't do it here because it could shift latextype_line etc.
288 if string.lower(label[:8]) == "counter_":
289 counter = string.lower(label[8:])
290 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
292 # Remember the LabelString line
293 match = re_LabelString.match(lines[i])
295 labelstring = match.group(4)
298 # Remember the LabelStringAppendix line
299 match = re_LabelStringAppendix.match(lines[i])
301 labelstringappendix = match.group(4)
302 labelstringappendix_line = i
304 # Remember the LatexType line
305 match = re_LatexType.match(lines[i])
307 latextype = string.lower(match.group(4))
310 # Reset variables at the beginning of a style definition
311 match = re_Style.match(lines[i])
313 style = string.lower(match.group(4))
318 labelstringappendix = ""
319 labelstring_line = -1
320 labelstringappendix_line = -1
325 if re_End.match(lines[i]):
327 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
328 # (or change the existing LatexType)
329 if string.lower(label) == "bibliography":
330 if (latextype_line < 0):
331 lines.insert(i, "%sLatexType Bib_Environment" % space1)
334 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
336 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
337 if latextype == "item_environment" and string.lower(label) == "static":
338 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
340 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
341 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
342 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
343 # Don't add the LabelCounter line later
348 # LabelString "Chapter"
352 # LabelString "Chapter \arabic{chapter}"
354 # if this style has a counter. Ditto for LabelStringAppendix.
355 # This emulates the hardcoded article style numbering of 1.3
358 if counters.has_key(style):
359 if labelstring_line < 0:
360 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
363 new_labelstring = concatenate_label(labelstring, counters[style])
364 lines[labelstring_line] = re_LabelString.sub(
365 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
366 lines[labelstring_line])
367 if appendixcounters.has_key(style):
368 if labelstringappendix_line < 0:
369 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
372 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
373 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
374 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
375 lines[labelstringappendix_line])
377 # Now we can safely add the LabelCounter line
378 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
381 # Add the TocLevel setting for sectioning styles
382 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
383 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
398 input = open(argv[1], 'rb')
399 output = open(argv[2], 'wb')
401 error(usage(argv[0]))
406 while (format < currentFormat):
407 format = convert(lines)
418 if __name__ == "__main__":