]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
doc/doc_toc.py: - fix the input encoding of the TOC.lyx files
[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
84     # counters for sectioning styles (hardcoded in 1.3)
85     counters = {"part"          : "\\Roman{part}",
86                 "chapter"       : "\\arabic{chapter}",
87                 "section"       : "\\arabic{section}",
88                 "subsection"    : "\\arabic{section}.\\arabic{subsection}",
89                 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
90                 "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
91                 "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
92
93     # counters for sectioning styles in appendix (hardcoded in 1.3)
94     appendixcounters = {"chapter"       : "\\Alph{chapter}",
95                         "section"       : "\\Alph{section}",
96                         "subsection"    : "\\arabic{section}.\\arabic{subsection}",
97                         "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
98                         "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
99                         "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
100
101     # Value of TocLevel for sectioning styles
102     toclevels = {"part"          : 0,
103                  "chapter"       : 0,
104                  "section"       : 1,
105                  "subsection"    : 2,
106                  "subsubsection" : 3,
107                  "paragraph"     : 4,
108                  "subparagraph"  : 5}
109
110     i = 0
111     only_comment = 1
112     counter = ""
113     label = ""
114     labelstring = ""
115     labelstringappendix = ""
116     space1 = ""
117     labelstring_line = -1
118     labelstringappendix_line = -1
119     labeltype_line = -1
120     latextype = ""
121     latextype_line = -1
122     style = ""
123     maxcounter = 0
124     format = 1
125     while i < len(lines):
126
127         # Skip comments and empty lines
128         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
129             i = i + 1
130             continue
131
132         # insert file format if not already there
133         if (only_comment):
134                 match = re_Format.match(lines[i])
135                 if match:
136                         format = int(match.group(4))
137                         if format > 1 and format < 4:
138                             lines[i] = "Format %d" % (format + 1)
139                             only_comment = 0
140                         elif format == 4:
141                                 # nothing to do
142                                 return format
143                         else:
144                             error('Cannot convert file format %s' % format)
145                 else:
146                         lines.insert(i, "Format 2")
147                         only_comment = 0
148                         continue
149
150         # Don't get confused by LaTeX code
151         if re_Preamble.match(lines[i]):
152             i = i + 1
153             while i < len(lines) and not re_EndPreamble.match(lines[i]):
154                 i = i + 1
155             continue
156
157         if format == 3:
158             # convert 'providesamsmath x',  'providesmakeidx x',  'providesnatbib x',  'providesurl x' to
159             #         'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
160             # x is either 0 or 1
161             match = re_Provides.match(lines[i])
162             if match:
163                 lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2).lower(),
164                                                   match.group(3), match.group(4))
165             i = i + 1
166             continue
167
168         if format == 2:
169             caption = []
170
171             # delete caption styles
172             match = re_Style.match(lines[i])
173             if match:
174                 style = string.lower(match.group(4))
175                 if style == "caption":
176                     del lines[i]
177                     while i < len(lines) and not re_End.match(lines[i]):
178                         caption.append(lines[i])
179                         del lines[i]
180                     if i == len(lines):
181                         error('Incomplete caption style.')
182                     else:
183                         del lines[i]
184                         continue
185
186             # delete undefinition of caption styles
187             match = re_NoStyle.match(lines[i])
188             if match:
189                 style = string.lower(match.group(4))
190                 if style == "caption":
191                     del lines[i]
192                     continue
193
194             # replace the CopyStyle statement with the definition of the real
195             # style. This may result in duplicate statements, but that is OK
196             # since the second one will overwrite the first one.
197             match = re_CopyStyle.match(lines[i])
198             if match:
199                 style = string.lower(match.group(4))
200                 if style == "caption":
201                     if len(caption) > 0:
202                         lines[i:i+1] = caption
203                     else:
204                         # FIXME: This style comes from an include file, we
205                         # should replace the real style and not this default.
206                         lines[i:i+1] = ['       Margin                First_Dynamic',
207                                         '       LatexType             Command',
208                                         '       LatexName             caption',
209                                         '       NeedProtect           1',
210                                         '       LabelSep              xx',
211                                         '       ParSkip               0.4',
212                                         '       TopSep                0.5',
213                                         '       Align                 Center',
214                                         '       AlignPossible         Center',
215                                         '       LabelType             Sensitive',
216                                         '       LabelString           "Senseless!"',
217                                         '       OptionalArgs          1',
218                                         '       LabelFont',
219                                         '         Series              Bold',
220                                         '       EndFont']
221
222             i = i + 1
223             continue
224
225         # Delete MaxCounter and remember the value of it
226         match = re_MaxCounter.match(lines[i])
227         if match:
228             level = match.group(4)
229             if string.lower(level) == "counter_chapter":
230                 maxcounter = 0
231             elif string.lower(level) == "counter_section":
232                 maxcounter = 1
233             elif string.lower(level) == "counter_subsection":
234                 maxcounter = 2
235             elif string.lower(level) == "counter_subsubsection":
236                 maxcounter = 3
237             elif string.lower(level) == "counter_paragraph":
238                 maxcounter = 4
239             elif string.lower(level) == "counter_subparagraph":
240                 maxcounter = 5
241             elif string.lower(level) == "counter_enumi":
242                 maxcounter = 6
243             elif string.lower(level) == "counter_enumii":
244                 maxcounter = 7
245             elif string.lower(level) == "counter_enumiii":
246                 maxcounter = 8
247             del lines[i]
248             continue
249
250         # Replace line
251         #
252         # LabelType Counter_EnumI
253         #
254         # with two lines
255         #
256         # LabelType Counter
257         # LabelCounter EnumI
258         #
259         match = re_LabelType.match(lines[i])
260         if match:
261             label = match.group(4)
262             # Remember indenting space for later reuse in added lines
263             space1 = match.group(1)
264             # Remember the line for adding the LabelCounter later.
265             # We can't do it here because it could shift latextype_line etc.
266             labeltype_line = i
267             if string.lower(label[:8]) == "counter_":
268                 counter = string.lower(label[8:])
269                 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
270
271         # Remember the LabelString line
272         match = re_LabelString.match(lines[i])
273         if match:
274             labelstring = match.group(4)
275             labelstring_line = i
276
277         # Remember the LabelStringAppendix line
278         match = re_LabelStringAppendix.match(lines[i])
279         if match:
280             labelstringappendix = match.group(4)
281             labelstringappendix_line = i
282
283         # Remember the LatexType line
284         match = re_LatexType.match(lines[i])
285         if match:
286             latextype = string.lower(match.group(4))
287             latextype_line = i
288
289         # Reset variables at the beginning of a style definition
290         match = re_Style.match(lines[i])
291         if match:
292             style = string.lower(match.group(4))
293             counter = ""
294             label = ""
295             space1 = ""
296             labelstring = ""
297             labelstringappendix = ""
298             labelstring_line = -1
299             labelstringappendix_line = -1
300             labeltype_line = -1
301             latextype = ""
302             latextype_line = -1
303
304         if re_End.match(lines[i]):
305
306             # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
307             # (or change the existing LatexType)
308             if string.lower(label) == "bibliography":
309                 if (latextype_line < 0):
310                     lines.insert(i, "%sLatexType Bib_Environment" % space1)
311                     i = i + 1
312                 else:
313                     lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
314
315             # Change "LabelType Static" to "LabelType Itemize" for itemize environments
316             if latextype == "item_environment" and string.lower(label) == "static":
317                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
318
319             # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
320             if latextype == "item_environment" and string.lower(label) == "counter_enumi":
321                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
322                 # Don't add the LabelCounter line later
323                 counter = ""
324
325             # Replace
326             #
327             # LabelString "Chapter"
328             #
329             # with
330             #
331             # LabelString "Chapter \arabic{chapter}"
332             #
333             # if this style has a counter. Ditto for LabelStringAppendix.
334             # This emulates the hardcoded article style numbering of 1.3
335             #
336             if counter != "":
337                 if counters.has_key(style):
338                     if labelstring_line < 0:
339                         lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
340                         i = i + 1
341                     else:
342                         new_labelstring = concatenate_label(labelstring, counters[style])
343                         lines[labelstring_line] = re_LabelString.sub(
344                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
345                                 lines[labelstring_line])
346                 if appendixcounters.has_key(style):
347                     if labelstringappendix_line < 0:
348                         lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
349                         i = i + 1
350                     else:
351                         new_labelstring = concatenate_label(labelstring, appendixcounters[style])
352                         lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
353                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
354                                 lines[labelstringappendix_line])
355
356                 # Now we can safely add the LabelCounter line
357                 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
358                 i = i + 1
359
360             # Add the TocLevel setting for sectioning styles
361             if toclevels.has_key(style) and maxcounter <= toclevels[style]:
362                 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
363                 i = i + 1
364
365         i = i + 1
366
367     return format + 1
368
369
370 def main(argv):
371
372     # Open files
373     if len(argv) == 1:
374         input = sys.stdin
375         output = sys.stdout
376     elif len(argv) == 3:
377         input = open(argv[1], 'rb')
378         output = open(argv[2], 'wb')
379     else:
380         error(usage(argv[0]))
381
382     # Do the real work
383     lines = read(input)
384     format = 1
385     while (format < 4):
386         format = convert(lines)
387     write(output, lines)
388
389     # Close files
390     if len(argv) == 3:
391         input.close()
392         output.close()
393
394     return 0
395
396
397 if __name__ == "__main__":
398     main(sys.argv)