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 3
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_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
80 re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
81 re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
82 re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
83 re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
85 # counters for sectioning styles (hardcoded in 1.3)
86 counters = {"part" : "\\Roman{part}",
87 "chapter" : "\\arabic{chapter}",
88 "section" : "\\arabic{section}",
89 "subsection" : "\\arabic{section}.\\arabic{subsection}",
90 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
91 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
92 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
94 # counters for sectioning styles in appendix (hardcoded in 1.3)
95 appendixcounters = {"chapter" : "\\Alph{chapter}",
96 "section" : "\\Alph{section}",
97 "subsection" : "\\arabic{section}.\\arabic{subsection}",
98 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
99 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
100 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
102 # Value of TocLevel for sectioning styles
103 toclevels = {"part" : 0,
116 labelstringappendix = ""
118 labelstring_line = -1
119 labelstringappendix_line = -1
126 while i < len(lines):
128 # Skip comments and empty lines
129 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
133 # insert file format if not already there
135 match = re_Format.match(lines[i])
137 format = int(match.group(4))
138 if format > 1 and format < 5:
139 lines[i] = "Format %d" % (format + 1)
145 error('Cannot convert file format %s' % format)
147 lines.insert(i, "Format 2")
151 # Don't get confused by LaTeX code
152 if re_Preamble.match(lines[i]):
154 while i < len(lines) and not re_EndPreamble.match(lines[i]):
159 # Handle conversion to long CharStyle names
160 match = re_CharStyle.match(lines[i])
162 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
164 lines.insert(i, "\tLyXType charstyle")
167 lines[i] = "\tLabelString %s" % (match.group(3))
172 # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
173 # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
175 match = re_Provides.match(lines[i])
177 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
178 match.group(3), match.group(4))
185 # delete caption styles
186 match = re_Style.match(lines[i])
188 style = string.lower(match.group(4))
189 if style == "caption":
191 while i < len(lines) and not re_End.match(lines[i]):
192 caption.append(lines[i])
195 error('Incomplete caption style.')
200 # delete undefinition of caption styles
201 match = re_NoStyle.match(lines[i])
203 style = string.lower(match.group(4))
204 if style == "caption":
208 # replace the CopyStyle statement with the definition of the real
209 # style. This may result in duplicate statements, but that is OK
210 # since the second one will overwrite the first one.
211 match = re_CopyStyle.match(lines[i])
213 style = string.lower(match.group(4))
214 if style == "caption":
216 lines[i:i+1] = caption
218 # FIXME: This style comes from an include file, we
219 # should replace the real style and not this default.
220 lines[i:i+1] = [' Margin First_Dynamic',
221 ' LatexType Command',
222 ' LatexName caption',
228 ' AlignPossible Center',
229 ' LabelType Sensitive',
230 ' LabelString "Senseless!"',
239 # Delete MaxCounter and remember the value of it
240 match = re_MaxCounter.match(lines[i])
242 level = match.group(4)
243 if string.lower(level) == "counter_chapter":
245 elif string.lower(level) == "counter_section":
247 elif string.lower(level) == "counter_subsection":
249 elif string.lower(level) == "counter_subsubsection":
251 elif string.lower(level) == "counter_paragraph":
253 elif string.lower(level) == "counter_subparagraph":
255 elif string.lower(level) == "counter_enumi":
257 elif string.lower(level) == "counter_enumii":
259 elif string.lower(level) == "counter_enumiii":
266 # LabelType Counter_EnumI
273 match = re_LabelType.match(lines[i])
275 label = match.group(4)
276 # Remember indenting space for later reuse in added lines
277 space1 = match.group(1)
278 # Remember the line for adding the LabelCounter later.
279 # We can't do it here because it could shift latextype_line etc.
281 if string.lower(label[:8]) == "counter_":
282 counter = string.lower(label[8:])
283 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
285 # Remember the LabelString line
286 match = re_LabelString.match(lines[i])
288 labelstring = match.group(4)
291 # Remember the LabelStringAppendix line
292 match = re_LabelStringAppendix.match(lines[i])
294 labelstringappendix = match.group(4)
295 labelstringappendix_line = i
297 # Remember the LatexType line
298 match = re_LatexType.match(lines[i])
300 latextype = string.lower(match.group(4))
303 # Reset variables at the beginning of a style definition
304 match = re_Style.match(lines[i])
306 style = string.lower(match.group(4))
311 labelstringappendix = ""
312 labelstring_line = -1
313 labelstringappendix_line = -1
318 if re_End.match(lines[i]):
320 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
321 # (or change the existing LatexType)
322 if string.lower(label) == "bibliography":
323 if (latextype_line < 0):
324 lines.insert(i, "%sLatexType Bib_Environment" % space1)
327 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
329 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
330 if latextype == "item_environment" and string.lower(label) == "static":
331 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
333 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
334 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
335 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
336 # Don't add the LabelCounter line later
341 # LabelString "Chapter"
345 # LabelString "Chapter \arabic{chapter}"
347 # if this style has a counter. Ditto for LabelStringAppendix.
348 # This emulates the hardcoded article style numbering of 1.3
351 if counters.has_key(style):
352 if labelstring_line < 0:
353 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
356 new_labelstring = concatenate_label(labelstring, counters[style])
357 lines[labelstring_line] = re_LabelString.sub(
358 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
359 lines[labelstring_line])
360 if appendixcounters.has_key(style):
361 if labelstringappendix_line < 0:
362 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
365 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
366 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
367 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
368 lines[labelstringappendix_line])
370 # Now we can safely add the LabelCounter line
371 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
374 # Add the TocLevel setting for sectioning styles
375 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
376 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
391 input = open(argv[1], 'rb')
392 output = open(argv[2], 'wb')
394 error(usage(argv[0]))
400 format = convert(lines)
411 if __name__ == "__main__":