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