]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
e539714921f563db467d614472ebe36bc91588f9
[lyx.git] / lib / scripts / layout2layout.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file layout2layout.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Georg Baum
9
10 # Full author contact details are available in file CREDITS
11
12 # This script will update a .layout file to format 3
13
14
15 import os, re, string, sys
16
17
18 def usage(prog_name):
19     return ("Usage: %s inputfile outputfile\n" % prog_name +
20             "or     %s <inputfile >outputfile" % prog_name)
21
22
23 def error(message):
24     sys.stderr.write(message + '\n')
25     sys.exit(1)
26
27
28 def trim_eol(line):
29     " Remove end of line char(s)."
30     if line[-2:-1] == '\r':
31         return line[:-2]
32     elif line[-1:] == '\r' or line[-1:] == '\n':
33         return line[:-1]
34     else:
35         # file with no EOL in last line
36         return line
37
38
39 def read(input):
40     " Read input file and strip lineendings."
41     lines = list()
42     while 1:
43         line = input.readline()
44         if not line:
45             break
46         lines.append(trim_eol(line))
47     return lines
48
49
50 def write(output, lines):
51     " Write output file with native lineendings."
52     for line in lines:
53         output.write(line + os.linesep)
54
55
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
60     if old[0] == '"':
61         return old[0:-1] + new + '"'
62     else:
63         return '"' + old + new + '"'
64
65
66 def convert(lines):
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)
84
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}"}
93
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}"}
101
102     # Value of TocLevel for sectioning styles
103     toclevels = {"part"          : 0,
104                  "chapter"       : 0,
105                  "section"       : 1,
106                  "subsection"    : 2,
107                  "subsubsection" : 3,
108                  "paragraph"     : 4,
109                  "subparagraph"  : 5}
110
111     i = 0
112     only_comment = 1
113     counter = ""
114     label = ""
115     labelstring = ""
116     labelstringappendix = ""
117     space1 = ""
118     labelstring_line = -1
119     labelstringappendix_line = -1
120     labeltype_line = -1
121     latextype = ""
122     latextype_line = -1
123     style = ""
124     maxcounter = 0
125     format = 1
126     while i < len(lines):
127
128         # Skip comments and empty lines
129         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
130             i += 1
131             continue
132
133         # insert file format if not already there
134         if (only_comment):
135                 match = re_Format.match(lines[i])
136                 if match:
137                         format = int(match.group(4))
138                         if format > 1 and format < 5:
139                             lines[i] = "Format %d" % (format + 1)
140                             only_comment = 0
141                         elif format == 5:
142                                 # nothing to do
143                                 return format
144                         else:
145                             error('Cannot convert file format %s' % format)
146                 else:
147                         lines.insert(i, "Format 2")
148                         only_comment = 0
149                         continue
150
151         # Don't get confused by LaTeX code
152         if re_Preamble.match(lines[i]):
153             i += 1
154             while i < len(lines) and not re_EndPreamble.match(lines[i]):
155                 i += 1
156             continue
157
158         if format == 4:
159             # Handle conversion to long CharStyle names
160             match = re_CharStyle.match(lines[i])
161             if match:
162                 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
163                 i += 1
164                 lines.insert(i, "\tLyXType charstyle")
165                 i += 1
166                 lines.insert(i, "")
167                 lines[i] = "\tLabelString %s" % (match.group(3))
168             i += 1
169             continue
170
171         if format == 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'
174             # x is either 0 or 1
175             match = re_Provides.match(lines[i])
176             if match:
177                 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
178                                                   match.group(3), match.group(4))
179             i += 1
180             continue
181
182         if format == 2:
183             caption = []
184
185             # delete caption styles
186             match = re_Style.match(lines[i])
187             if match:
188                 style = string.lower(match.group(4))
189                 if style == "caption":
190                     del lines[i]
191                     while i < len(lines) and not re_End.match(lines[i]):
192                         caption.append(lines[i])
193                         del lines[i]
194                     if i == len(lines):
195                         error('Incomplete caption style.')
196                     else:
197                         del lines[i]
198                         continue
199
200             # delete undefinition of caption styles
201             match = re_NoStyle.match(lines[i])
202             if match:
203                 style = string.lower(match.group(4))
204                 if style == "caption":
205                     del lines[i]
206                     continue
207
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])
212             if match:
213                 style = string.lower(match.group(4))
214                 if style == "caption":
215                     if len(caption) > 0:
216                         lines[i:i+1] = caption
217                     else:
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',
223                                         '       NeedProtect           1',
224                                         '       LabelSep              xx',
225                                         '       ParSkip               0.4',
226                                         '       TopSep                0.5',
227                                         '       Align                 Center',
228                                         '       AlignPossible         Center',
229                                         '       LabelType             Sensitive',
230                                         '       LabelString           "Senseless!"',
231                                         '       OptionalArgs          1',
232                                         '       LabelFont',
233                                         '         Series              Bold',
234                                         '       EndFont']
235
236             i += 1
237             continue
238
239         # Delete MaxCounter and remember the value of it
240         match = re_MaxCounter.match(lines[i])
241         if match:
242             level = match.group(4)
243             if string.lower(level) == "counter_chapter":
244                 maxcounter = 0
245             elif string.lower(level) == "counter_section":
246                 maxcounter = 1
247             elif string.lower(level) == "counter_subsection":
248                 maxcounter = 2
249             elif string.lower(level) == "counter_subsubsection":
250                 maxcounter = 3
251             elif string.lower(level) == "counter_paragraph":
252                 maxcounter = 4
253             elif string.lower(level) == "counter_subparagraph":
254                 maxcounter = 5
255             elif string.lower(level) == "counter_enumi":
256                 maxcounter = 6
257             elif string.lower(level) == "counter_enumii":
258                 maxcounter = 7
259             elif string.lower(level) == "counter_enumiii":
260                 maxcounter = 8
261             del lines[i]
262             continue
263
264         # Replace line
265         #
266         # LabelType Counter_EnumI
267         #
268         # with two lines
269         #
270         # LabelType Counter
271         # LabelCounter EnumI
272         #
273         match = re_LabelType.match(lines[i])
274         if match:
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.
280             labeltype_line = i
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])
284
285         # Remember the LabelString line
286         match = re_LabelString.match(lines[i])
287         if match:
288             labelstring = match.group(4)
289             labelstring_line = i
290
291         # Remember the LabelStringAppendix line
292         match = re_LabelStringAppendix.match(lines[i])
293         if match:
294             labelstringappendix = match.group(4)
295             labelstringappendix_line = i
296
297         # Remember the LatexType line
298         match = re_LatexType.match(lines[i])
299         if match:
300             latextype = string.lower(match.group(4))
301             latextype_line = i
302
303         # Reset variables at the beginning of a style definition
304         match = re_Style.match(lines[i])
305         if match:
306             style = string.lower(match.group(4))
307             counter = ""
308             label = ""
309             space1 = ""
310             labelstring = ""
311             labelstringappendix = ""
312             labelstring_line = -1
313             labelstringappendix_line = -1
314             labeltype_line = -1
315             latextype = ""
316             latextype_line = -1
317
318         if re_End.match(lines[i]):
319
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)
325                     i += 1
326                 else:
327                     lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
328
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])
332
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
337                 counter = ""
338
339             # Replace
340             #
341             # LabelString "Chapter"
342             #
343             # with
344             #
345             # LabelString "Chapter \arabic{chapter}"
346             #
347             # if this style has a counter. Ditto for LabelStringAppendix.
348             # This emulates the hardcoded article style numbering of 1.3
349             #
350             if counter != "":
351                 if counters.has_key(style):
352                     if labelstring_line < 0:
353                         lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
354                         i += 1
355                     else:
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]))
363                         i += 1
364                     else:
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])
369
370                 # Now we can safely add the LabelCounter line
371                 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
372                 i += 1
373
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]))
377                 i += 1
378
379         i += 1
380
381     return format + 1
382
383
384 def main(argv):
385
386     # Open files
387     if len(argv) == 1:
388         input = sys.stdin
389         output = sys.stdout
390     elif len(argv) == 3:
391         input = open(argv[1], 'rb')
392         output = open(argv[2], 'wb')
393     else:
394         error(usage(argv[0]))
395
396     # Do the real work
397     lines = read(input)
398     format = 1
399     while (format < 4):
400         format = convert(lines)
401     write(output, lines)
402
403     # Close files
404     if len(argv) == 3:
405         input.close()
406         output.close()
407
408     return 0
409
410
411 if __name__ == "__main__":
412     main(sys.argv)