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