]> git.lyx.org Git - lyx.git/blob - lib/scripts/layout2layout.py
Generate python code in GraphicsConverter.C, replace convertDefault.sh by convertDefa...
[lyx.git] / lib / scripts / layout2layout.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
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 2
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_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
80
81     # counters for sectioning styles (hardcoded in 1.3)
82     counters = {"part"          : "\\Roman{part}",
83                 "chapter"       : "\\arabic{chapter}",
84                 "section"       : "\\arabic{section}",
85                 "subsection"    : "\\arabic{section}.\\arabic{subsection}",
86                 "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
87                 "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
88                 "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
89
90     # counters for sectioning styles in appendix (hardcoded in 1.3)
91     appendixcounters = {"chapter"       : "\\Alph{chapter}",
92                         "section"       : "\\Alph{section}",
93                         "subsection"    : "\\arabic{section}.\\arabic{subsection}",
94                         "subsubsection" : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}",
95                         "paragraph"     : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}",
96                         "subparagraph"  : "\\arabic{section}.\\arabic{subsection}.\\arabic{subsubsection}.\\arabic{paragraph}.\\arabic{subparagraph}"}
97
98     # Value of TocLevel for sectioning styles
99     toclevels = {"part"          : 0,
100                  "chapter"       : 0,
101                  "section"       : 1,
102                  "subsection"    : 2,
103                  "subsubsection" : 3,
104                  "paragraph"     : 4,
105                  "subparagraph"  : 5}
106
107     i = 0
108     only_comment = 1
109     counter = ""
110     label = ""
111     labelstring = ""
112     labelstringappendix = ""
113     space1 = ""
114     labelstring_line = -1
115     labelstringappendix_line = -1
116     labeltype_line = -1
117     latextype = ""
118     latextype_line = -1
119     style = ""
120     maxcounter = 0
121     while i < len(lines):
122
123         # Skip comments and empty lines
124         if re_Comment.match(lines[i]) or re_Empty.match(lines[i]):
125             i = i + 1
126             continue
127
128         # insert file format if not already there
129         if (only_comment):
130                 match = re_Format.match(lines[i])
131                 if match:
132                         format = match.group(4)
133                         if format == '2':
134                                 # nothing to do
135                                 return
136                         error('Cannot convert file format %s' % format)
137                 else:
138                         lines.insert(i, "Format 2")
139                         only_comment = 0
140                         continue
141
142         # Don't get confused by LaTeX code
143         if re_Preamble.match(lines[i]):
144             i = i + 1
145             while i < len(lines) and not re_EndPreamble.match(lines[i]):
146                 i = i + 1
147             continue
148
149         # Delete MaxCounter and remember the value of it
150         match = re_MaxCounter.match(lines[i])
151         if match:
152             level = match.group(4)
153             if string.lower(level) == "counter_chapter":
154                 maxcounter = 0
155             elif string.lower(level) == "counter_section":
156                 maxcounter = 1
157             elif string.lower(level) == "counter_subsection":
158                 maxcounter = 2
159             elif string.lower(level) == "counter_subsubsection":
160                 maxcounter = 3
161             elif string.lower(level) == "counter_paragraph":
162                 maxcounter = 4
163             elif string.lower(level) == "counter_subparagraph":
164                 maxcounter = 5
165             elif string.lower(level) == "counter_enumi":
166                 maxcounter = 6
167             elif string.lower(level) == "counter_enumii":
168                 maxcounter = 7
169             elif string.lower(level) == "counter_enumiii":
170                 maxcounter = 8
171             del lines[i]
172             continue
173
174         # Replace line
175         #
176         # LabelType Counter_EnumI
177         #
178         # with two lines
179         #
180         # LabelType Counter
181         # LabelCounter EnumI
182         #
183         match = re_LabelType.match(lines[i])
184         if match:
185             label = match.group(4)
186             # Remember indenting space for later reuse in added lines
187             space1 = match.group(1)
188             # Remember the line for adding the LabelCounter later.
189             # We can't do it here because it could shift latextype_line etc.
190             labeltype_line = i
191             if string.lower(label[:8]) == "counter_":
192                 counter = string.lower(label[8:])
193                 lines[i] = re_LabelType.sub(r'\1\2\3Counter', lines[i])
194
195         # Remember the LabelString line
196         match = re_LabelString.match(lines[i])
197         if match:
198             labelstring = match.group(4)
199             labelstring_line = i
200
201         # Remember the LabelStringAppendix line
202         match = re_LabelStringAppendix.match(lines[i])
203         if match:
204             labelstringappendix = match.group(4)
205             labelstringappendix_line = i
206
207         # Remember the LatexType line
208         match = re_LatexType.match(lines[i])
209         if match:
210             latextype = string.lower(match.group(4))
211             latextype_line = i
212
213         # Reset variables at the beginning of a style definition
214         match = re_Style.match(lines[i])
215         if match:
216             style = string.lower(match.group(4))
217             counter = ""
218             label = ""
219             space1 = ""
220             labelstring = ""
221             labelstringappendix = ""
222             labelstring_line = -1
223             labelstringappendix_line = -1
224             labeltype_line = -1
225             latextype = ""
226             latextype_line = -1
227
228         if re_End.match(lines[i]):
229
230             # Add a line "LatexType Bib_Environment" if LabelType is Bibliography
231             # (or change the existing LatexType)
232             if string.lower(label) == "bibliography":
233                 if (latextype_line < 0):
234                     lines.insert(i, "%sLatexType Bib_Environment" % space1)
235                     i = i + 1
236                 else:
237                     lines[latextype_line] = re_LatexType.sub(r'\1\2\3Bib_Environment', lines[latextype_line])
238
239             # Change "LabelType Static" to "LabelType Itemize" for itemize environments
240             if latextype == "item_environment" and string.lower(label) == "static":
241                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Itemize', lines[labeltype_line])
242
243             # Change "LabelType Counter_EnumI" to "LabelType Enumerate" for enumerate environments
244             if latextype == "item_environment" and string.lower(label) == "counter_enumi":
245                 lines[labeltype_line] = re_LabelType.sub(r'\1\2\3Enumerate', lines[labeltype_line])
246                 # Don't add the LabelCounter line later
247                 counter = ""
248
249             # Replace
250             #
251             # LabelString "Chapter"
252             #
253             # with
254             #
255             # LabelString "Chapter \arabic{chapter}"
256             #
257             # if this style has a counter. Ditto for LabelStringAppendix.
258             # This emulates the hardcoded article style numbering of 1.3
259             #
260             if counter != "":
261                 if counters.has_key(style):
262                     if labelstring_line < 0:
263                         lines.insert(i, '%sLabelString "%s"' % (space1, counters[style]))
264                         i = i + 1
265                     else:
266                         new_labelstring = concatenate_label(labelstring, counters[style])
267                         lines[labelstring_line] = re_LabelString.sub(
268                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
269                                 lines[labelstring_line])
270                 if appendixcounters.has_key(style):
271                     if labelstringappendix_line < 0:
272                         lines.insert(i, '%sLabelStringAppendix "%s"' % (space1, appendixcounters[style]))
273                         i = i + 1
274                     else:
275                         new_labelstring = concatenate_label(labelstring, appendixcounters[style])
276                         lines[labelstringappendix_line] = re_LabelStringAppendix.sub(
277                                 r'\1\2\3%s' % new_labelstring.replace("\\", "\\\\"),
278                                 lines[labelstringappendix_line])
279
280                 # Now we can safely add the LabelCounter line
281                 lines.insert(labeltype_line + 1, "%sLabelCounter %s" % (space1, counter))
282                 i = i + 1
283
284             # Add the TocLevel setting for sectioning styles
285             if toclevels.has_key(style) and maxcounter <= toclevels[style]:
286                 lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style]))
287                 i = i + 1
288
289         i = i + 1
290
291
292 def main(argv):
293
294     # Open files
295     if len(argv) == 1:
296         input = sys.stdin
297         output = sys.stdout
298     elif len(argv) == 3:
299         input = open(argv[1], 'rb')
300         output = open(argv[2], 'wb')
301     else:
302         error(usage(argv[0]))
303
304     # Do the real work
305     lines = read(input)
306     convert(lines)
307     write(output, lines)
308
309     # Close files
310     if len(argv) == 3:
311         input.close()
312         output.close()
313
314     return 0
315
316
317 if __name__ == "__main__":
318     main(sys.argv)