2 # -*- coding: iso-8859-1 -*-
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 2
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_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
81 # counters for sectioning styles (hardcoded in 1.3)
82 counters = {"part" : "\\Roman{part}",
83 "chapter" : "\\arabic{chapter}",
84 "section" : "\\arabic{section}",
85 "subsection" : "\\arabic{section}.\\arabic{subsection}",
86 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
87 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
88 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
90 # counters for sectioning styles in appendix (hardcoded in 1.3)
91 appendixcounters = {"chapter" : "\\Alph{chapter}",
92 "section" : "\\Alph{section}",
93 "subsection" : "\\arabic{section}.\\arabic{subsection}",
94 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
95 "paragraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
96 "subparagraph" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
98 # Value of TocLevel for sectioning styles
99 toclevels = {"part" : 0,
112 labelstringappendix = ""
114 labelstring_line = -1
115 labelstringappendix_line = -1
121 while i < len(lines):
123 # Skip comments and empty lines
124 if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
128 # insert file format if not already there
130 match = re_Format.match(lines[i])
132 format = match.group(4)
136 error('Cannot convert file format %s' % format)
138 lines.insert(i, "Format 2")
142 # Don't get confused by LaTeX code
143 if re_Preamble.match(lines[i]):
145 while i < len(lines) and not re_EndPreamble.match(lines[i]):
149 # Delete MaxCounter and remember the value of it
150 match = re_MaxCounter.match(lines[i])
152 level = match.group(4)
153 if string.lower(level) == "counter_chapter":
155 elif string.lower(level) == "counter_section":
157 elif string.lower(level) == "counter_subsection":
159 elif string.lower(level) == "counter_subsubsection":
161 elif string.lower(level) == "counter_paragraph":
163 elif string.lower(level) == "counter_subparagraph":
165 elif string.lower(level) == "counter_enumi":
167 elif string.lower(level) == "counter_enumii":
169 elif string.lower(level) == "counter_enumiii":
176 # LabelType Counter_EnumI
183 match = re_LabelType.match(lines[i])
185 label = match.group(4)
186 # Remember indenting space for later reuse in added lines
187 space1 = match.group(1)
188 # Remember the line for adding the LabelCounter later.
189 # We can't do it here because it could shift latextype_line etc.
191 if string.lower(label[:8]) == "counter_":
192 counter = string.lower(label[8:])
193 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
195 # Remember the LabelString line
196 match = re_LabelString.match(lines[i])
198 labelstring = match.group(4)
201 # Remember the LabelStringAppendix line
202 match = re_LabelStringAppendix.match(lines[i])
204 labelstringappendix = match.group(4)
205 labelstringappendix_line = i
207 # Remember the LatexType line
208 match = re_LatexType.match(lines[i])
210 latextype = string.lower(match.group(4))
213 # Reset variables at the beginning of a style definition
214 match = re_Style.match(lines[i])
216 style = string.lower(match.group(4))
221 labelstringappendix = ""
222 labelstring_line = -1
223 labelstringappendix_line = -1
228 if re_End.match(lines[i]):
230 # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
231 # (or change the existing LatexType)
232 if string.lower(label) == "bibliography":
233 if (latextype_line < 0):
234 lines.insert(i, "%sLatexType Bib_Environment" % space1)
237 lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
239 # Change "LabelType Static" to "LabelType Itemize" for itemize environments
240 if latextype == "item_environment" and string.lower(label) == "static":
241 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
243 # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
244 if latextype == "item_environment" and string.lower(label) == "counter_enumi":
245 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
246 # Don't add the LabelCounter line later
251 # LabelString "Chapter"
255 # LabelString "Chapter \arabic{chapter}"
257 # if this style has a counter. Ditto for LabelStringAppendix.
258 # This emulates the hardcoded article style numbering of 1.3
261 if counters.has_key(style):
262 if labelstring_line < 0:
263 lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
266 new_labelstring = concatenate_label(labelstring, counters[style])
267 lines[labelstring_line] = re_LabelString.sub(
268 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
269 lines[labelstring_line])
270 if appendixcounters.has_key(style):
271 if labelstringappendix_line < 0:
272 lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
275 new_labelstring = concatenate_label(labelstring, appendixcounters[style])
276 lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
277 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
278 lines[labelstringappendix_line])
280 # Now we can safely add the LabelCounter line
281 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
284 # Add the TocLevel setting for sectioning styles
285 if toclevels.has_key(style) and maxcounter <= toclevels[style]:
286 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
299 input = open(argv[1], 'rb')
300 output = open(argv[2], 'wb')
302 error(usage(argv[0]))
317 if __name__ == "__main__":