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