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