]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx_2_0.py
* Add possibility to append active branch names to the output file name (#3105).
[features.git] / lib / lyx2lyx / lyx_2_0.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008 José Matos  <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 """ Convert files to the file format generated by lyx 2.0"""
20
21 import re, string
22 import unicodedata
23 import sys, os
24
25 from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string
26
27 ####################################################################
28 # Private helper functions
29
30 def find_end_of_inset(lines, i):
31     " Find end of inset, where lines[i] is included."
32     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
33
34
35 def add_to_preamble(document, text):
36     """ Add text to the preamble if it is not already there.
37     Only the first line is checked!"""
38
39     if find_token(document.preamble, text[0], 0) != -1:
40         return
41
42     document.preamble.extend(text)
43
44
45 def insert_to_preamble(index, document, text):
46     """ Insert text to the preamble at a given line"""
47
48     document.preamble.insert(index, text)
49
50
51 def read_unicodesymbols():
52     " Read the unicodesymbols list of unicode characters and corresponding commands."
53     pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
54     fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
55     spec_chars = []
56     # Two backslashes, followed by some non-word character, and then a character
57     # in brackets. The idea is to check for constructs like: \"{u}, which is how
58     # they are written in the unicodesymbols file; but they can also be written
59     # as: \"u or even \" u.
60     r = re.compile(r'\\\\(\W)\{(\w)\}')
61     for line in fp.readlines():
62         if line[0] != '#' and line.strip() != "":
63             line=line.replace(' "',' ') # remove all quotation marks with spaces before
64             line=line.replace('" ',' ') # remove all quotation marks with spaces after
65             line=line.replace(r'\"','"') # replace \" by " (for characters with diaeresis)
66             try:
67                 [ucs4,command,dead] = line.split(None,2)
68                 if command[0:1] != "\\":
69                     continue
70                 spec_chars.append([command, unichr(eval(ucs4))])
71             except:
72                 continue
73             m = r.match(command)
74             if m != None:
75                 command = "\\\\"
76                 # If the character is a double-quote, then we need to escape it, too,
77                 # since it is done that way in the LyX file.
78                 if m.group(1) == "\"":
79                     command += "\\"
80                 commandbl = command
81                 command += m.group(1) + m.group(2)
82                 commandbl += m.group(1) + ' ' + m.group(2)
83                 spec_chars.append([command, unichr(eval(ucs4))])
84                 spec_chars.append([commandbl, unichr(eval(ucs4))])
85     fp.close()
86     return spec_chars
87
88
89 unicode_reps = read_unicodesymbols()
90
91
92 def put_cmd_in_ert(string):
93     for rep in unicode_reps:
94         string = string.replace(rep[1], rep[0].replace('\\\\', '\\'))
95     string = string.replace('\\', "\\backslash\n")
96     string = "\\begin_inset ERT\nstatus collapsed\n\\begin_layout Standard\n" \
97       + string + "\n\\end_layout\n\\end_inset"
98     return string
99
100
101 def lyx2latex(document, lines):
102     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
103     # clean up multiline stuff
104     content = ""
105     ert_end = 0
106
107     for curline in range(len(lines)):
108       line = lines[curline]
109       if line.startswith("\\begin_inset ERT"):
110           # We don't want to replace things inside ERT, so figure out
111           # where the end of the inset is.
112           ert_end = find_end_of_inset(lines, curline + 1)
113           continue
114       elif line.startswith("\\begin_inset Formula"):
115           line = line[20:]
116       elif line.startswith("\\begin_inset Quotes"):
117           # For now, we do a very basic reversion. Someone who understands
118           # quotes is welcome to fix it up.
119           qtype = line[20:].strip()
120           # lang = qtype[0]
121           side = qtype[1]
122           dbls = qtype[2]
123           if side == "l":
124               if dbls == "d":
125                   line = "``"
126               else:
127                   line = "`"
128           else:
129               if dbls == "d":
130                   line = "''"
131               else:
132                   line = "'"
133       elif line.isspace() or \
134             line.startswith("\\begin_layout") or \
135             line.startswith("\\end_layout") or \
136             line.startswith("\\begin_inset") or \
137             line.startswith("\\end_inset") or \
138             line.startswith("\\lang") or \
139             line.strip() == "status collapsed" or \
140             line.strip() == "status open":
141           #skip all that stuff
142           continue
143
144       # this needs to be added to the preamble because of cases like
145       # \textmu, \textbackslash, etc.
146       add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
147                                  '\\@ifundefined{textmu}',
148                                  ' {\\usepackage{textcomp}}{}'])
149       # a lossless reversion is not possible
150       # try at least to handle some common insets and settings
151       if ert_end >= curline:
152           line = line.replace(r'\backslash', r'\\')
153       else:
154           line = line.replace('&', '\\&{}')
155           line = line.replace('#', '\\#{}')
156           line = line.replace('^', '\\^{}')
157           line = line.replace('%', '\\%{}')
158           line = line.replace('_', '\\_{}')
159           line = line.replace('$', '\\${}')
160
161           # Do the LyX text --> LaTeX conversion
162           for rep in unicode_reps:
163             line = line.replace(rep[1], rep[0] + "{}")
164           line = line.replace(r'\backslash', r'\textbackslash{}')
165           line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
166           line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
167           line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
168           line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
169           line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
170           line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
171           line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
172           line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
173           line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
174       content += line
175     return content
176
177
178 ####################################################################
179
180
181 def revert_swiss(document):
182     " Set language german-ch to ngerman "
183     i = 0
184     if document.language == "german-ch":
185         document.language = "ngerman"
186         i = find_token(document.header, "\\language", 0)
187         if i != -1:
188             document.header[i] = "\\language ngerman"
189     j = 0
190     while True:
191         j = find_token(document.body, "\\lang german-ch", j)
192         if j == -1:
193             return
194         document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
195         j = j + 1
196
197
198 def revert_tabularvalign(document):
199    " Revert the tabular valign option "
200    i = 0
201    while True:
202        i = find_token(document.body, "\\begin_inset Tabular", i)
203        if i == -1:
204            return
205        j = find_end_of_inset(document.body, i)
206        if j == -1:
207            document.warning("Malformed LyX document: Could not find end of tabular.")
208            i = j
209            continue
210        # don't set a box for longtables, only delete tabularvalignment
211        # the alignment is 2 lines below \\begin_inset Tabular
212        p = document.body[i+2].find("islongtable")
213        if p > -1:
214            q = document.body[i+2].find("tabularvalignment")
215            if q > -1:
216                document.body[i+2] = document.body[i+2][:q-1]
217                document.body[i+2] = document.body[i+2] + '>'
218            i = i + 1
219
220        # when no longtable
221        if p == -1:
222          tabularvalignment = 'c'
223          # which valignment is specified?
224          m = document.body[i+2].find('tabularvalignment="top"')
225          if m > -1:
226              tabularvalignment = 't'
227          m = document.body[i+2].find('tabularvalignment="bottom"')
228          if m > -1:
229              tabularvalignment = 'b'
230          # delete tabularvalignment
231          q = document.body[i+2].find("tabularvalignment")
232          if q > -1:
233              document.body[i+2] = document.body[i+2][:q-1]
234              document.body[i+2] = document.body[i+2] + '>'
235
236          # don't add a box when centered
237          if tabularvalignment == 'c':
238              i = j
239              continue
240          subst = ['\\end_layout', '\\end_inset']
241          document.body[j+1:j+1] = subst # just inserts those lines
242          subst = ['\\begin_inset Box Frameless',
243              'position "' + tabularvalignment +'"',
244              'hor_pos "c"',
245              'has_inner_box 1',
246              'inner_pos "c"',
247              'use_parbox 0',
248              # we don't know the width, assume 50%
249              'width "50col%"',
250              'special "none"',
251              'height "1in"',
252              'height_special "totalheight"',
253              'status open',
254              '',
255              '\\begin_layout Plain Layout']
256          document.body[i:i] = subst # this just inserts the array at i
257          i += len(subst) + 2 # adjust i to save a few cycles
258
259
260 def revert_phantom(document):
261     " Reverts phantom to ERT "
262     i = 0
263     j = 0
264     while True:
265       i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
266       if i == -1:
267           return
268       substi = document.body[i].replace('\\begin_inset Phantom Phantom', \
269                 '\\begin_inset ERT\nstatus collapsed\n\n' \
270                 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
271                 'phantom{\n\\end_layout\n\n\\end_inset\n')
272       substi = substi.split('\n')
273       document.body[i : i+4] = substi
274       i += len(substi)
275       j = find_token(document.body, "\\end_layout", i)
276       if j == -1:
277           document.warning("Malformed LyX document: Could not find end of Phantom inset.")
278           return
279       substj = document.body[j].replace('\\end_layout', \
280                 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
281                 '\\begin_layout Plain Layout\n\n' \
282                 '}\n\\end_layout\n\n\\end_inset\n')
283       substj = substj.split('\n')
284       document.body[j : j+4] = substj
285       i += len(substj)
286
287
288 def revert_hphantom(document):
289     " Reverts hphantom to ERT "
290     i = 0
291     j = 0
292     while True:
293       i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
294       if i == -1:
295           return
296       substi = document.body[i].replace('\\begin_inset Phantom HPhantom', \
297                 '\\begin_inset ERT\nstatus collapsed\n\n' \
298                 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
299                 'hphantom{\n\\end_layout\n\n\\end_inset\n')
300       substi = substi.split('\n')
301       document.body[i : i+4] = substi
302       i += len(substi)
303       j = find_token(document.body, "\\end_layout", i)
304       if j == -1:
305           document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
306           return
307       substj = document.body[j].replace('\\end_layout', \
308                 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
309                 '\\begin_layout Plain Layout\n\n' \
310                 '}\n\\end_layout\n\n\\end_inset\n')
311       substj = substj.split('\n')
312       document.body[j : j+4] = substj
313       i += len(substj)
314
315
316 def revert_vphantom(document):
317     " Reverts vphantom to ERT "
318     i = 0
319     j = 0
320     while True:
321       i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
322       if i == -1:
323           return
324       substi = document.body[i].replace('\\begin_inset Phantom VPhantom', \
325                 '\\begin_inset ERT\nstatus collapsed\n\n' \
326                 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
327                 'vphantom{\n\\end_layout\n\n\\end_inset\n')
328       substi = substi.split('\n')
329       document.body[i : i+4] = substi
330       i += len(substi)
331       j = find_token(document.body, "\\end_layout", i)
332       if j == -1:
333           document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
334           return
335       substj = document.body[j].replace('\\end_layout', \
336                 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
337                 '\\begin_layout Plain Layout\n\n' \
338                 '}\n\\end_layout\n\n\\end_inset\n')
339       substj = substj.split('\n')
340       document.body[j : j+4] = substj
341       i += len(substj)
342
343
344 def revert_xetex(document):
345     " Reverts documents that use XeTeX "
346     i = find_token(document.header, '\\use_xetex', 0)
347     if i == -1:
348         document.warning("Malformed LyX document: Missing \\use_xetex.")
349         return
350     if get_value(document.header, "\\use_xetex", i) == 'false':
351         del document.header[i]
352         return
353     del document.header[i]
354     # 1.) set doc encoding to utf8-plain
355     i = find_token(document.header, "\\inputencoding", 0)
356     if i == -1:
357         document.warning("Malformed LyX document: Missing \\inputencoding.")
358     document.header[i] = "\\inputencoding utf8-plain"
359     # 2.) check font settings
360     l = find_token(document.header, "\\font_roman", 0)
361     if l == -1:
362         document.warning("Malformed LyX document: Missing \\font_roman.")
363     line = document.header[l]
364     l = re.compile(r'\\font_roman (.*)$')
365     m = l.match(line)
366     roman = m.group(1)
367     l = find_token(document.header, "\\font_sans", 0)
368     if l == -1:
369         document.warning("Malformed LyX document: Missing \\font_sans.")
370     line = document.header[l]
371     l = re.compile(r'\\font_sans (.*)$')
372     m = l.match(line)
373     sans = m.group(1)
374     l = find_token(document.header, "\\font_typewriter", 0)
375     if l == -1:
376         document.warning("Malformed LyX document: Missing \\font_typewriter.")
377     line = document.header[l]
378     l = re.compile(r'\\font_typewriter (.*)$')
379     m = l.match(line)
380     typewriter = m.group(1)
381     osf = get_value(document.header, '\\font_osf', 0) == "true"
382     sf_scale = float(get_value(document.header, '\\font_sf_scale', 0))
383     tt_scale = float(get_value(document.header, '\\font_tt_scale', 0))
384     # 3.) set preamble stuff
385     pretext = '%% This document must be processed with xelatex!\n'
386     pretext += '\\usepackage{fontspec}\n'
387     if roman != "default":
388         pretext += '\\setmainfont[Mapping=tex-text]{' + roman + '}\n'
389     if sans != "default":
390         pretext += '\\setsansfont['
391         if sf_scale != 100:
392             pretext += 'Scale=' + str(sf_scale / 100) + ','
393         pretext += 'Mapping=tex-text]{' + sans + '}\n'
394     if typewriter != "default":
395         pretext += '\\setmonofont'
396         if tt_scale != 100:
397             pretext += '[Scale=' + str(tt_scale / 100) + ']'
398         pretext += '{' + typewriter + '}\n'
399     if osf:
400         pretext += '\\defaultfontfeatures{Numbers=OldStyle}\n'
401     pretext += '\usepackage{xunicode}\n'
402     pretext += '\usepackage{xltxtra}\n'
403     insert_to_preamble(0, document, pretext)
404     # 4.) reset font settings
405     i = find_token(document.header, "\\font_roman", 0)
406     if i == -1:
407         document.warning("Malformed LyX document: Missing \\font_roman.")
408     document.header[i] = "\\font_roman default"
409     i = find_token(document.header, "\\font_sans", 0)
410     if i == -1:
411         document.warning("Malformed LyX document: Missing \\font_sans.")
412     document.header[i] = "\\font_sans default"
413     i = find_token(document.header, "\\font_typewriter", 0)
414     if i == -1:
415         document.warning("Malformed LyX document: Missing \\font_typewriter.")
416     document.header[i] = "\\font_typewriter default"
417     i = find_token(document.header, "\\font_osf", 0)
418     if i == -1:
419         document.warning("Malformed LyX document: Missing \\font_osf.")
420     document.header[i] = "\\font_osf false"
421     i = find_token(document.header, "\\font_sc", 0)
422     if i == -1:
423         document.warning("Malformed LyX document: Missing \\font_sc.")
424     document.header[i] = "\\font_sc false"
425     i = find_token(document.header, "\\font_sf_scale", 0)
426     if i == -1:
427         document.warning("Malformed LyX document: Missing \\font_sf_scale.")
428     document.header[i] = "\\font_sf_scale 100"
429     i = find_token(document.header, "\\font_tt_scale", 0)
430     if i == -1:
431         document.warning("Malformed LyX document: Missing \\font_tt_scale.")
432     document.header[i] = "\\font_tt_scale 100"
433
434
435 def revert_outputformat(document):
436     " Remove default output format param "
437     i = find_token(document.header, '\\default_output_format', 0)
438     if i == -1:
439         document.warning("Malformed LyX document: Missing \\default_output_format.")
440         return
441     del document.header[i]
442
443
444 def revert_backgroundcolor(document):
445     " Reverts background color to preamble code "
446     i = 0
447     colorcode = ""
448     while True:
449       i = find_token(document.header, "\\backgroundcolor", i)
450       if i == -1:
451           return
452       colorcode = get_value(document.header, '\\backgroundcolor', 0)
453       del document.header[i]
454       # don't clutter the preamble if backgroundcolor is not set
455       if colorcode == "#ffffff":
456           continue
457       # the color code is in the form #rrggbb where every character denotes a hex number
458       # convert the string to an int
459       red = string.atoi(colorcode[1:3],16)
460       # we want the output "0.5" for the value "127" therefore add here
461       if red != 0:
462           red = red + 1
463       redout = float(red) / 256
464       green = string.atoi(colorcode[3:5],16)
465       if green != 0:
466           green = green + 1
467       greenout = float(green) / 256
468       blue = string.atoi(colorcode[5:7],16)
469       if blue != 0:
470           blue = blue + 1
471       blueout = float(blue) / 256
472       # write the preamble
473       insert_to_preamble(0, document,
474                            '% Commands inserted by lyx2lyx to set the background color\n'
475                            + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n'
476                            + '\\definecolor{page_backgroundcolor}{rgb}{'
477                            + str(redout) + ', ' + str(greenout)
478                            + ', ' + str(blueout) + '}\n'
479                            + '\\pagecolor{page_backgroundcolor}\n')
480
481
482 def revert_splitindex(document):
483     " Reverts splitindex-aware documents "
484     i = find_token(document.header, '\\use_indices', 0)
485     if i == -1:
486         document.warning("Malformed LyX document: Missing \\use_indices.")
487         return
488     indices = get_value(document.header, "\\use_indices", i)
489     preamble = ""
490     if indices == "true":
491          preamble += "\\usepackage{splitidx}\n"
492     del document.header[i]
493     i = 0
494     while True:
495         i = find_token(document.header, "\\index", i)
496         if i == -1:
497             break
498         k = find_token(document.header, "\\end_index", i)
499         if k == -1:
500             document.warning("Malformed LyX document: Missing \\end_index.")
501             return
502         line = document.header[i]
503         l = re.compile(r'\\index (.*)$')
504         m = l.match(line)
505         iname = m.group(1)
506         ishortcut = get_value(document.header, '\\shortcut', i, k)
507         if ishortcut != "" and indices == "true":
508             preamble += "\\newindex[" + iname + "]{" + ishortcut + "}\n"
509         del document.header[i:k+1]
510         i = 0
511     if preamble != "":
512         insert_to_preamble(0, document, preamble)
513     i = 0
514     while True:
515         i = find_token(document.body, "\\begin_inset Index", i)
516         if i == -1:
517             break
518         line = document.body[i]
519         l = re.compile(r'\\begin_inset Index (.*)$')
520         m = l.match(line)
521         itype = m.group(1)
522         if itype == "idx" or indices == "false":
523             document.body[i] = "\\begin_inset Index"
524         else:
525             k = find_end_of_inset(document.body, i)
526             if k == -1:
527                  return
528             content = lyx2latex(document, document.body[i:k])
529             # escape quotes
530             content = content.replace('"', r'\"')
531             subst = [put_cmd_in_ert("\\sindex[" + itype + "]{" + content + "}")]
532             document.body[i:k+1] = subst
533         i = i + 1
534     i = 0
535     while True:
536         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
537         if i == -1:
538             return
539         k = find_end_of_inset(document.body, i)
540         ptype = get_value(document.body, 'type', i, k).strip('"')
541         if ptype == "idx":
542             j = find_token(document.body, "type", i, k)
543             del document.body[j]
544         elif indices == "false":
545             del document.body[i:k+1]
546         else:
547             subst = [put_cmd_in_ert("\\printindex[" + ptype + "]{}")]
548             document.body[i:k+1] = subst
549         i = i + 1
550
551
552 def convert_splitindex(document):
553     " Converts index and printindex insets to splitindex-aware format "
554     i = 0
555     while True:
556         i = find_token(document.body, "\\begin_inset Index", i)
557         if i == -1:
558             break
559         document.body[i] = document.body[i].replace("\\begin_inset Index",
560             "\\begin_inset Index idx")
561         i = i + 1
562     i = 0
563     while True:
564         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
565         if i == -1:
566             return
567         if document.body[i + 1].find('LatexCommand printindex') == -1:
568             document.warning("Malformed LyX document: Incomplete printindex inset.")
569             return
570         subst = ["LatexCommand printindex", 
571             "type \"idx\""]
572         document.body[i + 1:i + 2] = subst
573         i = i + 1
574
575
576 def revert_subindex(document):
577     " Reverts \\printsubindex CommandInset types "
578     i = find_token(document.header, '\\use_indices', 0)
579     if i == -1:
580         document.warning("Malformed LyX document: Missing \\use_indices.")
581         return
582     indices = get_value(document.header, "\\use_indices", i)
583     i = 0
584     while True:
585         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
586         if i == -1:
587             return
588         k = find_end_of_inset(document.body, i)
589         ctype = get_value(document.body, 'LatexCommand', i, k)
590         if ctype != "printsubindex":
591             i = i + 1
592             continue
593         ptype = get_value(document.body, 'type', i, k).strip('"')
594         if indices == "false":
595             del document.body[i:k+1]
596         else:
597             subst = [put_cmd_in_ert("\\printsubindex[" + ptype + "]{}")]
598             document.body[i:k+1] = subst
599         i = i + 1
600
601
602 def revert_printindexall(document):
603     " Reverts \\print[sub]index* CommandInset types "
604     i = find_token(document.header, '\\use_indices', 0)
605     if i == -1:
606         document.warning("Malformed LyX document: Missing \\use_indices.")
607         return
608     indices = get_value(document.header, "\\use_indices", i)
609     i = 0
610     while True:
611         i = find_token(document.body, "\\begin_inset CommandInset index_print", i)
612         if i == -1:
613             return
614         k = find_end_of_inset(document.body, i)
615         ctype = get_value(document.body, 'LatexCommand', i, k)
616         if ctype != "printindex*" and ctype != "printsubindex*":
617             i = i + 1
618             continue
619         if indices == "false":
620             del document.body[i:k+1]
621         else:
622             subst = [put_cmd_in_ert("\\" + ctype + "{}")]
623             document.body[i:k+1] = subst
624         i = i + 1
625
626
627 def revert_strikeout(document):
628     " Reverts \\strikeout character style "
629     while True:
630         i = find_token(document.body, '\\strikeout', 0)
631         if i == -1:
632             return
633         del document.body[i]
634
635
636 def revert_uulinewave(document):
637     " Reverts \\uuline, and \\uwave character styles "
638     while True:
639         i = find_token(document.body, '\\uuline', 0)
640         if i == -1:
641             break
642         del document.body[i]
643     while True:
644         i = find_token(document.body, '\\uwave', 0)
645         if i == -1:
646             return
647         del document.body[i]
648
649
650 def revert_ulinelatex(document):
651     " Reverts \\uline character style "
652     i = find_token(document.body, '\\bar under', 0)
653     if i == -1:
654         return
655     insert_to_preamble(0, document,
656             '% Commands inserted by lyx2lyx for proper underlining\n'
657             + '\\PassOptionsToPackage{normalem}{ulem}\n'
658             + '\\usepackage{ulem}\n'
659             + '\\let\\cite@rig\\cite\n'
660             + '\\newcommand{\\b@xcite}[2][\\%]{\\def\\def@pt{\\%}\\def\\pas@pt{#1}\n'
661             + '  \\mbox{\\ifx\\def@pt\\pas@pt\\cite@rig{#2}\\else\\cite@rig[#1]{#2}\\fi}}\n'
662             + '\\renewcommand{\\underbar}[1]{{\\let\\cite\\b@xcite\\uline{#1}}}\n')
663
664
665 def revert_custom_processors(document):
666     " Remove bibtex_command and index_command params "
667     i = find_token(document.header, '\\bibtex_command', 0)
668     if i == -1:
669         document.warning("Malformed LyX document: Missing \\bibtex_command.")
670         return
671     del document.header[i]
672     i = find_token(document.header, '\\index_command', 0)
673     if i == -1:
674         document.warning("Malformed LyX document: Missing \\index_command.")
675         return
676     del document.header[i]
677
678
679 def convert_nomencl_width(document):
680     " Add set_width param to nomencl_print "
681     i = 0
682     while True:
683       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
684       if i == -1:
685         break
686       document.body.insert(i + 2, "set_width \"none\"")
687       i = i + 1
688
689
690 def revert_nomencl_width(document):
691     " Remove set_width param from nomencl_print "
692     i = 0
693     while True:
694       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
695       if i == -1:
696         break
697       j = find_end_of_inset(document.body, i)
698       l = find_token(document.body, "set_width", i, j)
699       if l == -1:
700             document.warning("Can't find set_width option for nomencl_print!")
701             i = j
702             continue
703       del document.body[l]
704       i = i + 1
705
706
707 def revert_nomencl_cwidth(document):
708     " Remove width param from nomencl_print "
709     i = 0
710     while True:
711       i = find_token(document.body, "\\begin_inset CommandInset nomencl_print", i)
712       if i == -1:
713         break
714       j = find_end_of_inset(document.body, i)
715       l = find_token(document.body, "width", i, j)
716       if l == -1:
717             document.warning("Can't find width option for nomencl_print!")
718             i = j
719             continue
720       width = get_value(document.body, "width", i, j).strip('"')
721       del document.body[l]
722       add_to_preamble(document, ["\\setlength{\\nomlabelwidth}{" + width + "}"])
723       i = i + 1
724
725
726 def revert_applemac(document):
727     " Revert applemac encoding to auto "
728     i = 0
729     if document.encoding == "applemac":
730         document.encoding = "auto"
731         i = find_token(document.header, "\\encoding", 0)
732         if i != -1:
733             document.header[i] = "\\encoding auto"
734
735
736 def revert_longtable_align(document):
737     " Remove longtable alignment setting "
738     i = 0
739     j = 0
740     while True:
741       i = find_token(document.body, "\\begin_inset Tabular", i)
742       if i == -1:
743           break
744       # the alignment is 2 lines below \\begin_inset Tabular
745       j = document.body[i+2].find("longtabularalignment")
746       if j == -1:
747           break
748       document.body[i+2] = document.body[i+2][:j-1]
749       document.body[i+2] = document.body[i+2] + '>'
750       i = i + 1
751
752
753 def revert_branch_filename(document):
754     " Remove \\filename_suffix parameter from branches "
755     i = 0
756     while True:
757         i = find_token(document.header, "\\filename_suffix", i)
758         if i == -1:
759             return
760         del document.header[i]
761
762
763 ##
764 # Conversion hub
765 #
766
767 supported_versions = ["2.0.0","2.0"]
768 convert = [[346, []],
769            [347, []],
770            [348, []],
771            [349, []],
772            [350, []],
773            [351, []],
774            [352, [convert_splitindex]],
775            [353, []],
776            [354, []],
777            [355, []],
778            [356, []],
779            [357, []],
780            [358, []],
781            [359, [convert_nomencl_width]],
782            [360, []],
783            [361, []],
784            [362, []],
785            [363, []],
786            [364, []]
787           ]
788
789 revert =  [[363, [revert_branch_filename]],
790            [362, [revert_longtable_align]],
791            [361, [revert_applemac]],
792            [360, []],
793            [359, [revert_nomencl_cwidth]],
794            [358, [revert_nomencl_width]],
795            [357, [revert_custom_processors]],
796            [356, [revert_ulinelatex]],
797            [355, [revert_uulinewave]],
798            [354, [revert_strikeout]],
799            [353, [revert_printindexall]],
800            [352, [revert_subindex]],
801            [351, [revert_splitindex]],
802            [350, [revert_backgroundcolor]],
803            [349, [revert_outputformat]],
804            [348, [revert_xetex]],
805            [347, [revert_phantom, revert_hphantom, revert_vphantom]],
806            [346, [revert_tabularvalign]],
807            [345, [revert_swiss]]
808           ]
809
810
811 if __name__ == "__main__":
812     pass