]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_215.py
add bibtopic support (bug 870).
[lyx.git] / lib / lyx2lyx / lyxconvert_215.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 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 import re
20 from parser_tools import *
21
22 layout_exp = re.compile(r"\\layout (\S*)")
23
24 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
25
26 def replace_protected_separator(lines):
27     i=0
28     while 1:
29         i = find_token(lines, "\\protected_separator", i)
30         if i == -1:
31             break
32         j = find_token_backwards(lines, "\\layout", i)
33         #if j == -1: print error
34         layout = layout_exp.match(lines[j]).group(1)
35
36         if layout == "LyX-Code":
37             result = ""
38             while lines[i] == "\\protected_separator ":
39                 result = result + " "
40                 del lines[i]
41
42             lines[i-1] = lines[i-1] + result + lines[i]
43         else:
44             lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
45
46         del lines[i]
47
48 def merge_formula_inset(lines):
49     i=0
50     while 1:
51         i = find_token(lines, "\\begin_inset Formula", i)
52         if i == -1: break
53         if lines[i+1] in math_env:
54             lines[i] = lines[i] + lines[i+1]
55             del lines[i+1]
56         i = i + 1
57
58 # Update from tabular format 4 to 5 if necessary
59 def update_tabular(lines):
60     lyxtable_re = re.compile(r".*\\LyXTable$")
61     i=0
62     while 1:
63         i = find_re(lines, lyxtable_re, i)
64         if i == -1:
65             break
66         i = i + 1
67         format = lines[i][8]
68         if format != '4':
69             continue
70
71         lines[i]='multicol5'
72         i = i + 1
73         rows = int(string.split(lines[i])[0])
74         columns = int(string.split(lines[i])[1])
75
76         i = i + rows + 1
77         for j in range(columns):
78             col_info = string.split(lines[i])
79             if len(col_info) == 3:
80                 lines[i] = lines[i] + '"" ""'
81             else:
82                 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
83             i = i + 1
84
85         while lines[i]:
86             lines[i] = lines[i] + ' "" ""'
87             i = i + 1
88
89 def update_toc(lines):
90     i = 0
91     while 1:
92         i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
93         if i == -1:
94             break
95         lines[i] = lines[i] + '{}'
96         i = i + 1
97
98 def remove_cursor(lines):
99     i = find_token(lines, '\\cursor', 0)
100     if i != -1:
101         del lines[i]
102
103 def remove_vcid(lines):
104     i = find_token(lines, '\\lyxvcid', 0)
105     if i != -1:
106         del lines[i]
107     i = find_token(lines, '\\lyxrcsid', 0)
108     if i != -1:
109         del lines[i]
110
111 def first_layout(lines):
112     while (lines[0] == ""):
113         del lines[0]
114     if lines[0][:7] != "\\layout":
115         lines[:0] = ["\\layout Standard"]
116
117 def remove_space_in_units(lines):
118     margins = ["\\topmargin","\\rightmargin",
119                "\\leftmargin","\\bottommargin"]
120
121     unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
122
123     begin_preamble = find_token(lines,"\\begin_preamble", 0)
124     end_preamble = find_token(lines, "\\end_preamble", 0)
125     for margin in margins:
126         i = 0
127         while 1:
128             i = find_token(lines, margin, i)
129             if i == -1:
130                 break
131
132             if i > begin_preamble and i < end_preamble:
133                 i = i + 1
134                 continue
135
136             result = unit_rexp.search(lines[i])
137             if result:
138                 lines[i] = margin + " " + result.group(1) + result.group(2)
139             i = i + 1
140
141 def convert(header,body):
142     first_layout(body)
143     remove_vcid(header)
144     remove_cursor(body)
145     update_toc(body)
146     replace_protected_separator(body)
147     merge_formula_inset(body)
148     update_tabular(body)
149     remove_space_in_units(header)
150
151 if __name__ == "__main__":
152     pass
153