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