]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_0.py
More elses after returns.
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2008 José Matos  <jamatos@lyx.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 """ Convert files to the file format generated by lyx 2.0"""
21
22 import re, string
23 import unicodedata
24 import sys, os
25
26 from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string
27
28 ####################################################################
29 # Private helper functions
30
31 def remove_option(document, m, option):
32     l = document.body[m].find(option)
33     if l != -1:
34         val = document.body[m][l:].split('"')[1]
35         document.body[m] = document.body[m][:l-1] + document.body[m][l+len(option + '="' + val + '"'):]
36     return l
37
38 def find_end_of_inset(lines, i):
39     " Find end of inset, where lines[i] is included."
40     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
41
42
43 def add_to_preamble(document, text):
44     """ Add text to the preamble if it is not already there.
45     Only the first line is checked!"""
46
47     if find_token(document.preamble, text[0], 0) != -1:
48         return
49
50     document.preamble.extend(text)
51
52
53 def insert_to_preamble(index, document, text):
54     """ Insert text to the preamble at a given line"""
55
56     document.preamble.insert(index, text)
57
58
59 def read_unicodesymbols():
60     " Read the unicodesymbols list of unicode characters and corresponding commands."
61     pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
62     fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
63     spec_chars = []
64     # Two backslashes, followed by some non-word character, and then a character
65     # in brackets. The idea is to check for constructs like: \"{u}, which is how
66     # they are written in the unicodesymbols file; but they can also be written
67     # as: \"u or even \" u.
68     r = re.compile(r'\\\\(\W)\{(\w)\}')
69     for line in fp.readlines():
70         if line[0] != '#' and line.strip() != "":
71             line=line.replace(' "',' ') # remove all quotation marks with spaces before
72             line=line.replace('" ',' ') # remove all quotation marks with spaces after
73             line=line.replace(r'\"','"') # replace \" by " (for characters with diaeresis)
74             try:
75                 [ucs4,command,dead] = line.split(None,2)
76                 if command[0:1] != "\\":
77                     continue
78                 spec_chars.append([command, unichr(eval(ucs4))])
79             except:
80                 continue
81             m = r.match(command)
82             if m != None:
83                 command = "\\\\"
84                 # If the character is a double-quote, then we need to escape it, too,
85                 # since it is done that way in the LyX file.
86                 if m.group(1) == "\"":
87                     command += "\\"
88                 commandbl = command
89                 command += m.group(1) + m.group(2)
90                 commandbl += m.group(1) + ' ' + m.group(2)
91                 spec_chars.append([command, unichr(eval(ucs4))])
92                 spec_chars.append([commandbl, unichr(eval(ucs4))])
93     fp.close()
94     return spec_chars
95
96
97 unicode_reps = read_unicodesymbols()
98
99
100 # DO NOT USE THIS ROUTINE ANY MORE. Better yet, replace the uses that
101 # have been made of it with uses of put_cmd_in_ert.
102 def old_put_cmd_in_ert(string):
103     for rep in unicode_reps:
104         string = string.replace(rep[1], rep[0].replace('\\\\', '\\'))
105     string = string.replace('\\', "\\backslash\n")
106     string = "\\begin_inset ERT\nstatus collapsed\n\\begin_layout Plain Layout\n" \
107       + string + "\n\\end_layout\n\\end_inset"
108     return string
109
110
111 # This routine wraps some content in an ERT inset. It returns a 
112 # LIST of strings. This is how lyx2lyx works: with a list of strings, 
113 # each representing a line of a LyX file. Embedded newlines confuse
114 # lyx2lyx very much.
115 # For this same reason, we expect as input a LIST of strings, not
116 # something with embedded newlines. That said, if any of your strings
117 # do have embedded newlines, the string will eventually get split on
118 # them and you'll get a list back.
119 #
120 # A call to this routine will often go something like this:
121 #   i = find_token('\\begin_inset FunkyInset', ...)
122 #   ...
123 #   j = find_end_of_inset(document.body, i)
124 #   content = ...extract content from insets
125 #   ert = put_cmd_in_ert(content)
126 #   document.body[i:j] = ert
127 # Now, before we continue, we need to reset i appropriately. Normally,
128 # this would be: 
129 #   i += len(ert)
130 # That puts us right after the ERT we just inserted.
131 def put_cmd_in_ert(strlist):
132     ret = ["\\begin_inset ERT", "status collapsed", "\\begin_layout Plain Layout", ""]
133     # Despite the warnings just given, it will be faster for us to work
134     # with a single string internally. That way, we only go through the
135     # unicode_reps loop once.
136     s = "\n".join(strlist)
137     for rep in unicode_reps:
138         s = s.replace(rep[1], rep[0].replace('\\\\', '\\'))
139     s = s.replace('\\', "\\backslash\n")
140     ret += s.splitlines()
141     ret += ["\\end_layout", "\\end_inset"]
142     return ret
143
144             
145 def lyx2latex(document, lines):
146     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
147     # clean up multiline stuff
148     content = ""
149     ert_end = 0
150
151     for curline in range(len(lines)):
152       line = lines[curline]
153       if line.startswith("\\begin_inset ERT"):
154           # We don't want to replace things inside ERT, so figure out
155           # where the end of the inset is.
156           ert_end = find_end_of_inset(lines, curline + 1)
157           continue
158       elif line.startswith("\\begin_inset Formula"):
159           line = line[20:]
160       elif line.startswith("\\begin_inset Quotes"):
161           # For now, we do a very basic reversion. Someone who understands
162           # quotes is welcome to fix it up.
163           qtype = line[20:].strip()
164           # lang = qtype[0]
165           side = qtype[1]
166           dbls = qtype[2]
167           if side == "l":
168               if dbls == "d":
169                   line = "``"
170               else:
171                   line = "`"
172           else:
173               if dbls == "d":
174                   line = "''"
175               else:
176                   line = "'"
177       elif line.isspace() or \
178             line.startswith("\\begin_layout") or \
179             line.startswith("\\end_layout") or \
180             line.startswith("\\begin_inset") or \
181             line.startswith("\\end_inset") or \
182             line.startswith("\\lang") or \
183             line.strip() == "status collapsed" or \
184             line.strip() == "status open":
185           #skip all that stuff
186           continue
187
188       # this needs to be added to the preamble because of cases like
189       # \textmu, \textbackslash, etc.
190       add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
191                                  '\\@ifundefined{textmu}',
192                                  ' {\\usepackage{textcomp}}{}'])
193       # a lossless reversion is not possible
194       # try at least to handle some common insets and settings
195       if ert_end >= curline:
196           line = line.replace(r'\backslash', r'\\')
197       else:
198           line = line.replace('&', '\\&{}')
199           line = line.replace('#', '\\#{}')
200           line = line.replace('^', '\\^{}')
201           line = line.replace('%', '\\%{}')
202           line = line.replace('_', '\\_{}')
203           line = line.replace('$', '\\${}')
204
205           # Do the LyX text --> LaTeX conversion
206           for rep in unicode_reps:
207             line = line.replace(rep[1], rep[0] + "{}")
208           line = line.replace(r'\backslash', r'\textbackslash{}')
209           line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
210           line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
211           line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
212           line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
213           line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
214           line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
215           line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
216           line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
217           line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
218       content += line
219     return content
220
221
222 def latex_length(string):
223     'Convert lengths to their LaTeX representation.'
224     i = 0
225     percent = False
226     # the string has the form
227     # ValueUnit+ValueUnit-ValueUnit or
228     # ValueUnit+-ValueUnit
229     # the + and - (glue lengths) are optional
230     # the + always precedes the -
231
232     # Convert relative lengths to LaTeX units
233     units = {"text%":"\\textwidth", "col%":"\\columnwidth",
234              "page%":"\\paperwidth", "line%":"\\linewidth",
235              "theight%":"\\textheight", "pheight%":"\\paperheight"}
236     for unit in units.keys():
237         i = string.find(unit)
238         if i != -1:
239             percent = True
240             minus = string.rfind("-", 1, i)
241             plus = string.rfind("+", 0, i)
242             latex_unit = units[unit]
243             if plus == -1 and minus == -1:
244                 value = string[:i]
245                 value = str(float(value)/100)
246                 end = string[i + len(unit):]
247                 string = value + latex_unit + end
248             if plus > minus:
249                 value = string[plus+1:i]
250                 value = str(float(value)/100)
251                 begin = string[:plus+1]
252                 end = string[i+len(unit):]
253                 string = begin + value + latex_unit + end
254             if plus < minus:
255                 value = string[minus+1:i]
256                 value = str(float(value)/100)
257                 begin = string[:minus+1]
258                 string = begin + value + latex_unit
259
260     # replace + and -, but only if the - is not the first character
261     string = string[0] + string[1:].replace("+", " plus ").replace("-", " minus ")
262     # handle the case where "+-1mm" was used, because LaTeX only understands
263     # "plus 1mm minus 1mm"
264     if string.find("plus  minus"):
265         lastvaluepos = string.rfind(" ")
266         lastvalue = string[lastvaluepos:]
267         string = string.replace("  ", lastvalue + " ")
268     if percent ==  False:
269         return "False," + string
270     else:
271         return "True," + string
272
273
274 def revert_flex_inset(document, name, LaTeXname, position):
275   " Convert flex insets to TeX code "
276   i = position
277   while True:
278     i = find_token(document.body, '\\begin_inset Flex ' + name, i)
279     if i == -1:
280       return
281     z = find_end_of_inset(document.body, i)
282     if z == -1:
283       document.warning("Malformed LyX document: Can't find end of Flex " + name + " inset.")
284       return
285     # remove the \end_inset
286     document.body[z - 2:z + 1] = put_cmd_in_ert("}")
287     # we need to reset character layouts if necessary
288     j = find_token(document.body, '\\emph on', i)
289     k = find_token(document.body, '\\noun on', i)
290     l = find_token(document.body, '\\series', i)
291     m = find_token(document.body, '\\family', i)
292     n = find_token(document.body, '\\shape', i)
293     o = find_token(document.body, '\\color', i)
294     p = find_token(document.body, '\\size', i)
295     q = find_token(document.body, '\\bar under', i)
296     r = find_token(document.body, '\\uuline on', i)
297     s = find_token(document.body, '\\uwave on', i)
298     t = find_token(document.body, '\\strikeout on', i)
299     if j != -1 and j < z:
300       document.body.insert(z-2, "\\emph default")
301     if k != -1 and k < z:
302       document.body.insert(z-2, "\\noun default")
303     if l != -1 and l < z:
304       document.body.insert(z-2, "\\series default")
305     if m != -1 and m < z:
306       document.body.insert(z-2, "\\family default")
307     if n != -1 and n < z:
308       document.body.insert(z-2, "\\shape default")
309     if o != -1 and o < z:
310       document.body.insert(z-2, "\\color inherit")
311     if p != -1 and p < z:
312       document.body.insert(z-2, "\\size default")
313     if q != -1 and q < z:
314       document.body.insert(z-2, "\\bar default")
315     if r != -1 and r < z:
316       document.body.insert(z-2, "\\uuline default")
317     if s != -1 and s < z:
318       document.body.insert(z-2, "\\uwave default")
319     if t != -1 and t < z:
320       document.body.insert(z-2, "\\strikeout default")
321     document.body[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
322     i += 1
323
324
325 def revert_charstyles(document, name, LaTeXname, changed):
326   " Reverts character styles to TeX code "
327   i = 0
328   while True:
329     i = find_token(document.body, name + ' on', i)
330     if i == -1:
331       return changed
332     j = find_token(document.body, name + ' default', i)
333     k = find_token(document.body, name + ' on', i + 1)
334     # if there is no default set, the style ends with the layout
335     # assure hereby that we found the correct layout end
336     if j != -1 and (j < k or k == -1):
337       document.body[j:j+1] = put_cmd_in_ert("}")
338     else:
339       j = find_token(document.body, '\\end_layout', i)
340       document.body[j:j] = put_cmd_in_ert("}")
341     document.body[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
342     changed = True
343     i += 1
344
345
346 def revert_layout_command(document, name, LaTeXname, position):
347   " Reverts a command from a layout to TeX code "
348   i = position
349   while True:
350     i = find_token(document.body, '\\begin_layout ' + name, i)
351     if i == -1:
352       return
353     k = -1
354     # find the next layout
355     j = i + 1
356     while k == -1:
357       j = find_token(document.body, '\\begin_layout', j)
358       l = len(document.body)
359       # if nothing was found it was the last layout of the document
360       if j == -1:
361         document.body[l - 4:l - 4] = put_cmd_in_ert("}")
362         k = 0
363       # exclude plain layout because this can be TeX code or another inset
364       elif document.body[j] != '\\begin_layout Plain Layout':
365         document.body[j - 2:j - 2] = put_cmd_in_ert("}")
366         k = 0
367       else:
368         j += 1
369     document.body[i] = '\\begin_layout Standard'
370     document.body[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
371     i += 1
372
373
374 ####################################################################
375
376
377 def revert_swiss(document):
378     " Set language german-ch to ngerman "
379     i = 0
380     if document.language == "german-ch":
381         document.language = "ngerman"
382         i = find_token(document.header, "\\language", 0)
383         if i != -1:
384             document.header[i] = "\\language ngerman"
385     j = 0
386     while True:
387         j = find_token(document.body, "\\lang german-ch", j)
388         if j == -1:
389             return
390         document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
391         j = j + 1
392
393
394 def revert_tabularvalign(document):
395    " Revert the tabular valign option "
396    i = 0
397    while True:
398        i = find_token(document.body, "\\begin_inset Tabular", i)
399        if i == -1:
400            return
401        j = find_token(document.body, "</cell>", i)
402        if j == -1:
403            document.warning("Malformed LyX document: Could not find end of tabular cell.")
404            i = j
405            continue
406        # don't set a box for longtables, only delete tabularvalignment
407        # the alignment is 2 lines below \\begin_inset Tabular
408        p = document.body[i+2].find("islongtable")
409        if p > -1:
410            q = document.body[i+2].find("tabularvalignment")
411            if q > -1:
412                document.body[i+2] = document.body[i+2][:q-1]
413                document.body[i+2] = document.body[i+2] + '>'
414            i = i + 1
415
416        # when no longtable
417        if p == -1:
418          tabularvalignment = 'c'
419          # which valignment is specified?
420          m = document.body[i+2].find('tabularvalignment="top"')
421          if m > -1:
422              tabularvalignment = 't'
423          m = document.body[i+2].find('tabularvalignment="bottom"')
424          if m > -1:
425              tabularvalignment = 'b'
426          # delete tabularvalignment
427          q = document.body[i+2].find("tabularvalignment")
428          if q > -1:
429              document.body[i+2] = document.body[i+2][:q-1]
430              document.body[i+2] = document.body[i+2] + '>'
431
432          # don't add a box when centered
433          if tabularvalignment == 'c':
434              i = j
435              continue
436          subst = ['\\end_layout', '\\end_inset']
437          document.body[j:j] = subst # just inserts those lines
438          subst = ['\\begin_inset Box Frameless',
439              'position "' + tabularvalignment +'"',
440              'hor_pos "c"',
441              'has_inner_box 1',
442              'inner_pos "c"',
443              'use_parbox 0',
444              # we don't know the width, assume 50%
445              'width "50col%"',
446              'special "none"',
447              'height "1in"',
448              'height_special "totalheight"',
449              'status open',
450              '',
451              '\\begin_layout Plain Layout']
452          document.body[i:i] = subst # this just inserts the array at i
453          i += len(subst) + 2 # adjust i to save a few cycles
454
455
456 def revert_phantom(document):
457     " Reverts phantom to ERT "
458     i = 0
459     j = 0
460     while True:
461       i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
462       if i == -1:
463           return
464       substi = document.body[i].replace('\\begin_inset Phantom Phantom', \
465                 '\\begin_inset ERT\nstatus collapsed\n\n' \
466                 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
467                 'phantom{\n\\end_layout\n\n\\end_inset\n')
468       substi = substi.split('\n')
469       document.body[i : i+4] = substi
470       i += len(substi)
471       j = find_token(document.body, "\\end_layout", i)
472       if j == -1:
473           document.warning("Malformed LyX document: Could not find end of Phantom inset.")
474           return
475       substj = document.body[j].replace('\\end_layout', \
476                 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
477                 '\\begin_layout Plain Layout\n\n' \
478                 '}\n\\end_layout\n\n\\end_inset\n')
479       substj = substj.split('\n')
480       document.body[j : j+4] = substj
481       i += len(substj)
482
483
484 def revert_hphantom(document):
485     " Reverts hphantom to ERT "
486     i = 0
487     j = 0
488     while True:
489       i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
490       if i == -1:
491           return
492       substi = document.body[i].replace('\\begin_inset Phantom HPhantom', \
493                 '\\begin_inset ERT\nstatus collapsed\n\n' \
494                 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
495                 'hphantom{\n\\end_layout\n\n\\end_inset\n')
496       substi = substi.split('\n')
497       document.body[i : i+4] = substi
498       i += len(substi)
499       j = find_token(document.body, "\\end_layout", i)
500       if j == -1:
501           document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
502           return
503       substj = document.body[j].replace('\\end_layout', \
504                 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
505                 '\\begin_layout Plain Layout\n\n' \
506                 '}\n\\end_layout\n\n\\end_inset\n')
507       substj = substj.split('\n')
508       document.body[j : j+4] = substj
509       i += len(substj)
510
511
512 def revert_vphantom(document):
513     " Reverts vphantom to ERT "
514     i = 0
515     j = 0
516     while True:
517       i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
518       if i == -1:
519           return
520       substi = document.body[i].replace('\\begin_inset Phantom VPhantom', \
521                 '\\begin_inset ERT\nstatus collapsed\n\n' \
522                 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
523                 'vphantom{\n\\end_layout\n\n\\end_inset\n')
524       substi = substi.split('\n')
525       document.body[i : i+4] = substi
526       i += len(substi)
527       j = find_token(document.body, "\\end_layout", i)
528       if j == -1:
529           document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
530           return
531       substj = document.body[j].replace('\\end_layout', \
532                 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
533                 '\\begin_layout Plain Layout\n\n' \
534                 '}\n\\end_layout\n\n\\end_inset\n')
535       substj = substj.split('\n')
536       document.body[j : j+4] = substj
537       i += len(substj)
538
539
540 def revert_xetex(document):
541     " Reverts documents that use XeTeX "
542     i = find_token(document.header, '\\use_xetex', 0)
543     if i == -1:
544         document.warning("Malformed LyX document: Missing \\use_xetex.")
545         return
546     if get_value(document.header, "\\use_xetex", i) == 'false':
547         del document.header[i]
548         return
549     del document.header[i]
550     # 1.) set doc encoding to utf8-plain
551     i = find_token(document.header, "\\inputencoding", 0)
552     if i == -1:
553         document.warning("Malformed LyX document: Missing \\inputencoding.")
554     document.header[i] = "\\inputencoding utf8-plain"
555     # 2.) check font settings
556     l = find_token(document.header, "\\font_roman", 0)
557     if l == -1:
558         document.warning("Malformed LyX document: Missing \\font_roman.")
559     line = document.header[l]
560     l = re.compile(r'\\font_roman (.*)$')
561     m = l.match(line)
562     roman = m.group(1)
563     l = find_token(document.header, "\\font_sans", 0)
564     if l == -1:
565         document.warning("Malformed LyX document: Missing \\font_sans.")
566     line = document.header[l]
567     l = re.compile(r'\\font_sans (.*)$')
568     m = l.match(line)
569     sans = m.group(1)
570     l = find_token(document.header, "\\font_typewriter", 0)
571     if l == -1:
572         document.warning("Malformed LyX document: Missing \\font_typewriter.")
573     line = document.header[l]
574     l = re.compile(r'\\font_typewriter (.*)$')
575     m = l.match(line)
576     typewriter = m.group(1)
577     osf = get_value(document.header, '\\font_osf', 0) == "true"
578     sf_scale = float(get_value(document.header, '\\font_sf_scale', 0))
579     tt_scale = float(get_value(document.header, '\\font_tt_scale', 0))
580     # 3.) set preamble stuff
581     pretext = '%% This document must be processed with xelatex!\n'
582     pretext += '\\usepackage{fontspec}\n'
583     if roman != "default":
584         pretext += '\\setmainfont[Mapping=tex-text]{' + roman + '}\n'
585     if sans != "default":
586         pretext += '\\setsansfont['
587         if sf_scale != 100:
588             pretext += 'Scale=' + str(sf_scale / 100) + ','
589         pretext += 'Mapping=tex-text]{' + sans + '}\n'
590     if typewriter != "default":
591         pretext += '\\setmonofont'
592         if tt_scale != 100:
593             pretext += '[Scale=' + str(tt_scale / 100) + ']'
594         pretext += '{' + typewriter + '}\n'
595     if osf:
596         pretext += '\\defaultfontfeatures{Numbers=OldStyle}\n'
597     pretext += '\usepackage{xunicode}\n'
598     pretext += '\usepackage{xltxtra}\n'
599     insert_to_preamble(0, document, pretext)
600     # 4.) reset font settings
601     i = find_token(document.header, "\\font_roman", 0)
602     if i == -1:
603         document.warning("Malformed LyX document: Missing \\font_roman.")
604     document.header[i] = "\\font_roman default"
605     i = find_token(document.header, "\\font_sans", 0)
606     if i == -1:
607         document.warning("Malformed LyX document: Missing \\font_sans.")
608     document.header[i] = "\\font_sans default"
609     i = find_token(document.header, "\\font_typewriter", 0)
610     if i == -1:
611         document.warning("Malformed LyX document: Missing \\font_typewriter.")
612     document.header[i] = "\\font_typewriter default"
613     i = find_token(document.header, "\\font_osf", 0)
614     if i == -1:
615         document.warning("Malformed LyX document: Missing \\font_osf.")
616     document.header[i] = "\\font_osf false"
617     i = find_token(document.header, "\\font_sc", 0)
618     if i == -1:
619         document.warning("Malformed LyX document: Missing \\font_sc.")
620     document.header[i] = "\\font_sc false"
621     i = find_token(document.header, "\\font_sf_scale", 0)
622     if i == -1:
623         document.warning("Malformed LyX document: Missing \\font_sf_scale.")
624     document.header[i] = "\\font_sf_scale 100"
625     i = find_token(document.header, "\\font_tt_scale", 0)
626     if i == -1:
627         document.warning("Malformed LyX document: Missing \\font_tt_scale.")
628     document.header[i] = "\\font_tt_scale 100"
629
630
631 def revert_outputformat(document):
632     " Remove default output format param "
633     i = find_token(document.header, '\\default_output_format', 0)
634     if i == -1:
635         document.warning("Malformed LyX document: Missing \\default_output_format.")
636         return
637     del document.header[i]
638
639
640 def revert_backgroundcolor(document):
641     " Reverts background color to preamble code "
642     i = 0
643     colorcode = ""
644     while True:
645       i = find_token(document.header, "\\backgroundcolor", i)
646       if i == -1:
647           return
648       colorcode = get_value(document.header, '\\backgroundcolor', 0)
649       del document.header[i]
650       # don't clutter the preamble if backgroundcolor is not set
651       if colorcode == "#ffffff":
652           continue
653       # the color code is in the form #rrggbb where every character denotes a hex number
654       # convert the string to an int
655       red = string.atoi(colorcode[1:3],16)
656       # we want the output "0.5" for the value "127" therefore add here
657       if red != 0:
658           red = red + 1
659       redout = float(red) / 256
660       green = string.atoi(colorcode[3:5],16)
661       if green != 0:
662           green = green + 1
663       greenout = float(green) / 256
664       blue = string.atoi(colorcode[5:7],16)
665       if blue != 0:
666           blue = blue + 1
667       blueout = float(blue) / 256
668       # write the preamble
669       insert_to_preamble(0, document,
670                            '% Commands inserted by lyx2lyx to set the background color\n'
671                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
672                            + '\\definecolor{page_backgroundcolor}{rgb}{'
673                            + str(redout) + ', ' + str(greenout)
674                            + ', ' + str(blueout) + '}\n'
675                            + '\\pagecolor{page_backgroundcolor}\n')
676
677
678 def revert_splitindex(document):
679     " Reverts splitindex-aware documents "
680     i = find_token(document.header, '\\use_indices', 0)
681     if i == -1:
682         document.warning("Malformed LyX document: Missing \\use_indices.")
683         return
684     indices = get_value(document.header, "\\use_indices", i)
685     preamble = ""
686     if indices == "true":
687          preamble += "\\usepackage{splitidx}\n"
688     del document.header[i]
689     i = 0
690     while True:
691         i = find_token(document.header, "\\index", i)
692         if i == -1:
693             break
694         k = find_token(document.header, "\\end_index", i)
695         if k == -1:
696             document.warning("Malformed LyX document: Missing \\end_index.")
697             return
698         line = document.header[i]
699         l = re.compile(r'\\index (.*)$')
700         m = l.match(line)
701         iname = m.group(1)
702         ishortcut = get_value(document.header, '\\shortcut', i, k)
703         if ishortcut != "" and indices == "true":
704             preamble += "\\newindex[" + iname + "]{" + ishortcut + "}\n"
705         del document.header[i:k+1]
706         i = 0
707     if preamble != "":
708         insert_to_preamble(0, document, preamble)
709     i = 0
710     while True:
711         i = find_token(document.body, "\\begin_inset Index", i)
712         if i == -1:
713             break
714         line = document.body[i]
715         l = re.compile(r'\\begin_inset Index (.*)$')
716         m = l.match(line)
717         itype = m.group(1)
718         if itype == "idx" or indices == "false":
719             document.body[i] = "\\begin_inset Index"
720         else:
721             k = find_end_of_inset(document.body, i)
722             if k == -1:
723                  return
724             content = lyx2latex(document, document.body[i:k])
725             # escape quotes
726             content = content.replace('"', r'\"')
727             subst = [old_put_cmd_in_ert("\\sindex[" + itype + "]{" + content + "}")]
728             document.body[i:k+1] = subst
729         i = i + 1
730     i = 0
731     while True:
732         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
733         if i == -1:
734             return
735         k = find_end_of_inset(document.body, i)
736         ptype = get_value(document.body, 'type', i, k).strip('"')
737         if ptype == "idx":
738             j = find_token(document.body, "type", i, k)
739             del document.body[j]
740         elif indices == "false":
741             del document.body[i:k+1]
742         else:
743             subst = [old_put_cmd_in_ert("\\printindex[" + ptype + "]{}")]
744             document.body[i:k+1] = subst
745         i = i + 1
746
747
748 def convert_splitindex(document):
749     " Converts index and printindex insets to splitindex-aware format "
750     i = 0
751     while True:
752         i = find_token(document.body, "\\begin_inset Index", i)
753         if i == -1:
754             break
755         document.body[i] = document.body[i].replace("\\begin_inset Index",
756             "\\begin_inset Index idx")
757         i = i + 1
758     i = 0
759     while True:
760         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
761         if i == -1:
762             return
763         if document.body[i + 1].find('LatexCommand printindex') == -1:
764             document.warning("Malformed LyX document: Incomplete printindex inset.")
765             return
766         subst = ["LatexCommand printindex", 
767             "type \"idx\""]
768         document.body[i + 1:i + 2] = subst
769         i = i + 1
770
771
772 def revert_subindex(document):
773     " Reverts \\printsubindex CommandInset types "
774     i = find_token(document.header, '\\use_indices', 0)
775     if i == -1:
776         document.warning("Malformed LyX document: Missing \\use_indices.")
777         return
778     indices = get_value(document.header, "\\use_indices", i)
779     i = 0
780     while True:
781         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
782         if i == -1:
783             return
784         k = find_end_of_inset(document.body, i)
785         ctype = get_value(document.body, 'LatexCommand', i, k)
786         if ctype != "printsubindex":
787             i = i + 1
788             continue
789         ptype = get_value(document.body, 'type', i, k).strip('"')
790         if indices == "false":
791             del document.body[i:k+1]
792         else:
793             subst = [old_put_cmd_in_ert("\\printsubindex[" + ptype + "]{}")]
794             document.body[i:k+1] = subst
795         i = i + 1
796
797
798 def revert_printindexall(document):
799     " Reverts \\print[sub]index* CommandInset types "
800     i = find_token(document.header, '\\use_indices', 0)
801     if i == -1:
802         document.warning("Malformed LyX document: Missing \\use_indices.")
803         return
804     indices = get_value(document.header, "\\use_indices", i)
805     i = 0
806     while True:
807         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
808         if i == -1:
809             return
810         k = find_end_of_inset(document.body, i)
811         ctype = get_value(document.body, 'LatexCommand', i, k)
812         if ctype != "printindex*" and ctype != "printsubindex*":
813             i = i + 1
814             continue
815         if indices == "false":
816             del document.body[i:k+1]
817         else:
818             subst = [old_put_cmd_in_ert("\\" + ctype + "{}")]
819             document.body[i:k+1] = subst
820         i = i + 1
821
822
823 def revert_strikeout(document):
824   " Reverts \\strikeout character style "
825   changed = False
826   changed = revert_charstyles(document, "\\uuline", "\\uuline", changed)
827   changed = revert_charstyles(document, "\\uwave", "\\uwave", changed)
828   changed = revert_charstyles(document, "\\strikeout", "\\sout", changed)
829   if changed == True:
830     insert_to_preamble(0, document,
831         '% Commands inserted by lyx2lyx for proper underlining\n'
832         + '\\PassOptionsToPackage{normalem}{ulem}\n'
833         + '\\usepackage{ulem}\n')
834
835
836 def revert_ulinelatex(document):
837     " Reverts \\uline character style "
838     i = find_token(document.body, '\\bar under', 0)
839     if i == -1:
840         return
841     insert_to_preamble(0, document,
842             '% Commands inserted by lyx2lyx for proper underlining\n'
843             + '\\PassOptionsToPackage{normalem}{ulem}\n'
844             + '\\usepackage{ulem}\n'
845             + '\\let\\cite@rig\\cite\n'
846             + '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}\n'
847             + '  \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}\n'
848             + '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}\n')
849
850
851 def revert_custom_processors(document):
852     " Remove bibtex_command and index_command params "
853     i = find_token(document.header, '\\bibtex_command', 0)
854     if i == -1:
855         document.warning("Malformed LyX document: Missing \\bibtex_command.")
856         return
857     del document.header[i]
858     i = find_token(document.header, '\\index_command', 0)
859     if i == -1:
860         document.warning("Malformed LyX document: Missing \\index_command.")
861         return
862     del document.header[i]
863
864
865 def convert_nomencl_width(document):
866     " Add set_width param to nomencl_print "
867     i = 0
868     while True:
869       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
870       if i == -1:
871         break
872       document.body.insert(i + 2, "set_width \"none\"")
873       i = i + 1
874
875
876 def revert_nomencl_width(document):
877     " Remove set_width param from nomencl_print "
878     i = 0
879     while True:
880       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
881       if i == -1:
882         break
883       j = find_end_of_inset(document.body, i)
884       l = find_token(document.body, "set_width", i, j)
885       if l == -1:
886             document.warning("Can't find set_width option for nomencl_print!")
887             i = j
888             continue
889       del document.body[l]
890       i = i + 1
891
892
893 def revert_nomencl_cwidth(document):
894     " Remove width param from nomencl_print "
895     i = 0
896     while True:
897       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
898       if i == -1:
899         break
900       j = find_end_of_inset(document.body, i)
901       l = find_token(document.body, "width", i, j)
902       if l == -1:
903             #Can't find width option for nomencl_print
904             i = j
905             continue
906       width = get_value(document.body, "width", i, j).strip('"')
907       del document.body[l]
908       add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
909       add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
910       i = i + 1
911
912
913 def revert_applemac(document):
914     " Revert applemac encoding to auto "
915     i = 0
916     if document.encoding == "applemac":
917         document.encoding = "auto"
918         i = find_token(document.header, "\\encoding", 0)
919         if i != -1:
920             document.header[i] = "\\encoding auto"
921
922
923 def revert_longtable_align(document):
924     " Remove longtable alignment setting "
925     i = 0
926     j = 0
927     while True:
928       i = find_token(document.body, "\\begin_inset Tabular", i)
929       if i == -1:
930           break
931       # the alignment is 2 lines below \\begin_inset Tabular
932       j = document.body[i+2].find("longtabularalignment")
933       if j == -1:
934           break
935       document.body[i+2] = document.body[i+2][:j-1]
936       document.body[i+2] = document.body[i+2] + '>'
937       i = i + 1
938
939
940 def revert_branch_filename(document):
941     " Remove \\filename_suffix parameter from branches "
942     i = 0
943     while True:
944         i = find_token(document.header, "\\filename_suffix", i)
945         if i == -1:
946             return
947         del document.header[i]
948
949
950 def revert_paragraph_indentation(document):
951     " Revert custom paragraph indentation to preamble code "
952     i = 0
953     while True:
954       i = find_token(document.header, "\\paragraph_indentation", i)
955       if i == -1:
956           break
957       # only remove the preamble line if default
958       # otherwise also write the value to the preamble
959       length = get_value(document.header, "\\paragraph_indentation", i)
960       if length == "default":
961           del document.header[i]
962           break
963       else:
964           # handle percent lengths
965           # latex_length returns "bool,length"
966           length = latex_length(length).split(",")[1]
967           add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
968           add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
969           del document.header[i]
970       i = i + 1
971
972
973 def revert_percent_skip_lengths(document):
974     " Revert relative lengths for paragraph skip separation to preamble code "
975     i = 0
976     while True:
977       i = find_token(document.header, "\\defskip", i)
978       if i == -1:
979           break
980       length = get_value(document.header, "\\defskip", i)
981       # only revert when a custom length was set and when
982       # it used a percent length
983       if length not in ('smallskip', 'medskip', 'bigskip'):
984           # handle percent lengths
985           length = latex_length(length)
986           # latex_length returns "bool,length"
987           percent = length.split(",")[0]
988           length = length.split(",")[1]
989           if percent == "True":
990               add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
991               add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
992               # set defskip to medskip as default
993               document.header[i] = "\\defskip medskip"
994       i = i + 1
995
996
997 def revert_percent_vspace_lengths(document):
998     " Revert relative VSpace lengths to ERT "
999     i = 0
1000     while True:
1001       i = find_token(document.body, "\\begin_inset VSpace", i)
1002       if i == -1:
1003           break
1004       # only revert if a custom length was set and if
1005       # it used a percent length
1006       line = document.body[i]
1007       r = re.compile(r'\\begin_inset VSpace (.*)$')
1008       m = r.match(line)
1009       length = m.group(1)
1010       if length not in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
1011           # check if the space has a star (protected space)
1012           protected = (document.body[i].rfind("*") != -1)
1013           if protected:
1014               length = length.rstrip('*')
1015           # handle percent lengths
1016           length = latex_length(length)
1017           # latex_length returns "bool,length"
1018           percent = length.split(",")[0]
1019           length = length.split(",")[1]
1020           # revert the VSpace inset to ERT
1021           if percent == "True":
1022               if protected:
1023                   subst = [old_put_cmd_in_ert("\\vspace*{" + length + "}")]
1024               else:
1025                   subst = [old_put_cmd_in_ert("\\vspace{" + length + "}")]
1026               document.body[i:i+2] = subst
1027       i = i + 1
1028
1029
1030 def revert_percent_hspace_lengths(document):
1031     " Revert relative HSpace lengths to ERT "
1032     i = 0
1033     while True:
1034       i = find_token(document.body, "\\begin_inset space \\hspace", i)
1035       if i == -1:
1036           break
1037       protected = (document.body[i].find("\\hspace*{}") != -1)
1038       # only revert if a custom length was set and if
1039       # it used a percent length
1040       length = get_value(document.body, '\\length', i+1)
1041       if length == '':
1042           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1043           return
1044       # handle percent lengths
1045       length = latex_length(length)
1046       # latex_length returns "bool,length"
1047       percent = length.split(",")[0]
1048       length = length.split(",")[1]
1049       # revert the HSpace inset to ERT
1050       if percent == "True":
1051           if protected:
1052               subst = [old_put_cmd_in_ert("\\hspace*{" + length + "}")]
1053           else:
1054               subst = [old_put_cmd_in_ert("\\hspace{" + length + "}")]
1055           document.body[i:i+3] = subst
1056       i = i + 2
1057
1058
1059 def revert_hspace_glue_lengths(document):
1060     " Revert HSpace glue lengths to ERT "
1061     i = 0
1062     while True:
1063       i = find_token(document.body, "\\begin_inset space \\hspace", i)
1064       if i == -1:
1065           break
1066       protected = (document.body[i].find("\\hspace*{}") != -1)
1067       length = get_value(document.body, '\\length', i+1)
1068       if length == '':
1069           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1070           return
1071       # only revert if the length contains a plus or minus at pos != 0
1072       glue  = re.compile(r'.+[\+-]')
1073       if glue.search(length):
1074           # handle percent lengths
1075           # latex_length returns "bool,length"
1076           length = latex_length(length).split(",")[1]
1077           # revert the HSpace inset to ERT
1078           if protected:
1079               subst = [old_put_cmd_in_ert("\\hspace*{" + length + "}")]
1080           else:
1081               subst = [old_put_cmd_in_ert("\\hspace{" + length + "}")]
1082           document.body[i:i+3] = subst
1083       i = i + 2
1084
1085 def convert_author_id(document):
1086     " Add the author_id to the \\author definition and make sure 0 is not used"
1087     i = 0
1088     j = 1
1089     while True:
1090         i = find_token(document.header, "\\author", i)
1091         if i == -1:
1092             break
1093         
1094         r = re.compile(r'(\\author) (\".*\")\s?(.*)$')
1095         m = r.match(document.header[i])
1096         if m != None:
1097             name = m.group(2)
1098             
1099             email = ''
1100             if m.lastindex == 3:
1101                 email = m.group(3)
1102             document.header[i] = "\\author %i %s %s" % (j, name, email)
1103         j = j + 1
1104         i = i + 1
1105         
1106     k = 0
1107     while True:
1108         k = find_token(document.body, "\\change_", k)
1109         if k == -1:
1110             break
1111
1112         change = document.body[k].split(' ');
1113         if len(change) == 3:
1114             type = change[0]
1115             author_id = int(change[1])
1116             time = change[2]
1117             document.body[k] = "%s %i %s" % (type, author_id + 1, time)
1118         k = k + 1
1119
1120 def revert_author_id(document):
1121     " Remove the author_id from the \\author definition "
1122     i = 0
1123     j = 0
1124     idmap = dict()
1125     while True:
1126         i = find_token(document.header, "\\author", i)
1127         if i == -1:
1128             break
1129         
1130         r = re.compile(r'(\\author) (\d+) (\".*\")\s?(.*)$')
1131         m = r.match(document.header[i])
1132         if m != None:
1133             author_id = int(m.group(2))
1134             idmap[author_id] = j
1135             name = m.group(3)
1136             
1137             email = ''
1138             if m.lastindex == 4:
1139                 email = m.group(4)
1140             document.header[i] = "\\author %s %s" % (name, email)
1141         i = i + 1
1142         j = j + 1
1143
1144     k = 0
1145     while True:
1146         k = find_token(document.body, "\\change_", k)
1147         if k == -1:
1148             break
1149
1150         change = document.body[k].split(' ');
1151         if len(change) == 3:
1152             type = change[0]
1153             author_id = int(change[1])
1154             time = change[2]
1155             document.body[k] = "%s %i %s" % (type, idmap[author_id], time)
1156         k = k + 1
1157
1158
1159 def revert_suppress_date(document):
1160     " Revert suppressing of default document date to preamble code "
1161     i = 0
1162     while True:
1163       i = find_token(document.header, "\\suppress_date", i)
1164       if i == -1:
1165           break
1166       # remove the preamble line and write to the preamble
1167       # when suppress_date was true
1168       date = get_value(document.header, "\\suppress_date", i)
1169       if date == "true":
1170           add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1171           add_to_preamble(document, ["\\date{}"])
1172       del document.header[i]
1173       i = i + 1
1174
1175
1176 def revert_mhchem(document):
1177     "Revert mhchem loading to preamble code"
1178     i = 0
1179     j = 0
1180     k = 0
1181     mhchem = "off"
1182     i = find_token(document.header, "\\use_mhchem 1", 0)
1183     if i != -1:
1184         mhchem = "auto"
1185     else:
1186         i = find_token(document.header, "\\use_mhchem 2", 0)
1187         if i != -1:
1188             mhchem = "on"
1189     if mhchem == "auto":
1190         j = find_token(document.body, "\\cf{", 0)
1191         if j != -1:
1192             mhchem = "on"
1193         else:
1194             j = find_token(document.body, "\\ce{", 0)
1195             if j != -1:
1196                 mhchem = "on"
1197     if mhchem == "on":
1198         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1199         add_to_preamble(document, ["\\PassOptionsToPackage{version=3}{mhchem}"])
1200         add_to_preamble(document, ["\\usepackage{mhchem}"])
1201     k = find_token(document.header, "\\use_mhchem", 0)
1202     if k == -1:
1203         document.warning("Malformed LyX document: Could not find mhchem setting.")
1204         return
1205     del document.header[k]
1206
1207
1208 def revert_fontenc(document):
1209     " Remove fontencoding param "
1210     i = find_token(document.header, '\\fontencoding', 0)
1211     if i == -1:
1212         document.warning("Malformed LyX document: Missing \\fontencoding.")
1213         return
1214     del document.header[i]
1215
1216
1217 def merge_gbrief(document):
1218     " Merge g-brief-en and g-brief-de to one class "
1219
1220     if document.textclass != "g-brief-de":
1221         if document.textclass == "g-brief-en":
1222             document.textclass = "g-brief"
1223             document.set_textclass()
1224         return
1225
1226     obsoletedby = { "Brieftext":       "Letter",
1227                     "Unterschrift":    "Signature",
1228                     "Strasse":         "Street",
1229                     "Zusatz":          "Addition",
1230                     "Ort":             "Town",
1231                     "Land":            "State",
1232                     "RetourAdresse":   "ReturnAddress",
1233                     "MeinZeichen":     "MyRef",
1234                     "IhrZeichen":      "YourRef",
1235                     "IhrSchreiben":    "YourMail",
1236                     "Telefon":         "Phone",
1237                     "BLZ":             "BankCode",
1238                     "Konto":           "BankAccount",
1239                     "Postvermerk":     "PostalComment",
1240                     "Adresse":         "Address",
1241                     "Datum":           "Date",
1242                     "Betreff":         "Reference",
1243                     "Anrede":          "Opening",
1244                     "Anlagen":         "Encl.",
1245                     "Verteiler":       "cc",
1246                     "Gruss":           "Closing"}
1247     i = 0
1248     while 1:
1249         i = find_token(document.body, "\\begin_layout", i)
1250         if i == -1:
1251             break
1252
1253         layout = document.body[i][14:]
1254         if layout in obsoletedby:
1255             document.body[i] = "\\begin_layout " + obsoletedby[layout]
1256
1257         i += 1
1258         
1259     document.textclass = "g-brief"
1260     document.set_textclass()
1261
1262
1263 def revert_gbrief(document):
1264     " Revert g-brief to g-brief-en "
1265     if document.textclass == "g-brief":
1266         document.textclass = "g-brief-en"
1267         document.set_textclass()
1268
1269
1270 def revert_html_options(document):
1271     " Remove html options "
1272     i = find_token(document.header, '\\html_use_mathml', 0)
1273     if i != -1:
1274         del document.header[i]
1275     i = find_token(document.header, '\\html_be_strict', 0)
1276     if i != -1:
1277         del document.header[i]
1278
1279
1280 def revert_includeonly(document):
1281     i = 0
1282     while True:
1283         i = find_token(document.header, "\\begin_includeonly", i)
1284         if i == -1:
1285             return
1286         j = find_end_of(document.header, i, "\\begin_includeonly", "\\end_includeonly")
1287         if j == -1:
1288             # this should not happen
1289             break
1290         document.header[i : j + 1] = []
1291
1292
1293 def revert_includeall(document):
1294     " Remove maintain_unincluded_children param "
1295     i = find_token(document.header, '\\maintain_unincluded_children', 0)
1296     if i != -1:
1297         del document.header[i]
1298
1299
1300 def revert_multirow(document):
1301     " Revert multirow cells in tables "
1302     i = 0
1303     multirow = False
1304     while True:
1305       # cell type 3 is multirow begin cell
1306       i = find_token(document.body, '<cell multirow="3"', i)
1307       if i == -1:
1308           break
1309       # a multirow cell was found
1310       multirow = True
1311       # remove the multirow tag, set the valignment to top
1312       # and remove the bottom line
1313       document.body[i] = document.body[i].replace(' multirow="3" ', ' ')
1314       document.body[i] = document.body[i].replace('valignment="middle"', 'valignment="top"')
1315       document.body[i] = document.body[i].replace(' bottomline="true" ', ' ')
1316       # write ERT to create the multirow cell
1317       # use 2 rows and 2cm as default with because the multirow span
1318       # and the column width is only hardly accessible
1319       subst = [old_put_cmd_in_ert("\\multirow{2}{2cm}{")]
1320       document.body[i + 4:i + 4] = subst
1321       i = find_token(document.body, "</cell>", i)
1322       if i == -1:
1323            document.warning("Malformed LyX document: Could not find end of tabular cell.")
1324            break
1325       subst = [old_put_cmd_in_ert("}")]
1326       document.body[i - 3:i - 3] = subst
1327       # cell type 4 is multirow part cell
1328       i = find_token(document.body, '<cell multirow="4"', i)
1329       if i == -1:
1330           break
1331       # remove the multirow tag, set the valignment to top
1332       # and remove the top line
1333       document.body[i] = document.body[i].replace(' multirow="4" ', ' ')
1334       document.body[i] = document.body[i].replace('valignment="middle"', 'valignment="top"')
1335       document.body[i] = document.body[i].replace(' topline="true" ', ' ')
1336       i = i + 1
1337     if multirow == True:
1338         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1339         add_to_preamble(document, ["\\usepackage{multirow}"])
1340
1341
1342 def convert_math_output(document):
1343     " Convert \html_use_mathml to \html_math_output "
1344     i = find_token(document.header, "\\html_use_mathml", 0)
1345     if i == -1:
1346         return
1347     rgx = re.compile(r'\\html_use_mathml\s+(\w+)')
1348     m = rgx.match(document.header[i])
1349     if rgx:
1350         newval = "0" # MathML
1351         val = m.group(1)
1352         if val != "true":
1353             newval = "2" # Images
1354         document.header[i] = "\\html_math_output " + newval
1355
1356
1357 def revert_math_output(document):
1358     " Revert \html_math_output to \html_use_mathml "
1359     i = find_token(document.header, "\\html_math_output", 0)
1360     if i == -1:
1361         return
1362     rgx = re.compile(r'\\html_math_output\s+(\d)')
1363     m = rgx.match(document.header[i])
1364     newval = "true"
1365     if rgx:
1366         val = m.group(1)
1367         if val == "1" or val == "2":
1368             newval = "false"
1369     else:
1370         document.warning("Unable to match " + document.header[i])
1371     document.header[i] = "\\html_use_mathml " + newval
1372                 
1373
1374
1375 def revert_inset_preview(document):
1376     " Dissolves the preview inset "
1377     i = 0
1378     j = 0
1379     k = 0
1380     while True:
1381       i = find_token(document.body, "\\begin_inset Preview", i)
1382       if i == -1:
1383           return
1384       j = find_end_of_inset(document.body, i)
1385       if j == -1:
1386           document.warning("Malformed LyX document: Could not find end of Preview inset.")
1387           return
1388       #If the layout is Standard we need to remove it, otherwise there
1389       #will be paragraph breaks that shouldn't be there.
1390       k = find_token(document.body, "\\begin_layout Standard", i)
1391       if k == i+2:
1392           del document.body[i : i+3]
1393           del document.body[j-5 : j-2]
1394           i -= 6
1395       else:
1396           del document.body[i]
1397           del document.body[j-1]
1398           i -= 2
1399                 
1400
1401 def revert_equalspacing_xymatrix(document):
1402     " Revert a Formula with xymatrix@! to an ERT inset "
1403     i = 0
1404     j = 0
1405     has_preamble = False
1406     has_equal_spacing = False
1407     while True:
1408       found = -1
1409       i = find_token(document.body, "\\begin_inset Formula", i)
1410       if i == -1:
1411           break
1412       j = find_end_of_inset(document.body, i)
1413       if j == -1:
1414           document.warning("Malformed LyX document: Could not find end of Formula inset.")
1415           break
1416           
1417       for curline in range(i,j):
1418           found = document.body[curline].find("\\xymatrix@!")
1419           if found != -1:
1420               break
1421  
1422       if found != -1:
1423           has_equal_spacing = True
1424           content = [document.body[i][21:]]
1425           content += document.body[i+1:j]
1426           subst = put_cmd_in_ert(content)
1427           document.body[i:j+1] = subst
1428           i += len(subst)
1429       else:
1430           for curline in range(i,j):
1431               l = document.body[curline].find("\\xymatrix")
1432               if l != -1:
1433                   has_preamble = True;
1434                   break;
1435           i = j + 1
1436     if has_equal_spacing and not has_preamble:
1437         add_to_preamble(document, ['\\usepackage[all]{xy}'])
1438
1439
1440 def revert_notefontcolor(document):
1441     " Reverts greyed-out note font color to preamble code "
1442     i = 0
1443     colorcode = ""
1444     while True:
1445       i = find_token(document.header, "\\notefontcolor", i)
1446       if i == -1:
1447           return
1448       colorcode = get_value(document.header, '\\notefontcolor', 0)
1449       del document.header[i]
1450       # the color code is in the form #rrggbb where every character denotes a hex number
1451       # convert the string to an int
1452       red = string.atoi(colorcode[1:3],16)
1453       # we want the output "0.5" for the value "127" therefore increment here
1454       if red != 0:
1455           red = red + 1
1456       redout = float(red) / 256
1457       green = string.atoi(colorcode[3:5],16)
1458       if green != 0:
1459           green = green + 1
1460       greenout = float(green) / 256
1461       blue = string.atoi(colorcode[5:7],16)
1462       if blue != 0:
1463           blue = blue + 1
1464       blueout = float(blue) / 256
1465       # write the preamble
1466       insert_to_preamble(0, document,
1467                            '% Commands inserted by lyx2lyx to set the font color\n'
1468                            '% for greyed-out notes\n'
1469                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1470                            + '\\definecolor{note_fontcolor}{rgb}{'
1471                            + str(redout) + ', ' + str(greenout)
1472                            + ', ' + str(blueout) + '}\n'
1473                            + '\\renewenvironment{lyxgreyedout}\n'
1474                            + ' {\\textcolor{note_fontcolor}\\bgroup}{\\egroup}\n')
1475
1476
1477 def revert_turkmen(document):
1478     "Set language Turkmen to English" 
1479     i = 0 
1480     if document.language == "turkmen": 
1481         document.language = "english" 
1482         i = find_token(document.header, "\\language", 0) 
1483         if i != -1: 
1484             document.header[i] = "\\language english" 
1485     j = 0 
1486     while True: 
1487         j = find_token(document.body, "\\lang turkmen", j) 
1488         if j == -1: 
1489             return 
1490         document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english") 
1491         j = j + 1 
1492
1493
1494 def revert_fontcolor(document):
1495     " Reverts font color to preamble code "
1496     i = 0
1497     colorcode = ""
1498     while True:
1499       i = find_token(document.header, "\\fontcolor", i)
1500       if i == -1:
1501           return
1502       colorcode = get_value(document.header, '\\fontcolor', 0)
1503       del document.header[i]
1504       # don't clutter the preamble if backgroundcolor is not set
1505       if colorcode == "#000000":
1506           continue
1507       # the color code is in the form #rrggbb where every character denotes a hex number
1508       # convert the string to an int
1509       red = string.atoi(colorcode[1:3],16)
1510       # we want the output "0.5" for the value "127" therefore add here
1511       if red != 0:
1512           red = red + 1
1513       redout = float(red) / 256
1514       green = string.atoi(colorcode[3:5],16)
1515       if green != 0:
1516           green = green + 1
1517       greenout = float(green) / 256
1518       blue = string.atoi(colorcode[5:7],16)
1519       if blue != 0:
1520           blue = blue + 1
1521       blueout = float(blue) / 256
1522       # write the preamble
1523       insert_to_preamble(0, document,
1524                            '% Commands inserted by lyx2lyx to set the font color\n'
1525                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1526                            + '\\definecolor{document_fontcolor}{rgb}{'
1527                            + str(redout) + ', ' + str(greenout)
1528                            + ', ' + str(blueout) + '}\n'
1529                            + '\\color{document_fontcolor}\n')
1530
1531
1532 def revert_shadedboxcolor(document):
1533     " Reverts shaded box color to preamble code "
1534     i = 0
1535     colorcode = ""
1536     while True:
1537       i = find_token(document.header, "\\boxbgcolor", i)
1538       if i == -1:
1539           return
1540       colorcode = get_value(document.header, '\\boxbgcolor', 0)
1541       del document.header[i]
1542       # the color code is in the form #rrggbb where every character denotes a hex number
1543       # convert the string to an int
1544       red = string.atoi(colorcode[1:3],16)
1545       # we want the output "0.5" for the value "127" therefore increment here
1546       if red != 0:
1547           red = red + 1
1548       redout = float(red) / 256
1549       green = string.atoi(colorcode[3:5],16)
1550       if green != 0:
1551           green = green + 1
1552       greenout = float(green) / 256
1553       blue = string.atoi(colorcode[5:7],16)
1554       if blue != 0:
1555           blue = blue + 1
1556       blueout = float(blue) / 256
1557       # write the preamble
1558       insert_to_preamble(0, document,
1559                            '% Commands inserted by lyx2lyx to set the color\n'
1560                            '% of boxes with shaded background\n'
1561                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1562                            + '\\definecolor{shadecolor}{rgb}{'
1563                            + str(redout) + ', ' + str(greenout)
1564                            + ', ' + str(blueout) + '}\n')
1565
1566
1567 def revert_lyx_version(document):
1568     " Reverts LyX Version information from Inset Info "
1569     version = "LyX version"
1570     try:
1571         import lyx2lyx_version
1572         version = lyx2lyx_version.version
1573     except:
1574         pass
1575
1576     i = 0
1577     while 1:
1578         i = find_token(document.body, '\\begin_inset Info', i)
1579         if i == -1:
1580             return
1581         j = find_end_of_inset(document.body, i + 1)
1582         if j == -1:
1583             # should not happen
1584             document.warning("Malformed LyX document: Could not find end of Info inset.")
1585         # We expect:
1586         # \begin_inset Info
1587         # type  "lyxinfo"
1588         # arg   "version"
1589         # \end_inset
1590         # but we shall try to be forgiving.
1591         arg = typ = ""
1592         for k in range(i, j):
1593             if document.body[k].startswith("arg"):
1594                 arg = document.body[k][3:].strip().strip('"')
1595             if document.body[k].startswith("type"):
1596                 typ = document.body[k][4:].strip().strip('"')
1597         if arg != "version" or typ != "lyxinfo":
1598             i = j+1
1599             continue
1600
1601         # We do not actually know the version of LyX used to produce the document.
1602         # But we can use our version, since we are reverting.
1603         s = [version]
1604         # Now we want to check if the line after "\end_inset" is empty. It normally
1605         # is, so we want to remove it, too.
1606         lastline = j+1
1607         if document.body[j+1].strip() == "":
1608             lastline = j+2
1609         document.body[i: lastline] = s
1610         i = i+1
1611
1612
1613 def revert_math_scale(document):
1614   " Remove math scaling and LaTeX options "
1615   i = find_token(document.header, '\\html_math_img_scale', 0)
1616   if i != -1:
1617     del document.header[i]
1618   i = find_token(document.header, '\\html_latex_start', 0)
1619   if i != -1:
1620     del document.header[i]
1621   i = find_token(document.header, '\\html_latex_end', 0)
1622   if i != -1:
1623     del document.header[i]
1624
1625
1626 def revert_pagesizes(document):
1627   i = 0
1628   " Revert page sizes to default "
1629   i = find_token(document.header, '\\papersize', 0)
1630   if i != -1:
1631     size = document.header[i][11:]
1632     if size == "a0paper" or size == "a1paper" or size == "a2paper" \
1633     or size == "a6paper" or size == "b0paper" or size == "b1paper" \
1634     or size == "b2paper" or size == "b6paper" or size == "b0j" \
1635     or size == "b1j" or size == "b2j" or size == "b3j" or size == "b4j" \
1636     or size == "b5j" or size == "b6j":
1637       del document.header[i]
1638
1639
1640 def convert_html_quotes(document):
1641   " Remove quotes around html_latex_start and html_latex_end "
1642
1643   i = find_token(document.header, '\\html_latex_start', 0)
1644   if i != -1:
1645     line = document.header[i]
1646     l = re.compile(r'\\html_latex_start\s+"(.*)"')
1647     m = l.match(line)
1648     if m != None:
1649       document.header[i] = "\\html_latex_start " + m.group(1)
1650       
1651   i = find_token(document.header, '\\html_latex_end', 0)
1652   if i != -1:
1653     line = document.header[i]
1654     l = re.compile(r'\\html_latex_end\s+"(.*)"')
1655     m = l.match(line)
1656     if m != None:
1657       document.header[i] = "\\html_latex_end " + m.group(1)
1658       
1659
1660 def revert_html_quotes(document):
1661   " Remove quotes around html_latex_start and html_latex_end "
1662   
1663   i = find_token(document.header, '\\html_latex_start', 0)
1664   if i != -1:
1665     line = document.header[i]
1666     l = re.compile(r'\\html_latex_start\s+(.*)')
1667     m = l.match(line)
1668     document.header[i] = "\\html_latex_start \"" + m.group(1) + "\""
1669       
1670   i = find_token(document.header, '\\html_latex_end', 0)
1671   if i != -1:
1672     line = document.header[i]
1673     l = re.compile(r'\\html_latex_end\s+(.*)')
1674     m = l.match(line)
1675     document.header[i] = "\\html_latex_end \"" + m.group(1) + "\""
1676
1677
1678 def revert_output_sync(document):
1679   " Remove forward search options "
1680   i = find_token(document.header, '\\output_sync_macro', 0)
1681   if i != -1:
1682     del document.header[i]
1683   i = find_token(document.header, '\\output_sync', 0)
1684   if i != -1:
1685     del document.header[i]
1686
1687
1688 def convert_beamer_args(document):
1689   " Convert ERT arguments in Beamer to InsetArguments "
1690
1691   if document.textclass != "beamer" and document.textclass != "article-beamer":
1692     return
1693   
1694   layouts = ("Block", "ExampleBlock", "AlertBlock")
1695   for layout in layouts:
1696     blay = 0
1697     while True:
1698       blay = find_token(document.body, '\\begin_layout ' + layout, blay)
1699       if blay == -1:
1700         break
1701       elay = find_end_of(document.body, blay, '\\begin_layout', '\\end_layout')
1702       if elay == -1:
1703         document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1704         blay += 1
1705         continue
1706       bert = find_token(document.body, '\\begin_inset ERT', blay)
1707       if bert == -1:
1708         document.warning("Malformed Beamer LyX document: Can't find argument of " + layout + " layout.")
1709         blay = elay + 1
1710         continue
1711       eert = find_end_of_inset(document.body, bert)
1712       if eert == -1:
1713         document.warning("Malformed LyX document: Can't find end of ERT.")
1714         blay = elay + 1
1715         continue
1716       
1717       # So the ERT inset begins at line k and goes to line l. We now wrap it in 
1718       # an argument inset.
1719       # Do the end first, so as not to mess up the variables.
1720       document.body[eert + 1:eert + 1] = ['', '\\end_layout', '', '\\end_inset', '']
1721       document.body[bert:bert] = ['\\begin_inset OptArg', 'status open', '', 
1722           '\\begin_layout Plain Layout']
1723       blay = elay + 9
1724
1725
1726 def revert_beamer_args(document):
1727   " Revert Beamer arguments to ERT "
1728   
1729   if document.textclass != "beamer" and document.textclass != "article-beamer":
1730     return
1731     
1732   layouts = ("Block", "ExampleBlock", "AlertBlock")
1733   for layout in layouts:
1734     blay = 0
1735     while True:
1736       blay = find_token(document.body, '\\begin_layout ' + layout, blay)
1737       if blay == -1:
1738         break
1739       elay = find_end_of(document.body, blay, '\\begin_layout', '\\end_layout')
1740       if elay == -1:
1741         document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1742         blay += 1
1743         continue
1744       bopt = find_token(document.body, '\\begin_inset OptArg', blay)
1745       if bopt == -1:
1746         # it is legal not to have one of these
1747         blay = elay + 1
1748         continue
1749       eopt = find_end_of_inset(document.body, bopt)
1750       if eopt == -1:
1751         document.warning("Malformed LyX document: Can't find end of argument.")
1752         blay = elay + 1
1753         continue
1754       bplay = find_token(document.body, '\\begin_layout Plain Layout', blay)
1755       if bplay == -1:
1756         document.warning("Malformed LyX document: Can't find plain layout.")
1757         blay = elay + 1
1758         continue
1759       eplay = find_end_of(document.body, bplay, '\\begin_layout', '\\end_layout')
1760       if eplay == -1:
1761         document.warning("Malformed LyX document: Can't find end of plain layout.")
1762         blay = elay + 1
1763         continue
1764       # So the content of the argument inset goes from bplay + 1 to eplay - 1
1765       bcont = bplay + 1
1766       if bcont >= eplay:
1767         # Hmm.
1768         document.warning(str(bcont) + " " + str(eplay))
1769         blay = blay + 1
1770         continue
1771       # we convert the content of the argument into pure LaTeX...
1772       content = lyx2latex(document, document.body[bcont:eplay])
1773       strlist = put_cmd_in_ert(["{" + content + "}"])
1774       
1775       # now replace the optional argument with the ERT
1776       document.body[bopt:eopt + 1] = strlist
1777       blay = blay + 1
1778
1779
1780 def revert_align_decimal(document):
1781   l = 0
1782   while True:
1783     l = document.body[l].find('alignment=decimal')
1784     if l == -1:
1785         break
1786     remove_option(document, l, 'decimal_point')
1787     document.body[l].replace('decimal', 'center')
1788
1789
1790 def convert_optarg(document):
1791   " Convert \\begin_inset OptArg to \\begin_inset Argument "
1792   i = 0
1793   while 1:
1794     i = find_token(document.body, '\\begin_inset OptArg', i)
1795     if i == -1:
1796       return
1797     document.body[i] = "\\begin_inset Argument"
1798     i += 1
1799
1800
1801 def revert_argument(document):
1802   " Convert \\begin_inset Argument to \\begin_inset OptArg "
1803   i = 0
1804   while 1:
1805     i = find_token(document.body, '\\begin_inset Argument', i)
1806     if i == -1:
1807       return
1808     document.body[i] = "\\begin_inset OptArg"
1809     i += 1
1810
1811
1812 def revert_makebox(document):
1813   " Convert \\makebox to TeX code "
1814   i = 0
1815   while 1:
1816     # only revert frameless boxes without an inner box
1817     i = find_token(document.body, '\\begin_inset Box Frameless', i)
1818     if i == -1:
1819       return
1820     z = find_end_of_inset(document.body, i)
1821     if z == -1:
1822       document.warning("Malformed LyX document: Can't find end of box inset.")
1823       return
1824     j = find_token(document.body, 'use_makebox 1', i)
1825     # assure we found the makebox of the current box
1826     if j > i + 7 or j == -1:
1827       return
1828     # remove the \end_inset
1829     document.body[z - 2:z + 1] = put_cmd_in_ert("}")
1830     # determine the alignment
1831     k = find_token(document.body, 'hor_pos', j - 4)
1832     align = document.body[k][9]
1833     # determine the width
1834     l = find_token(document.body, 'width "', j + 1)
1835     length = document.body[l][7:]
1836     # remove trailing '"'
1837     length = length[:-1]
1838     # latex_length returns "bool,length"
1839     length = latex_length(length).split(",")[1]
1840     subst = "\\makebox[" + length + "][" \
1841       + align + "]{"
1842     document.body[i:i + 13] = put_cmd_in_ert(subst)
1843     i += 1
1844
1845
1846 def revert_IEEEtran(document):
1847   " Convert IEEEtran layouts and styles to TeX code "
1848   if document.textclass != "IEEEtran":
1849     return
1850
1851   revert_flex_inset(document, "IEEE membership", "\\IEEEmembership", 0)
1852   revert_flex_inset(document, "Lowercase", "\\MakeLowercase", 0)
1853   revert_layout_command(document, "Special Paper Notice", "\\IEEEspecialpapernotice", 0)
1854   revert_layout_command(document, "After Title Text", "\\IEEEaftertitletext", 0)
1855   revert_layout_command(document, "Page headings", "\\markboth", 0)
1856   revert_layout_command(document, "Publication ID", "\\IEEEpubid", 0)
1857   note = '\\begin_layout Standard\n' \
1858          + '\\begin_inset Note Note\n' \
1859          + 'status open\n\n' \
1860          + '\\begin_layout Plain Layout\n\n' \
1861          + '\series bold\n' \
1862          + 'IMPORTANT NOTICE!!!:\n' \
1863          + '\\series default\n' \
1864          + ' \n' \
1865          + '\\color red\n' \
1866          + 'This document was created from a newer LyX version.\n' \
1867          + ' To be able to view/export it with LyX 1.6.x or earlier, the title and author\n' \
1868          + ' must be specified by using TeX code.\n' \
1869          + ' Do not use then the standard title and author environment!\n' \
1870          + '\\end_layout\n\n' \
1871          + '\\end_inset\n\n\n' \
1872          + '\\end_layout\n\n'
1873   # insert a note that title and author must be given in TeX code
1874   document.body.insert(0, note)
1875   # we need to revert title and author to TeX code to set \maketitle correctly
1876   revert_layout_command(document, "Title", "\\title", 0)
1877   revert_layout_command(document, "Author", "\\author", 0)
1878   # write \markboth code to the preamble
1879   insert_to_preamble(0, document,
1880                         '% Commands inserted by lyx2lyx\n'
1881                         + '% protect \\markboth against an old bug reintroduced in babel >= 3.8g\n'
1882                         + '\\let\\oldforeign@language\\foreign@language\n'
1883                         + '\\DeclareRobustCommand{\\foreign@language}[1]{%\n'
1884                         + '  \\lowercase{\\oldforeign@language{#1}}}\n')
1885   # set maketitle
1886   y = find_token(document.body, '\\begin_layout Abstract', 0)
1887   if y == -1:
1888     document.warning("Malformed LyX document: Can't find abstract of IEEEtran paper.")
1889     return
1890   maketitle = ['\\begin_layout Standard\n', \
1891               '\\begin_inset ERT\n', \
1892               'status collapsed\n', \
1893               '\\begin_layout Plain Layout\n', \
1894               '\\backslash\n', \
1895               'maketitle \n', \
1896               '\\end_layout\n', \
1897               '\\end_inset\n', \
1898               '\\end_layout\n']
1899   document.body[y:y] = maketitle
1900
1901
1902 ##
1903 # Conversion hub
1904 #
1905
1906 supported_versions = ["2.0.0","2.0"]
1907 convert = [[346, []],
1908            [347, []],
1909            [348, []],
1910            [349, []],
1911            [350, []],
1912            [351, []],
1913            [352, [convert_splitindex]],
1914            [353, []],
1915            [354, []],
1916            [355, []],
1917            [356, []],
1918            [357, []],
1919            [358, []],
1920            [359, [convert_nomencl_width]],
1921            [360, []],
1922            [361, []],
1923            [362, []],
1924            [363, []],
1925            [364, []],
1926            [365, []],
1927            [366, []],
1928            [367, []],
1929            [368, []],
1930            [369, [convert_author_id]],
1931            [370, []],
1932            [371, []],
1933            [372, []],
1934            [373, [merge_gbrief]],
1935            [374, []],
1936            [375, []],
1937            [376, []],
1938            [377, []],
1939            [378, []],
1940            [379, [convert_math_output]],
1941            [380, []],
1942            [381, []],
1943            [382, []],
1944            [383, []],
1945            [384, []],
1946            [385, []],
1947            [386, []],
1948            [387, []],
1949            [388, []],
1950            [389, [convert_html_quotes]],
1951            [390, []],
1952            [391, []],
1953            [392, [convert_beamer_args]],
1954            [393, [convert_optarg]],
1955            [394, []]
1956           ]
1957
1958 revert =  [[393, [revert_makebox]],
1959            [392, [revert_argument]],
1960            [391, [revert_beamer_args]],
1961            [390, [revert_align_decimal, revert_IEEEtran]],
1962            [389, [revert_output_sync]],
1963            [388, [revert_html_quotes]],
1964            [387, [revert_pagesizes]],
1965            [386, [revert_math_scale]],
1966            [385, [revert_lyx_version]],
1967            [384, [revert_shadedboxcolor]],
1968            [383, [revert_fontcolor]],
1969            [382, [revert_turkmen]],
1970            [381, [revert_notefontcolor]],
1971            [380, [revert_equalspacing_xymatrix]],
1972            [379, [revert_inset_preview]],
1973            [378, [revert_math_output]],
1974            [377, []],
1975            [376, [revert_multirow]],
1976            [375, [revert_includeall]],
1977            [374, [revert_includeonly]],
1978            [373, [revert_html_options]],
1979            [372, [revert_gbrief]],
1980            [371, [revert_fontenc]],
1981            [370, [revert_mhchem]],
1982            [369, [revert_suppress_date]],
1983            [368, [revert_author_id]],
1984            [367, [revert_hspace_glue_lengths]],
1985            [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
1986            [365, [revert_percent_skip_lengths]],
1987            [364, [revert_paragraph_indentation]],
1988            [363, [revert_branch_filename]],
1989            [362, [revert_longtable_align]],
1990            [361, [revert_applemac]],
1991            [360, []],
1992            [359, [revert_nomencl_cwidth]],
1993            [358, [revert_nomencl_width]],
1994            [357, [revert_custom_processors]],
1995            [356, [revert_ulinelatex]],
1996            [355, []],
1997            [354, [revert_strikeout]],
1998            [353, [revert_printindexall]],
1999            [352, [revert_subindex]],
2000            [351, [revert_splitindex]],
2001            [350, [revert_backgroundcolor]],
2002            [349, [revert_outputformat]],
2003            [348, [revert_xetex]],
2004            [347, [revert_phantom, revert_hphantom, revert_vphantom]],
2005            [346, [revert_tabularvalign]],
2006            [345, [revert_swiss]]
2007           ]
2008
2009
2010 if __name__ == "__main__":
2011     pass