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