]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_5.py
Keep a copy of output from lib/configure.py to configure.log
[lyx.git] / lib / lyx2lyx / lyx_1_5.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2006 José Matos <jamatos@lyx.org>
4 # Copyright (C) 2004-2006 Georg Baum <Georg.Baum@post.rwth-aachen.de>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 """ Convert files to the file format generated by lyx 1.5"""
21
22 import re
23 from parser_tools import find_token, find_token_exact, find_tokens, find_end_of, get_value
24
25
26 ####################################################################
27 # Private helper functions
28
29 def find_end_of_inset(lines, i):
30     " Find beginning of inset, where lines[i] is included."
31     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
32
33 # End of helper functions
34 ####################################################################
35
36
37 ##
38 #  Notes: Framed/Shaded
39 #
40
41 def revert_framed(document):
42     "Revert framed notes. "
43     i = 0
44     while 1:
45         i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
46
47         if i == -1:
48             return
49         document.body[i] = "\\begin_inset Note"
50         i = i + 1
51
52
53 ##
54 #  Fonts
55 #
56
57 roman_fonts      = {'default' : 'default', 'ae'       : 'ae',
58                     'times'   : 'times',   'palatino' : 'palatino',
59                     'helvet'  : 'default', 'avant'    : 'default',
60                     'newcent' : 'newcent', 'bookman'  : 'bookman',
61                     'pslatex' : 'times'}
62 sans_fonts       = {'default' : 'default', 'ae'       : 'default',
63                     'times'   : 'default', 'palatino' : 'default',
64                     'helvet'  : 'helvet',  'avant'    : 'avant',
65                     'newcent' : 'default', 'bookman'  : 'default',
66                     'pslatex' : 'helvet'}
67 typewriter_fonts = {'default' : 'default', 'ae'       : 'default',
68                     'times'   : 'default', 'palatino' : 'default',
69                     'helvet'  : 'default', 'avant'    : 'default',
70                     'newcent' : 'default', 'bookman'  : 'default',
71                     'pslatex' : 'courier'}
72
73 def convert_font_settings(document):
74     " Convert font settings. "
75     i = 0
76     i = find_token_exact(document.header, "\\fontscheme", i)
77     if i == -1:
78         document.warning("Malformed LyX document: Missing `\\fontscheme'.")
79         return
80     font_scheme = get_value(document.header, "\\fontscheme", i, i + 1)
81     if font_scheme == '':
82         document.warning("Malformed LyX document: Empty `\\fontscheme'.")
83         font_scheme = 'default'
84     if not font_scheme in roman_fonts.keys():
85         document.warning("Malformed LyX document: Unknown `\\fontscheme' `%s'." % font_scheme)
86         font_scheme = 'default'
87     document.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
88                           '\\font_sans %s' % sans_fonts[font_scheme],
89                           '\\font_typewriter %s' % typewriter_fonts[font_scheme],
90                           '\\font_default_family default',
91                           '\\font_sc false',
92                           '\\font_osf false',
93                           '\\font_sf_scale 100',
94                           '\\font_tt_scale 100']
95
96
97 def revert_font_settings(document):
98     " Revert font settings. "
99     i = 0
100     insert_line = -1
101     fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
102     for family in 'roman', 'sans', 'typewriter':
103         name = '\\font_%s' % family
104         i = find_token_exact(document.header, name, i)
105         if i == -1:
106             document.warning("Malformed LyX document: Missing `%s'." % name)
107             i = 0
108         else:
109             if (insert_line < 0):
110                 insert_line = i
111             fonts[family] = get_value(document.header, name, i, i + 1)
112             del document.header[i]
113     i = find_token_exact(document.header, '\\font_default_family', i)
114     if i == -1:
115         document.warning("Malformed LyX document: Missing `\\font_default_family'.")
116         font_default_family = 'default'
117     else:
118         font_default_family = get_value(document.header, "\\font_default_family", i, i + 1)
119         del document.header[i]
120     i = find_token_exact(document.header, '\\font_sc', i)
121     if i == -1:
122         document.warning("Malformed LyX document: Missing `\\font_sc'.")
123         font_sc = 'false'
124     else:
125         font_sc = get_value(document.header, '\\font_sc', i, i + 1)
126         del document.header[i]
127     if font_sc != 'false':
128         document.warning("Conversion of '\\font_sc' not yet implemented.")
129     i = find_token_exact(document.header, '\\font_osf', i)
130     if i == -1:
131         document.warning("Malformed LyX document: Missing `\\font_osf'.")
132         font_osf = 'false'
133     else:
134         font_osf = get_value(document.header, '\\font_osf', i, i + 1)
135         del document.header[i]
136     i = find_token_exact(document.header, '\\font_sf_scale', i)
137     if i == -1:
138         document.warning("Malformed LyX document: Missing `\\font_sf_scale'.")
139         font_sf_scale = '100'
140     else:
141         font_sf_scale = get_value(document.header, '\\font_sf_scale', i, i + 1)
142         del document.header[i]
143     if font_sf_scale != '100':
144         document.warning("Conversion of '\\font_sf_scale' not yet implemented.")
145     i = find_token_exact(document.header, '\\font_tt_scale', i)
146     if i == -1:
147         document.warning("Malformed LyX document: Missing `\\font_tt_scale'.")
148         font_tt_scale = '100'
149     else:
150         font_tt_scale = get_value(document.header, '\\font_tt_scale', i, i + 1)
151         del document.header[i]
152     if font_tt_scale != '100':
153         document.warning("Conversion of '\\font_tt_scale' not yet implemented.")
154     for font_scheme in roman_fonts.keys():
155         if (roman_fonts[font_scheme] == fonts['roman'] and
156             sans_fonts[font_scheme] == fonts['sans'] and
157             typewriter_fonts[font_scheme] == fonts['typewriter']):
158             document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
159             if font_default_family != 'default':
160                 document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
161             if font_osf == 'true':
162                 document.warning("Ignoring `\\font_osf = true'")
163             return
164     font_scheme = 'default'
165     document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
166     if fonts['roman'] == 'cmr':
167         document.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
168         if font_osf == 'true':
169             document.preamble.append('\\usepackage{eco}')
170             font_osf = 'false'
171     for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
172         if fonts['roman'] == font:
173             document.preamble.append('\\usepackage{%s}' % font)
174     for font in 'cmss', 'lmss', 'cmbr':
175         if fonts['sans'] == font:
176             document.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
177     for font in 'berasans':
178         if fonts['sans'] == font:
179             document.preamble.append('\\usepackage{%s}' % font)
180     for font in 'cmtt', 'lmtt', 'cmtl':
181         if fonts['typewriter'] == font:
182             document.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
183     for font in 'courier', 'beramono', 'luximono':
184         if fonts['typewriter'] == font:
185             document.preamble.append('\\usepackage{%s}' % font)
186     if font_default_family != 'default':
187         document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
188     if font_osf == 'true':
189         document.warning("Ignoring `\\font_osf = true'")
190
191
192 def revert_booktabs(document):
193     " We remove the booktabs flag or everything else will become a mess. "
194     re_row = re.compile(r'^<row.*space="[^"]+".*>$')
195     re_tspace = re.compile(r'\s+topspace="[^"]+"')
196     re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
197     re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
198     i = 0
199     while 1:
200         i = find_token(document.body, "\\begin_inset Tabular", i)
201         if i == -1:
202             return
203         j = find_end_of_inset(document.body, i + 1)
204         if j == -1:
205             document.warning("Malformed LyX document: Could not find end of tabular.")
206             continue
207         for k in range(i, j):
208             if re.search('^<features.* booktabs="true".*>$', document.body[k]):
209                 document.warning("Converting 'booktabs' table to normal table.")
210                 document.body[k] = document.body[k].replace(' booktabs="true"', '')
211             if re.search(re_row, document.body[k]):
212                 document.warning("Removing extra row space.")
213                 document.body[k] = re_tspace.sub('', document.body[k])
214                 document.body[k] = re_bspace.sub('', document.body[k])
215                 document.body[k] = re_ispace.sub('', document.body[k])
216         i = i + 1
217
218
219 def convert_utf8(document):
220     i = find_token(document.header, "\\inputencoding", 0)
221     if i == -1:
222         document.header.append("\\inputencoding utf-8")
223     else:
224         document.header[i] = "\\inputencoding utf-8"
225     document.inputencoding = "utf-8"
226     document.encoding = "utf-8"
227
228 ##
229 # Conversion hub
230 #
231
232 supported_versions = ["1.5.0","1.5"]
233 convert = [[246, []],
234            [247, [convert_font_settings]],
235            [248, []]
236            # ,[xxx, [convert_utf8]] uncomment to support convertion to utf-8
237           ]
238
239 revert =  [[247, [revert_booktabs]],
240            [246, [revert_font_settings]],
241            [245, [revert_framed]]]
242
243
244 if __name__ == "__main__":
245     pass
246