]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
Add Vincent to credits.
[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 6
13
14
15 import os, re, string, sys
16
17 # Incremented to format 4, 6 April 2007, lasgouttes
18 # Introduction of generic "Provides" declaration
19
20 # Incremented to format 5, 22 August 2007 by vermeer
21 # InsetLayout material
22
23 # Incremented to format 6, 7 January 2008 by spitz
24 # Requires tag added to layout files
25
26 # Incremented to format 7, 24 March 2008 by rgh
27 # AddToPreamble tag added to layout files
28
29 # Incremented to format 8, 25 July 2008 by rgh
30 # UseModule tag added to layout files
31 # CopyStyle added to InsetLayout
32 currentFormat = 8
33
34
35 def usage(prog_name):
36     return ("Usage: %s inputfile outputfile\n" % prog_name +
37             "or     %s <inputfile >outputfile" % prog_name)
38
39
40 def error(message):
41     sys.stderr.write(message + '\n')
42     sys.exit(1)
43
44
45 def trim_eol(line):
46     " Remove end of line char(s)."
47     if line[-2:-1] == '\r':
48         return line[:-2]
49     elif line[-1:] == '\r' or line[-1:] == '\n':
50         return line[:-1]
51     else:
52         # file with no EOL in last line
53         return line
54
55
56 def read(input):
57     " Read input file and strip lineendings."
58     lines = list()
59     while 1:
60         line = input.readline()
61         if not line:
62             break
63         lines.append(trim_eol(line))
64     return lines
65
66
67 def write(output, lines):
68     " Write output file with native lineendings."
69     for line in lines:
70         output.write(line + os.linesep)
71
72
73 # Concatenates old and new in an intelligent way:
74 # If old is wrapped in ", they are stripped. The result is wrapped in ".
75 def concatenate_label(old, new):
76     # Don't use strip as long as we support python 1.5.2
77     if old[0] == '"':
78         return old[0:-1] + new + '"'
79     else:
80         return '"' + old + new + '"'
81
82
83 def convert(lines):
84     " Convert to new format."
85     re_Comment = re.compile(r'^(\s*)#')
86     re_Empty = re.compile(r'^(\s*)$')
87     re_Format = re.compile(r'^(\s*)(Format)(\s+)(\S+)', re.IGNORECASE)
88     re_Preamble = re.compile(r'^(\s*)Preamble', re.IGNORECASE)
89     re_EndPreamble = re.compile(r'^(\s*)EndPreamble', re.IGNORECASE)
90     re_MaxCounter = re.compile(r'^(\s*)(MaxCounter)(\s+)(\S+)', re.IGNORECASE)
91     re_LabelType = re.compile(r'^(\s*)(LabelType)(\s+)(\S+)', re.IGNORECASE)
92     re_LabelString = re.compile(r'^(\s*)(LabelString)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
93     re_LabelStringAppendix = re.compile(r'^(\s*)(LabelStringAppendix)(\s+)(("[^"]+")|(\S+))', re.IGNORECASE)
94     re_LatexType = re.compile(r'^(\s*)(LatexType)(\s+)(\S+)', re.IGNORECASE)
95     re_Style = re.compile(r'^(\s*)(Style)(\s+)(\S+)', re.IGNORECASE)
96     re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
97     re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
98     re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
99     re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
100     re_CharStyle = re.compile(r'^(\s*)CharStyle(\s+)(\S+)$', re.IGNORECASE)
101
102     # counters for sectioning styles (hardcoded in 1.3)
103     counters = {"part"          : "\\Roman{part}",
104                 "chapter"       : "\\arabic{chapter}",
105                 "section"       : "\\arabic{section}",
106                 "subsection"    : "\\arabic{section}.\\arabic{subsection}",
107                 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
108                 "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
109                 "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
110
111     # counters for sectioning styles in appendix (hardcoded in 1.3)
112     appendixcounters = {"chapter"       : "\\Alph{chapter}",
113                         "section"       : "\\Alph{section}",
114                         "subsection"    : "\\arabic{section}.\\arabic{subsection}",
115                         "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
116                         "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
117                         "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
118
119     # Value of TocLevel for sectioning styles
120     toclevels = {"part"          : 0,
121                  "chapter"       : 0,
122                  "section"       : 1,
123                  "subsection"    : 2,
124                  "subsubsection" : 3,
125                  "paragraph"     : 4,
126                  "subparagraph"  : 5}
127
128     i = 0
129     only_comment = 1
130     counter = ""
131     label = ""
132     labelstring = ""
133     labelstringappendix = ""
134     space1 = ""
135     labelstring_line = -1
136     labelstringappendix_line = -1
137     labeltype_line = -1
138     latextype = ""
139     latextype_line = -1
140     style = ""
141     maxcounter = 0
142     format = 1
143     while i < len(lines):
144
145         # Skip comments and empty lines
146         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
147             i += 1
148             continue
149
150         # insert file format if not already there
151         if (only_comment):
152                 match = re_Format.match(lines[i])
153                 if match:
154                         format = int(match.group(4))
155                         if format > 1 and format < currentFormat:
156                             lines[i] = "Format %d" % (format + 1)
157                             only_comment = 0
158                         elif format == currentFormat:
159                                 # nothing to do
160                                 return format
161                         else:
162                             error('Cannot convert file format %s' % format)
163                 else:
164                         lines.insert(i, "Format 2")
165                         only_comment = 0
166                         continue
167
168         # Don't get confused by LaTeX code
169         if re_Preamble.match(lines[i]):
170             i += 1
171             while i < len(lines) and not re_EndPreamble.match(lines[i]):
172                 i += 1
173             continue
174
175         if format == 7:
176           i += 1
177           continue
178
179         if format == 6:
180           i += 1
181           continue
182
183         if format == 5:
184           i += 1
185           continue
186
187         if format == 4:
188             # Handle conversion to long CharStyle names
189             match = re_CharStyle.match(lines[i])
190             if match:
191                 lines[i] = "InsetLayout CharStyle:%s" % (match.group(3))
192                 i += 1
193                 lines.insert(i, "\tLyXType charstyle")
194                 i += 1
195                 lines.insert(i, "")
196                 lines[i] = "\tLabelString %s" % (match.group(3))
197             i += 1
198             continue
199
200         if format == 3:
201             # convert 'providesamsmath x',  'providesmakeidx x',  'providesnatbib x',  'providesurl x' to
202             #         'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
203             # x is either 0 or 1
204             match = re_Provides.match(lines[i])
205             if match:
206                 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
207                                                   match.group(3), match.group(4))
208             i += 1
209             continue
210
211         if format == 2:
212             caption = []
213
214             # delete caption styles
215             match = re_Style.match(lines[i])
216             if match:
217                 style = string.lower(match.group(4))
218                 if style == "caption":
219                     del lines[i]
220                     while i < len(lines) and not re_End.match(lines[i]):
221                         caption.append(lines[i])
222                         del lines[i]
223                     if i == len(lines):
224                         error('Incomplete caption style.')
225                     else:
226                         del lines[i]
227                         continue
228
229             # delete undefinition of caption styles
230             match = re_NoStyle.match(lines[i])
231             if match:
232                 style = string.lower(match.group(4))
233                 if style == "caption":
234                     del lines[i]
235                     continue
236
237             # replace the CopyStyle statement with the definition of the real
238             # style. This may result in duplicate statements, but that is OK
239             # since the second one will overwrite the first one.
240             match = re_CopyStyle.match(lines[i])
241             if match:
242                 style = string.lower(match.group(4))
243                 if style == "caption":
244                     if len(caption) > 0:
245                         lines[i:i+1] = caption
246                     else:
247                         # FIXME: This style comes from an include file, we
248                         # should replace the real style and not this default.
249                         lines[i:i+1] = ['       Margin                First_Dynamic',
250                                         '       LatexType             Command',
251                                         '       LatexName             caption',
252                                         '       NeedProtect           1',
253                                         '       LabelSep              xx',
254                                         '       ParSkip               0.4',
255                                         '       TopSep                0.5',
256                                         '       Align                 Center',
257                                         '       AlignPossible         Center',
258                                         '       LabelType             Sensitive',
259                                         '       LabelString           "Senseless!"',
260                                         '       OptionalArgs          1',
261                                         '       LabelFont',
262                                         '         Series              Bold',
263                                         '       EndFont']
264
265             i += 1
266             continue
267
268         # Delete MaxCounter and remember the value of it
269         match = re_MaxCounter.match(lines[i])
270         if match:
271             level = match.group(4)
272             if string.lower(level) == "counter_chapter":
273                 maxcounter = 0
274             elif string.lower(level) == "counter_section":
275                 maxcounter = 1
276             elif string.lower(level) == "counter_subsection":
277                 maxcounter = 2
278             elif string.lower(level) == "counter_subsubsection":
279                 maxcounter = 3
280             elif string.lower(level) == "counter_paragraph":
281                 maxcounter = 4
282             elif string.lower(level) == "counter_subparagraph":
283                 maxcounter = 5
284             elif string.lower(level) == "counter_enumi":
285                 maxcounter = 6
286             elif string.lower(level) == "counter_enumii":
287                 maxcounter = 7
288             elif string.lower(level) == "counter_enumiii":
289                 maxcounter = 8
290             del lines[i]
291             continue
292
293         # Replace line
294         #
295         # LabelType Counter_EnumI
296         #
297         # with two lines
298         #
299         # LabelType Counter
300         # LabelCounter EnumI
301         #
302         match = re_LabelType.match(lines[i])
303         if match:
304             label = match.group(4)
305             # Remember indenting space for later reuse in added lines
306             space1 = match.group(1)
307             # Remember the line for adding the LabelCounter later.
308             # We can't do it here because it could shift latextype_line etc.
309             labeltype_line = i
310             if string.lower(label[:8]) == "counter_":
311                 counter = string.lower(label[8:])
312                 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
313
314         # Remember the LabelString line
315         match = re_LabelString.match(lines[i])
316         if match:
317             labelstring = match.group(4)
318             labelstring_line = i
319
320         # Remember the LabelStringAppendix line
321         match = re_LabelStringAppendix.match(lines[i])
322         if match:
323             labelstringappendix = match.group(4)
324             labelstringappendix_line = i
325
326         # Remember the LatexType line
327         match = re_LatexType.match(lines[i])
328         if match:
329             latextype = string.lower(match.group(4))
330             latextype_line = i
331
332         # Reset variables at the beginning of a style definition
333         match = re_Style.match(lines[i])
334         if match:
335             style = string.lower(match.group(4))
336             counter = ""
337             label = ""
338             space1 = ""
339             labelstring = ""
340             labelstringappendix = ""
341             labelstring_line = -1
342             labelstringappendix_line = -1
343             labeltype_line = -1
344             latextype = ""
345             latextype_line = -1
346
347         if re_End.match(lines[i]):
348
349             # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
350             # (or change the existing LatexType)
351             if string.lower(label) == "bibliography":
352                 if (latextype_line < 0):
353                     lines.insert(i, "%sLatexType Bib_Environment" % space1)
354                     i += 1
355                 else:
356                     lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
357
358             # Change "LabelType Static" to "LabelType Itemize" for itemize environments
359             if latextype == "item_environment" and string.lower(label) == "static":
360                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
361
362             # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
363             if latextype == "item_environment" and string.lower(label) == "counter_enumi":
364                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
365                 # Don't add the LabelCounter line later
366                 counter = ""
367
368             # Replace
369             #
370             # LabelString "Chapter"
371             #
372             # with
373             #
374             # LabelString "Chapter \arabic{chapter}"
375             #
376             # if this style has a counter. Ditto for LabelStringAppendix.
377             # This emulates the hardcoded article style numbering of 1.3
378             #
379             if counter != "":
380                 if counters.has_key(style):
381                     if labelstring_line < 0:
382                         lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
383                         i += 1
384                     else:
385                         new_labelstring = concatenate_label(labelstring, counters[style])
386                         lines[labelstring_line] = re_LabelString.sub(
387                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
388                                 lines[labelstring_line])
389                 if appendixcounters.has_key(style):
390                     if labelstringappendix_line < 0:
391                         lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
392                         i += 1
393                     else:
394                         new_labelstring = concatenate_label(labelstring, appendixcounters[style])
395                         lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
396                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
397                                 lines[labelstringappendix_line])
398
399                 # Now we can safely add the LabelCounter line
400                 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
401                 i += 1
402
403             # Add the TocLevel setting for sectioning styles
404             if toclevels.has_key(style) and maxcounter <= toclevels[style]:
405                 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
406                 i += 1
407
408         i += 1
409
410     return format + 1
411
412
413 def main(argv):
414
415     # Open files
416     if len(argv) == 1:
417         input = sys.stdin
418         output = sys.stdout
419     elif len(argv) == 3:
420         input = open(argv[1], 'rb')
421         output = open(argv[2], 'wb')
422     else:
423         error(usage(argv[0]))
424
425     # Do the real work
426     lines = read(input)
427     format = 1
428     while (format < currentFormat):
429         format = convert(lines)
430     write(output, lines)
431
432     # Close files
433     if len(argv) == 3:
434         input.close()
435         output.close()
436
437     return 0
438
439
440 if __name__ == "__main__":
441     main(sys.argv)