]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_0.py
Clean up remove_option routine.
[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, get_value, get_value_string
27
28 ####################################################################
29 # Private helper functions
30
31 def remove_option(document, m, option):
32     ''' removes option from line m. returns whether we did anything '''
33     l = document.body[m].find(option)
34     if l == -1:
35         return False
36     val = document.body[m][l:].split('"')[1]
37     document.body[m] = document.body[m][:l - 1] + document.body[m][l+len(option + '="' + val + '"'):]
38     return True
39
40
41 def find_end_of_inset(lines, i):
42     " Find end of inset, where lines[i] is included."
43     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
44
45
46 def find_end_of_layout(lines, i):
47     " Find end of layout, where lines[i] is included."
48     return find_end_of(lines, i, "\\begin_layout", "\\end_layout")
49
50
51 # Note that text can be either a list of lines or a single line.
52 def add_to_preamble(document, text):
53     """ Add text to the preamble if it is not already there.
54     Only the first line is checked!"""
55
56     if not type(text) is list:
57       # split on \n just in case
58       # it'll give us the one element list we want
59       # if there's no \n, too
60       text = text.split('\n')
61
62     if find_token(document.preamble, text[0], 0) != -1:
63         return
64
65     document.preamble.extend(text)
66
67
68 # Note that text can be either a list of lines or a single line.
69 # It should really be a list.
70 def insert_to_preamble(index, document, text):
71     """ Insert text to the preamble at a given line"""
72     
73     if not type(text) is list:
74       # split on \n just in case
75       # it'll give us the one element list we want
76       # if there's no \n, too
77       text = text.split('\n')
78
79     document.preamble[index:index] = text
80
81
82 def read_unicodesymbols():
83     " Read the unicodesymbols list of unicode characters and corresponding commands."
84     pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
85     fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
86     spec_chars = []
87     # Two backslashes, followed by some non-word character, and then a character
88     # in brackets. The idea is to check for constructs like: \"{u}, which is how
89     # they are written in the unicodesymbols file; but they can also be written
90     # as: \"u or even \" u.
91     r = re.compile(r'\\\\(\W)\{(\w)\}')
92     for line in fp.readlines():
93         if line[0] != '#' and line.strip() != "":
94             line=line.replace(' "',' ') # remove all quotation marks with spaces before
95             line=line.replace('" ',' ') # remove all quotation marks with spaces after
96             line=line.replace(r'\"','"') # replace \" by " (for characters with diaeresis)
97             try:
98                 [ucs4,command,dead] = line.split(None,2)
99                 if command[0:1] != "\\":
100                     continue
101                 spec_chars.append([command, unichr(eval(ucs4))])
102             except:
103                 continue
104             m = r.match(command)
105             if m != None:
106                 command = "\\\\"
107                 # If the character is a double-quote, then we need to escape it, too,
108                 # since it is done that way in the LyX file.
109                 if m.group(1) == "\"":
110                     command += "\\"
111                 commandbl = command
112                 command += m.group(1) + m.group(2)
113                 commandbl += m.group(1) + ' ' + m.group(2)
114                 spec_chars.append([command, unichr(eval(ucs4))])
115                 spec_chars.append([commandbl, unichr(eval(ucs4))])
116     fp.close()
117     return spec_chars
118
119
120 unicode_reps = read_unicodesymbols()
121
122
123 # DO NOT USE THIS ROUTINE ANY MORE. Better yet, replace the uses that
124 # have been made of it with uses of put_cmd_in_ert.
125 def old_put_cmd_in_ert(string):
126     for rep in unicode_reps:
127         string = string.replace(rep[1], rep[0].replace('\\\\', '\\'))
128     string = string.replace('\\', "\\backslash\n")
129     string = "\\begin_inset ERT\nstatus collapsed\n\\begin_layout Plain Layout\n" \
130       + string + "\n\\end_layout\n\\end_inset"
131     return string
132
133
134 # This routine wraps some content in an ERT inset. 
135 #
136 # NOTE: The function accepts either a single string or a LIST of strings as
137 # argument. But it returns a LIST of strings, split on \n, so that it does 
138 # not have embedded newlines.
139
140 # This is how lyx2lyx represents a LyX document: as a list of strings, 
141 # each representing a line of a LyX file. Embedded newlines confuse 
142 # lyx2lyx very much.
143 #
144 # A call to this routine will often go something like this:
145 #   i = find_token('\\begin_inset FunkyInset', ...)
146 #   ...
147 #   j = find_end_of_inset(document.body, i)
148 #   content = ...extract content from insets
149 #   # that could be as simple as: 
150 #   # content = lyx2latex(document[i:j + 1])
151 #   ert = put_cmd_in_ert(content)
152 #   document.body[i:j] = ert
153 # Now, before we continue, we need to reset i appropriately. Normally,
154 # this would be: 
155 #   i += len(ert)
156 # That puts us right after the ERT we just inserted.
157 #
158 def put_cmd_in_ert(arg):
159     ret = ["\\begin_inset ERT", "status collapsed", "\\begin_layout Plain Layout", ""]
160     # Despite the warnings just given, it will be faster for us to work
161     # with a single string internally. That way, we only go through the
162     # unicode_reps loop once.
163     if type(arg) is list:
164       s = "\n".join(arg)
165     else:
166       s = arg
167     for rep in unicode_reps:
168       s = s.replace(rep[1], rep[0].replace('\\\\', '\\'))
169     s = s.replace('\\', "\\backslash\n")
170     ret += s.splitlines()
171     ret += ["\\end_layout", "\\end_inset"]
172     return ret
173
174             
175 def lyx2latex(document, lines):
176     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
177     # clean up multiline stuff
178     content = ""
179     ert_end = 0
180     note_end = 0
181     hspace = ""
182
183     for curline in range(len(lines)):
184       line = lines[curline]
185       if line.startswith("\\begin_inset Note Note"):
186           # We want to skip LyX notes, so remember where the inset ends
187           note_end = find_end_of_inset(lines, curline + 1)
188           continue
189       elif note_end >= curline:
190           # Skip LyX notes
191           continue
192       elif line.startswith("\\begin_inset ERT"):
193           # We don't want to replace things inside ERT, so figure out
194           # where the end of the inset is.
195           ert_end = find_end_of_inset(lines, curline + 1)
196           continue
197       elif line.startswith("\\begin_inset Formula"):
198           line = line[20:]
199       elif line.startswith("\\begin_inset Quotes"):
200           # For now, we do a very basic reversion. Someone who understands
201           # quotes is welcome to fix it up.
202           qtype = line[20:].strip()
203           # lang = qtype[0]
204           side = qtype[1]
205           dbls = qtype[2]
206           if side == "l":
207               if dbls == "d":
208                   line = "``"
209               else:
210                   line = "`"
211           else:
212               if dbls == "d":
213                   line = "''"
214               else:
215                   line = "'"
216       elif line.startswith("\\begin_inset space"):
217           line = line[18:].strip()
218           if line.startswith("\\hspace"):
219               # Account for both \hspace and \hspace*
220               hspace = line[:-2]
221               continue
222           elif line == "\\space{}":
223               line = "\\ "
224           elif line == "\\thinspace{}":
225               line = "\\,"
226       elif hspace != "":
227           # The LyX length is in line[8:], after the \length keyword
228           length = latex_length(line[8:])[1]
229           line = hspace + "{" + length + "}"
230           hspace = ""
231       elif line.isspace() or \
232             line.startswith("\\begin_layout") or \
233             line.startswith("\\end_layout") or \
234             line.startswith("\\begin_inset") or \
235             line.startswith("\\end_inset") or \
236             line.startswith("\\lang") or \
237             line.strip() == "status collapsed" or \
238             line.strip() == "status open":
239           #skip all that stuff
240           continue
241
242       # this needs to be added to the preamble because of cases like
243       # \textmu, \textbackslash, etc.
244       add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
245                                  '\\@ifundefined{textmu}',
246                                  ' {\\usepackage{textcomp}}{}'])
247       # a lossless reversion is not possible
248       # try at least to handle some common insets and settings
249       if ert_end >= curline:
250           line = line.replace(r'\backslash', '\\')
251       else:
252           # No need to add "{}" after single-nonletter macros
253           line = line.replace('&', '\\&')
254           line = line.replace('#', '\\#')
255           line = line.replace('^', '\\textasciicircum{}')
256           line = line.replace('%', '\\%')
257           line = line.replace('_', '\\_')
258           line = line.replace('$', '\\$')
259
260           # Do the LyX text --> LaTeX conversion
261           for rep in unicode_reps:
262             line = line.replace(rep[1], rep[0] + "{}")
263           line = line.replace(r'\backslash', r'\textbackslash{}')
264           line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
265           line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
266           line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
267           line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
268           line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
269           line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
270           line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
271           line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
272           line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
273       content += line
274     return content
275
276
277 def latex_length(slen):
278     ''' 
279     Convert lengths to their LaTeX representation. Returns (bool, length),
280     where the bool tells us if it was a percentage, and the length is the
281     LaTeX representation.
282     '''
283     i = 0
284     percent = False
285     # the slen has the form
286     # ValueUnit+ValueUnit-ValueUnit or
287     # ValueUnit+-ValueUnit
288     # the + and - (glue lengths) are optional
289     # the + always precedes the -
290
291     # Convert relative lengths to LaTeX units
292     units = {"text%":"\\textwidth", "col%":"\\columnwidth",
293              "page%":"\\paperwidth", "line%":"\\linewidth",
294              "theight%":"\\textheight", "pheight%":"\\paperheight"}
295     for unit in units.keys():
296         i = slen.find(unit)
297         if i == -1:
298             continue
299         percent = True
300         minus = slen.rfind("-", 1, i)
301         plus = slen.rfind("+", 0, i)
302         latex_unit = units[unit]
303         if plus == -1 and minus == -1:
304             value = slen[:i]
305             value = str(float(value)/100)
306             end = slen[i + len(unit):]
307             slen = value + latex_unit + end
308         if plus > minus:
309             value = slen[plus + 1:i]
310             value = str(float(value)/100)
311             begin = slen[:plus + 1]
312             end = slen[i+len(unit):]
313             slen = begin + value + latex_unit + end
314         if plus < minus:
315             value = slen[minus + 1:i]
316             value = str(float(value)/100)
317             begin = slen[:minus + 1]
318             slen = begin + value + latex_unit
319
320     # replace + and -, but only if the - is not the first character
321     slen = slen[0] + slen[1:].replace("+", " plus ").replace("-", " minus ")
322     # handle the case where "+-1mm" was used, because LaTeX only understands
323     # "plus 1mm minus 1mm"
324     if slen.find("plus  minus"):
325         lastvaluepos = slen.rfind(" ")
326         lastvalue = slen[lastvaluepos:]
327         slen = slen.replace("  ", lastvalue + " ")
328     return (percent, slen)
329
330
331 def revert_flex_inset(document, name, LaTeXname, position):
332   " Convert flex insets to TeX code "
333   i = position
334   while True:
335     i = find_token(document.body, '\\begin_inset Flex ' + name, i)
336     if i == -1:
337       return
338     z = find_end_of_inset(document.body, i)
339     if z == -1:
340       document.warning("Malformed LyX document: Can't find end of Flex " + name + " inset.")
341       return
342     # remove the \end_inset
343     document.body[z - 2:z + 1] = put_cmd_in_ert("}")
344     # we need to reset character layouts if necessary
345     j = find_token(document.body, '\\emph on', i, z)
346     k = find_token(document.body, '\\noun on', i, z)
347     l = find_token(document.body, '\\series', i, z)
348     m = find_token(document.body, '\\family', i, z)
349     n = find_token(document.body, '\\shape', i, z)
350     o = find_token(document.body, '\\color', i, z)
351     p = find_token(document.body, '\\size', i, z)
352     q = find_token(document.body, '\\bar under', i, z)
353     r = find_token(document.body, '\\uuline on', i, z)
354     s = find_token(document.body, '\\uwave on', i, z)
355     t = find_token(document.body, '\\strikeout on', i, z)
356     if j != -1:
357       document.body.insert(z - 2, "\\emph default")
358     if k != -1:
359       document.body.insert(z - 2, "\\noun default")
360     if l != -1:
361       document.body.insert(z - 2, "\\series default")
362     if m != -1:
363       document.body.insert(z - 2, "\\family default")
364     if n != -1:
365       document.body.insert(z - 2, "\\shape default")
366     if o != -1:
367       document.body.insert(z - 2, "\\color inherit")
368     if p != -1:
369       document.body.insert(z - 2, "\\size default")
370     if q != -1:
371       document.body.insert(z - 2, "\\bar default")
372     if r != -1:
373       document.body.insert(z - 2, "\\uuline default")
374     if s != -1:
375       document.body.insert(z - 2, "\\uwave default")
376     if t != -1:
377       document.body.insert(z - 2, "\\strikeout default")
378     document.body[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
379     i += 1
380
381
382 def revert_font_attrs(document, name, LaTeXname):
383   " Reverts font changes to TeX code "
384   i = 0
385   changed = False
386   while True:
387     i = find_token(document.body, name + ' on', i)
388     if i == -1:
389       return changed
390     j = find_token(document.body, name + ' default', i)
391     k = find_token(document.body, name + ' on', i + 1)
392     # if there is no default set, the style ends with the layout
393     # assure hereby that we found the correct layout end
394     if j != -1 and (j < k or k == -1):
395       document.body[j:j + 1] = put_cmd_in_ert("}")
396     else:
397       j = find_token(document.body, '\\end_layout', i)
398       document.body[j:j] = put_cmd_in_ert("}")
399     document.body[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
400     changed = True
401     i += 1
402
403
404 def revert_layout_command(document, name, LaTeXname, position):
405   " Reverts a command from a layout to TeX code "
406   i = position
407   while True:
408     i = find_token(document.body, '\\begin_layout ' + name, i)
409     if i == -1:
410       return
411     k = -1
412     # find the next layout
413     j = i + 1
414     while k == -1:
415       j = find_token(document.body, '\\begin_layout', j)
416       l = len(document.body)
417       # if nothing was found it was the last layout of the document
418       if j == -1:
419         document.body[l - 4:l - 4] = put_cmd_in_ert("}")
420         k = 0
421       # exclude plain layout because this can be TeX code or another inset
422       elif document.body[j] != '\\begin_layout Plain Layout':
423         document.body[j - 2:j - 2] = put_cmd_in_ert("}")
424         k = 0
425       else:
426         j += 1
427     document.body[i] = '\\begin_layout Standard'
428     document.body[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
429     i += 1
430
431
432 def hex2ratio(s):
433     val = string.atoi(s, 16)
434     if val != 0:
435       val += 1
436     return str(val / 256.0)
437
438
439 ###############################################################################
440 ###
441 ### Conversion and reversion routines
442 ###
443 ###############################################################################
444
445 def revert_swiss(document):
446     " Set language german-ch to ngerman "
447     i = 0
448     if document.language == "german-ch":
449         document.language = "ngerman"
450         i = find_token(document.header, "\\language", 0)
451         if i != -1:
452             document.header[i] = "\\language ngerman"
453     j = 0
454     while True:
455         j = find_token(document.body, "\\lang german-ch", j)
456         if j == -1:
457             return
458         document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
459         j = j + 1
460
461
462 def revert_tabularvalign(document):
463    " Revert the tabular valign option "
464    i = 0
465    while True:
466       i = find_token(document.body, "\\begin_inset Tabular", i)
467       if i == -1:
468           return
469       end = find_end_of_inset(document.body, i)
470       if end == -1:
471           document.warning("Can't find end of inset at line " + str(i))
472           i += 1
473           continue
474       fline = find_token(document.body, "<features", i, end)
475       if fline == -1:
476           document.warning("Can't find features for inset at line " + str(i))
477           i += 1
478           continue
479       p = document.body[fline].find("islongtable")
480       if p != -1:
481           q = document.body[fline].find("tabularvalignment")
482           if q != -1:
483               # FIXME
484               # This seems wrong: It removes everything after 
485               # tabularvalignment, too.
486               document.body[fline] = document.body[fline][:q - 1] + '>'
487           i += 1
488           continue
489
490        # no longtable
491       tabularvalignment = 'c'
492       # which valignment is specified?
493       m = document.body[fline].find('tabularvalignment="top"')
494       if m != -1:
495           tabularvalignment = 't'
496       m = document.body[fline].find('tabularvalignment="bottom"')
497       if m != -1:
498           tabularvalignment = 'b'
499       # delete tabularvalignment
500       q = document.body[fline].find("tabularvalignment")
501       if q != -1:
502           # FIXME
503           # This seems wrong: It removes everything after 
504           # tabularvalignment, too.
505           document.body[fline] = document.body[fline][:q - 1] + '>'
506
507       # don't add a box when centered
508       if tabularvalignment == 'c':
509           i = end
510           continue
511       subst = ['\\end_layout', '\\end_inset']
512       document.body[end:end] = subst # just inserts those lines
513       subst = ['\\begin_inset Box Frameless',
514           'position "' + tabularvalignment +'"',
515           'hor_pos "c"',
516           'has_inner_box 1',
517           'inner_pos "c"',
518           'use_parbox 0',
519           # we don't know the width, assume 50%
520           'width "50col%"',
521           'special "none"',
522           'height "1in"',
523           'height_special "totalheight"',
524           'status open',
525           '',
526           '\\begin_layout Plain Layout']
527       document.body[i:i] = subst # this just inserts the array at i
528       # since there could be a tabular inside a tabular, we cannot
529       # jump to end
530       i += len(subst)
531
532
533 def revert_phantom_types(document, ptype, cmd):
534     " Reverts phantom to ERT "
535     i = 0
536     while True:
537       i = find_token(document.body, "\\begin_inset Phantom " + ptype, i)
538       if i == -1:
539           return
540       end = find_end_of_inset(document.body, i)
541       if end == -1:
542           document.warning("Can't find end of inset at line " + str(i))
543           i += 1
544           continue
545       blay = find_token(document.body, "\\begin_layout Plain Layout", i, end)
546       if blay == -1:
547           document.warning("Can't find layout for inset at line " + str(i))
548           i = end
549           continue
550       bend = find_token(document.body, "\\end_layout", blay, end)
551       if bend == -1:
552           document.warning("Malformed LyX document: Could not find end of Phantom inset's layout.")
553           i = end
554           continue
555       substi = ["\\begin_inset ERT", "status collapsed", "",
556                 "\\begin_layout Plain Layout", "", "", "\\backslash", 
557                 cmd + "{", "\\end_layout", "", "\\end_inset"]
558       substj = ["\\size default", "", "\\begin_inset ERT", "status collapsed", "",
559                 "\\begin_layout Plain Layout", "", "}", "\\end_layout", "", "\\end_inset"]
560       # do the later one first so as not to mess up the numbering
561       document.body[bend:end + 1] = substj
562       document.body[i:blay + 1] = substi
563       i = end + len(substi) + len(substj) - (end - bend) - (blay - i) - 2
564
565
566 def revert_phantom(document):
567     revert_phantom_types(document, "Phantom", "phantom")
568     
569 def revert_hphantom(document):
570     revert_phantom_types(document, "HPhantom", "hphantom")
571
572 def revert_vphantom(document):
573     revert_phantom_types(document, "VPhantom", "vphantom")
574
575
576 def revert_xetex(document):
577     " Reverts documents that use XeTeX "
578     i = find_token(document.header, '\\use_xetex', 0)
579     if i == -1:
580         document.warning("Malformed LyX document: Missing \\use_xetex.")
581         return
582     if get_value(document.header, "\\use_xetex", i) == 'false':
583         del document.header[i]
584         return
585     del document.header[i]
586     # 1.) set doc encoding to utf8-plain
587     i = find_token(document.header, "\\inputencoding", 0)
588     if i == -1:
589         document.warning("Malformed LyX document: Missing \\inputencoding.")
590     document.header[i] = "\\inputencoding utf8-plain"
591     # 2.) check font settings
592     l = find_token(document.header, "\\font_roman", 0)
593     if l == -1:
594         document.warning("Malformed LyX document: Missing \\font_roman.")
595     line = document.header[l]
596     l = re.compile(r'\\font_roman (.*)$')
597     m = l.match(line)
598     roman = m.group(1)
599     l = find_token(document.header, "\\font_sans", 0)
600     if l == -1:
601         document.warning("Malformed LyX document: Missing \\font_sans.")
602     line = document.header[l]
603     l = re.compile(r'\\font_sans (.*)$')
604     m = l.match(line)
605     sans = m.group(1)
606     l = find_token(document.header, "\\font_typewriter", 0)
607     if l == -1:
608         document.warning("Malformed LyX document: Missing \\font_typewriter.")
609     line = document.header[l]
610     l = re.compile(r'\\font_typewriter (.*)$')
611     m = l.match(line)
612     typewriter = m.group(1)
613     osf = get_value(document.header, '\\font_osf', 0) == "true"
614     sf_scale = float(get_value(document.header, '\\font_sf_scale', 0))
615     tt_scale = float(get_value(document.header, '\\font_tt_scale', 0))
616     # 3.) set preamble stuff
617     pretext = '%% This document must be processed with xelatex!\n'
618     pretext += '\\usepackage{fontspec}\n'
619     if roman != "default":
620         pretext += '\\setmainfont[Mapping=tex-text]{' + roman + '}\n'
621     if sans != "default":
622         pretext += '\\setsansfont['
623         if sf_scale != 100:
624             pretext += 'Scale=' + str(sf_scale / 100) + ','
625         pretext += 'Mapping=tex-text]{' + sans + '}\n'
626     if typewriter != "default":
627         pretext += '\\setmonofont'
628         if tt_scale != 100:
629             pretext += '[Scale=' + str(tt_scale / 100) + ']'
630         pretext += '{' + typewriter + '}\n'
631     if osf:
632         pretext += '\\defaultfontfeatures{Numbers=OldStyle}\n'
633     pretext += '\usepackage{xunicode}\n'
634     pretext += '\usepackage{xltxtra}\n'
635     insert_to_preamble(0, document, pretext)
636     # 4.) reset font settings
637     i = find_token(document.header, "\\font_roman", 0)
638     if i == -1:
639         document.warning("Malformed LyX document: Missing \\font_roman.")
640     document.header[i] = "\\font_roman default"
641     i = find_token(document.header, "\\font_sans", 0)
642     if i == -1:
643         document.warning("Malformed LyX document: Missing \\font_sans.")
644     document.header[i] = "\\font_sans default"
645     i = find_token(document.header, "\\font_typewriter", 0)
646     if i == -1:
647         document.warning("Malformed LyX document: Missing \\font_typewriter.")
648     document.header[i] = "\\font_typewriter default"
649     i = find_token(document.header, "\\font_osf", 0)
650     if i == -1:
651         document.warning("Malformed LyX document: Missing \\font_osf.")
652     document.header[i] = "\\font_osf false"
653     i = find_token(document.header, "\\font_sc", 0)
654     if i == -1:
655         document.warning("Malformed LyX document: Missing \\font_sc.")
656     document.header[i] = "\\font_sc false"
657     i = find_token(document.header, "\\font_sf_scale", 0)
658     if i == -1:
659         document.warning("Malformed LyX document: Missing \\font_sf_scale.")
660     document.header[i] = "\\font_sf_scale 100"
661     i = find_token(document.header, "\\font_tt_scale", 0)
662     if i == -1:
663         document.warning("Malformed LyX document: Missing \\font_tt_scale.")
664     document.header[i] = "\\font_tt_scale 100"
665
666
667 def revert_outputformat(document):
668     " Remove default output format param "
669     i = find_token(document.header, '\\default_output_format', 0)
670     if i == -1:
671         document.warning("Malformed LyX document: Missing \\default_output_format.")
672         return
673     del document.header[i]
674
675
676 def revert_backgroundcolor(document):
677     " Reverts background color to preamble code "
678     i = find_token(document.header, "\\backgroundcolor", 0)
679     if i == -1:
680         return
681     colorcode = get_value(document.header, '\\backgroundcolor', i)
682     del document.header[i]
683     # don't clutter the preamble if backgroundcolor is not set
684     if colorcode == "#ffffff":
685         return
686     red   = hex2ratio(colorcode[1:3])
687     green = hex2ratio(colorcode[3:5])
688     blue  = hex2ratio(colorcode[5:7])
689     insert_to_preamble(0, document,
690                           '% Commands inserted by lyx2lyx to set the background color\n'
691                           + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
692                           + '\\definecolor{page_backgroundcolor}{rgb}{'
693                           + red + ',' + green + ',' + blue + '}\n'
694                           + '\\pagecolor{page_backgroundcolor}\n')
695
696
697 def revert_splitindex(document):
698     " Reverts splitindex-aware documents "
699     i = find_token(document.header, '\\use_indices', 0)
700     if i == -1:
701         document.warning("Malformed LyX document: Missing \\use_indices.")
702         return
703     indices = get_value(document.header, "\\use_indices", i)
704     preamble = ""
705     useindices = (indices == "true")
706     if useindices:
707          preamble += "\\usepackage{splitidx}\n"
708     del document.header[i]
709     
710     # deal with index declarations in the preamble
711     i = 0
712     while True:
713         i = find_token(document.header, "\\index", i)
714         if i == -1:
715             break
716         k = find_token(document.header, "\\end_index", i)
717         if k == -1:
718             document.warning("Malformed LyX document: Missing \\end_index.")
719             return
720         if useindices:    
721           line = document.header[i]
722           l = re.compile(r'\\index (.*)$')
723           m = l.match(line)
724           iname = m.group(1)
725           ishortcut = get_value(document.header, '\\shortcut', i, k)
726           if ishortcut != "":
727               preamble += "\\newindex[" + iname + "]{" + ishortcut + "}\n"
728         del document.header[i:k + 1]
729     if preamble != "":
730         insert_to_preamble(0, document, preamble)
731         
732     # deal with index insets
733     # these need to have the argument removed
734     i = 0
735     while True:
736         i = find_token(document.body, "\\begin_inset Index", i)
737         if i == -1:
738             break
739         line = document.body[i]
740         l = re.compile(r'\\begin_inset Index (.*)$')
741         m = l.match(line)
742         itype = m.group(1)
743         if itype == "idx" or indices == "false":
744             document.body[i] = "\\begin_inset Index"
745         else:
746             k = find_end_of_inset(document.body, i)
747             if k == -1:
748                 document.warning("Can't find end of index inset!")
749                 i += 1
750                 continue
751             content = lyx2latex(document, document.body[i:k])
752             # escape quotes
753             content = content.replace('"', r'\"')
754             subst = put_cmd_in_ert("\\sindex[" + itype + "]{" + content + "}")
755             document.body[i:k + 1] = subst
756         i = i + 1
757         
758     # deal with index_print insets
759     i = 0
760     while True:
761         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
762         if i == -1:
763             return
764         k = find_end_of_inset(document.body, i)
765         ptype = get_value(document.body, 'type', i, k).strip('"')
766         if ptype == "idx":
767             j = find_token(document.body, "type", i, k)
768             del document.body[j]
769         elif not useindices:
770             del document.body[i:k + 1]
771         else:
772             subst = put_cmd_in_ert("\\printindex[" + ptype + "]{}")
773             document.body[i:k + 1] = subst
774         i = i + 1
775
776
777 def convert_splitindex(document):
778     " Converts index and printindex insets to splitindex-aware format "
779     i = 0
780     while True:
781         i = find_token(document.body, "\\begin_inset Index", i)
782         if i == -1:
783             break
784         document.body[i] = document.body[i].replace("\\begin_inset Index",
785             "\\begin_inset Index idx")
786         i = i + 1
787     i = 0
788     while True:
789         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
790         if i == -1:
791             return
792         if document.body[i + 1].find('LatexCommand printindex') == -1:
793             document.warning("Malformed LyX document: Incomplete printindex inset.")
794             return
795         subst = ["LatexCommand printindex", 
796             "type \"idx\""]
797         document.body[i + 1:i + 2] = subst
798         i = i + 1
799
800
801 def revert_subindex(document):
802     " Reverts \\printsubindex CommandInset types "
803     i = find_token(document.header, '\\use_indices', 0)
804     if i == -1:
805         document.warning("Malformed LyX document: Missing \\use_indices.")
806         return
807     indices = get_value(document.header, "\\use_indices", i)
808     useindices = (indices == "true")
809     i = 0
810     while True:
811         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
812         if i == -1:
813             return
814         k = find_end_of_inset(document.body, i)
815         ctype = get_value(document.body, 'LatexCommand', i, k)
816         if ctype != "printsubindex":
817             i = k + 1
818             continue
819         ptype = get_value(document.body, 'type', i, k).strip('"')
820         if not useindices:
821             del document.body[i:k + 1]
822         else:
823             subst = put_cmd_in_ert("\\printsubindex[" + ptype + "]{}")
824             document.body[i:k + 1] = subst
825         i = i + 1
826
827
828 def revert_printindexall(document):
829     " Reverts \\print[sub]index* CommandInset types "
830     i = find_token(document.header, '\\use_indices', 0)
831     if i == -1:
832         document.warning("Malformed LyX document: Missing \\use_indices.")
833         return
834     indices = get_value(document.header, "\\use_indices", i)
835     useindices = (indices == "true")
836     i = 0
837     while True:
838         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
839         if i == -1:
840             return
841         k = find_end_of_inset(document.body, i)
842         ctype = get_value(document.body, 'LatexCommand', i, k)
843         if ctype != "printindex*" and ctype != "printsubindex*":
844             i = k
845             continue
846         if not useindices:
847             del document.body[i:k + 1]
848         else:
849             subst = put_cmd_in_ert("\\" + ctype + "{}")
850             document.body[i:k + 1] = subst
851         i = i + 1
852
853
854 def revert_strikeout(document):
855   " Reverts \\strikeout font attribute "
856   changed = revert_font_attrs(document, "\\uuline", "\\uuline")
857   changed = revert_font_attrs(document, "\\uwave", "\\uwave") or changed
858   changed = revert_font_attrs(document, "\\strikeout", "\\sout")  or changed
859   if changed == True:
860     insert_to_preamble(0, document,
861         '% Commands inserted by lyx2lyx for proper underlining\n'
862         + '\\PassOptionsToPackage{normalem}{ulem}\n'
863         + '\\usepackage{ulem}\n')
864
865
866 def revert_ulinelatex(document):
867     " Reverts \\uline font attribute "
868     i = find_token(document.body, '\\bar under', 0)
869     if i == -1:
870         return
871     insert_to_preamble(0, document,
872             '% Commands inserted by lyx2lyx for proper underlining\n'
873             + '\\PassOptionsToPackage{normalem}{ulem}\n'
874             + '\\usepackage{ulem}\n'
875             + '\\let\\cite@rig\\cite\n'
876             + '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}\n'
877             + '  \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}\n'
878             + '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}\n')
879
880
881 def revert_custom_processors(document):
882     " Remove bibtex_command and index_command params "
883     i = find_token(document.header, '\\bibtex_command', 0)
884     if i == -1:
885         document.warning("Malformed LyX document: Missing \\bibtex_command.")
886     else:
887         del document.header[i]
888     i = find_token(document.header, '\\index_command', 0)
889     if i == -1:
890         document.warning("Malformed LyX document: Missing \\index_command.")
891     else:
892         del document.header[i]
893
894
895 def convert_nomencl_width(document):
896     " Add set_width param to 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       document.body.insert(i + 2, "set_width \"none\"")
903       i = i + 1
904
905
906 def revert_nomencl_width(document):
907     " Remove set_width param from nomencl_print "
908     i = 0
909     while True:
910       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
911       if i == -1:
912         break
913       j = find_end_of_inset(document.body, i)
914       l = find_token(document.body, "set_width", i, j)
915       if l == -1:
916             document.warning("Can't find set_width option for nomencl_print!")
917             i = j
918             continue
919       del document.body[l]
920       i = j - 1
921
922
923 def revert_nomencl_cwidth(document):
924     " Remove width param from nomencl_print "
925     i = 0
926     while True:
927       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
928       if i == -1:
929         break
930       j = find_end_of_inset(document.body, i)
931       l = find_token(document.body, "width", i, j)
932       if l == -1:
933         document.warning("Can't find width option for nomencl_print!")
934         i = j
935         continue
936       width = get_value(document.body, "width", i, j).strip('"')
937       del document.body[l]
938       add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
939       add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
940       i = j - 1
941
942
943 def revert_applemac(document):
944     " Revert applemac encoding to auto "
945     if document.encoding != "applemac":
946       return
947     document.encoding = "auto"
948     i = find_token(document.header, "\\encoding", 0)
949     if i != -1:
950         document.header[i] = "\\encoding auto"
951
952
953 def revert_longtable_align(document):
954     " Remove longtable alignment setting "
955     i = 0
956     while True:
957       i = find_token(document.body, "\\begin_inset Tabular", i)
958       if i == -1:
959           break
960       end = find_end_of_inset(document.body, i)
961       if end == -1:
962           document.warning("Can't find end of inset at line " + str(i))
963           i += 1
964           continue
965       fline = find_token(document.body, "<features", i, end)
966       if fline == -1:
967           document.warning("Can't find features for inset at line " + str(i))
968           i += 1
969           continue
970       j = document.body[fline].find("longtabularalignment")
971       if j == -1:
972           i += 1
973           continue
974       # FIXME Is this correct? It wipes out everything after the 
975       # one we found.
976       document.body[fline] = document.body[fline][:j - 1] + '>'
977       # since there could be a tabular inside this one, we 
978       # cannot jump to end.
979       i += 1
980
981
982 def revert_branch_filename(document):
983     " Remove \\filename_suffix parameter from branches "
984     i = 0
985     while True:
986         i = find_token(document.header, "\\filename_suffix", i)
987         if i == -1:
988             return
989         del document.header[i]
990
991
992 def revert_paragraph_indentation(document):
993     " Revert custom paragraph indentation to preamble code "
994     i = find_token(document.header, "\\paragraph_indentation", 0)
995     if i == -1:
996       return
997     length = get_value(document.header, "\\paragraph_indentation", i)
998     # we need only remove the line if indentation is default
999     if length != "default":
1000       # handle percent lengths
1001       length = latex_length(length)[1]
1002       add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1003       add_to_preamble(document, ["\\setlength{\\parindent}{" + length + "}"])
1004     del document.header[i]
1005
1006
1007 def revert_percent_skip_lengths(document):
1008     " Revert relative lengths for paragraph skip separation to preamble code "
1009     i = find_token(document.header, "\\defskip", 0)
1010     if i == -1:
1011         return
1012     length = get_value(document.header, "\\defskip", i)
1013     # only revert when a custom length was set and when
1014     # it used a percent length
1015     if length in ('smallskip', 'medskip', 'bigskip'):
1016         return
1017     # handle percent lengths
1018     percent, length = latex_length(length)
1019     if percent == "True":
1020         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1021         add_to_preamble(document, ["\\setlength{\\parskip}{" + length + "}"])
1022         # set defskip to medskip as default
1023         document.header[i] = "\\defskip medskip"
1024
1025
1026 def revert_percent_vspace_lengths(document):
1027     " Revert relative VSpace lengths to ERT "
1028     i = 0
1029     while True:
1030       i = find_token(document.body, "\\begin_inset VSpace", i)
1031       if i == -1:
1032           break
1033       # only revert if a custom length was set and if
1034       # it used a percent length
1035       r = re.compile(r'\\begin_inset VSpace (.*)$')
1036       m = r.match(document.body[i])
1037       length = m.group(1)
1038       if length in ('defskip', 'smallskip', 'medskip', 'bigskip', 'vfill'):
1039          i += 1
1040          continue
1041       # check if the space has a star (protected space)
1042       protected = (document.body[i].rfind("*") != -1)
1043       if protected:
1044           length = length.rstrip('*')
1045       # handle percent lengths
1046       percent, length = latex_length(length)
1047       # revert the VSpace inset to ERT
1048       if percent == "True":
1049           if protected:
1050               subst = put_cmd_in_ert("\\vspace*{" + length + "}")
1051           else:
1052               subst = put_cmd_in_ert("\\vspace{" + length + "}")
1053           document.body[i:i + 2] = subst
1054       i += 1
1055
1056
1057 def revert_percent_hspace_lengths(document):
1058     " Revert relative HSpace lengths to ERT "
1059     i = 0
1060     while True:
1061       i = find_token(document.body, "\\begin_inset space \\hspace", i)
1062       if i == -1:
1063           break
1064       j = find_end_of_inset(document.body, i)
1065       if j == -1:
1066           document.warning("Can't find end of inset at line " + str(i))
1067           i += 1
1068           continue
1069       # only revert if a custom length was set...
1070       length = get_value(document.body, '\\length', i + 1, j)
1071       if length == '':
1072           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1073           i = j
1074           continue
1075       protected = ""
1076       if document.body[i].find("\\hspace*{}") != -1:
1077           protected = "*"
1078       # ...and if it used a percent length
1079       percent, length = latex_length(length)
1080       # revert the HSpace inset to ERT
1081       if percent == "True":
1082           subst = put_cmd_in_ert("\\hspace" + protected + "{" + length + "}")
1083           document.body[i:j + 1] = subst
1084       # if we did a substitution, this will still be ok
1085       i = j
1086
1087
1088 def revert_hspace_glue_lengths(document):
1089     " Revert HSpace glue lengths to ERT "
1090     i = 0
1091     while True:
1092       i = find_token(document.body, "\\begin_inset space \\hspace", i)
1093       if i == -1:
1094           break
1095       j = find_end_of_inset(document.body, i)
1096       if j == -1:
1097           document.warning("Can't find end of inset at line " + str(i))
1098           i += 1
1099           continue
1100       length = get_value(document.body, '\\length', i + 1, j)
1101       if length == '':
1102           document.warning("Malformed lyx document: Missing '\\length' in Space inset.")
1103           i = j
1104           continue
1105       protected = ""
1106       if document.body[i].find("\\hspace*{}") != -1:
1107           protected = "*"
1108       # only revert if the length contains a plus or minus at pos != 0
1109       if length.find('-',1) != -1 or length.find('+',1) != -1:
1110           # handle percent lengths
1111           length = latex_length(length)[1]
1112           # revert the HSpace inset to ERT
1113           subst = put_cmd_in_ert("\\hspace" + protected + "{" + length + "}")
1114           document.body[i:j+1] = subst
1115       i = j
1116
1117
1118 def convert_author_id(document):
1119     " Add the author_id to the \\author definition and make sure 0 is not used"
1120     i = 0
1121     anum = 1
1122     re_author = re.compile(r'(\\author) (\".*\")\s*(.*)$')
1123     
1124     while True:
1125         i = find_token(document.header, "\\author", i)
1126         if i == -1:
1127             break
1128         m = re_author.match(document.header[i])
1129         if m:
1130             name = m.group(2)
1131             email = m.group(3)
1132             document.header[i] = "\\author %i %s %s" % (anum, name, email)
1133         # FIXME Should this really be incremented if we didn't match?
1134         anum += 1
1135         i += 1
1136         
1137     i = 0
1138     while True:
1139         i = find_token(document.body, "\\change_", i)
1140         if i == -1:
1141             break
1142         change = document.body[i].split(' ');
1143         if len(change) == 3:
1144             type = change[0]
1145             author_id = int(change[1])
1146             time = change[2]
1147             document.body[i] = "%s %i %s" % (type, author_id + 1, time)
1148         i += 1
1149
1150
1151 def revert_author_id(document):
1152     " Remove the author_id from the \\author definition "
1153     i = 0
1154     anum = 0
1155     rx = re.compile(r'(\\author)\s+(\d+)\s+(\".*\")\s*(.*)$')
1156     idmap = dict()
1157
1158     while True:
1159         i = find_token(document.header, "\\author", i)
1160         if i == -1:
1161             break
1162         m = rx.match(document.header[i])
1163         if m:
1164             author_id = int(m.group(2))
1165             idmap[author_id] = anum
1166             name = m.group(3)
1167             email = m.group(4)
1168             document.header[i] = "\\author %s %s" % (name, email)
1169         i += 1
1170         # FIXME Should this be incremented if we didn't match?
1171         anum += 1
1172
1173     i = 0
1174     while True:
1175         i = find_token(document.body, "\\change_", i)
1176         if i == -1:
1177             break
1178         change = document.body[i].split(' ');
1179         if len(change) == 3:
1180             type = change[0]
1181             author_id = int(change[1])
1182             time = change[2]
1183             document.body[i] = "%s %i %s" % (type, idmap[author_id], time)
1184         i += 1
1185
1186
1187 def revert_suppress_date(document):
1188     " Revert suppressing of default document date to preamble code "
1189     i = find_token(document.header, "\\suppress_date", 0)
1190     if i == -1:
1191         return
1192     # remove the preamble line and write to the preamble
1193     # when suppress_date was true
1194     date = get_value(document.header, "\\suppress_date", i)
1195     if date == "true":
1196         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
1197         add_to_preamble(document, ["\\date{}"])
1198     del document.header[i]
1199
1200
1201 def revert_mhchem(document):
1202     "Revert mhchem loading to preamble code"
1203
1204     mhchem = "off"
1205     i = find_token(document.header, "\\use_mhchem", 0)
1206     if i == -1:
1207         document.warning("Malformed LyX document: Could not find mhchem setting.")
1208         mhchem = "auto"
1209     else:
1210         val = get_value(document.header, "\\use_mhchem", i)
1211         if val == "1":
1212             mhchem = "auto"
1213         elif val == "2":
1214             mhchem = "on"
1215         del document.header[i]
1216
1217     if mhchem == "auto":
1218         i = 0
1219         while True:
1220             i = find_token(document.body, "\\begin_inset Formula", i)
1221             if i == -1:
1222                break
1223             line = document.body[i]
1224             if line.find("\\ce{") != -1 or line.find("\\cf{") != 1:
1225               mhchem = "on"
1226               break
1227             i += 1
1228
1229     if mhchem == "on":
1230         pre = ["% lyx2lyx mhchem commands", 
1231           "\\PassOptionsToPackage{version=3}{mhchem}", 
1232           "\\usepackage{mhchem}"]
1233         add_to_preamble(document, pre) 
1234
1235
1236 def revert_fontenc(document):
1237     " Remove fontencoding param "
1238     i = find_token(document.header, '\\fontencoding', 0)
1239     if i == -1:
1240         document.warning("Malformed LyX document: Missing \\fontencoding.")
1241         return
1242     del document.header[i]
1243
1244
1245 def merge_gbrief(document):
1246     " Merge g-brief-en and g-brief-de to one class "
1247
1248     if document.textclass != "g-brief-de":
1249         if document.textclass == "g-brief-en":
1250             document.textclass = "g-brief"
1251             document.set_textclass()
1252         return
1253
1254     obsoletedby = { "Brieftext":       "Letter",
1255                     "Unterschrift":    "Signature",
1256                     "Strasse":         "Street",
1257                     "Zusatz":          "Addition",
1258                     "Ort":             "Town",
1259                     "Land":            "State",
1260                     "RetourAdresse":   "ReturnAddress",
1261                     "MeinZeichen":     "MyRef",
1262                     "IhrZeichen":      "YourRef",
1263                     "IhrSchreiben":    "YourMail",
1264                     "Telefon":         "Phone",
1265                     "BLZ":             "BankCode",
1266                     "Konto":           "BankAccount",
1267                     "Postvermerk":     "PostalComment",
1268                     "Adresse":         "Address",
1269                     "Datum":           "Date",
1270                     "Betreff":         "Reference",
1271                     "Anrede":          "Opening",
1272                     "Anlagen":         "Encl.",
1273                     "Verteiler":       "cc",
1274                     "Gruss":           "Closing"}
1275     i = 0
1276     while 1:
1277         i = find_token(document.body, "\\begin_layout", i)
1278         if i == -1:
1279             break
1280
1281         layout = document.body[i][14:]
1282         if layout in obsoletedby:
1283             document.body[i] = "\\begin_layout " + obsoletedby[layout]
1284
1285         i += 1
1286         
1287     document.textclass = "g-brief"
1288     document.set_textclass()
1289
1290
1291 def revert_gbrief(document):
1292     " Revert g-brief to g-brief-en "
1293     if document.textclass == "g-brief":
1294         document.textclass = "g-brief-en"
1295         document.set_textclass()
1296
1297
1298 def revert_html_options(document):
1299     " Remove html options "
1300     i = find_token(document.header, '\\html_use_mathml', 0)
1301     if i != -1:
1302         del document.header[i]
1303     i = find_token(document.header, '\\html_be_strict', 0)
1304     if i != -1:
1305         del document.header[i]
1306
1307
1308 def revert_includeonly(document):
1309     i = 0
1310     while True:
1311         i = find_token(document.header, "\\begin_includeonly", i)
1312         if i == -1:
1313             return
1314         j = find_end_of(document.header, i, "\\begin_includeonly", "\\end_includeonly")
1315         if j == -1:
1316             document.warning("Unable to find end of includeonly section!!")
1317             break
1318         document.header[i : j + 1] = []
1319
1320
1321 def revert_includeall(document):
1322     " Remove maintain_unincluded_children param "
1323     i = find_token(document.header, '\\maintain_unincluded_children', 0)
1324     if i != -1:
1325         del document.header[i]
1326
1327
1328 def revert_multirow(document):
1329     " Revert multirow cells in tables to TeX-code"
1330     i = 0
1331     multirow = False
1332     while True:
1333       # cell type 3 is multirow begin cell
1334       i = find_token(document.body, '<cell multirow="3"', i)
1335       if i == -1:
1336           break
1337       # a multirow cell was found
1338       multirow = True
1339       # remove the multirow tag, set the valignment to top
1340       # and remove the bottom line
1341       # FIXME Are we sure these always have space around them?
1342       document.body[i] = document.body[i].replace(' multirow="3" ', ' ')
1343       document.body[i] = document.body[i].replace('valignment="middle"', 'valignment="top"')
1344       document.body[i] = document.body[i].replace(' bottomline="true" ', ' ')
1345       # write ERT to create the multirow cell
1346       # use 2 rows and 2cm as default with because the multirow span
1347       # and the column width is only hardly accessible
1348       cend = find_token(document.body, "</cell>", i)
1349       if cend == -1:
1350           document.warning("Malformed LyX document: Could not find end of tabular cell.")
1351           i += 1
1352           continue
1353       blay = find_token(document.body, "\\begin_layout", i, cend)
1354       if blay == -1:
1355           document.warning("Can't find layout for cell!")
1356           i = j
1357           continue
1358       bend = find_end_of_layout(document.body, blay)
1359       if blay == -1:
1360           document.warning("Can't find end of layout for cell!")
1361           i = cend
1362           continue
1363
1364       # do the later one first, so as not to mess up the numbering
1365       # we are wrapping the whole cell in this ert
1366       # so before the end of the layout...
1367       document.body[bend:bend] = put_cmd_in_ert("}")
1368       # ...and after the beginning
1369       document.body[blay+1:blay+1] = put_cmd_in_ert("\\multirow{2}{2cm}{")
1370
1371       while True:
1372           # cell type 4 is multirow part cell
1373           k = find_token(document.body, '<cell multirow="4"', cend)
1374           if k == -1:
1375               break
1376           # remove the multirow tag, set the valignment to top
1377           # and remove the top line
1378           # FIXME Are we sure these always have space around them?
1379           document.body[k] = document.body[k].replace(' multirow="4" ', ' ')
1380           document.body[k] = document.body[k].replace('valignment="middle"', 'valignment="top"')
1381           document.body[k] = document.body[k].replace(' topline="true" ', ' ')
1382           k += 1
1383       # this will always be ok
1384       i = cend
1385
1386     if multirow == True:
1387         add_to_preamble(document, 
1388           ["% lyx2lyx multirow additions ", "\\usepackage{multirow}"])
1389
1390
1391 def convert_math_output(document):
1392     " Convert \html_use_mathml to \html_math_output "
1393     i = find_token(document.header, "\\html_use_mathml", 0)
1394     if i == -1:
1395         return
1396     rgx = re.compile(r'\\html_use_mathml\s+(\w+)')
1397     m = rgx.match(document.header[i])
1398     newval = "0" # MathML
1399     if m:
1400       val = m.group(1)
1401       if val != "true":
1402         newval = "2" # Images
1403     else:
1404       document.warning("Can't match " + document.header[i])
1405     document.header[i] = "\\html_math_output " + newval
1406
1407
1408 def revert_math_output(document):
1409     " Revert \html_math_output to \html_use_mathml "
1410     i = find_token(document.header, "\\html_math_output", 0)
1411     if i == -1:
1412         return
1413     rgx = re.compile(r'\\html_math_output\s+(\d)')
1414     m = rgx.match(document.header[i])
1415     newval = "true"
1416     if m:
1417         val = m.group(1)
1418         if val == "1" or val == "2":
1419             newval = "false"
1420     else:
1421         document.warning("Unable to match " + document.header[i])
1422     document.header[i] = "\\html_use_mathml " + newval
1423                 
1424
1425
1426 def revert_inset_preview(document):
1427     " Dissolves the preview inset "
1428     i = 0
1429     while True:
1430       i = find_token(document.body, "\\begin_inset Preview", i)
1431       if i == -1:
1432           return
1433       iend = find_end_of_inset(document.body, i)
1434       if iend == -1:
1435           document.warning("Malformed LyX document: Could not find end of Preview inset.")
1436           i += 1
1437           continue
1438       
1439       # This has several issues.
1440       # We need to do something about the layouts inside InsetPreview.
1441       # If we just leave the first one, then we have something like:
1442       # \begin_layout Standard
1443       # ...
1444       # \begin_layout Standard
1445       # and we get a "no \end_layout" error. So something has to be done.
1446       # Ideally, we would check if it is the same as the layout we are in.
1447       # If so, we just remove it; if not, we end the active one. But it is 
1448       # not easy to know what layout we are in, due to depth changes, etc,
1449       # and it is not clear to me how much work it is worth doing. In most
1450       # cases, the layout will probably be the same.
1451       # 
1452       # For the same reason, we have to remove the \end_layout tag at the
1453       # end of the last layout in the inset. Again, that will sometimes be
1454       # wrong, but it will usually be right. To know what to do, we would
1455       # again have to know what layout the inset is in.
1456       
1457       blay = find_token(document.body, "\\begin_layout", i, iend)
1458       if blay == -1:
1459           document.warning("Can't find layout for preview inset!")
1460           # always do the later one first...
1461           del document.body[iend]
1462           del document.body[i]
1463           # deletions mean we do not need to reset i
1464           continue
1465
1466       # This is where we would check what layout we are in.
1467       # The check for Standard is definitely wrong.
1468       # 
1469       # lay = document.body[blay].split(None, 1)[1]
1470       # if lay != oldlayout:
1471       #     # record a boolean to tell us what to do later....
1472       #     # better to do it later, since (a) it won't mess up
1473       #     # the numbering and (b) we only modify at the end.
1474         
1475       # we want to delete the last \\end_layout in this inset, too.
1476       # note that this may not be the \\end_layout that goes with blay!!
1477       bend = find_end_of_layout(document.body, blay)
1478       while True:
1479           tmp = find_token(document.body, "\\end_layout", bend + 1, iend)
1480           if tmp == -1:
1481               break
1482           bend = tmp
1483       if bend == blay:
1484           document.warning("Unable to find last layout in preview inset!")
1485           del document.body[iend]
1486           del document.body[i]
1487           # deletions mean we do not need to reset i
1488           continue
1489       # always do the later one first...
1490       del document.body[iend]
1491       del document.body[bend]
1492       del document.body[i:blay + 1]
1493       # we do not need to reset i
1494                 
1495
1496 def revert_equalspacing_xymatrix(document):
1497     " Revert a Formula with xymatrix@! to an ERT inset "
1498     i = 0
1499     has_preamble = False
1500     has_equal_spacing = False
1501
1502     while True:
1503       i = find_token(document.body, "\\begin_inset Formula", i)
1504       if i == -1:
1505           break
1506       j = find_end_of_inset(document.body, i)
1507       if j == -1:
1508           document.warning("Malformed LyX document: Could not find end of Formula inset.")
1509           i += 1
1510           continue
1511       
1512       for curline in range(i,j):
1513           found = document.body[curline].find("\\xymatrix@!")
1514           if found != -1:
1515               break
1516  
1517       if found != -1:
1518           has_equal_spacing = True
1519           content = [document.body[i][21:]]
1520           content += document.body[i + 1:j]
1521           subst = put_cmd_in_ert(content)
1522           document.body[i:j + 1] = subst
1523           i += len(subst) - (j - i) + 1
1524       else:
1525           for curline in range(i,j):
1526               l = document.body[curline].find("\\xymatrix")
1527               if l != -1:
1528                   has_preamble = True;
1529                   break;
1530           i = j + 1
1531   
1532     if has_equal_spacing and not has_preamble:
1533         add_to_preamble(document, ['% lyx2lyx xymatrix addition', '\\usepackage[all]{xy}'])
1534
1535
1536 def revert_notefontcolor(document):
1537     " Reverts greyed-out note font color to preamble code "
1538
1539     i = find_token(document.header, "\\notefontcolor", 0)
1540     if i == -1:
1541         return
1542     colorcode = get_value(document.header, '\\notefontcolor', i)
1543     del document.header[i]
1544     # the color code is in the form #rrggbb where every character denotes a hex number
1545     red = hex2ratio(colorcode[1:3])
1546     green = hex2ratio(colorcode[3:5])
1547     blue = hex2ratio(colorcode[5:7])
1548     # write the preamble
1549     insert_to_preamble(0, document,
1550       ['% Commands inserted by lyx2lyx to set the font color',
1551         '% for greyed-out notes',
1552         '\\@ifundefined{definecolor}{\\usepackage{color}}{}'
1553         '\\definecolor{note_fontcolor}{rgb}{'
1554           + str(red) + ', ' + str(green)
1555           + ', ' + str(blue) + '}',
1556         '\\renewenvironment{lyxgreyedout}',
1557         ' {\\textcolor{note_fontcolor}\\bgroup}{\\egroup}'])
1558
1559
1560 def revert_turkmen(document):
1561     "Set language Turkmen to English" 
1562     i = 0 
1563     if document.language == "turkmen": 
1564         document.language = "english" 
1565         i = find_token(document.header, "\\language", 0) 
1566         if i != -1: 
1567             document.header[i] = "\\language english" 
1568     j = 0 
1569     while True: 
1570         j = find_token(document.body, "\\lang turkmen", j) 
1571         if j == -1: 
1572             return 
1573         document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english") 
1574         j = j + 1 
1575
1576
1577 def revert_fontcolor(document):
1578     " Reverts font color to preamble code "
1579     i = 0
1580     colorcode = ""
1581     while True:
1582       i = find_token(document.header, "\\fontcolor", i)
1583       if i == -1:
1584           return
1585       colorcode = get_value(document.header, '\\fontcolor', 0)
1586       del document.header[i]
1587       # don't clutter the preamble if backgroundcolor is not set
1588       if colorcode == "#000000":
1589           continue
1590       # the color code is in the form #rrggbb where every character denotes a hex number
1591       # convert the string to an int
1592       red = string.atoi(colorcode[1:3],16)
1593       # we want the output "0.5" for the value "127" therefore add here
1594       if red != 0:
1595           red = red + 1
1596       redout = float(red) / 256
1597       green = string.atoi(colorcode[3:5],16)
1598       if green != 0:
1599           green = green + 1
1600       greenout = float(green) / 256
1601       blue = string.atoi(colorcode[5:7],16)
1602       if blue != 0:
1603           blue = blue + 1
1604       blueout = float(blue) / 256
1605       # write the preamble
1606       insert_to_preamble(0, document,
1607                            '% Commands inserted by lyx2lyx to set the font color\n'
1608                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1609                            + '\\definecolor{document_fontcolor}{rgb}{'
1610                            + str(redout) + ', ' + str(greenout)
1611                            + ', ' + str(blueout) + '}\n'
1612                            + '\\color{document_fontcolor}\n')
1613
1614 def revert_shadedboxcolor(document):
1615     " Reverts shaded box color to preamble code "
1616     i = 0
1617     colorcode = ""
1618     while True:
1619       i = find_token(document.header, "\\boxbgcolor", i)
1620       if i == -1:
1621           return
1622       colorcode = get_value(document.header, '\\boxbgcolor', 0)
1623       del document.header[i]
1624       # the color code is in the form #rrggbb where every character denotes a hex number
1625       # convert the string to an int
1626       red = string.atoi(colorcode[1:3],16)
1627       # we want the output "0.5" for the value "127" therefore increment here
1628       if red != 0:
1629           red = red + 1
1630       redout = float(red) / 256
1631       green = string.atoi(colorcode[3:5],16)
1632       if green != 0:
1633           green = green + 1
1634       greenout = float(green) / 256
1635       blue = string.atoi(colorcode[5:7],16)
1636       if blue != 0:
1637           blue = blue + 1
1638       blueout = float(blue) / 256
1639       # write the preamble
1640       insert_to_preamble(0, document,
1641                            '% Commands inserted by lyx2lyx to set the color\n'
1642                            '% of boxes with shaded background\n'
1643                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
1644                            + '\\definecolor{shadecolor}{rgb}{'
1645                            + str(redout) + ', ' + str(greenout)
1646                            + ', ' + str(blueout) + '}\n')
1647
1648
1649 def revert_lyx_version(document):
1650     " Reverts LyX Version information from Inset Info "
1651     version = "LyX version"
1652     try:
1653         import lyx2lyx_version
1654         version = lyx2lyx_version.version
1655     except:
1656         pass
1657
1658     i = 0
1659     while 1:
1660         i = find_token(document.body, '\\begin_inset Info', i)
1661         if i == -1:
1662             return
1663         j = find_end_of_inset(document.body, i + 1)
1664         if j == -1:
1665             # should not happen
1666             document.warning("Malformed LyX document: Could not find end of Info inset.")
1667         # We expect:
1668         # \begin_inset Info
1669         # type  "lyxinfo"
1670         # arg   "version"
1671         # \end_inset
1672         # but we shall try to be forgiving.
1673         arg = typ = ""
1674         for k in range(i, j):
1675             if document.body[k].startswith("arg"):
1676                 arg = document.body[k][3:].strip().strip('"')
1677             if document.body[k].startswith("type"):
1678                 typ = document.body[k][4:].strip().strip('"')
1679         if arg != "version" or typ != "lyxinfo":
1680             i = j + 1
1681             continue
1682
1683         # We do not actually know the version of LyX used to produce the document.
1684         # But we can use our version, since we are reverting.
1685         s = [version]
1686         # Now we want to check if the line after "\end_inset" is empty. It normally
1687         # is, so we want to remove it, too.
1688         lastline = j + 1
1689         if document.body[j + 1].strip() == "":
1690             lastline = j + 2
1691         document.body[i: lastline] = s
1692         i = i + 1
1693
1694
1695 def revert_math_scale(document):
1696   " Remove math scaling and LaTeX options "
1697   i = find_token(document.header, '\\html_math_img_scale', 0)
1698   if i != -1:
1699     del document.header[i]
1700   i = find_token(document.header, '\\html_latex_start', 0)
1701   if i != -1:
1702     del document.header[i]
1703   i = find_token(document.header, '\\html_latex_end', 0)
1704   if i != -1:
1705     del document.header[i]
1706
1707
1708 def revert_pagesizes(document):
1709   i = 0
1710   " Revert page sizes to default "
1711   i = find_token(document.header, '\\papersize', 0)
1712   if i != -1:
1713     size = document.header[i][11:]
1714     if size == "a0paper" or size == "a1paper" or size == "a2paper" \
1715     or size == "a6paper" or size == "b0paper" or size == "b1paper" \
1716     or size == "b2paper" or size == "b6paper" or size == "b0j" \
1717     or size == "b1j" or size == "b2j" or size == "b3j" or size == "b4j" \
1718     or size == "b5j" or size == "b6j":
1719       del document.header[i]
1720
1721
1722 def revert_DIN_C_pagesizes(document):
1723   i = 0
1724   " Revert DIN C page sizes to default "
1725   i = find_token(document.header, '\\papersize', 0)
1726   if i != -1:
1727     size = document.header[i][11:]
1728     if size == "c0paper" or size == "c1paper" or size == "c2paper" \
1729     or size == "c3paper" or size == "c4paper" or size == "c5paper" \
1730     or size == "c6paper":
1731       del document.header[i]
1732
1733
1734 def convert_html_quotes(document):
1735   " Remove quotes around html_latex_start and html_latex_end "
1736
1737   i = find_token(document.header, '\\html_latex_start', 0)
1738   if i != -1:
1739     line = document.header[i]
1740     l = re.compile(r'\\html_latex_start\s+"(.*)"')
1741     m = l.match(line)
1742     if m != None:
1743       document.header[i] = "\\html_latex_start " + m.group(1)
1744       
1745   i = find_token(document.header, '\\html_latex_end', 0)
1746   if i != -1:
1747     line = document.header[i]
1748     l = re.compile(r'\\html_latex_end\s+"(.*)"')
1749     m = l.match(line)
1750     if m != None:
1751       document.header[i] = "\\html_latex_end " + m.group(1)
1752       
1753
1754 def revert_html_quotes(document):
1755   " Remove quotes around html_latex_start and html_latex_end "
1756   
1757   i = find_token(document.header, '\\html_latex_start', 0)
1758   if i != -1:
1759     line = document.header[i]
1760     l = re.compile(r'\\html_latex_start\s+(.*)')
1761     m = l.match(line)
1762     document.header[i] = "\\html_latex_start \"" + m.group(1) + "\""
1763       
1764   i = find_token(document.header, '\\html_latex_end', 0)
1765   if i != -1:
1766     line = document.header[i]
1767     l = re.compile(r'\\html_latex_end\s+(.*)')
1768     m = l.match(line)
1769     document.header[i] = "\\html_latex_end \"" + m.group(1) + "\""
1770
1771
1772 def revert_output_sync(document):
1773   " Remove forward search options "
1774   i = find_token(document.header, '\\output_sync_macro', 0)
1775   if i != -1:
1776     del document.header[i]
1777   i = find_token(document.header, '\\output_sync', 0)
1778   if i != -1:
1779     del document.header[i]
1780
1781
1782 def convert_beamer_args(document):
1783   " Convert ERT arguments in Beamer to InsetArguments "
1784
1785   if document.textclass != "beamer" and document.textclass != "article-beamer":
1786     return
1787   
1788   layouts = ("Block", "ExampleBlock", "AlertBlock")
1789   for layout in layouts:
1790     blay = 0
1791     while True:
1792       blay = find_token(document.body, '\\begin_layout ' + layout, blay)
1793       if blay == -1:
1794         break
1795       elay = find_end_of(document.body, blay, '\\begin_layout', '\\end_layout')
1796       if elay == -1:
1797         document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1798         blay += 1
1799         continue
1800       bert = find_token(document.body, '\\begin_inset ERT', blay)
1801       if bert == -1:
1802         document.warning("Malformed Beamer LyX document: Can't find argument of " + layout + " layout.")
1803         blay = elay + 1
1804         continue
1805       eert = find_end_of_inset(document.body, bert)
1806       if eert == -1:
1807         document.warning("Malformed LyX document: Can't find end of ERT.")
1808         blay = elay + 1
1809         continue
1810       
1811       # So the ERT inset begins at line k and goes to line l. We now wrap it in 
1812       # an argument inset.
1813       # Do the end first, so as not to mess up the variables.
1814       document.body[eert + 1:eert + 1] = ['', '\\end_layout', '', '\\end_inset', '']
1815       document.body[bert:bert] = ['\\begin_inset OptArg', 'status open', '', 
1816           '\\begin_layout Plain Layout']
1817       blay = elay + 9
1818
1819
1820 def revert_beamer_args(document):
1821   " Revert Beamer arguments to ERT "
1822   
1823   if document.textclass != "beamer" and document.textclass != "article-beamer":
1824     return
1825     
1826   layouts = ("Block", "ExampleBlock", "AlertBlock")
1827   for layout in layouts:
1828     blay = 0
1829     while True:
1830       blay = find_token(document.body, '\\begin_layout ' + layout, blay)
1831       if blay == -1:
1832         break
1833       elay = find_end_of(document.body, blay, '\\begin_layout', '\\end_layout')
1834       if elay == -1:
1835         document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1836         blay += 1
1837         continue
1838       bopt = find_token(document.body, '\\begin_inset OptArg', blay)
1839       if bopt == -1:
1840         # it is legal not to have one of these
1841         blay = elay + 1
1842         continue
1843       eopt = find_end_of_inset(document.body, bopt)
1844       if eopt == -1:
1845         document.warning("Malformed LyX document: Can't find end of argument.")
1846         blay = elay + 1
1847         continue
1848       bplay = find_token(document.body, '\\begin_layout Plain Layout', blay)
1849       if bplay == -1:
1850         document.warning("Malformed LyX document: Can't find plain layout.")
1851         blay = elay + 1
1852         continue
1853       eplay = find_end_of(document.body, bplay, '\\begin_layout', '\\end_layout')
1854       if eplay == -1:
1855         document.warning("Malformed LyX document: Can't find end of plain layout.")
1856         blay = elay + 1
1857         continue
1858       # So the content of the argument inset goes from bplay + 1 to eplay - 1
1859       bcont = bplay + 1
1860       if bcont >= eplay:
1861         # Hmm.
1862         document.warning(str(bcont) + " " + str(eplay))
1863         blay = blay + 1
1864         continue
1865       # we convert the content of the argument into pure LaTeX...
1866       content = lyx2latex(document, document.body[bcont:eplay])
1867       strlist = put_cmd_in_ert(["{" + content + "}"])
1868       
1869       # now replace the optional argument with the ERT
1870       document.body[bopt:eopt + 1] = strlist
1871       blay = blay + 1
1872
1873
1874 def revert_align_decimal(document):
1875   l = 0
1876   while True:
1877     l = document.body[l].find('alignment=decimal')
1878     if l == -1:
1879         break
1880     remove_option(document, l, 'decimal_point')
1881     document.body[l].replace('decimal', 'center')
1882
1883
1884 def convert_optarg(document):
1885   " Convert \\begin_inset OptArg to \\begin_inset Argument "
1886   i = 0
1887   while 1:
1888     i = find_token(document.body, '\\begin_inset OptArg', i)
1889     if i == -1:
1890       return
1891     document.body[i] = "\\begin_inset Argument"
1892     i += 1
1893
1894
1895 def revert_argument(document):
1896   " Convert \\begin_inset Argument to \\begin_inset OptArg "
1897   i = 0
1898   while 1:
1899     i = find_token(document.body, '\\begin_inset Argument', i)
1900     if i == -1:
1901       return
1902     document.body[i] = "\\begin_inset OptArg"
1903     i += 1
1904
1905
1906 def revert_makebox(document):
1907   " Convert \\makebox to TeX code "
1908   i = 0
1909   while 1:
1910     # only revert frameless boxes without an inner box
1911     i = find_token(document.body, '\\begin_inset Box Frameless', i)
1912     if i == -1:
1913       # remove the option use_makebox
1914       revert_use_makebox(document)
1915       return
1916     z = find_end_of_inset(document.body, i)
1917     if z == -1:
1918       document.warning("Malformed LyX document: Can't find end of box inset.")
1919       return
1920     j = find_token(document.body, 'use_makebox 1', i)
1921     # assure we found the makebox of the current box
1922     if j < z and j != -1:
1923       y = find_token(document.body, "\\begin_layout", i)
1924       if y > z or y == -1:
1925         document.warning("Malformed LyX document: Can't find layout in box.")
1926         return
1927       # remove the \end_layout \end_inset pair
1928       document.body[z - 2:z + 1] = put_cmd_in_ert("}")
1929       # determine the alignment
1930       k = find_token(document.body, 'hor_pos', j - 4)
1931       align = document.body[k][9]
1932       # determine the width
1933       l = find_token(document.body, 'width "', j + 1)
1934       length = document.body[l][7:]
1935       # remove trailing '"'
1936       length = length[:-1]
1937       length = latex_length(length)[1]
1938       subst = "\\makebox[" + length + "][" \
1939         + align + "]{"
1940       document.body[i:y + 1] = put_cmd_in_ert(subst)
1941     i += 1
1942
1943
1944 def revert_use_makebox(document):
1945   " Deletes use_makebox option of boxes "
1946   h = 0
1947   while 1:
1948     # remove the option use_makebox
1949     h = find_token(document.body, 'use_makebox', 0)
1950     if h == -1:
1951       return
1952     del document.body[h]
1953     h += 1
1954
1955
1956 def convert_use_makebox(document):
1957   " Adds use_makebox option for boxes "
1958   i = 0
1959   while 1:
1960     # remove the option use_makebox
1961     i = find_token(document.body, '\\begin_inset Box', i)
1962     if i == -1:
1963       return
1964     k = find_token(document.body, 'use_parbox', i)
1965     if k == -1:
1966       document.warning("Malformed LyX document: Can't find use_parbox statement in box.")
1967       return
1968     document.body.insert(k + 1, "use_makebox 0")
1969     i = k + 1
1970
1971
1972 def revert_IEEEtran(document):
1973   " Convert IEEEtran layouts and styles to TeX code "
1974   if document.textclass != "IEEEtran":
1975     return
1976   revert_flex_inset(document, "IEEE membership", "\\IEEEmembership", 0)
1977   revert_flex_inset(document, "Lowercase", "\\MakeLowercase", 0)
1978   layouts = ("Special Paper Notice", "After Title Text", "Publication ID",
1979              "Page headings", "Biography without photo")
1980   latexcmd = {"Special Paper Notice": "\\IEEEspecialpapernotice",
1981               "After Title Text":     "\\IEEEaftertitletext",
1982               "Publication ID":       "\\IEEEpubid"}
1983   obsoletedby = {"Page headings":            "MarkBoth",
1984                  "Biography without photo":  "BiographyNoPhoto"}
1985   for layout in layouts:
1986     i = 0
1987     while True:
1988         i = find_token(document.body, '\\begin_layout ' + layout, i)
1989         if i == -1:
1990           break
1991         j = find_end_of(document.body, i, '\\begin_layout', '\\end_layout')
1992         if j == -1:
1993           document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
1994           i += 1
1995           continue
1996         if layout in obsoletedby:
1997           document.body[i] = "\\begin_layout " + obsoletedby[layout]
1998           i = j
1999         else:
2000           content = lyx2latex(document, document.body[i:j + 1])
2001           add_to_preamble(document, [latexcmd[layout] + "{" + content + "}"])
2002           del document.body[i:j + 1]
2003
2004
2005 def convert_prettyref(document):
2006         " Converts prettyref references to neutral formatted refs "
2007         re_ref = re.compile("^\s*reference\s+\"(\w+):(\S+)\"")
2008         nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
2009
2010         i = 0
2011         while True:
2012                 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
2013                 if i == -1:
2014                         break
2015                 j = find_end_of_inset(document.body, i)
2016                 if j == -1:
2017                         document.warning("Malformed LyX document: No end of InsetRef!")
2018                         i += 1
2019                         continue
2020                 k = find_token(document.body, "LatexCommand prettyref", i)
2021                 if k != -1 and k < j:
2022                         document.body[k] = "LatexCommand formatted"
2023                 i = j + 1
2024         document.header.insert(-1, "\\use_refstyle 0")
2025                 
2026  
2027 def revert_refstyle(document):
2028         " Reverts neutral formatted refs to prettyref "
2029         re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
2030         nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
2031
2032         i = 0
2033         while True:
2034                 i = find_token(document.body, "\\begin_inset CommandInset ref", i)
2035                 if i == -1:
2036                         break
2037                 j = find_end_of_inset(document.body, i)
2038                 if j == -1:
2039                         document.warning("Malformed LyX document: No end of InsetRef")
2040                         i += 1
2041                         continue
2042                 k = find_token(document.body, "LatexCommand formatted", i)
2043                 if k != -1 and k < j:
2044                         document.body[k] = "LatexCommand prettyref"
2045                 i = j + 1
2046         i = find_token(document.header, "\\use_refstyle", 0)
2047         if i != -1:
2048                 document.header.pop(i)
2049  
2050
2051 def revert_nameref(document):
2052   " Convert namerefs to regular references "
2053   cmds = ["Nameref", "nameref"]
2054   foundone = False
2055   rx = re.compile(r'reference "(.*)"')
2056   for cmd in cmds:
2057     i = 0
2058     oldcmd = "LatexCommand " + cmd
2059     while 1:
2060       # It seems better to look for this, as most of the reference
2061       # insets won't be ones we care about.
2062       i = find_token(document.body, oldcmd, i)
2063       if i == -1:
2064         break
2065       cmdloc = i
2066       i += 1
2067       # Make sure it is actually in an inset!
2068       # We could just check document.lines[i-1], but that relies
2069       # upon something that might easily change.
2070       # We'll look back a few lines.
2071       stins = cmdloc - 10
2072       if stins < 0:
2073         stins = 0
2074       stins = find_token(document.body, "\\begin_inset CommandInset ref", stins)
2075       if stins == -1 or stins > cmdloc:
2076         continue
2077       endins = find_end_of_inset(document.body, stins)
2078       if endins == -1:
2079         document.warning("Can't find end of inset at line " + stins + "!!")
2080         continue
2081       if endins < cmdloc:
2082         continue
2083       refline = find_token(document.body, "reference", stins)
2084       if refline == -1 or refline > endins:
2085         document.warning("Can't find reference for inset at line " + stinst + "!!")
2086         continue
2087       m = rx.match(document.body[refline])
2088       if not m:
2089         document.warning("Can't match reference line: " + document.body[ref])
2090         continue
2091       foundone = True
2092       ref = m.group(1)
2093       newcontent = ['\\begin_inset ERT', 'status collapsed', '', \
2094         '\\begin_layout Plain Layout', '', '\\backslash', \
2095         cmd + '{' + ref + '}', '\\end_layout', '', '\\end_inset']
2096       document.body[stins:endins + 1] = newcontent
2097   if foundone:
2098     add_to_preamble(document, "\usepackage{nameref}")
2099
2100
2101 def remove_Nameref(document):
2102   " Convert Nameref commands to nameref commands "
2103   i = 0
2104   while 1:
2105     # It seems better to look for this, as most of the reference
2106     # insets won't be ones we care about.
2107     i = find_token(document.body, "LatexCommand Nameref" , i)
2108     if i == -1:
2109       break
2110     cmdloc = i
2111     i += 1
2112     
2113     # Make sure it is actually in an inset!
2114     # We could just check document.lines[i-1], but that relies
2115     # upon something that might easily change.
2116     # We'll look back a few lines.
2117     stins = cmdloc - 10
2118     if stins < 0:
2119       stins = 0
2120     stins = find_token(document.body, "\\begin_inset CommandInset ref", stins)
2121     if stins == -1 or stins > cmdloc:
2122       continue
2123     endins = find_end_of_inset(document.body, stins)
2124     if endins == -1:
2125       document.warning("Can't find end of inset at line " + stins + "!!")
2126       continue
2127     if endins < cmdloc:
2128       continue
2129     document.body[cmdloc] = "LatexCommand nameref"
2130
2131
2132 def revert_mathrsfs(document):
2133     " Load mathrsfs if \mathrsfs us use in the document "
2134     i = 0
2135     end = len(document.body) - 1
2136     while True:
2137       j = document.body[i].find("\\mathscr{")
2138       if j != -1:
2139         add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
2140         add_to_preamble(document, ["\\usepackage{mathrsfs}"])
2141         break
2142       if i == end:
2143         break
2144       i += 1
2145
2146
2147 def convert_flexnames(document):
2148     "Convert \\begin_inset Flex Custom:Style to \\begin_inset Flex Style and similarly for CharStyle and Element."
2149     
2150     i = 0
2151     rx = re.compile(r'^\\begin_inset Flex (?:Custom|CharStyle|Element):(.+)$')
2152     while True:
2153       i = find_token(document.body, "\\begin_inset Flex", i)
2154       if i == -1:
2155         return
2156       m = rx.match(document.body[i])
2157       if m:
2158         document.body[i] = "\\begin_inset Flex " + m.group(1)
2159       i += 1
2160
2161
2162 flex_insets = [
2163   ["Alert", "CharStyle:Alert"],
2164   ["Code", "CharStyle:Code"],
2165   ["Concepts", "CharStyle:Concepts"],
2166   ["E-Mail", "CharStyle:E-Mail"],
2167   ["Emph", "CharStyle:Emph"],
2168   ["Expression", "CharStyle:Expression"],
2169   ["Initial", "CharStyle:Initial"],
2170   ["Institute", "CharStyle:Institute"],
2171   ["Meaning", "CharStyle:Meaning"],
2172   ["Noun", "CharStyle:Noun"],
2173   ["Strong", "CharStyle:Strong"],
2174   ["Structure", "CharStyle:Structure"],
2175   ["ArticleMode", "Custom:ArticleMode"],
2176   ["Endnote", "Custom:Endnote"],
2177   ["Glosse", "Custom:Glosse"],
2178   ["PresentationMode", "Custom:PresentationMode"],
2179   ["Tri-Glosse", "Custom:Tri-Glosse"]
2180 ]
2181
2182 flex_elements = [
2183   ["Abbrev", "Element:Abbrev"],
2184   ["CCC-Code", "Element:CCC-Code"],
2185   ["Citation-number", "Element:Citation-number"],
2186   ["City", "Element:City"],
2187   ["Code", "Element:Code"],
2188   ["CODEN", "Element:CODEN"],
2189   ["Country", "Element:Country"],
2190   ["Day", "Element:Day"],
2191   ["Directory", "Element:Directory"],
2192   ["Dscr", "Element:Dscr"],
2193   ["Email", "Element:Email"],
2194   ["Emph", "Element:Emph"],
2195   ["Filename", "Element:Filename"],
2196   ["Firstname", "Element:Firstname"],
2197   ["Fname", "Element:Fname"],
2198   ["GuiButton", "Element:GuiButton"],
2199   ["GuiMenu", "Element:GuiMenu"],
2200   ["GuiMenuItem", "Element:GuiMenuItem"],
2201   ["ISSN", "Element:ISSN"],
2202   ["Issue-day", "Element:Issue-day"],
2203   ["Issue-months", "Element:Issue-months"],
2204   ["Issue-number", "Element:Issue-number"],
2205   ["KeyCap", "Element:KeyCap"],
2206   ["KeyCombo", "Element:KeyCombo"],
2207   ["Keyword", "Element:Keyword"],
2208   ["Literal", "Element:Literal"],
2209   ["MenuChoice", "Element:MenuChoice"],
2210   ["Month", "Element:Month"],
2211   ["Orgdiv", "Element:Orgdiv"],
2212   ["Orgname", "Element:Orgname"],
2213   ["Postcode", "Element:Postcode"],
2214   ["SS-Code", "Element:SS-Code"],
2215   ["SS-Title", "Element:SS-Title"],
2216   ["State", "Element:State"],
2217   ["Street", "Element:Street"],
2218   ["Surname", "Element:Surname"],
2219   ["Volume", "Element:Volume"],
2220   ["Year", "Element:Year"]
2221 ]
2222
2223
2224 def revert_flexnames(document):
2225   if document.backend == "latex":
2226     flexlist = flex_insets
2227   else:
2228     flexlist = flex_elements
2229   
2230   rx = re.compile(r'^\\begin_inset Flex\s+(.+)$')
2231   i = 0
2232   while True:
2233     i = find_token(document.body, "\\begin_inset Flex", i)
2234     if i == -1:
2235       return
2236     m = rx.match(document.body[i])
2237     if not m:
2238       document.warning("Illegal flex inset: " + document.body[i])
2239       i += 1
2240       continue
2241     
2242     style = m.group(1)
2243     for f in flexlist:
2244       if f[0] == style:
2245         document.body[i] = "\\begin_inset Flex " + f[1]
2246         break
2247
2248     i += 1
2249
2250
2251 def convert_mathdots(document):
2252     " Load mathdots automatically "
2253     while True:
2254       i = find_token(document.header, "\\use_esint" , 0)
2255       if i != -1:
2256         document.header.insert(i + 1, "\\use_mathdots 1")
2257       break
2258
2259
2260 def revert_mathdots(document):
2261     " Load mathdots if used in the document "
2262     i = 0
2263     ddots = re.compile(r'\\begin_inset Formula .*\\ddots', re.DOTALL)
2264     vdots = re.compile(r'\\begin_inset Formula .*\\vdots', re.DOTALL)
2265     iddots = re.compile(r'\\begin_inset Formula .*\\iddots', re.DOTALL)
2266     mathdots = find_token(document.header, "\\use_mathdots" , 0)
2267     no = find_token(document.header, "\\use_mathdots 0" , 0)
2268     auto = find_token(document.header, "\\use_mathdots 1" , 0)
2269     yes = find_token(document.header, "\\use_mathdots 2" , 0)
2270     if mathdots != -1:
2271       del document.header[mathdots]
2272     while True:
2273       i = find_token(document.body, '\\begin_inset Formula', i)
2274       if i == -1:
2275         return
2276       j = find_end_of_inset(document.body, i)
2277       if j == -1:
2278         document.warning("Malformed LyX document: Can't find end of Formula inset.")
2279         return 
2280       k = ddots.search("\n".join(document.body[i:j]))
2281       l = vdots.search("\n".join(document.body[i:j]))
2282       m = iddots.search("\n".join(document.body[i:j]))
2283       if (yes == -1) and ((no != -1) or (not k and not l and not m) or (auto != -1 and not m)):
2284         i += 1
2285         continue
2286       # use \@ifundefined to catch also the "auto" case
2287       add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
2288       add_to_preamble(document, ["\\@ifundefined{iddots}{\\usepackage{mathdots}}\n"])
2289       return
2290
2291
2292 def convert_rule(document):
2293     " Convert \\lyxline to CommandInset line "
2294     i = 0
2295     while True:
2296       i = find_token(document.body, "\\lyxline" , i)
2297       if i == -1:
2298         return
2299         
2300       j = find_token(document.body, "\\color" , i - 2)
2301       if j == i - 2:
2302         color = document.body[j] + '\n'
2303       else:
2304         color = ''
2305       k = find_token(document.body, "\\begin_layout Standard" , i - 4)
2306       # we need to handle the case that \lyxline is in a separate paragraph and that it is colored
2307       # the result is then an extra empty paragraph which we get by adding an empty ERT inset
2308       if k == i - 4 and j == i - 2 and document.body[i - 1] == '':
2309         layout = '\\begin_inset ERT\nstatus collapsed\n\n\\begin_layout Plain Layout\n\n\n\\end_layout\n\n\\end_inset\n' \
2310           + '\\end_layout\n\n' \
2311           + '\\begin_layout Standard\n'
2312       elif k == i - 2 and document.body[i - 1] == '':
2313         layout = ''
2314       else:
2315         layout = '\\end_layout\n\n' \
2316           + '\\begin_layout Standard\n'
2317       l = find_token(document.body, "\\begin_layout Standard" , i + 4)
2318       if l == i + 4 and document.body[i + 1] == '':
2319         layout2 = ''
2320       else:
2321         layout2 = '\\end_layout\n' \
2322           + '\n\\begin_layout Standard\n'
2323       subst = layout \
2324         + '\\noindent\n\n' \
2325         + color \
2326         + '\\begin_inset CommandInset line\n' \
2327         + 'LatexCommand rule\n' \
2328         + 'offset "0.5ex"\n' \
2329         + 'width "100line%"\n' \
2330         + 'height "1pt"\n' \
2331         + '\n\\end_inset\n\n\n' \
2332         + layout2
2333       document.body[i] = subst
2334       i += 1
2335
2336
2337 def revert_rule(document):
2338     " Revert line insets to Tex code "
2339     i = 0
2340     while 1:
2341       i = find_token(document.body, "\\begin_inset CommandInset line" , i)
2342       if i == -1:
2343         return
2344       # find end of inset
2345       j = find_token(document.body, "\\end_inset" , i)
2346       # assure we found the end_inset of the current inset
2347       if j > i + 6 or j == -1:
2348         document.warning("Malformed LyX document: Can't find end of line inset.")
2349         return
2350       # determine the optional offset
2351       k = find_token(document.body, 'offset', i, j)
2352       if k != -1:
2353         offset = document.body[k][8:-1]
2354       else:
2355         offset = ""
2356       # determine the width
2357       l = find_token(document.body, 'width', i, j)
2358       if l != -1:
2359         width = document.body[l][7:-1]
2360       else:
2361         width = "100col%"
2362       # determine the height
2363       m = find_token(document.body, 'height', i, j)
2364       if m != -1:
2365         height = document.body[m][8:-1]
2366       else:
2367         height = "1pt"
2368       # output the \rule command
2369       if offset:
2370         subst = "\\rule[" + offset + "]{" + width + "}{" + height + "}"
2371       else:
2372         subst = "\\rule{" + width + "}{" + height + "}"
2373       document.body[i:j + 1] = put_cmd_in_ert(subst)
2374       i += 1
2375
2376
2377 def revert_diagram(document):
2378   " Add the feyn package if \\Diagram is used in math "
2379   i = 0
2380   re_diagram = re.compile(r'\\begin_inset Formula .*\\Diagram', re.DOTALL)
2381   while True:
2382     i = find_token(document.body, '\\begin_inset Formula', i)
2383     if i == -1:
2384       return
2385     j = find_end_of_inset(document.body, i)
2386     if j == -1:
2387         document.warning("Malformed LyX document: Can't find end of Formula inset.")
2388         return 
2389     m = re_diagram.search("\n".join(document.body[i:j]))
2390     if not m:
2391       i += 1
2392       continue
2393     add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
2394     add_to_preamble(document, "\\usepackage{feyn}")
2395     # only need to do it once!
2396     return
2397
2398
2399 def convert_bibtex_clearpage(document):
2400   " insert a clear(double)page bibliographystyle if bibtotoc option is used "
2401
2402   i = find_token(document.header, '\\papersides', 0)
2403   if i == -1:
2404     document.warning("Malformed LyX document: Can't find papersides definition.")
2405     return
2406   sides = int(document.header[i][12])
2407
2408   j = 0
2409   while True:
2410     j = find_token(document.body, "\\begin_inset CommandInset bibtex", j)
2411     if j == -1:
2412       return
2413
2414     k = find_end_of_inset(document.body, j)
2415     if k == -1:
2416       document.warning("Can't find end of Bibliography inset at line " + str(j))
2417       j += 1
2418       continue
2419
2420     # only act if there is the option "bibtotoc"
2421     m = find_token(document.body, 'options', j, k)
2422     if m == -1:
2423       document.warning("Can't find options for bibliography inset at line " + str(j))
2424       j = k
2425       continue
2426     
2427     optline = document.body[m]
2428     idx = optline.find("bibtotoc")
2429     if idx == -1:
2430       j = k
2431       continue
2432     
2433     # so we want to insert a new page right before the paragraph that
2434     # this bibliography thing is in. we'll look for it backwards.
2435     lay = j - 1
2436     while lay >= 0:
2437       if document.body[lay].startswith("\\begin_layout"):
2438         break
2439       lay -= 1
2440
2441     if lay < 0:
2442       document.warning("Can't find layout containing bibliography inset at line " + str(j))
2443       j = k
2444       continue
2445
2446     subst1 = '\\begin_layout Standard\n' \
2447       + '\\begin_inset Newpage clearpage\n' \
2448       + '\\end_inset\n\n\n' \
2449       + '\\end_layout\n'
2450     subst2 = '\\begin_layout Standard\n' \
2451       + '\\begin_inset Newpage cleardoublepage\n' \
2452       + '\\end_inset\n\n\n' \
2453       + '\\end_layout\n'
2454     if sides == 1:
2455       document.body.insert(lay, subst1)
2456       document.warning(subst1)
2457     else:
2458       document.body.insert(lay, subst2)
2459       document.warning(subst2)
2460
2461     j = k
2462
2463
2464 ##
2465 # Conversion hub
2466 #
2467
2468 supported_versions = ["2.0.0","2.0"]
2469 convert = [[346, []],
2470            [347, []],
2471            [348, []],
2472            [349, []],
2473            [350, []],
2474            [351, []],
2475            [352, [convert_splitindex]],
2476            [353, []],
2477            [354, []],
2478            [355, []],
2479            [356, []],
2480            [357, []],
2481            [358, []],
2482            [359, [convert_nomencl_width]],
2483            [360, []],
2484            [361, []],
2485            [362, []],
2486            [363, []],
2487            [364, []],
2488            [365, []],
2489            [366, []],
2490            [367, []],
2491            [368, []],
2492            [369, [convert_author_id]],
2493            [370, []],
2494            [371, []],
2495            [372, []],
2496            [373, [merge_gbrief]],
2497            [374, []],
2498            [375, []],
2499            [376, []],
2500            [377, []],
2501            [378, []],
2502            [379, [convert_math_output]],
2503            [380, []],
2504            [381, []],
2505            [382, []],
2506            [383, []],
2507            [384, []],
2508            [385, []],
2509            [386, []],
2510            [387, []],
2511            [388, []],
2512            [389, [convert_html_quotes]],
2513            [390, []],
2514            [391, []],
2515            [392, []],
2516            [393, [convert_optarg]],
2517            [394, [convert_use_makebox]],
2518            [395, []],
2519            [396, []],
2520            [397, [remove_Nameref]],
2521            [398, []],
2522            [399, [convert_mathdots]],
2523            [400, [convert_rule]],
2524            [401, []],
2525            [402, [convert_bibtex_clearpage]],
2526            [403, [convert_flexnames]],
2527            [404, [convert_prettyref]]
2528 ]
2529
2530 revert =  [[403, [revert_refstyle]],
2531            [402, [revert_flexnames]],
2532            [401, []],
2533            [400, [revert_diagram]],
2534            [399, [revert_rule]],
2535            [398, [revert_mathdots]],
2536            [397, [revert_mathrsfs]],
2537            [396, []],
2538            [395, [revert_nameref]],
2539            [394, [revert_DIN_C_pagesizes]],
2540            [393, [revert_makebox]],
2541            [392, [revert_argument]],
2542            [391, [revert_beamer_args]],
2543            [390, [revert_align_decimal, revert_IEEEtran]],
2544            [389, [revert_output_sync]],
2545            [388, [revert_html_quotes]],
2546            [387, [revert_pagesizes]],
2547            [386, [revert_math_scale]],
2548            [385, [revert_lyx_version]],
2549            [384, [revert_shadedboxcolor]],
2550            [383, [revert_fontcolor]],
2551            [382, [revert_turkmen]],
2552            [381, [revert_notefontcolor]],
2553            [380, [revert_equalspacing_xymatrix]],
2554            [379, [revert_inset_preview]],
2555            [378, [revert_math_output]],
2556            [377, []],
2557            [376, [revert_multirow]],
2558            [375, [revert_includeall]],
2559            [374, [revert_includeonly]],
2560            [373, [revert_html_options]],
2561            [372, [revert_gbrief]],
2562            [371, [revert_fontenc]],
2563            [370, [revert_mhchem]],
2564            [369, [revert_suppress_date]],
2565            [368, [revert_author_id]],
2566            [367, [revert_hspace_glue_lengths]],
2567            [366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
2568            [365, [revert_percent_skip_lengths]],
2569            [364, [revert_paragraph_indentation]],
2570            [363, [revert_branch_filename]],
2571            [362, [revert_longtable_align]],
2572            [361, [revert_applemac]],
2573            [360, []],
2574            [359, [revert_nomencl_cwidth]],
2575            [358, [revert_nomencl_width]],
2576            [357, [revert_custom_processors]],
2577            [356, [revert_ulinelatex]],
2578            [355, []],
2579            [354, [revert_strikeout]],
2580            [353, [revert_printindexall]],
2581            [352, [revert_subindex]],
2582            [351, [revert_splitindex]],
2583            [350, [revert_backgroundcolor]],
2584            [349, [revert_outputformat]],
2585            [348, [revert_xetex]],
2586            [347, [revert_phantom, revert_hphantom, revert_vphantom]],
2587            [346, [revert_tabularvalign]],
2588            [345, [revert_swiss]]
2589           ]
2590
2591
2592 if __name__ == "__main__":
2593     pass