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