]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_0.py
These commands should just take some lines.
[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) 2010 The LyX team
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, \
27   find_end_of_inset, find_end_of_layout, find_token_backwards, \
28   is_in_inset, get_value, get_quoted_value, del_token
29   
30 from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
31   put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
32   revert_font_attrs, hex2ratio, str2bool
33
34 ####################################################################
35 # Private helper functions
36
37 def remove_option(lines, m, option):
38     ''' removes option from line m. returns whether we did anything '''
39     l = lines[m].find(option)
40     if l == -1:
41         return False
42     val = lines[m][l:].split('"')[1]
43     lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
44     return True
45
46
47 # DO NOT USE THIS ROUTINE ANY MORE. Better yet, replace the uses that
48 # have been made of it with uses of put_cmd_in_ert.
49 def old_put_cmd_in_ert(string):
50     for rep in unicode_reps:
51         string = string.replace(rep[1], rep[0].replace('\\\\', '\\'))
52     string = string.replace('\\', "\\backslash\n")
53     string = "\\begin_inset ERT\nstatus collapsed\n\\begin_layout Plain Layout\n" \
54       + string + "\n\\end_layout\n\\end_inset"
55     return string
56
57
58 ###############################################################################
59 ###
60 ### Conversion and reversion routines
61 ###
62 ###############################################################################
63
64 def revert_swiss(document):
65     " Set language german-ch to ngerman "
66     i = 0
67     if document.language == "german-ch":
68         document.language = "ngerman"
69         i = find_token(document.header, "\\language", 0)
70         if i != -1:
71             document.header[i] = "\\language ngerman"
72     j = 0
73     while True:
74         j = find_token(document.body, "\\lang german-ch", j)
75         if j == -1:
76             return
77         document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
78         j = j + 1
79
80
81 def revert_tabularvalign(document):
82    " Revert the tabular valign option "
83    i = 0
84    while True:
85       i = find_token(document.body, "\\begin_inset Tabular", i)
86       if i == -1:
87           return
88       end = find_end_of_inset(document.body, i)
89       if end == -1:
90           document.warning("Can't find end of inset at line " + str(i))
91           i += 1
92           continue
93       fline = find_token(document.body, "<features", i, end)
94       if fline == -1:
95           document.warning("Can't find features for inset at line " + str(i))
96           i += 1
97           continue
98       p = document.body[fline].find("islongtable")
99       if p != -1:
100           q = document.body[fline].find("tabularvalignment")
101           if q != -1:
102               # FIXME
103               # This seems wrong: It removes everything after 
104               # tabularvalignment, too.
105               document.body[fline] = document.body[fline][:q - 1] + '>'
106           i += 1
107           continue
108
109        # no longtable
110       tabularvalignment = 'c'
111       # which valignment is specified?
112       m = document.body[fline].find('tabularvalignment="top"')
113       if m != -1:
114           tabularvalignment = 't'
115       m = document.body[fline].find('tabularvalignment="bottom"')
116       if m != -1:
117           tabularvalignment = 'b'
118       # delete tabularvalignment
119       q = document.body[fline].find("tabularvalignment")
120       if q != -1:
121           # FIXME
122           # This seems wrong: It removes everything after 
123           # tabularvalignment, too.
124           document.body[fline] = document.body[fline][:q - 1] + '>'
125
126       # don't add a box when centered
127       if tabularvalignment == 'c':
128           i = end
129           continue
130       subst = ['\\end_layout', '\\end_inset']
131       document.body[end:end] = subst # just inserts those lines
132       subst = ['\\begin_inset Box Frameless',
133           'position "' + tabularvalignment +'"',
134           'hor_pos "c"',
135           'has_inner_box 1',
136           'inner_pos "c"',
137           'use_parbox 0',
138           # we don't know the width, assume 50%
139           'width "50col%"',
140           'special "none"',
141           'height "1in"',
142           'height_special "totalheight"',
143           'status open',
144           '',
145           '\\begin_layout Plain Layout']
146       document.body[i:i] = subst # this just inserts the array at i
147       # since there could be a tabular inside a tabular, we cannot
148       # jump to end
149       i += len(subst)
150
151
152 def revert_phantom_types(document, ptype, cmd):
153     " Reverts phantom to ERT "
154     i = 0
155     while True:
156       i = find_token(document.body, "\\begin_inset Phantom " + ptype, i)
157       if i == -1:
158           return
159       end = find_end_of_inset(document.body, i)
160       if end == -1:
161           document.warning("Can't find end of inset at line " + str(i))
162           i += 1
163           continue
164       blay = find_token(document.body, "\\begin_layout Plain Layout", i, end)
165       if blay == -1:
166           document.warning("Can't find layout for inset at line " + str(i))
167           i = end
168           continue
169       bend = find_token(document.body, "\\end_layout", blay, end)
170       if bend == -1:
171           document.warning("Malformed LyX document: Could not find end of Phantom inset's layout.")
172           i = end
173           continue
174       substi = ["\\begin_inset ERT", "status collapsed", "",
175                 "\\begin_layout Plain Layout", "", "", "\\backslash", 
176                 cmd + "{", "\\end_layout", "", "\\end_inset"]
177       substj = ["\\size default", "", "\\begin_inset ERT", "status collapsed", "",
178                 "\\begin_layout Plain Layout", "", "}", "\\end_layout", "", "\\end_inset"]
179       # do the later one first so as not to mess up the numbering
180       document.body[bend:end + 1] = substj
181       document.body[i:blay + 1] = substi
182       i = end + len(substi) + len(substj) - (end - bend) - (blay - i) - 2
183
184
185 def revert_phantom(document):
186     revert_phantom_types(document, "Phantom", "phantom")
187     
188 def revert_hphantom(document):
189     revert_phantom_types(document, "HPhantom", "hphantom")
190
191 def revert_vphantom(document):
192     revert_phantom_types(document, "VPhantom", "vphantom")
193
194
195 def revert_xetex(document):
196     " Reverts documents that use XeTeX "
197
198     i = find_token(document.header, '\\use_xetex', 0)
199     if i == -1:
200         document.warning("Malformed LyX document: Missing \\use_xetex.")
201         return
202     if not str2bool(get_value(document.header, "\\use_xetex", i)):
203         del document.header[i]
204         return
205     del document.header[i]
206
207     # 1.) set doc encoding to utf8-plain
208     i = find_token(document.header, "\\inputencoding", 0)
209     if i == -1:
210         document.warning("Malformed LyX document: Missing \\inputencoding.")
211     else:
212         document.header[i] = "\\inputencoding utf8-plain"
213
214     # 2.) check font settings
215     # defaults
216     roman = sans = typew = default
217     osf = False
218     sf_scale = tt_scale = 100.0
219     
220     i = find_token(document.header, "\\font_roman", 0)
221     if i == -1:
222         document.warning("Malformed LyX document: Missing \\font_roman.")
223     else:
224         roman = get_value(document.header, "\\font_roman", i)
225         document.header[i] = "\\font_roman default"
226
227     i = find_token(document.header, "\\font_sans", 0)
228     if i == -1:
229         document.warning("Malformed LyX document: Missing \\font_sans.")
230     else:
231         sans = get_value(document.header, "\\font_sans", i)
232         document.header[i] = "\\font_sans default"
233     
234     i = find_token(document.header, "\\font_typewriter", 0)
235     if i == -1:
236         document.warning("Malformed LyX document: Missing \\font_typewriter.")
237     else:
238         typew = get_value(document.header, "\\font_typewriter", i)
239         document.header[i] = "\\font_typewriter default"
240
241     i = find_token(document.header, "\\font_osf", 0)
242     if i == -1:
243         document.warning("Malformed LyX document: Missing \\font_osf.")
244     else:
245         osf = str2bool(get_value(document.header, "\\font_osf", i))
246         document.header[i] = "\\font_osf false"
247
248     i = find_token(document.header, "\\font_sc", 0)
249     if i == -1:
250         document.warning("Malformed LyX document: Missing \\font_sc.")
251     else:
252         # we do not need this value.
253         document.header[i] = "\\font_sc false"
254     
255     i = find_token(document.header, "\\font_sf_scale", 0)
256     if i == -1:
257         document.warning("Malformed LyX document: Missing \\font_sf_scale.")
258     else:
259       val = get_value(document.header, '\\font_sf_scale', i)
260       try:
261         # float() can throw
262         sf_scale = float(val)
263       except:
264         document.warning("Invalid font_sf_scale value: " + val)
265       document.header[i] = "\\font_sf_scale 100"
266
267     i = find_token(document.header, "\\font_tt_scale", 0)
268     if i == -1:
269         document.warning("Malformed LyX document: Missing \\font_tt_scale.")
270     else:
271         val = get_value(document.header, '\\font_tt_scale', i)
272         try:
273           # float() can throw
274           tt_scale = float(val)
275         except:
276           document.warning("Invalid font_tt_scale value: " + val)
277         document.header[i] = "\\font_tt_scale 100"
278
279     # 3.) set preamble stuff
280     pretext = ['%% This document must be processed with xelatex!']
281     pretext.append('\\usepackage{fontspec}')
282     if roman != "default":
283         pretext.append('\\setmainfont[Mapping=tex-text]{' + roman + '}')
284     if sans != "default":
285         sf = '\\setsansfont['
286         if sf_scale != 100.0:
287             sf += 'Scale=' + str(sf_scale / 100.0) + ','
288         sf += 'Mapping=tex-text]{' + sans + '}'
289         pretext.append(sf)
290     if typewriter != "default":
291         tw = '\\setmonofont'
292         if tt_scale != 100.0:
293             tw += '[Scale=' + str(tt_scale / 100.0) + ']'
294         tw += '{' + typewriter + '}'
295         pretext.append(tw)
296     if osf:
297         pretext.append('\\defaultfontfeatures{Numbers=OldStyle}')
298     pretext.append('\usepackage{xunicode}')
299     pretext.append('\usepackage{xltxtra}')
300     insert_to_preamble(0, document, pretext)
301
302
303 def revert_outputformat(document):
304     " Remove default output format param "
305     
306     if not del_token(document.header, '\\default_output_format', 0):
307         document.warning("Malformed LyX document: Missing \\default_output_format.")
308
309
310 def revert_backgroundcolor(document):
311     " Reverts background color to preamble code "
312     i = find_token(document.header, "\\backgroundcolor", 0)
313     if i == -1:
314         return
315     colorcode = get_value(document.header, '\\backgroundcolor', i)
316     del document.header[i]
317     # don't clutter the preamble if backgroundcolor is not set
318     if colorcode == "#ffffff":
319         return
320     red   = hex2ratio(colorcode[1:3])
321     green = hex2ratio(colorcode[3:5])
322     blue  = hex2ratio(colorcode[5:7])
323     insert_to_preamble(0, document,
324                           '% Commands inserted by lyx2lyx to set the background color\n'
325                           + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
326                           + '\\definecolor{page_backgroundcolor}{rgb}{'
327                           + red + ',' + green + ',' + blue + '}\n'
328                           + '\\pagecolor{page_backgroundcolor}\n')
329
330
331 def revert_splitindex(document):
332     " Reverts splitindex-aware documents "
333     i = find_token(document.header, '\\use_indices', 0)
334     if i == -1:
335         document.warning("Malformed LyX document: Missing \\use_indices.")
336         return
337     useindices = str2bool(get_value(document.header, "\\use_indices", i))
338     del document.header[i]
339     preamble = []
340     if useindices:
341          preamble.append("\\usepackage{splitidx})")
342     
343     # deal with index declarations in the preamble
344     i = 0
345     while True:
346         i = find_token(document.header, "\\index", i)
347         if i == -1:
348             break
349         k = find_token(document.header, "\\end_index", i)
350         if k == -1:
351             document.warning("Malformed LyX document: Missing \\end_index.")
352             return
353         if useindices:    
354           line = document.header[i]
355           l = re.compile(r'\\index (.*)$')
356           m = l.match(line)
357           iname = m.group(1)
358           ishortcut = get_value(document.header, '\\shortcut', i, k)
359           if ishortcut != "":
360               preamble.append("\\newindex[" + iname + "]{" + ishortcut + "}")
361         del document.header[i:k + 1]
362     if preamble:
363         insert_to_preamble(0, document, preamble)
364         
365     # deal with index insets
366     # these need to have the argument removed
367     i = 0
368     while True:
369         i = find_token(document.body, "\\begin_inset Index", i)
370         if i == -1:
371             break
372         line = document.body[i]
373         l = re.compile(r'\\begin_inset Index (.*)$')
374         m = l.match(line)
375         itype = m.group(1)
376         if itype == "idx" or indices == "false":
377             document.body[i] = "\\begin_inset Index"
378         else:
379             k = find_end_of_inset(document.body, i)
380             if k == -1:
381                 document.warning("Can't find end of index inset!")
382                 i += 1
383                 continue
384             content = lyx2latex(document, document.body[i:k])
385             # escape quotes
386             content = content.replace('"', r'\"')
387             subst = put_cmd_in_ert("\\sindex[" + itype + "]{" + content + "}")
388             document.body[i:k + 1] = subst
389         i = i + 1
390         
391     # deal with index_print insets
392     i = 0
393     while True:
394         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
395         if i == -1:
396             return
397         k = find_end_of_inset(document.body, i)
398         ptype = get_quoted_value(document.body, 'type', i, k)
399         if ptype == "idx":
400             j = find_token(document.body, "type", i, k)
401             del document.body[j]
402         elif not useindices:
403             del document.body[i:k + 1]
404         else:
405             subst = put_cmd_in_ert("\\printindex[" + ptype + "]{}")
406             document.body[i:k + 1] = subst
407         i = i + 1
408
409
410 def convert_splitindex(document):
411     " Converts index and printindex insets to splitindex-aware format "
412     i = 0
413     while True:
414         i = find_token(document.body, "\\begin_inset Index", i)
415         if i == -1:
416             break
417         document.body[i] = document.body[i].replace("\\begin_inset Index",
418             "\\begin_inset Index idx")
419         i = i + 1
420     i = 0
421     while True:
422         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
423         if i == -1:
424             return
425         if document.body[i + 1].find('LatexCommand printindex') == -1:
426             document.warning("Malformed LyX document: Incomplete printindex inset.")
427             return
428         subst = ["LatexCommand printindex", 
429             "type \"idx\""]
430         document.body[i + 1:i + 2] = subst
431         i = i + 1
432
433
434 def revert_subindex(document):
435     " Reverts \\printsubindex CommandInset types "
436     i = find_token(document.header, '\\use_indices', 0)
437     if i == -1:
438         document.warning("Malformed LyX document: Missing \\use_indices.")
439         return
440     useindices = str2bool(get_value(document.header, "\\use_indices", i))
441     i = 0
442     while True:
443         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
444         if i == -1:
445             return
446         k = find_end_of_inset(document.body, i)
447         ctype = get_value(document.body, 'LatexCommand', i, k)
448         if ctype != "printsubindex":
449             i = k + 1
450             continue
451         ptype = get_quoted_value(document.body, 'type', i, k)
452         if not useindices:
453             del document.body[i:k + 1]
454         else:
455             subst = put_cmd_in_ert("\\printsubindex[" + ptype + "]{}")
456             document.body[i:k + 1] = subst
457         i = i + 1
458
459
460 def revert_printindexall(document):
461     " Reverts \\print[sub]index* CommandInset types "
462     i = find_token(document.header, '\\use_indices', 0)
463     if i == -1:
464         document.warning("Malformed LyX document: Missing \\use_indices.")
465         return
466     useindices = str2bool(get_value(document.header, "\\use_indices", i))
467     i = 0
468     while True:
469         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
470         if i == -1:
471             return
472         k = find_end_of_inset(document.body, i)
473         ctype = get_value(document.body, 'LatexCommand', i, k)
474         if ctype != "printindex*" and ctype != "printsubindex*":
475             i = k
476             continue
477         if not useindices:
478             del document.body[i:k + 1]
479         else:
480             subst = put_cmd_in_ert("\\" + ctype + "{}")
481             document.body[i:k + 1] = subst
482         i = i + 1
483
484
485 def revert_strikeout(document):
486   " Reverts \\strikeout font attribute "
487   changed = revert_font_attrs(document.body, "\\uuline", "\\uuline")
488   changed = revert_font_attrs(document.body, "\\uwave", "\\uwave") or changed
489   changed = revert_font_attrs(document.body, "\\strikeout", "\\sout")  or changed
490   if changed == True:
491     insert_to_preamble(0, document,
492         '% Commands inserted by lyx2lyx for proper underlining\n'
493         + '\\PassOptionsToPackage{normalem}{ulem}\n'
494         + '\\usepackage{ulem}\n')
495
496
497 def revert_ulinelatex(document):
498     " Reverts \\uline font attribute "
499     i = find_token(document.body, '\\bar under', 0)
500     if i == -1:
501         return
502     insert_to_preamble(0, document,
503             '% Commands inserted by lyx2lyx for proper underlining\n'
504             + '\\PassOptionsToPackage{normalem}{ulem}\n'
505             + '\\usepackage{ulem}\n'
506             + '\\let\\cite@rig\\cite\n'
507             + '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}\n'
508             + '  \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}\n'
509             + '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}\n')
510
511
512 def revert_custom_processors(document):
513     " Remove bibtex_command and index_command params "
514     
515     if not del_token(document.header, '\\bibtex_command', 0):
516         document.warning("Malformed LyX document: Missing \\bibtex_command.")
517     
518     if not del_token(document.header, '\\index_command', 0):
519         document.warning("Malformed LyX document: Missing \\index_command.")
520
521
522 def convert_nomencl_width(document):
523     " Add set_width param to nomencl_print "
524     i = 0
525     while True:
526       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
527       if i == -1:
528         break
529       document.body.insert(i + 2, "set_width \"none\"")
530       i = i + 1
531
532
533 def revert_nomencl_width(document):
534     " Remove set_width param from nomencl_print "
535     i = 0
536     while True:
537       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
538       if i == -1:
539         break
540       j = find_end_of_inset(document.body, i)
541       if not del_token(document.body, "set_width", i, j):
542             document.warning("Can't find set_width option for nomencl_print!")
543       i = j
544
545
546 def revert_nomencl_cwidth(document):
547     " Remove width param from nomencl_print "
548     i = 0
549     while True:
550       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
551       if i == -1:
552         break
553       j = find_end_of_inset(document.body, i)
554       l = find_token(document.body, "width", i, j)
555       if l == -1:
556         document.warning("Can't find width option for nomencl_print!")
557         i = j
558         continue
559       width = get_quoted_value(document.body, "width", i, j)
560       del document.body[l]
561       add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
562       add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
563       i = j - 1
564
565
566 def revert_applemac(document):
567     " Revert applemac encoding to auto "
568     if document.encoding != "applemac":
569       return
570     document.encoding = "auto"
571     i = find_token(document.header, "\\encoding", 0)
572     if i != -1:
573         document.header[i] = "\\encoding auto"
574
575
576 def revert_longtable_align(document):
577     " Remove longtable alignment setting "
578     i = 0
579     while True:
580       i = find_token(document.body, "\\begin_inset Tabular", i)
581       if i == -1:
582           break
583       end = find_end_of_inset(document.body, i)
584       if end == -1:
585           document.warning("Can't find end of inset at line " + str(i))
586           i += 1
587           continue
588       fline = find_token(document.body, "<features", i, end)
589       if fline == -1:
590           document.warning("Can't find features for inset at line " + str(i))
591           i += 1
592           continue
593       j = document.body[fline].find("longtabularalignment")
594       if j == -1:
595           i += 1
596           continue
597       # FIXME Is this correct? It wipes out everything after the 
598       # one we found.
599       document.body[fline] = document.body[fline][:j - 1] + '>'
600       # since there could be a tabular inside this one, we 
601       # cannot jump to end.
602       i += 1
603
604
605 def revert_branch_filename(document):
606     " Remove \\filename_suffix parameter from branches "
607     i = 0
608     while True:
609         i = find_token(document.header, "\\filename_suffix", i)
610         if i == -1:
611             return
612         del document.header[i]
613
614
615 def revert_paragraph_indentation(document):
616     " Revert custom paragraph indentation to preamble code "
617     i = find_token(document.header, "\\paragraph_indentation", 0)
618     if i == -1:
619       return
620     length = get_value(document.header, "\\paragraph_indentation", i)
621     # we need only remove the line if indentation is default
622     if length != "default":
623       # handle percent lengths
624       length = latex_length(length)[1]
625       add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
626       add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
627     del document.header[i]
628
629
630 def revert_percent_skip_lengths(document):
631     " Revert relative lengths for paragraph skip separation to preamble code "
632     i = find_token(document.header, "\\defskip", 0)
633     if i == -1:
634         return
635     length = get_value(document.header, "\\defskip", i)
636     # only revert when a custom length was set and when
637     # it used a percent length
638     if length in ('smallskip', 'medskip', 'bigskip'):
639         return
640     # handle percent lengths
641     percent, length = latex_length(length)
642     if percent:
643         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
644         add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
645         # set defskip to medskip as default
646         document.header[i] = "\\defskip medskip"
647
648
649 def revert_percent_vspace_lengths(document):
650     " Revert relative VSpace lengths to ERT "
651     i = 0
652     while True:
653       i = find_token(document.body, "\\begin_inset VSpace", i)
654       if i == -1:
655           break
656       # only revert if a custom length was set and if
657       # it used a percent length
658       r = re.compile(r'\\begin_inset VSpace (.*)$')
659       m = r.match(document.body[i])
660       length = m.group(1)
661       if length in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
662          i += 1
663          continue
664       # check if the space has a star (protected space)
665       protected = (document.body[i].rfind("*") != -1)
666       if protected:
667           length = length.rstrip('*')
668       # handle percent lengths
669       percent, length = latex_length(length)
670       # revert the VSpace inset to ERT
671       if percent:
672           if protected:
673               subst = put_cmd_in_ert("\\vspace*{" + length + "}")
674           else:
675               subst = put_cmd_in_ert("\\vspace{" + length + "}")
676           document.body[i:i + 2] = subst
677       i += 1
678
679
680 def revert_percent_hspace_lengths(document):
681     " Revert relative HSpace lengths to ERT "
682     i = 0
683     while True:
684       i = find_token(document.body, "\\begin_inset space \\hspace", i)
685       if i == -1:
686           break
687       j = find_end_of_inset(document.body, i)
688       if j == -1:
689           document.warning("Can't find end of inset at line " + str(i))
690           i += 1
691           continue
692       # only revert if a custom length was set...
693       length = get_value(document.body, '\\length', i + 1, j)
694       if length == '':
695           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
696           i = j
697           continue
698       protected = ""
699       if document.body[i].find("\\hspace*{}") != -1:
700           protected = "*"
701       # ...and if it used a percent length
702       percent, length = latex_length(length)
703       # revert the HSpace inset to ERT
704       if percent:
705           subst = put_cmd_in_ert("\\hspace" + protected + "{" + length + "}")
706           document.body[i:j + 1] = subst
707       # if we did a substitution, this will still be ok
708       i = j
709
710
711 def revert_hspace_glue_lengths(document):
712     " Revert HSpace glue lengths to ERT "
713     i = 0
714     while True:
715       i = find_token(document.body, "\\begin_inset space \\hspace", i)
716       if i == -1:
717           break
718       j = find_end_of_inset(document.body, i)
719       if j == -1:
720           document.warning("Can't find end of inset at line " + str(i))
721           i += 1
722           continue
723       length = get_value(document.body, '\\length', i + 1, j)
724       if length == '':
725           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
726           i = j
727           continue
728       protected = ""
729       if document.body[i].find("\\hspace*{}") != -1:
730           protected = "*"
731       # only revert if the length contains a plus or minus at pos != 0
732       if length.find('-',1) != -1 or length.find('+',1) != -1:
733           # handle percent lengths
734           length = latex_length(length)[1]
735           # revert the HSpace inset to ERT
736           subst = put_cmd_in_ert("\\hspace" + protected + "{" + length + "}")
737           document.body[i:j+1] = subst
738       i = j
739
740
741 def convert_author_id(document):
742     " Add the author_id to the \\author definition and make sure 0 is not used"
743     i = 0
744     anum = 1
745     re_author = re.compile(r'(\\author) (\".*\")\s*(.*)$')
746     
747     while True:
748         i = find_token(document.header, "\\author", i)
749         if i == -1:
750             break
751         m = re_author.match(document.header[i])
752         if m:
753             name = m.group(2)
754             email = m.group(3)
755             document.header[i] = "\\author %i %s %s" % (anum, name, email)
756         # FIXME Should this really be incremented if we didn't match?
757         anum += 1
758         i += 1
759         
760     i = 0
761     while True:
762         i = find_token(document.body, "\\change_", i)
763         if i == -1:
764             break
765         change = document.body[i].split(' ');
766         if len(change) == 3:
767             type = change[0]
768             author_id = int(change[1])
769             time = change[2]
770             document.body[i] = "%s %i %s" % (type, author_id + 1, time)
771         i += 1
772
773
774 def revert_author_id(document):
775     " Remove the author_id from the \\author definition "
776     i = 0
777     anum = 0
778     rx = re.compile(r'(\\author)\s+(\d+)\s+(\".*\")\s*(.*)$')
779     idmap = dict()
780
781     while True:
782         i = find_token(document.header, "\\author", i)
783         if i == -1:
784             break
785         m = rx.match(document.header[i])
786         if m:
787             author_id = int(m.group(2))
788             idmap[author_id] = anum
789             name = m.group(3)
790             email = m.group(4)
791             document.header[i] = "\\author %s %s" % (name, email)
792         i += 1
793         # FIXME Should this be incremented if we didn't match?
794         anum += 1
795
796     i = 0
797     while True:
798         i = find_token(document.body, "\\change_", i)
799         if i == -1:
800             break
801         change = document.body[i].split(' ');
802         if len(change) == 3:
803             type = change[0]
804             author_id = int(change[1])
805             time = change[2]
806             document.body[i] = "%s %i %s" % (type, idmap[author_id], time)
807         i += 1
808
809
810 def revert_suppress_date(document):
811     " Revert suppressing of default document date to preamble code "
812     i = find_token(document.header, "\\suppress_date", 0)
813     if i == -1:
814         return
815     # remove the preamble line and write to the preamble
816     # when suppress_date was true
817     date = str2bool(get_value(document.header, "\\suppress_date", i))
818     if date:
819         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
820         add_to_preamble(document, ["\\date{}"])
821     del document.header[i]
822
823
824 def revert_mhchem(document):
825     "Revert mhchem loading to preamble code"
826
827     mhchem = "off"
828     i = find_token(document.header, "\\use_mhchem", 0)
829     if i == -1:
830         document.warning("Malformed LyX document: Could not find mhchem setting.")
831         mhchem = "auto"
832     else:
833         val = get_value(document.header, "\\use_mhchem", i)
834         if val == "1":
835             mhchem = "auto"
836         elif val == "2":
837             mhchem = "on"
838         del document.header[i]
839
840     if mhchem == "off":
841       # don't load case
842       return 
843
844     if mhchem == "auto":
845         i = 0
846         while True:
847             i = find_token(document.body, "\\begin_inset Formula", i)
848             if i == -1:
849                break
850             line = document.body[i]
851             if line.find("\\ce{") != -1 or line.find("\\cf{") != -1:
852               mhchem = "on"
853               break
854             i += 1
855
856     if mhchem == "on":
857         pre = ["% lyx2lyx mhchem commands", 
858           "\\PassOptionsToPackage{version=3}{mhchem}", 
859           "\\usepackage{mhchem}"]
860         add_to_preamble(document, pre) 
861
862
863 def revert_fontenc(document):
864     " Remove fontencoding param "
865     if not del_token(document.header, '\\fontencoding', 0):
866         document.warning("Malformed LyX document: Missing \\fontencoding.")
867
868
869 def merge_gbrief(document):
870     " Merge g-brief-en and g-brief-de to one class "
871
872     if document.textclass != "g-brief-de":
873         if document.textclass == "g-brief-en":
874             document.textclass = "g-brief"
875             document.set_textclass()
876         return
877
878     obsoletedby = { "Brieftext":       "Letter",
879                     "Unterschrift":    "Signature",
880                     "Strasse":         "Street",
881                     "Zusatz":          "Addition",
882                     "Ort":             "Town",
883                     "Land":            "State",
884                     "RetourAdresse":   "ReturnAddress",
885                     "MeinZeichen":     "MyRef",
886                     "IhrZeichen":      "YourRef",
887                     "IhrSchreiben":    "YourMail",
888                     "Telefon":         "Phone",
889                     "BLZ":             "BankCode",
890                     "Konto":           "BankAccount",
891                     "Postvermerk":     "PostalComment",
892                     "Adresse":         "Address",
893                     "Datum":           "Date",
894                     "Betreff":         "Reference",
895                     "Anrede":          "Opening",
896                     "Anlagen":         "Encl.",
897                     "Verteiler":       "cc",
898                     "Gruss":           "Closing"}
899     i = 0
900     while 1:
901         i = find_token(document.body, "\\begin_layout", i)
902         if i == -1:
903             break
904
905         layout = document.body[i][14:]
906         if layout in obsoletedby:
907             document.body[i] = "\\begin_layout " + obsoletedby[layout]
908
909         i += 1
910         
911     document.textclass = "g-brief"
912     document.set_textclass()
913
914
915 def revert_gbrief(document):
916     " Revert g-brief to g-brief-en "
917     if document.textclass == "g-brief":
918         document.textclass = "g-brief-en"
919         document.set_textclass()
920
921
922 def revert_html_options(document):
923     " Remove html options "
924     del_token(document.header, '\\html_use_mathml', 0)
925     del_token(document.header, '\\html_be_strict', 0)
926
927
928 def revert_includeonly(document):
929     i = 0
930     while True:
931         i = find_token(document.header, "\\begin_includeonly", i)
932         if i == -1:
933             return
934         j = find_end_of(document.header, i, "\\begin_includeonly", "\\end_includeonly")
935         if j == -1:
936             document.warning("Unable to find end of includeonly section!!")
937             break
938         document.header[i : j + 1] = []
939
940
941 def revert_includeall(document):
942     " Remove maintain_unincluded_children param "
943     del_token(document.header, '\\maintain_unincluded_children', 0)
944
945
946 def revert_multirow(document):
947     " Revert multirow cells in tables to TeX-code"
948     i = 0
949     multirow = False
950     while True:
951       # cell type 3 is multirow begin cell
952       i = find_token(document.body, '<cell multirow="3"', i)
953       if i == -1:
954           break
955       # a multirow cell was found
956       multirow = True
957       # remove the multirow tag, set the valignment to top
958       # and remove the bottom line
959       # FIXME Are we sure these always have space around them?
960       document.body[i] = document.body[i].replace(' multirow="3" ', ' ')
961       document.body[i] = document.body[i].replace('valignment="middle"', 'valignment="top"')
962       document.body[i] = document.body[i].replace(' bottomline="true" ', ' ')
963       # write ERT to create the multirow cell
964       # use 2 rows and 2cm as default with because the multirow span
965       # and the column width is only hardly accessible
966       cend = find_token(document.body, "</cell>", i)
967       if cend == -1:
968           document.warning("Malformed LyX document: Could not find end of tabular cell.")
969           i += 1
970           continue
971       blay = find_token(document.body, "\\begin_layout", i, cend)
972       if blay == -1:
973           document.warning("Can't find layout for cell!")
974           i = j
975           continue
976       bend = find_end_of_layout(document.body, blay)
977       if blay == -1:
978           document.warning("Can't find end of layout for cell!")
979           i = cend
980           continue
981
982       # do the later one first, so as not to mess up the numbering
983       # we are wrapping the whole cell in this ert
984       # so before the end of the layout...
985       document.body[bend:bend] = put_cmd_in_ert("}")
986       # ...and after the beginning
987       document.body[blay+1:blay+1] = put_cmd_in_ert("\\multirow{2}{2cm}{")
988
989       while True:
990           # cell type 4 is multirow part cell
991           k = find_token(document.body, '<cell multirow="4"', cend)
992           if k == -1:
993               break
994           # remove the multirow tag, set the valignment to top
995           # and remove the top line
996           # FIXME Are we sure these always have space around them?
997           document.body[k] = document.body[k].replace(' multirow="4" ', ' ')
998           document.body[k] = document.body[k].replace('valignment="middle"', 'valignment="top"')
999           document.body[k] = document.body[k].replace(' topline="true" ', ' ')
1000           k += 1
1001       # this will always be ok
1002       i = cend
1003
1004     if multirow == True:
1005         add_to_preamble(document, 
1006           ["% lyx2lyx multirow additions ", "\\usepackage{multirow}"])
1007
1008
1009 def convert_math_output(document):
1010     " Convert \html_use_mathml to \html_math_output "
1011     i = find_token(document.header, "\\html_use_mathml", 0)
1012     if i == -1:
1013         return
1014     rgx = re.compile(r'\\html_use_mathml\s+(\w+)')
1015     m = rgx.match(document.header[i])
1016     newval = "0" # MathML
1017     if m:
1018       val = str2bool(m.group(1))
1019       if not val:
1020         newval = "2" # Images
1021     else:
1022       document.warning("Can't match " + document.header[i])
1023     document.header[i] = "\\html_math_output " + newval
1024
1025
1026 def revert_math_output(document):
1027     " Revert \html_math_output to \html_use_mathml "
1028     i = find_token(document.header, "\\html_math_output", 0)
1029     if i == -1:
1030         return
1031     rgx = re.compile(r'\\html_math_output\s+(\d)')
1032     m = rgx.match(document.header[i])
1033     newval = "true"
1034     if m:
1035         val = m.group(1)
1036         if val == "1" or val == "2":
1037             newval = "false"
1038     else:
1039         document.warning("Unable to match " + document.header[i])
1040     document.header[i] = "\\html_use_mathml " + newval
1041                 
1042
1043
1044 def revert_inset_preview(document):
1045     " Dissolves the preview inset "
1046     i = 0
1047     while True:
1048       i = find_token(document.body, "\\begin_inset Preview", i)
1049       if i == -1:
1050           return
1051       iend = find_end_of_inset(document.body, i)
1052       if iend == -1:
1053           document.warning("Malformed LyX document: Could not find end of Preview inset.")
1054           i += 1
1055           continue
1056       
1057       # This has several issues.
1058       # We need to do something about the layouts inside InsetPreview.
1059       # If we just leave the first one, then we have something like:
1060       # \begin_layout Standard
1061       # ...
1062       # \begin_layout Standard
1063       # and we get a "no \end_layout" error. So something has to be done.
1064       # Ideally, we would check if it is the same as the layout we are in.
1065       # If so, we just remove it; if not, we end the active one. But it is 
1066       # not easy to know what layout we are in, due to depth changes, etc,
1067       # and it is not clear to me how much work it is worth doing. In most
1068       # cases, the layout will probably be the same.
1069       # 
1070       # For the same reason, we have to remove the \end_layout tag at the
1071       # end of the last layout in the inset. Again, that will sometimes be
1072       # wrong, but it will usually be right. To know what to do, we would
1073       # again have to know what layout the inset is in.
1074       
1075       blay = find_token(document.body, "\\begin_layout", i, iend)
1076       if blay == -1:
1077           document.warning("Can't find layout for preview inset!")
1078           # always do the later one first...
1079           del document.body[iend]
1080           del document.body[i]
1081           # deletions mean we do not need to reset i
1082           continue
1083
1084       # This is where we would check what layout we are in.
1085       # The check for Standard is definitely wrong.
1086       # 
1087       # lay = document.body[blay].split(None, 1)[1]
1088       # if lay != oldlayout:
1089       #     # record a boolean to tell us what to do later....
1090       #     # better to do it later, since (a) it won't mess up
1091       #     # the numbering and (b) we only modify at the end.
1092         
1093       # we want to delete the last \\end_layout in this inset, too.
1094       # note that this may not be the \\end_layout that goes with blay!!
1095       bend = find_end_of_layout(document.body, blay)
1096       while True:
1097           tmp = find_token(document.body, "\\end_layout", bend + 1, iend)
1098           if tmp == -1:
1099               break
1100           bend = tmp
1101       if bend == blay:
1102           document.warning("Unable to find last layout in preview inset!")
1103           del document.body[iend]
1104           del document.body[i]
1105           # deletions mean we do not need to reset i
1106           continue
1107       # always do the later one first...
1108       del document.body[iend]
1109       del document.body[bend]
1110       del document.body[i:blay + 1]
1111       # we do not need to reset i
1112                 
1113
1114 def revert_equalspacing_xymatrix(document):
1115     " Revert a Formula with xymatrix@! to an ERT inset "
1116     i = 0
1117     has_preamble = False
1118     has_equal_spacing = False
1119
1120     while True:
1121       i = find_token(document.body, "\\begin_inset Formula", i)
1122       if i == -1:
1123           break
1124       j = find_end_of_inset(document.body, i)
1125       if j == -1:
1126           document.warning("Malformed LyX document: Could not find end of Formula inset.")
1127           i += 1
1128           continue
1129       
1130       for curline in range(i,j):
1131           found = document.body[curline].find("\\xymatrix@!")
1132           if found != -1:
1133               break
1134  
1135       if found != -1:
1136           has_equal_spacing = True
1137           content = [document.body[i][21:]]
1138           content += document.body[i + 1:j]
1139           subst = put_cmd_in_ert(content)
1140           document.body[i:j + 1] = subst
1141           i += len(subst) - (j - i) + 1
1142       else:
1143           for curline in range(i,j):
1144               l = document.body[curline].find("\\xymatrix")
1145               if l != -1:
1146                   has_preamble = True;
1147                   break;
1148           i = j + 1
1149   
1150     if has_equal_spacing and not has_preamble:
1151         add_to_preamble(document, ['% lyx2lyx xymatrix addition', '\\usepackage[all]{xy}'])
1152
1153
1154 def revert_notefontcolor(document):
1155     " Reverts greyed-out note font color to preamble code "
1156
1157     i = find_token(document.header, "\\notefontcolor", 0)
1158     if i == -1:
1159         return
1160
1161     # are there any grey notes?
1162     if find_token(document.body, "\\begin_inset Note Greyedout", 0) == -1:
1163         # no need to do anything, and \renewcommand will throw an error
1164         # since lyxgreyedout will not exist.
1165         return
1166
1167     colorcode = get_value(document.header, '\\notefontcolor', i)
1168     del document.header[i]
1169     # the color code is in the form #rrggbb where every character denotes a hex number
1170     red = hex2ratio(colorcode[1:3])
1171     green = hex2ratio(colorcode[3:5])
1172     blue = hex2ratio(colorcode[5:7])
1173     # write the preamble
1174     insert_to_preamble(0, document,
1175       ['% Commands inserted by lyx2lyx to set the font color',
1176         '% for greyed-out notes',
1177         '\\@ifundefined{definecolor}{\\usepackage{color}}{}'
1178         '\\definecolor{note_fontcolor}{rgb}{%s,%s,%s}' % (red, green, blue),
1179         '\\renewenvironment{lyxgreyedout}',
1180         ' {\\textcolor{note_fontcolor}\\bgroup}{\\egroup}'])
1181
1182
1183 def revert_turkmen(document):
1184     "Set language Turkmen to English" 
1185
1186     if document.language == "turkmen": 
1187         document.language = "english" 
1188         i = find_token(document.header, "\\language", 0) 
1189         if i != -1: 
1190             document.header[i] = "\\language english" 
1191
1192     j = 0 
1193     while True: 
1194         j = find_token(document.body, "\\lang turkmen", j) 
1195         if j == -1: 
1196             return 
1197         document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english") 
1198         j += 1 
1199
1200
1201 def revert_fontcolor(document):
1202     " Reverts font color to preamble code "
1203     i = find_token(document.header, "\\fontcolor", 0)
1204     if i == -1:
1205         return
1206     colorcode = get_value(document.header, '\\fontcolor', i)
1207     del document.header[i]
1208     # don't clutter the preamble if font color is not set
1209     if colorcode == "#000000":
1210         return
1211     # the color code is in the form #rrggbb where every character denotes a hex number
1212     red = hex2ratio(colorcode[1:3])
1213     green = hex2ratio(colorcode[3:5])
1214     blue = hex2ratio(colorcode[5:7])
1215     # write the preamble
1216     insert_to_preamble(0, document,
1217       ['% Commands inserted by lyx2lyx to set the font color',
1218       '\\@ifundefined{definecolor}{\\usepackage{color}}{}',
1219       '\\definecolor{document_fontcolor}{rgb}{%s,%s,%s}' % (red, green, blue),
1220       '\\color{document_fontcolor}'])
1221
1222
1223 def revert_shadedboxcolor(document):
1224     " Reverts shaded box color to preamble code "
1225     i = find_token(document.header, "\\boxbgcolor", 0)
1226     if i == -1:
1227         return
1228     colorcode = get_value(document.header, '\\boxbgcolor', i)
1229     del document.header[i]
1230     # the color code is in the form #rrggbb
1231     red = hex2ratio(colorcode[1:3])
1232     green = hex2ratio(colorcode[3:5])
1233     blue = hex2ratio(colorcode[5:7])
1234     # write the preamble
1235     insert_to_preamble(0, document,
1236       ['% Commands inserted by lyx2lyx to set the color of boxes with shaded background',
1237       '\\@ifundefined{definecolor}{\\usepackage{color}}{}',
1238       "\\definecolor{shadecolor}{rgb}{%s,%s,%s}" % (red, green, blue)])
1239
1240
1241 def revert_lyx_version(document):
1242     " Reverts LyX Version information from Inset Info "
1243     version = "LyX version"
1244     try:
1245         import lyx2lyx_version
1246         version = lyx2lyx_version.version
1247     except:
1248         pass
1249
1250     i = 0
1251     while 1:
1252         i = find_token(document.body, '\\begin_inset Info', i)
1253         if i == -1:
1254             return
1255         j = find_end_of_inset(document.body, i + 1)
1256         if j == -1:
1257             document.warning("Malformed LyX document: Could not find end of Info inset.")
1258             i += 1
1259             continue
1260
1261         # We expect:
1262         # \begin_inset Info
1263         # type  "lyxinfo"
1264         # arg   "version"
1265         # \end_inset
1266         typ = get_quoted_value(document.body, "type", i, j)
1267         arg = get_quoted_value(document.body, "arg", i, j)
1268         if arg != "version" or typ != "lyxinfo":
1269             i = j + 1
1270             continue
1271
1272         # We do not actually know the version of LyX used to produce the document.
1273         # But we can use our version, since we are reverting.
1274         s = [version]
1275         # Now we want to check if the line after "\end_inset" is empty. It normally
1276         # is, so we want to remove it, too.
1277         lastline = j + 1
1278         if document.body[j + 1].strip() == "":
1279             lastline = j + 2
1280         document.body[i: lastline] = s
1281         i = i + 1
1282
1283
1284 def revert_math_scale(document):
1285   " Remove math scaling and LaTeX options "
1286   del_token(document.header, '\\html_math_img_scale', 0)
1287   del_token(document.header, '\\html_latex_start', 0)
1288   del_token(document.header, '\\html_latex_end', 0)
1289
1290
1291 def revert_pagesizes(document):
1292   " Revert page sizes to default "
1293   i = find_token(document.header, '\\papersize', 0)
1294   if i != -1:
1295     size = document.header[i][11:]
1296     if size == "a0paper" or size == "a1paper" or size == "a2paper" \
1297     or size == "a6paper" or size == "b0paper" or size == "b1paper" \
1298     or size == "b2paper" or size == "b6paper" or size == "b0j" \
1299     or size == "b1j" or size == "b2j" or size == "b3j" or size == "b4j" \
1300     or size == "b5j" or size == "b6j":
1301       del document.header[i]
1302
1303
1304 def revert_DIN_C_pagesizes(document):
1305   " Revert DIN C page sizes to default "
1306   i = find_token(document.header, '\\papersize', 0)
1307   if i != -1:
1308     size = document.header[i][11:]
1309     if size == "c0paper" or size == "c1paper" or size == "c2paper" \
1310     or size == "c3paper" or size == "c4paper" or size == "c5paper" \
1311     or size == "c6paper":
1312       del document.header[i]
1313
1314
1315 def convert_html_quotes(document):
1316   " Remove quotes around html_latex_start and html_latex_end "
1317
1318   i = find_token(document.header, '\\html_latex_start', 0)
1319   if i != -1:
1320     line = document.header[i]
1321     l = re.compile(r'\\html_latex_start\s+"(.*)"')
1322     m = l.match(line)
1323     if m:
1324       document.header[i] = "\\html_latex_start " + m.group(1)
1325       
1326   i = find_token(document.header, '\\html_latex_end', 0)
1327   if i != -1:
1328     line = document.header[i]
1329     l = re.compile(r'\\html_latex_end\s+"(.*)"')
1330     m = l.match(line)
1331     if m:
1332       document.header[i] = "\\html_latex_end " + m.group(1)
1333       
1334
1335 def revert_html_quotes(document):
1336   " Remove quotes around html_latex_start and html_latex_end "
1337   
1338   i = find_token(document.header, '\\html_latex_start', 0)
1339   if i != -1:
1340     line = document.header[i]
1341     l = re.compile(r'\\html_latex_start\s+(.*)')
1342     m = l.match(line)
1343     if not m:
1344         document.warning("Weird html_latex_start line: " + line)
1345         del document.header[i]
1346     else:
1347         document.header[i] = "\\html_latex_start \"" + m.group(1) + "\""
1348       
1349   i = find_token(document.header, '\\html_latex_end', 0)
1350   if i != -1:
1351     line = document.header[i]
1352     l = re.compile(r'\\html_latex_end\s+(.*)')
1353     m = l.match(line)
1354     if not m:
1355         document.warning("Weird html_latex_end line: " + line)
1356         del document.header[i]
1357     else:
1358         document.header[i] = "\\html_latex_end \"" + m.group(1) + "\""
1359
1360
1361 def revert_output_sync(document):
1362   " Remove forward search options "
1363   del_token(document.header, '\\output_sync_macro', 0)
1364   del_token(document.header, '\\output_sync', 0)
1365
1366
1367 def revert_align_decimal(document):
1368   i = 0
1369   while True:
1370     i = find_token(document.body, "\\begin_inset Tabular", i)
1371     if i == -1:
1372       return
1373     j = find_end_of_inset(document.body, i)
1374     if j == -1:
1375       document.warning("Unable to find end of Tabular inset at line " + str(i))
1376       i += 1
1377       continue
1378     cell = find_token(document.body, "<cell", i, j)
1379     if cell == -1:
1380       document.warning("Can't find any cells in Tabular inset at line " + str(i))
1381       i = j
1382       continue
1383     k = i + 1
1384     while True:
1385       k = find_token(document.body, "<column", k, cell)
1386       if k == -1:
1387         return
1388       if document.body[k].find('alignment="decimal"') == -1:
1389         k += 1
1390         continue
1391       remove_option(document.body, k, 'decimal_point')
1392       document.body[k] = \
1393         document.body[k].replace('alignment="decimal"', 'alignment="center"')
1394       k += 1
1395
1396
1397 def convert_optarg(document):
1398   " Convert \\begin_inset OptArg to \\begin_inset Argument "
1399   i = 0
1400   while 1:
1401     i = find_token(document.body, '\\begin_inset OptArg', i)
1402     if i == -1:
1403       return
1404     document.body[i] = "\\begin_inset Argument"
1405     i += 1
1406
1407
1408 def revert_argument(document):
1409   " Convert \\begin_inset Argument to \\begin_inset OptArg "
1410   i = 0
1411   while 1:
1412     i = find_token(document.body, '\\begin_inset Argument', i)
1413     if i == -1:
1414       return
1415     document.body[i] = "\\begin_inset OptArg"
1416     i += 1
1417
1418
1419 def revert_makebox(document):
1420   " Convert \\makebox to TeX code "
1421   i = 0
1422   while 1:
1423     # only revert frameless boxes without an inner box
1424     i = find_token(document.body, '\\begin_inset Box Frameless', i)
1425     if i == -1:
1426       return
1427     z = find_end_of_inset(document.body, i)
1428     if z == -1:
1429       document.warning("Malformed LyX document: Can't find end of box inset.")
1430       i += 1
1431       continue
1432     blay = find_token(document.body, "\\begin_layout", i, z)
1433     if blay == -1:
1434       document.warning("Malformed LyX document: Can't find layout in box.")
1435       i = z
1436       continue
1437     # by looking before the layout we make sure we're actually finding
1438     # an option, not text.
1439     j = find_token(document.body, 'use_makebox', i, blay)
1440     if j == -1:
1441         i = z
1442         continue
1443     val = get_value(document.body, 'use_makebox', j)
1444     if val != "1":
1445         del document.body[j]
1446         i = z
1447         continue
1448     bend = find_end_of_layout(document.body, blay)
1449     if bend == -1 or bend > z:
1450         document.warning("Malformed LyX document: Can't find end of layout in box.")
1451         i = z
1452         continue
1453     # determine the alignment
1454     align = get_quoted_value(document.body, 'hor_pos', i, blay, "c")
1455     # determine the width
1456     length = get_quoted_value(document.body, 'width', i, blay, "50col%")
1457     length = latex_length(length)[1]
1458     # remove the \end_layout \end_inset pair
1459     document.body[bend:z + 1] = put_cmd_in_ert("}")
1460     subst = "\\makebox[" + length + "][" \
1461       + align + "]{"
1462     document.body[i:blay + 1] = put_cmd_in_ert(subst)
1463     i += 1
1464
1465
1466 def convert_use_makebox(document):
1467   " Adds use_makebox option for boxes "
1468   i = 0
1469   while 1:
1470     i = find_token(document.body, '\\begin_inset Box', i)
1471     if i == -1:
1472       return
1473     # all of this is to make sure we actually find the use_parbox
1474     # that is an option for this box, not some text elsewhere.
1475     z = find_end_of_inset(document.body, i)
1476     if z == -1:
1477       document.warning("Can't find end of box inset!!")
1478       i += 1
1479       continue
1480     blay = find_token(document.body, "\\begin_layout", i, z)
1481     if blay == -1:
1482       document.warning("Can't find layout in box inset!!")
1483       i = z
1484       continue
1485     # so now we are looking for use_parbox before the box's layout
1486     k = find_token(document.body, 'use_parbox', i, blay)
1487     if k == -1:
1488       document.warning("Malformed LyX document: Can't find use_parbox statement in box.")
1489       i = z
1490       continue
1491     document.body.insert(k + 1, "use_makebox 0")
1492     i = z + 1
1493
1494
1495 def revert_IEEEtran(document):
1496   " Convert IEEEtran layouts and styles to TeX code "
1497   if document.textclass != "IEEEtran":
1498     return
1499   revert_flex_inset(document.body, "IEEE membership", "\\IEEEmembership")
1500   revert_flex_inset(document.body, "Lowercase", "\\MakeLowercase")
1501   layouts = ("Special Paper Notice", "After Title Text", "Publication ID",
1502              "Page headings", "Biography without photo")
1503   latexcmd = {"Special Paper Notice": "\\IEEEspecialpapernotice",
1504               "After Title Text":     "\\IEEEaftertitletext",
1505               "Publication ID":       "\\IEEEpubid"}
1506   obsoletedby = {"Page headings":            "MarkBoth",
1507                  "Biography without photo":  "BiographyNoPhoto"}
1508   for layout in layouts:
1509     i = 0
1510     while True:
1511         i = find_token(document.body, '\\begin_layout ' + layout, i)
1512         if i == -1:
1513           break
1514         j = find_end_of_layout(document.body, i)
1515         if j == -1:
1516           document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1517           i += 1
1518           continue
1519         if layout in obsoletedby:
1520           document.body[i] = "\\begin_layout " + obsoletedby[layout]
1521           i = j
1522           continue
1523         content = lyx2latex(document, document.body[i:j + 1])
1524         add_to_preamble(document, [latexcmd[layout] + "{" + content + "}"])
1525         del document.body[i:j + 1]
1526         # no need to reset i
1527
1528
1529 def convert_prettyref(document):
1530         " Converts prettyref references to neutral formatted refs "
1531         re_ref = re.compile("^\s*reference\s+\"(\w+):(\S+)\"")
1532         nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
1533
1534         i = 0
1535         while True:
1536                 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1537                 if i == -1:
1538                         break
1539                 j = find_end_of_inset(document.body, i)
1540                 if j == -1:
1541                         document.warning("Malformed LyX document: No end of InsetRef!")
1542                         i += 1
1543                         continue
1544                 k = find_token(document.body, "LatexCommand prettyref", i, j)
1545                 if k != -1:
1546                         document.body[k] = "LatexCommand formatted"
1547                 i = j + 1
1548         document.header.insert(-1, "\\use_refstyle 0")
1549                 
1550  
1551 def revert_refstyle(document):
1552         " Reverts neutral formatted refs to prettyref "
1553         re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
1554         nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
1555
1556         i = 0
1557         while True:
1558                 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
1559                 if i == -1:
1560                         break
1561                 j = find_end_of_inset(document.body, i)
1562                 if j == -1:
1563                         document.warning("Malformed LyX document: No end of InsetRef")
1564                         i += 1
1565                         continue
1566                 k = find_token(document.body, "LatexCommand formatted", i, j)
1567                 if k != -1:
1568                         document.body[k] = "LatexCommand prettyref"
1569                 i = j + 1
1570         i = find_token(document.header, "\\use_refstyle", 0)
1571         if i != -1:
1572                 document.header.pop(i)
1573  
1574
1575 def revert_nameref(document):
1576   " Convert namerefs to regular references "
1577   cmds = ["Nameref", "nameref"]
1578   foundone = False
1579   rx = re.compile(r'reference "(.*)"')
1580   for cmd in cmds:
1581     i = 0
1582     oldcmd = "LatexCommand " + cmd
1583     while 1:
1584       # It seems better to look for this, as most of the reference
1585       # insets won't be ones we care about.
1586       i = find_token(document.body, oldcmd, i)
1587       if i == -1:
1588         break
1589       cmdloc = i
1590       i += 1
1591       # Make sure it is actually in an inset!
1592       # A normal line could begin with "LatexCommand nameref"!
1593       val = is_in_inset(document.body, cmdloc, \
1594           "\\begin_inset CommandInset ref")
1595       if not val:
1596           continue
1597       stins, endins = val
1598
1599       # ok, so it is in an InsetRef
1600       refline = find_token(document.body, "reference", stins, endins)
1601       if refline == -1:
1602         document.warning("Can't find reference for inset at line " + stinst + "!!")
1603         continue
1604       m = rx.match(document.body[refline])
1605       if not m:
1606         document.warning("Can't match reference line: " + document.body[ref])
1607         continue
1608       foundone = True
1609       ref = m.group(1)
1610       newcontent = put_cmd_in_ert('\\' + cmd + '{' + ref + '}')
1611       document.body[stins:endins + 1] = newcontent
1612
1613   if foundone:
1614     add_to_preamble(document, "\usepackage{nameref}")
1615
1616
1617 def remove_Nameref(document):
1618   " Convert Nameref commands to nameref commands "
1619   i = 0
1620   while 1:
1621     # It seems better to look for this, as most of the reference
1622     # insets won't be ones we care about.
1623     i = find_token(document.body, "LatexCommand Nameref" , i)
1624     if i == -1:
1625       break
1626     cmdloc = i
1627     i += 1
1628     
1629     # Make sure it is actually in an inset!
1630     val = is_in_inset(document.body, cmdloc, \
1631         "\\begin_inset CommandInset ref")
1632     if not val:
1633       continue
1634     document.body[cmdloc] = "LatexCommand nameref"
1635
1636
1637 def revert_mathrsfs(document):
1638     " Load mathrsfs if \mathrsfs us use in the document "
1639     i = 0
1640     for line in document.body:
1641       if line.find("\\mathscr{") != -1:
1642         add_to_preamble(document, ["% lyx2lyx mathrsfs addition", "\\usepackage{mathrsfs}"])
1643         return
1644
1645
1646 def convert_flexnames(document):
1647     "Convert \\begin_inset Flex Custom:Style to \\begin_inset Flex Style and similarly for CharStyle and Element."
1648     
1649     i = 0
1650     rx = re.compile(r'^\\begin_inset Flex (?:Custom|CharStyle|Element):(.+)$')
1651     while True:
1652       i = find_token(document.body, "\\begin_inset Flex", i)
1653       if i == -1:
1654         return
1655       m = rx.match(document.body[i])
1656       if m:
1657         document.body[i] = "\\begin_inset Flex " + m.group(1)
1658       i += 1
1659
1660
1661 flex_insets = {
1662   "Alert" : "CharStyle:Alert",
1663   "Code" : "CharStyle:Code",
1664   "Concepts" : "CharStyle:Concepts",
1665   "E-Mail" : "CharStyle:E-Mail",
1666   "Emph" : "CharStyle:Emph",
1667   "Expression" : "CharStyle:Expression",
1668   "Initial" : "CharStyle:Initial",
1669   "Institute" : "CharStyle:Institute",
1670   "Meaning" : "CharStyle:Meaning",
1671   "Noun" : "CharStyle:Noun",
1672   "Strong" : "CharStyle:Strong",
1673   "Structure" : "CharStyle:Structure",
1674   "ArticleMode" : "Custom:ArticleMode",
1675   "Endnote" : "Custom:Endnote",
1676   "Glosse" : "Custom:Glosse",
1677   "PresentationMode" : "Custom:PresentationMode",
1678   "Tri-Glosse" : "Custom:Tri-Glosse"
1679 }
1680
1681 flex_elements = {
1682   "Abbrev" : "Element:Abbrev",
1683   "CCC-Code" : "Element:CCC-Code",
1684   "Citation-number" : "Element:Citation-number",
1685   "City" : "Element:City",
1686   "Code" : "Element:Code",
1687   "CODEN" : "Element:CODEN",
1688   "Country" : "Element:Country",
1689   "Day" : "Element:Day",
1690   "Directory" : "Element:Directory",
1691   "Dscr" : "Element:Dscr",
1692   "Email" : "Element:Email",
1693   "Emph" : "Element:Emph",
1694   "Filename" : "Element:Filename",
1695   "Firstname" : "Element:Firstname",
1696   "Fname" : "Element:Fname",
1697   "GuiButton" : "Element:GuiButton",
1698   "GuiMenu" : "Element:GuiMenu",
1699   "GuiMenuItem" : "Element:GuiMenuItem",
1700   "ISSN" : "Element:ISSN",
1701   "Issue-day" : "Element:Issue-day",
1702   "Issue-months" : "Element:Issue-months",
1703   "Issue-number" : "Element:Issue-number",
1704   "KeyCap" : "Element:KeyCap",
1705   "KeyCombo" : "Element:KeyCombo",
1706   "Keyword" : "Element:Keyword",
1707   "Literal" : "Element:Literal",
1708   "MenuChoice" : "Element:MenuChoice",
1709   "Month" : "Element:Month",
1710   "Orgdiv" : "Element:Orgdiv",
1711   "Orgname" : "Element:Orgname",
1712   "Postcode" : "Element:Postcode",
1713   "SS-Code" : "Element:SS-Code",
1714   "SS-Title" : "Element:SS-Title",
1715   "State" : "Element:State",
1716   "Street" : "Element:Street",
1717   "Surname" : "Element:Surname",
1718   "Volume" : "Element:Volume",
1719   "Year" : "Element:Year"
1720 }
1721
1722
1723 def revert_flexnames(document):
1724   if document.backend == "latex":
1725     flexlist = flex_insets
1726   else:
1727     flexlist = flex_elements
1728   
1729   rx = re.compile(r'^\\begin_inset Flex\s+(.+)$')
1730   i = 0
1731   while True:
1732     i = find_token(document.body, "\\begin_inset Flex", i)
1733     if i == -1:
1734       return
1735     m = rx.match(document.body[i])
1736     if not m:
1737       document.warning("Illegal flex inset: " + document.body[i])
1738       i += 1
1739       continue
1740     style = m.group(1)
1741     if style in flexlist:
1742       document.body[i] = "\\begin_inset Flex " + flexlist[style]
1743     i += 1
1744
1745
1746 def convert_mathdots(document):
1747     " Load mathdots automatically "
1748     i = find_token(document.header, "\\use_esint" , 0)
1749     if i != -1:
1750       document.header.insert(i + 1, "\\use_mathdots 1")
1751
1752
1753 def revert_mathdots(document):
1754     " Load mathdots if used in the document "
1755
1756     mathdots = find_token(document.header, "\\use_mathdots" , 0)
1757     if mathdots == -1:
1758       document.warning("No \\usemathdots line. Assuming auto.")
1759     else:
1760       val = get_value(document.header, "\\use_mathdots", mathdots)
1761       del document.header[mathdots]
1762       try:
1763         usedots = int(val)
1764       except:
1765         document.warning("Invalid \\use_mathdots value: " + val + ". Assuming auto.")
1766         # probably usedots has not been changed, but be safe.
1767         usedots = 1
1768
1769       if usedots == 0:
1770         # do not load case
1771         return
1772       if usedots == 2:
1773         # force load case
1774         add_to_preamble(["% lyx2lyx mathdots addition", "\\usepackage{mathdots}"])
1775         return
1776     
1777     # so we are in the auto case. we want to load mathdots if \iddots is used.
1778     i = 0
1779     while True:
1780       i = find_token(document.body, '\\begin_inset Formula', i)
1781       if i == -1:
1782         return
1783       j = find_end_of_inset(document.body, i)
1784       if j == -1:
1785         document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
1786         i += 1
1787         continue
1788       code = "\n".join(document.body[i:j])
1789       if code.find("\\iddots") != -1:
1790         add_to_preamble(document, ["% lyx2lyx mathdots addition", 
1791         "\\@ifundefined{iddots}{\\usepackage{mathdots}}"])
1792         return
1793       i = j
1794
1795
1796 def convert_rule(document):
1797     " Convert \\lyxline to CommandInset line. "
1798     i = 0
1799     
1800     inset = ['\\begin_inset CommandInset line',
1801       'LatexCommand rule',
1802       'offset "0.5ex"',
1803       'width "100line%"',
1804       'height "1pt"', '',
1805       '\\end_inset', '', '']
1806
1807     # if paragraphs are indented, we may have to unindent to get the
1808     # line to be full-width.
1809     indent = get_value(document.header, "\\paragraph_separation", 0)
1810     have_indent = (indent == "indent")
1811
1812     while True:
1813       i = find_token(document.body, "\\lyxline" , i)
1814       if i == -1:
1815         return
1816
1817       # we need to find out if this line follows other content
1818       # in its paragraph. find its layout....
1819       lastlay = find_token_backwards(document.body, "\\begin_layout", i)
1820       if lastlay == -1:
1821         document.warning("Can't find layout for line at " + str(i))
1822         # do the best we can.
1823         document.body[i:i+1] = inset
1824         i += len(inset)
1825         continue
1826
1827       # ...and look for other content before it.
1828       lineisfirst = True
1829       for line in document.body[lastlay + 1:i]:
1830         # is it empty or a paragraph option?
1831         if not line or line[0] == '\\':
1832           continue
1833         lineisfirst = False
1834         break
1835
1836       if lineisfirst:
1837         document.body[i:i+1] = inset
1838         if indent:
1839           # we need to unindent, lest the line be too long
1840           document.body.insert(lastlay + 1, "\\noindent")
1841         i += len(inset)
1842       else:
1843         # so our line is in the middle of a paragraph
1844         # we need to add a new line, lest this line follow the
1845         # other content on that line and run off the side of the page
1846         document.body[i:i+1] = inset
1847         document.body[i:i] = ["\\begin_inset Newline newline", "\\end_inset", ""]
1848       i += len(inset)
1849
1850
1851 def revert_rule(document):
1852     " Revert line insets to Tex code "
1853     i = 0
1854     while 1:
1855       i = find_token(document.body, "\\begin_inset CommandInset line" , i)
1856       if i == -1:
1857         return
1858       # find end of inset
1859       j = find_token(document.body, "\\end_inset" , i)
1860       if j == -1:
1861         document.warning("Malformed LyX document: Can't find end of line inset.")
1862         return
1863       # determine the optional offset
1864       offset = get_quoted_value(document.body, 'offset', i, j)
1865       if offset:
1866         offset = '[' + offset + ']'
1867       # determine the width
1868       width = get_quoted_value(document.body, 'width', i, j, "100col%")
1869       width = latex_length(width)[1]
1870       # determine the height
1871       height = get_quoted_value(document.body, 'height', i, j, "1pt")
1872       height = latex_length(height)[1]
1873       # output the \rule command
1874       subst = "\\rule[" + offset + "]{" + width + "}{" + height + "}"
1875       document.body[i:j + 1] = put_cmd_in_ert(subst)
1876       i += len(subst) - (j - i)
1877
1878
1879 def revert_diagram(document):
1880   " Add the feyn package if \\Diagram is used in math "
1881   i = 0
1882   while True:
1883     i = find_token(document.body, '\\begin_inset Formula', i)
1884     if i == -1:
1885       return
1886     j = find_end_of_inset(document.body, i)
1887     if j == -1:
1888         document.warning("Malformed LyX document: Can't find end of Formula inset.")
1889         return 
1890     lines = "\n".join(document.body[i:j])
1891     if lines.find("\\Diagram") == -1:
1892       i = j
1893       continue
1894     add_to_preamble(document, ["% lyx2lyx feyn package insertion ", "\\usepackage{feyn}"])
1895     # only need to do it once!
1896     return
1897
1898
1899 def convert_bibtex_clearpage(document):
1900   " insert a clear(double)page bibliographystyle if bibtotoc option is used "
1901
1902   i = find_token(document.header, '\\papersides', 0)
1903   sides = 0
1904   if i == -1:
1905     document.warning("Malformed LyX document: Can't find papersides definition.")
1906     document.warning("Assuming single sided.")
1907     sides = 1
1908   else:
1909     val = get_value(document.header, "\\papersides", i)
1910     try:
1911       sides = int(val)
1912     except:
1913       pass
1914     if sides != 1 and sides != 2:
1915       document.warning("Invalid papersides value: " + val)
1916       document.warning("Assuming single sided.")
1917       sides = 1
1918
1919   j = 0
1920   while True:
1921     j = find_token(document.body, "\\begin_inset CommandInset bibtex", j)
1922     if j == -1:
1923       return
1924
1925     k = find_end_of_inset(document.body, j)
1926     if k == -1:
1927       document.warning("Can't find end of Bibliography inset at line " + str(j))
1928       j += 1
1929       continue
1930
1931     # only act if there is the option "bibtotoc"
1932     val = get_value(document.body, 'options', j, k)
1933     if not val:
1934       document.warning("Can't find options for bibliography inset at line " + str(j))
1935       j = k
1936       continue
1937     
1938     if val.find("bibtotoc") == -1:
1939       j = k
1940       continue
1941     
1942     # so we want to insert a new page right before the paragraph that
1943     # this bibliography thing is in. 
1944     lay = find_token_backwards(document.body, "\\begin_layout", j)
1945     if lay == -1:
1946       document.warning("Can't find layout containing bibliography inset at line " + str(j))
1947       j = k
1948       continue
1949
1950     if sides == 1:
1951       cmd = "clearpage"
1952     else:
1953       cmd = "cleardoublepage"
1954     subst = ['\\begin_layout Standard',
1955         '\\begin_inset Newpage ' + cmd,
1956         '\\end_inset', '', '',
1957         '\\end_layout', '']
1958     document.body[lay:lay] = subst
1959     j = k + len(subst)
1960
1961
1962 ##
1963 # Conversion hub
1964 #
1965
1966 supported_versions = ["2.0.0","2.0"]
1967 convert = [[346, []],
1968            [347, []],
1969            [348, []],
1970            [349, []],
1971            [350, []],
1972            [351, []],
1973            [352, [convert_splitindex]],
1974            [353, []],
1975            [354, []],
1976            [355, []],
1977            [356, []],
1978            [357, []],
1979            [358, []],
1980            [359, [convert_nomencl_width]],
1981            [360, []],
1982            [361, []],
1983            [362, []],
1984            [363, []],
1985            [364, []],
1986            [365, []],
1987            [366, []],
1988            [367, []],
1989            [368, []],
1990            [369, [convert_author_id]],
1991            [370, []],
1992            [371, []],
1993            [372, []],
1994            [373, [merge_gbrief]],
1995            [374, []],
1996            [375, []],
1997            [376, []],
1998            [377, []],
1999            [378, []],
2000            [379, [convert_math_output]],
2001            [380, []],
2002            [381, []],
2003            [382, []],
2004            [383, []],
2005            [384, []],
2006            [385, []],
2007            [386, []],
2008            [387, []],
2009            [388, []],
2010            [389, [convert_html_quotes]],
2011            [390, []],
2012            [391, []],
2013            [392, []],
2014            [393, [convert_optarg]],
2015            [394, [convert_use_makebox]],
2016            [395, []],
2017            [396, []],
2018            [397, [remove_Nameref]],
2019            [398, []],
2020            [399, [convert_mathdots]],
2021            [400, [convert_rule]],
2022            [401, []],
2023            [402, [convert_bibtex_clearpage]],
2024            [403, [convert_flexnames]],
2025            [404, [convert_prettyref]]
2026 ]
2027
2028 revert =  [[403, [revert_refstyle]],
2029            [402, [revert_flexnames]],
2030            [401, []],
2031            [400, [revert_diagram]],
2032            [399, [revert_rule]],
2033            [398, [revert_mathdots]],
2034            [397, [revert_mathrsfs]],
2035            [396, []],
2036            [395, [revert_nameref]],
2037            [394, [revert_DIN_C_pagesizes]],
2038            [393, [revert_makebox]],
2039            [392, [revert_argument]],
2040            [391, []],
2041            [390, [revert_align_decimal, revert_IEEEtran]],
2042            [389, [revert_output_sync]],
2043            [388, [revert_html_quotes]],
2044            [387, [revert_pagesizes]],
2045            [386, [revert_math_scale]],
2046            [385, [revert_lyx_version]],
2047            [384, [revert_shadedboxcolor]],
2048            [383, [revert_fontcolor]],
2049            [382, [revert_turkmen]],
2050            [381, [revert_notefontcolor]],
2051            [380, [revert_equalspacing_xymatrix]],
2052            [379, [revert_inset_preview]],
2053            [378, [revert_math_output]],
2054            [377, []],
2055            [376, [revert_multirow]],
2056            [375, [revert_includeall]],
2057            [374, [revert_includeonly]],
2058            [373, [revert_html_options]],
2059            [372, [revert_gbrief]],
2060            [371, [revert_fontenc]],
2061            [370, [revert_mhchem]],
2062            [369, [revert_suppress_date]],
2063            [368, [revert_author_id]],
2064            [367, [revert_hspace_glue_lengths]],
2065            [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
2066            [365, [revert_percent_skip_lengths]],
2067            [364, [revert_paragraph_indentation]],
2068            [363, [revert_branch_filename]],
2069            [362, [revert_longtable_align]],
2070            [361, [revert_applemac]],
2071            [360, []],
2072            [359, [revert_nomencl_cwidth]],
2073            [358, [revert_nomencl_width]],
2074            [357, [revert_custom_processors]],
2075            [356, [revert_ulinelatex]],
2076            [355, []],
2077            [354, [revert_strikeout]],
2078            [353, [revert_printindexall]],
2079            [352, [revert_subindex]],
2080            [351, [revert_splitindex]],
2081            [350, [revert_backgroundcolor]],
2082            [349, [revert_outputformat]],
2083            [348, [revert_xetex]],
2084            [347, [revert_phantom, revert_hphantom, revert_vphantom]],
2085            [346, [revert_tabularvalign]],
2086            [345, [revert_swiss]]
2087           ]
2088
2089
2090 if __name__ == "__main__":
2091     pass