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