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