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