]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_1.py
More sensible longtable caption handling (needed for bug #7412)
[lyx.git] / lib / lyx2lyx / lyx_2_1.py
1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 The LyX team
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
20 """ Convert files to the file format generated by lyx 2.1"""
21
22 import re, string
23 import unicodedata
24 import sys, os
25
26 # Uncomment only what you need to import, please.
27
28 from parser_tools import del_token, find_token, find_end_of, find_end_of_inset, \
29     get_option_value, get_value, get_quoted_value, set_option_value
30
31 #from parser_tools import find_token, find_end_of, find_tokens, \
32   #find_token_exact, find_end_of_inset, find_end_of_layout, \
33   #find_token_backwards, is_in_inset, get_value, get_quoted_value, \
34   #del_token, check_token, get_option_value
35
36 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
37
38 #from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
39 #  put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
40 #  revert_font_attrs, hex2ratio, str2bool
41
42 ####################################################################
43 # Private helper functions
44
45 #def remove_option(lines, m, option):
46     #''' removes option from line m. returns whether we did anything '''
47     #l = lines[m].find(option)
48     #if l == -1:
49         #return False
50     #val = lines[m][l:].split('"')[1]
51     #lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
52     #return True
53
54
55 ###############################################################################
56 ###
57 ### Conversion and reversion routines
58 ###
59 ###############################################################################
60
61 def revert_visible_space(document):
62     "Revert InsetSpace visible into its ERT counterpart"
63     i = 0
64     while True:
65       i = find_token(document.body, "\\begin_inset space \\textvisiblespace{}", i)
66       if i == -1:
67         return
68       end = find_end_of_inset(document.body, i)
69       subst = put_cmd_in_ert("\\textvisiblespace{}")
70       document.body[i:end + 1] = subst
71
72
73 def convert_undertilde(document):
74     " Load undertilde automatically "
75     i = find_token(document.header, "\\use_mathdots" , 0)
76     if i != -1:
77       document.header.insert(i + 1, "\\use_undertilde 1")
78
79
80 def revert_undertilde(document):
81     " Load undertilde if used in the document "
82     undertilde = find_token(document.header, "\\use_undertilde" , 0)
83     if undertilde == -1:
84       document.warning("No \\use_undertilde line. Assuming auto.")
85     else:
86       val = get_value(document.header, "\\use_undertilde", undertilde)
87       del document.header[undertilde]
88       try:
89         usetilde = int(val)
90       except:
91         document.warning("Invalid \\use_undertilde value: " + val + ". Assuming auto.")
92         # probably usedots has not been changed, but be safe.
93         usetilde = 1
94
95       if usetilde == 0:
96         # do not load case
97         return
98       if usetilde == 2:
99         # force load case
100         add_to_preamble(document, ["\\usepackage{undertilde}"])
101         return
102     
103     # so we are in the auto case. we want to load undertilde if \utilde is used.
104     i = 0
105     while True:
106       i = find_token(document.body, '\\begin_inset Formula', i)
107       if i == -1:
108         return
109       j = find_end_of_inset(document.body, i)
110       if j == -1:
111         document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
112         i += 1
113         continue
114       code = "\n".join(document.body[i:j])
115       if code.find("\\utilde") != -1:
116         add_to_preamble(document, ["\\@ifundefined{utilde}{\\usepackage{undertilde}}"])
117         return
118       i = j
119
120
121 def revert_negative_space(document):
122     "Revert InsetSpace negmedspace and negthickspace into its TeX-code counterpart"
123     i = 0
124     j = 0
125     reverted = False
126     while True:
127       i = find_token(document.body, "\\begin_inset space \\negmedspace{}", i)
128       if i == -1:
129         j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
130         if j == -1:
131           # load amsmath in the preamble if not already loaded if we are at the end of checking
132           if reverted == True:
133             i = find_token(document.header, "\\use_amsmath 2", 0)
134             if i == -1:
135               add_to_preamble(document, ["\\@ifundefined{negthickspace}{\\usepackage{amsmath}}"])
136           return
137       if i == -1:
138         return
139       end = find_end_of_inset(document.body, i)
140       subst = put_cmd_in_ert("\\negmedspace{}")
141       document.body[i:end + 1] = subst
142       j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
143       if j == -1:
144         return
145       end = find_end_of_inset(document.body, j)
146       subst = put_cmd_in_ert("\\negthickspace{}")
147       document.body[j:end + 1] = subst
148       reverted = True
149
150
151 def revert_math_spaces(document):
152     "Revert formulas with protected custom space and protected hfills to TeX-code"
153     i = 0
154     while True:
155       i = find_token(document.body, "\\begin_inset Formula", i)
156       if i == -1:
157         return
158       j = document.body[i].find("\\hspace*")
159       if j != -1:
160         end = find_end_of_inset(document.body, i)
161         subst = put_cmd_in_ert(document.body[i][21:])
162         document.body[i:end + 1] = subst
163       i = i + 1
164
165
166 def convert_japanese_encodings(document):
167     " Rename the japanese encodings to names understood by platex "
168     jap_enc_dict = {
169         "EUC-JP-pLaTeX": "euc",
170         "JIS-pLaTeX":    "jis",
171         "SJIS-pLaTeX":   "sjis"
172     }
173     i = find_token(document.header, "\\inputencoding" , 0)
174     if i == -1:
175         return
176     val = get_value(document.header, "\\inputencoding", i)
177     if val in jap_enc_dict.keys():
178         document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
179
180
181 def revert_japanese_encodings(document):
182     " Revert the japanese encodings name changes "
183     jap_enc_dict = {
184         "euc":  "EUC-JP-pLaTeX",
185         "jis":  "JIS-pLaTeX",
186         "sjis": "SJIS-pLaTeX"
187     }
188     i = find_token(document.header, "\\inputencoding" , 0)
189     if i == -1:
190         return
191     val = get_value(document.header, "\\inputencoding", i)
192     if val in jap_enc_dict.keys():
193         document.header[i] = "\\inputencoding %s" % jap_enc_dict[val]
194
195
196 def revert_justification(document):
197     " Revert the \\justification buffer param"
198     if not del_token(document.header, '\\justification', 0):
199         document.warning("Malformed LyX document: Missing \\justification.")
200
201
202 def revert_australian(document):
203     "Set English language variants Australian and Newzealand to English" 
204
205     if document.language == "australian" or document.language == "newzealand": 
206         document.language = "english" 
207         i = find_token(document.header, "\\language", 0) 
208         if i != -1: 
209             document.header[i] = "\\language english" 
210
211     j = 0 
212     while True: 
213         j = find_token(document.body, "\\lang australian", j) 
214         if j == -1: 
215             j = find_token(document.body, "\\lang newzealand", 0)
216             if j == -1:
217                 return
218             else:
219                 document.body[j] = document.body[j].replace("\\lang newzealand", "\\lang english")
220         else:
221             document.body[j] = document.body[j].replace("\\lang australian", "\\lang english") 
222         j += 1
223
224
225 def convert_biblio_style(document):
226     "Add a sensible default for \\biblio_style based on the citation engine."
227     i = find_token(document.header, "\\cite_engine", 0)
228     if i != -1:
229         engine = get_value(document.header, "\\cite_engine", i).split("_")[0]
230         style = {"basic": "plain", "natbib": "plainnat", "jurabib": "jurabib"}
231         document.header.insert(i + 1, "\\biblio_style " + style[engine])
232
233
234 def revert_biblio_style(document):
235     "BibTeX insets with default option use the style defined by \\biblio_style."
236     i = find_token(document.header, "\\biblio_style" , 0)
237     if i == -1:
238         document.warning("No \\biblio_style line. Nothing to do.")
239         return
240
241     default_style = get_value(document.header, "\\biblio_style", i)
242     del document.header[i]
243
244     # We are looking for bibtex insets having the default option
245     i = 0
246     while True:
247         i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
248         if i == -1:
249             return
250         j = find_end_of_inset(document.body, i)
251         if j == -1:
252             document.warning("Malformed LyX document: Can't find end of bibtex inset at line " + str(i))
253             i += 1
254             return
255         k = find_token(document.body, "options", i, j)
256         if k != -1:
257             options = get_quoted_value(document.body, "options", k)
258             if "default" in options.split(","):
259                 document.body[k] = 'options "%s"' \
260                     % options.replace("default", default_style)
261         i = j
262
263
264 def handle_longtable_captions(document, forward):
265     begin_table = 0
266     while True:
267         begin_table = find_token(document.body, '<lyxtabular version=', begin_table)
268         if begin_table == -1:
269             break
270         end_table = find_end_of(document.body, begin_table, '<lyxtabular', '</lyxtabular>')
271         if end_table == -1:
272             document.warning("Malformed LyX document: Could not find end of table.")
273             begin_table += 1
274             continue
275         fline = find_token(document.body, "<features", begin_table, end_table)
276         if fline == -1:
277             document.warning("Can't find features for inset at line " + str(begin_table))
278             begin_table += 1
279             continue
280         p = document.body[fline].find("islongtable")
281         if p == -1:
282             # no longtable
283             begin_table += 1
284             continue
285         numrows = get_option_value(document.body[begin_table], "rows")
286         try:
287             numrows = int(numrows)
288         except:
289             document.warning(document.body[begin_table])
290             document.warning("Unable to determine rows!")
291             begin_table = end_table
292             continue
293         begin_row = begin_table
294         for row in range(numrows):
295             begin_row = find_token(document.body, '<row', begin_row, end_table)
296             if begin_row == -1:
297                 document.warning("Can't find row " + str(row + 1))
298                 break
299             end_row = find_end_of(document.body, begin_row, '<row', '</row>')
300             if end_row == -1:
301                 document.warning("Can't find end of row " + str(row + 1))
302                 break
303             if forward:
304                 if (get_option_value(document.body[begin_row], 'caption') == 'true' and
305                     get_option_value(document.body[begin_row], 'endfirsthead') != 'true' and
306                     get_option_value(document.body[begin_row], 'endhead') != 'true' and
307                     get_option_value(document.body[begin_row], 'endfoot') != 'true' and
308                     get_option_value(document.body[begin_row], 'endlastfoot') != 'true'):
309                     document.body[begin_row] = set_option_value(document.body[begin_row], 'caption', 'true", endfirsthead="true')
310             elif get_option_value(document.body[begin_row], 'caption') == 'true':
311                 if get_option_value(document.body[begin_row], 'endfirsthead') == 'true':
312                     document.body[begin_row] = set_option_value(document.body[begin_row], 'endfirsthead', 'false')
313                 if get_option_value(document.body[begin_row], 'endhead') == 'true':
314                     document.body[begin_row] = set_option_value(document.body[begin_row], 'endhead', 'false')
315                 if get_option_value(document.body[begin_row], 'endfoot') == 'true':
316                     document.body[begin_row] = set_option_value(document.body[begin_row], 'endfoot', 'false')
317                 if get_option_value(document.body[begin_row], 'endlastfoot') == 'true':
318                     document.body[begin_row] = set_option_value(document.body[begin_row], 'endlastfoot', 'false')
319             begin_row = end_row
320         # since there could be a tabular inside this one, we 
321         # cannot jump to end.
322         begin_table += 1
323
324
325 def convert_longtable_captions(document):
326     "Add a firsthead flag to caption rows"
327     handle_longtable_captions(document, True)
328
329
330 def revert_longtable_captions(document):
331     "remove head/foot flag from caption rows"
332     handle_longtable_captions(document, False)
333
334
335 ##
336 # Conversion hub
337 #
338
339 supported_versions = ["2.1.0","2.1"]
340 convert = [
341            [414, []],
342            [415, [convert_undertilde]],
343            [416, []],
344            [417, [convert_japanese_encodings]],
345            [418, []],
346            [419, []],
347            [420, [convert_biblio_style]],
348            [421, [convert_longtable_captions]],
349           ]
350
351 revert =  [
352            [420, [revert_longtable_captions]],
353            [419, [revert_biblio_style]],
354            [418, [revert_australian]],
355            [417, [revert_justification]],
356            [416, [revert_japanese_encodings]],
357            [415, [revert_negative_space,revert_math_spaces]],
358            [414, [revert_undertilde]],
359            [413, [revert_visible_space]]
360           ]
361
362
363 if __name__ == "__main__":
364     pass