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
18 # incremented 24 March 2008 by rgh
19 # AddToPreamble tag added to layout files
24 return ("Usage: %s inputfile outputfile\n" % prog_name +
25 "or %s <inputfile >outputfile" % prog_name)
29 sys.stderr.write(message + '\n')
34 " Remove end of line char(s)."
35 if line[-2:-1] == '\r':
37 elif line[-1:] == '\r' or line[-1:] == '\n':
40 # file with no EOL in last line
45 " Read input file and strip lineendings."
48 line = input.readline()
51 lines.append(trim_eol(line))
55 def write(output, lines):
56 " Write output file with native lineendings."
58 output.write(line + os.linesep)
61 # Concatenates old and new in an intelligent way:
62 # If old is wrapped in ", they are stripped. The result is wrapped in ".
63 def concatenate_label(old, new):
64 # Don't use strip as long as we support python 1.5.2
66 return old[0:-1] + new + '"'
68 return '"' + old + new + '"'
72 " Convert to new format."
73 re_Comment = re.compile(r'^(\s*)#')
74 re_Empty = re.compile(r'^(\s*)$')
75 re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
76 re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
77 re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
78 re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
79 re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
80 re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
81 re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
82 re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
83 re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
84 re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
85 re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
86 re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
87 re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
88 re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
90 # counters for sectioning styles (hardcoded in 1.3)
91 counters = {"part" : "\\Roman{part}",
92 "chapter" : "\\arabic{chapter}",
93 "section" : "\\arabic{section}",
94 "subsection" : "\\arabic{section}.\\arabic{subsection}",
95 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
96 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
97 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
99 # counters for sectioning styles in appendix (hardcoded in 1.3)
100 appendixcounters = {"chapter" : "\\Alph{chapter}",
101 "section" : "\\Alph{section}",
102 "subsection" : "\\arabic{section}.\\arabic{subsection}",
103 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
104 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
105 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
107 # Value of TocLevel for sectioning styles
108 toclevels = {"part" : 0,
121 labelstringappendix = ""
123 labelstring_line = -1
124 labelstringappendix_line = -1
131 while i < len(lines):
133 # Skip comments and empty lines
134 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
138 # insert file format if not already there
140 match = re_Format.match(lines[i])
142 format = int(match.group(4))
143 if format > 1 and format < currentFormat:
144 lines[i] = "Format %d" % (format + 1)
146 elif format == currentFormat:
150 error('Cannot convert file format %s' % format)
152 lines.insert(i, "Format 2")
156 # Don't get confused by LaTeX code
157 if re_Preamble.match(lines[i]):
159 while i < len(lines) and not re_EndPreamble.match(lines[i]):
172 # Handle conversion to long CharStyle names
173 match = re_CharStyle.match(lines[i])
175 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
177 lines.insert(i, "\tLyXType charstyle")
180 lines[i] = "\tLabelString %s" % (match.group(3))
185 # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
186 # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
188 match = re_Provides.match(lines[i])
190 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
191 match.group(3), match.group(4))
198 # delete caption styles
199 match = re_Style.match(lines[i])
201 style = string.lower(match.group(4))
202 if style == "caption":
204 while i < len(lines) and not re_End.match(lines[i]):
205 caption.append(lines[i])
208 error('Incomplete caption style.')
213 # delete undefinition of caption styles
214 match = re_NoStyle.match(lines[i])
216 style = string.lower(match.group(4))
217 if style == "caption":
221 # replace the CopyStyle statement with the definition of the real
222 # style. This may result in duplicate statements, but that is OK
223 # since the second one will overwrite the first one.
224 match = re_CopyStyle.match(lines[i])
226 style = string.lower(match.group(4))
227 if style == "caption":
229 lines[i:i+1] = caption
231 # FIXME: This style comes from an include file, we
232 # should replace the real style and not this default.
233 lines[i:i+1] = [' Margin First_Dynamic',
234 ' LatexType Command',
235 ' LatexName caption',
241 ' AlignPossible Center',
242 ' LabelType Sensitive',
243 ' LabelString "Senseless!"',
252 # Delete MaxCounter and remember the value of it
253 match = re_MaxCounter.match(lines[i])
255 level = match.group(4)
256 if string.lower(level) == "counter_chapter":
258 elif string.lower(level) == "counter_section":
260 elif string.lower(level) == "counter_subsection":
262 elif string.lower(level) == "counter_subsubsection":
264 elif string.lower(level) == "counter_paragraph":
266 elif string.lower(level) == "counter_subparagraph":
268 elif string.lower(level) == "counter_enumi":
270 elif string.lower(level) == "counter_enumii":
272 elif string.lower(level) == "counter_enumiii":
279 # LabelType Counter_EnumI
286 match = re_LabelType.match(lines[i])
288 label = match.group(4)
289 # Remember indenting space for later reuse in added lines
290 space1 = match.group(1)
291 # Remember the line for adding the LabelCounter later.
292 # We can't do it here because it could shift latextype_line etc.
294 if string.lower(label[:8]) == "counter_":
295 counter = string.lower(label[8:])
296 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
298 # Remember the LabelString line
299 match = re_LabelString.match(lines[i])
301 labelstring = match.group(4)
304 # Remember the LabelStringAppendix line
305 match = re_LabelStringAppendix.match(lines[i])
307 labelstringappendix = match.group(4)
308 labelstringappendix_line = i
310 # Remember the LatexType line
311 match = re_LatexType.match(lines[i])
313 latextype = string.lower(match.group(4))
316 # Reset variables at the beginning of a style definition
317 match = re_Style.match(lines[i])
319 style = string.lower(match.group(4))
324 labelstringappendix = ""
325 labelstring_line = -1
326 labelstringappendix_line = -1
331 if re_End.match(lines[i]):
333 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
334 # (or change the existing LatexType)
335 if string.lower(label) == "bibliography":
336 if (latextype_line < 0):
337 lines.insert(i, "%sLatexType Bib_Environment" % space1)
340 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
342 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
343 if latextype == "item_environment" and string.lower(label) == "static":
344 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
346 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
347 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
348 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
349 # Don't add the LabelCounter line later
354 # LabelString "Chapter"
358 # LabelString "Chapter \arabic{chapter}"
360 # if this style has a counter. Ditto for LabelStringAppendix.
361 # This emulates the hardcoded article style numbering of 1.3
364 if counters.has_key(style):
365 if labelstring_line < 0:
366 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
369 new_labelstring = concatenate_label(labelstring, counters[style])
370 lines[labelstring_line] = re_LabelString.sub(
371 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
372 lines[labelstring_line])
373 if appendixcounters.has_key(style):
374 if labelstringappendix_line < 0:
375 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
378 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
379 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
380 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
381 lines[labelstringappendix_line])
383 # Now we can safely add the LabelCounter line
384 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
387 # Add the TocLevel setting for sectioning styles
388 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
389 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
404 input = open(argv[1], 'rb')
405 output = open(argv[2], 'wb')
407 error(usage(argv[0]))
412 while (format < currentFormat):
413 format = convert(lines)
424 if __name__ == "__main__":