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)
84 # counters for sectioning styles (hardcoded in 1.3)
85 counters = {"part" : "\\Roman{part}",
86 "chapter" : "\\arabic{chapter}",
87 "section" : "\\arabic{section}",
88 "subsection" : "\\arabic{section}.\\arabic{subsection}",
89 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
90 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
91 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
93 # counters for sectioning styles in appendix (hardcoded in 1.3)
94 appendixcounters = {"chapter" : "\\Alph{chapter}",
95 "section" : "\\Alph{section}",
96 "subsection" : "\\arabic{section}.\\arabic{subsection}",
97 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
98 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
99 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
101 # Value of TocLevel for sectioning styles
102 toclevels = {"part" : 0,
115 labelstringappendix = ""
117 labelstring_line = -1
118 labelstringappendix_line = -1
125 while i < len(lines):
127 # Skip comments and empty lines
128 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
132 # insert file format if not already there
134 match = re_Format.match(lines[i])
136 format = int(match.group(4))
137 if format > 1 and format < 4:
138 lines[i] = "Format %d" % (format + 1)
144 error('Cannot convert file format %s' % format)
146 lines.insert(i, "Format 2")
150 # Don't get confused by LaTeX code
151 if re_Preamble.match(lines[i]):
153 while i < len(lines) and not re_EndPreamble.match(lines[i]):
158 # convert 'providesamsmath x', 'providesmakeidx x', 'providesnatbib x', 'providesurl x' to
159 # 'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
161 match = re_Provides.match(lines[i])
163 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
164 match.group(3), match.group(4))
171 # delete caption styles
172 match = re_Style.match(lines[i])
174 style = string.lower(match.group(4))
175 if style == "caption":
177 while i < len(lines) and not re_End.match(lines[i]):
178 caption.append(lines[i])
181 error('Incomplete caption style.')
186 # delete undefinition of caption styles
187 match = re_NoStyle.match(lines[i])
189 style = string.lower(match.group(4))
190 if style == "caption":
194 # replace the CopyStyle statement with the definition of the real
195 # style. This may result in duplicate statements, but that is OK
196 # since the second one will overwrite the first one.
197 match = re_CopyStyle.match(lines[i])
199 style = string.lower(match.group(4))
200 if style == "caption":
202 lines[i:i+1] = caption
204 # FIXME: This style comes from an include file, we
205 # should replace the real style and not this default.
206 lines[i:i+1] = [' Margin First_Dynamic',
207 ' LatexType Command',
208 ' LatexName caption',
214 ' AlignPossible Center',
215 ' LabelType Sensitive',
216 ' LabelString "Senseless!"',
225 # Delete MaxCounter and remember the value of it
226 match = re_MaxCounter.match(lines[i])
228 level = match.group(4)
229 if string.lower(level) == "counter_chapter":
231 elif string.lower(level) == "counter_section":
233 elif string.lower(level) == "counter_subsection":
235 elif string.lower(level) == "counter_subsubsection":
237 elif string.lower(level) == "counter_paragraph":
239 elif string.lower(level) == "counter_subparagraph":
241 elif string.lower(level) == "counter_enumi":
243 elif string.lower(level) == "counter_enumii":
245 elif string.lower(level) == "counter_enumiii":
252 # LabelType Counter_EnumI
259 match = re_LabelType.match(lines[i])
261 label = match.group(4)
262 # Remember indenting space for later reuse in added lines
263 space1 = match.group(1)
264 # Remember the line for adding the LabelCounter later.
265 # We can't do it here because it could shift latextype_line etc.
267 if string.lower(label[:8]) == "counter_":
268 counter = string.lower(label[8:])
269 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
271 # Remember the LabelString line
272 match = re_LabelString.match(lines[i])
274 labelstring = match.group(4)
277 # Remember the LabelStringAppendix line
278 match = re_LabelStringAppendix.match(lines[i])
280 labelstringappendix = match.group(4)
281 labelstringappendix_line = i
283 # Remember the LatexType line
284 match = re_LatexType.match(lines[i])
286 latextype = string.lower(match.group(4))
289 # Reset variables at the beginning of a style definition
290 match = re_Style.match(lines[i])
292 style = string.lower(match.group(4))
297 labelstringappendix = ""
298 labelstring_line = -1
299 labelstringappendix_line = -1
304 if re_End.match(lines[i]):
306 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
307 # (or change the existing LatexType)
308 if string.lower(label) == "bibliography":
309 if (latextype_line < 0):
310 lines.insert(i, "%sLatexType Bib_Environment" % space1)
313 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
315 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
316 if latextype == "item_environment" and string.lower(label) == "static":
317 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
319 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
320 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
321 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
322 # Don't add the LabelCounter line later
327 # LabelString "Chapter"
331 # LabelString "Chapter \arabic{chapter}"
333 # if this style has a counter. Ditto for LabelStringAppendix.
334 # This emulates the hardcoded article style numbering of 1.3
337 if counters.has_key(style):
338 if labelstring_line < 0:
339 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
342 new_labelstring = concatenate_label(labelstring, counters[style])
343 lines[labelstring_line] = re_LabelString.sub(
344 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
345 lines[labelstring_line])
346 if appendixcounters.has_key(style):
347 if labelstringappendix_line < 0:
348 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
351 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
352 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
353 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
354 lines[labelstringappendix_line])
356 # Now we can safely add the LabelCounter line
357 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
360 # Add the TocLevel setting for sectioning styles
361 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
362 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
377 input = open(argv[1], 'rb')
378 output = open(argv[2], 'wb')
380 error(usage(argv[0]))
386 format = convert(lines)
397 if __name__ == "__main__":