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