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)
83 # counters for sectioning styles (hardcoded in 1.3)
84 counters = {"part" : "\\Roman{part}",
85 "chapter" : "\\arabic{chapter}",
86 "section" : "\\arabic{section}",
87 "subsection" : "\\arabic{section}.\\arabic{subsection}",
88 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
89 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
90 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
92 # counters for sectioning styles in appendix (hardcoded in 1.3)
93 appendixcounters = {"chapter" : "\\Alph{chapter}",
94 "section" : "\\Alph{section}",
95 "subsection" : "\\arabic{section}.\\arabic{subsection}",
96 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
97 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
98 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
100 # Value of TocLevel for sectioning styles
101 toclevels = {"part" : 0,
114 labelstringappendix = ""
116 labelstring_line = -1
117 labelstringappendix_line = -1
124 while i < len(lines):
126 # Skip comments and empty lines
127 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
131 # insert file format if not already there
133 match = re_Format.match(lines[i])
135 format = int(match.group(4))
137 lines[i] = "Format 3"
143 error('Cannot convert file format %s' % format)
145 lines.insert(i, "Format 3")
149 # Don't get confused by LaTeX code
150 if re_Preamble.match(lines[i]):
152 while i < len(lines) and not re_EndPreamble.match(lines[i]):
159 # delete caption styles
160 match = re_Style.match(lines[i])
162 style = string.lower(match.group(4))
163 if style == "caption":
165 while i < len(lines) and not re_End.match(lines[i]):
166 caption.append(lines[i])
169 error('Incomplete caption style.')
174 # delete undefinition of caption styles
175 match = re_NoStyle.match(lines[i])
177 style = string.lower(match.group(4))
178 if style == "caption":
182 # replace the CopyStyle statement with the definition of the real
183 # style. This may result in duplicate statements, but that is OK
184 # since the second one will overwrite the first one.
185 match = re_CopyStyle.match(lines[i])
187 style = string.lower(match.group(4))
188 if style == "caption":
190 lines[i:i+1] = caption
192 # FIXME: This style comes from an include file, we
193 # should replace the real style and not this default.
194 lines[i:i+1] = [' Margin First_Dynamic',
195 ' LatexType Command',
196 ' LatexName caption',
202 ' AlignPossible Center',
203 ' LabelType Sensitive',
204 ' LabelString "Senseless!"',
213 # Delete MaxCounter and remember the value of it
214 match = re_MaxCounter.match(lines[i])
216 level = match.group(4)
217 if string.lower(level) == "counter_chapter":
219 elif string.lower(level) == "counter_section":
221 elif string.lower(level) == "counter_subsection":
223 elif string.lower(level) == "counter_subsubsection":
225 elif string.lower(level) == "counter_paragraph":
227 elif string.lower(level) == "counter_subparagraph":
229 elif string.lower(level) == "counter_enumi":
231 elif string.lower(level) == "counter_enumii":
233 elif string.lower(level) == "counter_enumiii":
240 # LabelType Counter_EnumI
247 match = re_LabelType.match(lines[i])
249 label = match.group(4)
250 # Remember indenting space for later reuse in added lines
251 space1 = match.group(1)
252 # Remember the line for adding the LabelCounter later.
253 # We can't do it here because it could shift latextype_line etc.
255 if string.lower(label[:8]) == "counter_":
256 counter = string.lower(label[8:])
257 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
259 # Remember the LabelString line
260 match = re_LabelString.match(lines[i])
262 labelstring = match.group(4)
265 # Remember the LabelStringAppendix line
266 match = re_LabelStringAppendix.match(lines[i])
268 labelstringappendix = match.group(4)
269 labelstringappendix_line = i
271 # Remember the LatexType line
272 match = re_LatexType.match(lines[i])
274 latextype = string.lower(match.group(4))
277 # Reset variables at the beginning of a style definition
278 match = re_Style.match(lines[i])
280 style = string.lower(match.group(4))
285 labelstringappendix = ""
286 labelstring_line = -1
287 labelstringappendix_line = -1
292 if re_End.match(lines[i]):
294 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
295 # (or change the existing LatexType)
296 if string.lower(label) == "bibliography":
297 if (latextype_line < 0):
298 lines.insert(i, "%sLatexType Bib_Environment" % space1)
301 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
303 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
304 if latextype == "item_environment" and string.lower(label) == "static":
305 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
307 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
308 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
309 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
310 # Don't add the LabelCounter line later
315 # LabelString "Chapter"
319 # LabelString "Chapter \arabic{chapter}"
321 # if this style has a counter. Ditto for LabelStringAppendix.
322 # This emulates the hardcoded article style numbering of 1.3
325 if counters.has_key(style):
326 if labelstring_line < 0:
327 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
330 new_labelstring = concatenate_label(labelstring, counters[style])
331 lines[labelstring_line] = re_LabelString.sub(
332 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
333 lines[labelstring_line])
334 if appendixcounters.has_key(style):
335 if labelstringappendix_line < 0:
336 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
339 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
340 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
341 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
342 lines[labelstringappendix_line])
344 # Now we can safely add the LabelCounter line
345 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
348 # Add the TocLevel setting for sectioning styles
349 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
350 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
363 input = open(argv[1], 'rb')
364 output = open(argv[2], 'wb')
366 error(usage(argv[0]))
381 if __name__ == "__main__":