]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx_1_5.py
* LyX.py
[features.git] / lib / lyx2lyx / lyx_1_5.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
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 import re
21 from parser_tools import find_token, find_token_exact, find_tokens, find_end_of, get_value
22 from string import replace
23
24
25 ####################################################################
26 # Private helper functions
27
28 def find_end_of_inset(lines, i):
29     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
30
31 # End of helper functions
32 ####################################################################
33
34
35 ##
36 #  Notes: Framed/Shaded
37 #
38
39 def revert_framed(file):
40     i = 0
41     while 1:
42         i = find_tokens(file.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
43
44         if i == -1:
45             return
46         file.body[i] = "\\begin_inset Note"
47         i = i + 1
48
49
50 ##
51 #  Fonts
52 #
53
54 roman_fonts      = {'default' : 'default', 'ae'       : 'ae',
55                     'times'   : 'times',   'palatino' : 'palatino',
56                     'helvet'  : 'default', 'avant'    : 'default',
57                     'newcent' : 'newcent', 'bookman'  : 'bookman',
58                     'pslatex' : 'times'}
59 sans_fonts       = {'default' : 'default', 'ae'       : 'default',
60                     'times'   : 'default', 'palatino' : 'default',
61                     'helvet'  : 'helvet',  'avant'    : 'avant',
62                     'newcent' : 'default', 'bookman'  : 'default',
63                     'pslatex' : 'helvet'}
64 typewriter_fonts = {'default' : 'default', 'ae'       : 'default',
65                     'times'   : 'default', 'palatino' : 'default',
66                     'helvet'  : 'default', 'avant'    : 'default',
67                     'newcent' : 'default', 'bookman'  : 'default',
68                     'pslatex' : 'courier'}
69
70 def convert_font_settings(file):
71     i = 0
72     i = find_token_exact(file.header, "\\fontscheme", i)
73     if i == -1:
74         file.warning("Malformed LyX file: Missing `\\fontscheme'.")
75         return
76     font_scheme = get_value(file.header, "\\fontscheme", i, i + 1)
77     if font_scheme == '':
78         file.warning("Malformed LyX file: Empty `\\fontscheme'.")
79         font_scheme = 'default'
80     if not font_scheme in roman_fonts.keys():
81         file.warning("Malformed LyX file: Unknown `\\fontscheme' `%s'." % font_scheme)
82         font_scheme = 'default'
83     file.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
84                           '\\font_sans %s' % sans_fonts[font_scheme],
85                           '\\font_typewriter %s' % typewriter_fonts[font_scheme],
86                           '\\font_default_family default',
87                           '\\font_sc false',
88                           '\\font_osf false',
89                           '\\font_sf_scale 100',
90                           '\\font_tt_scale 100']
91
92
93 def revert_font_settings(file):
94     i = 0
95     insert_line = -1
96     fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
97     for family in 'roman', 'sans', 'typewriter':
98         name = '\\font_%s' % family
99         i = find_token_exact(file.header, name, i)
100         if i == -1:
101             file.warning("Malformed LyX file: Missing `%s'." % name)
102             i = 0
103         else:
104             if (insert_line < 0):
105                 insert_line = i
106             fonts[family] = get_value(file.header, name, i, i + 1)
107             del file.header[i]
108     i = find_token_exact(file.header, '\\font_default_family', i)
109     if i == -1:
110         file.warning("Malformed LyX file: Missing `\\font_default_family'.")
111         font_default_family = 'default'
112     else:
113         font_default_family = get_value(file.header, "\\font_default_family", i, i + 1)
114         del file.header[i]
115     i = find_token_exact(file.header, '\\font_sc', i)
116     if i == -1:
117         file.warning("Malformed LyX file: Missing `\\font_sc'.")
118         font_sc = 'false'
119     else:
120         font_sc = get_value(file.header, '\\font_sc', i, i + 1)
121         del file.header[i]
122     if font_sc != 'false':
123         file.warning("Conversion of '\\font_sc' not yet implemented.")
124     i = find_token_exact(file.header, '\\font_osf', i)
125     if i == -1:
126         file.warning("Malformed LyX file: Missing `\\font_osf'.")
127         font_osf = 'false'
128     else:
129         font_osf = get_value(file.header, '\\font_osf', i, i + 1)
130         del file.header[i]
131     i = find_token_exact(file.header, '\\font_sf_scale', i)
132     if i == -1:
133         file.warning("Malformed LyX file: Missing `\\font_sf_scale'.")
134         font_sf_scale = '100'
135     else:
136         font_sf_scale = get_value(file.header, '\\font_sf_scale', i, i + 1)
137         del file.header[i]
138     if font_sf_scale != '100':
139         file.warning("Conversion of '\\font_sf_scale' not yet implemented.")
140     i = find_token_exact(file.header, '\\font_tt_scale', i)
141     if i == -1:
142         file.warning("Malformed LyX file: Missing `\\font_tt_scale'.")
143         font_tt_scale = '100'
144     else:
145         font_tt_scale = get_value(file.header, '\\font_tt_scale', i, i + 1)
146         del file.header[i]
147     if font_tt_scale != '100':
148         file.warning("Conversion of '\\font_tt_scale' not yet implemented.")
149     for font_scheme in roman_fonts.keys():
150         if (roman_fonts[font_scheme] == fonts['roman'] and
151             sans_fonts[font_scheme] == fonts['sans'] and
152             typewriter_fonts[font_scheme] == fonts['typewriter']):
153             file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
154             if font_default_family != 'default':
155                 file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
156             if font_osf == 'true':
157                 file.warning("Ignoring `\\font_osf = true'")
158             return
159     font_scheme = 'default'
160     file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
161     if fonts['roman'] == 'cmr':
162         file.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
163         if font_osf == 'true':
164             file.preamble.append('\\usepackage{eco}')
165             font_osf = 'false'
166     for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
167         if fonts['roman'] == font:
168             file.preamble.append('\\usepackage{%s}' % font)
169     for font in 'cmss', 'lmss', 'cmbr':
170         if fonts['sans'] == font:
171             file.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
172     for font in 'berasans':
173         if fonts['sans'] == font:
174             file.preamble.append('\\usepackage{%s}' % font)
175     for font in 'cmtt', 'lmtt', 'cmtl':
176         if fonts['typewriter'] == font:
177             file.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
178     for font in 'courier', 'beramono', 'luximono':
179         if fonts['typewriter'] == font:
180             file.preamble.append('\\usepackage{%s}' % font)
181     if font_default_family != 'default':
182         file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
183     if font_osf == 'true':
184         file.warning("Ignoring `\\font_osf = true'")
185
186
187 def revert_booktabs(file):
188 # we just remove the booktabs flag, everything else will become a mess.
189     re_row = re.compile(r'^<row.*space="[^"]+".*>$')
190     re_tspace = re.compile(r'\s+topspace="[^"]+"')
191     re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
192     re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
193     i = 0
194     while 1:
195         i = find_token(file.body, "\\begin_inset Tabular", i)
196         if i == -1:
197             return
198         j = find_end_of_inset(file.body, i + 1)
199         if j == -1:
200             file.warning("Malformed LyX file: Could not find end of tabular.")
201             continue
202         for k in range(i, j):
203             if re.search('^<features.* booktabs="true".*>$', file.body[k]):
204                 file.warning("Converting 'booktabs' table to normal table.")
205                 file.body[k] = replace(file.body[k], ' booktabs="true"', '')
206             if re.search(re_row, file.body[k]):
207                 file.warning("Removing extra row space.")
208                 file.body[k] = re_tspace.sub('', file.body[k])
209                 file.body[k] = re_bspace.sub('', file.body[k])
210                 file.body[k] = re_ispace.sub('', file.body[k])
211         i = i + 1
212
213
214 ##
215 # Conversion hub
216 #
217
218 convert = [[246, []],
219            [247, [convert_font_settings]],
220            [248, []]]
221
222 revert =  [[247, [revert_booktabs]],
223            [246, [revert_font_settings]],
224            [245, [revert_framed]]]
225
226
227 if __name__ == "__main__":
228     pass
229