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