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