1 # -*- coding: utf-8 -*-
2 # This file is part of lyx2lyx
3 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 The LyX team
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.
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.
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
20 """ Convert files to the file format generated by lyx 2.1"""
26 # Uncomment only what you need to import, please.
28 from parser_tools import del_token, find_token, find_end_of, find_end_of_inset, \
29 find_re, get_option_value, get_value, get_quoted_value, set_option_value
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
36 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
38 #from lyx2lyx_tools import insert_to_preamble, \
39 # put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
40 # revert_font_attrs, hex2ratio, str2bool
42 ####################################################################
43 # Private helper functions
45 #def remove_option(lines, m, option):
46 #''' removes option from line m. returns whether we did anything '''
47 #l = lines[m].find(option)
50 #val = lines[m][l:].split('"')[1]
51 #lines[m] = lines[m][:l - 1] + lines[m][l+len(option + '="' + val + '"'):]
55 ###############################################################################
57 ### Conversion and reversion routines
59 ###############################################################################
61 def revert_visible_space(document):
62 "Revert InsetSpace visible into its ERT counterpart"
65 i = find_token(document.body, "\\begin_inset space \\textvisiblespace{}", i)
68 end = find_end_of_inset(document.body, i)
69 subst = put_cmd_in_ert("\\textvisiblespace{}")
70 document.body[i:end + 1] = subst
73 def convert_undertilde(document):
74 " Load undertilde automatically "
75 i = find_token(document.header, "\\use_mathdots" , 0)
77 document.header.insert(i + 1, "\\use_undertilde 1")
80 def revert_undertilde(document):
81 " Load undertilde if used in the document "
82 undertilde = find_token(document.header, "\\use_undertilde" , 0)
84 document.warning("No \\use_undertilde line. Assuming auto.")
86 val = get_value(document.header, "\\use_undertilde", undertilde)
87 del document.header[undertilde]
91 document.warning("Invalid \\use_undertilde value: " + val + ". Assuming auto.")
92 # probably usedots has not been changed, but be safe.
100 add_to_preamble(document, ["\\usepackage{undertilde}"])
103 # so we are in the auto case. we want to load undertilde if \utilde is used.
106 i = find_token(document.body, '\\begin_inset Formula', i)
109 j = find_end_of_inset(document.body, i)
111 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
114 code = "\n".join(document.body[i:j])
115 if code.find("\\utilde") != -1:
116 add_to_preamble(document, ["\\@ifundefined{utilde}{\\usepackage{undertilde}}"])
121 def revert_negative_space(document):
122 "Revert InsetSpace negmedspace and negthickspace into its TeX-code counterpart"
127 i = find_token(document.body, "\\begin_inset space \\negmedspace{}", i)
129 j = find_token(document.body, "\\begin_inset space \\negthickspace{}", j)
131 # load amsmath in the preamble if not already loaded if we are at the end of checking
133 i = find_token(document.header, "\\use_amsmath 2", 0)
135 add_to_preamble(document, ["\\@ifundefined{negthickspace}{\\usepackage{amsmath}}"])
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)
145 end = find_end_of_inset(document.body, j)
146 subst = put_cmd_in_ert("\\negthickspace{}")
147 document.body[j:end + 1] = subst
151 def revert_math_spaces(document):
152 "Revert formulas with protected custom space and protected hfills to TeX-code"
155 i = find_token(document.body, "\\begin_inset Formula", i)
158 j = document.body[i].find("\\hspace*")
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
166 def convert_japanese_encodings(document):
167 " Rename the japanese encodings to names understood by platex "
169 "EUC-JP-pLaTeX": "euc",
171 "SJIS-pLaTeX": "sjis"
173 i = find_token(document.header, "\\inputencoding" , 0)
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]
181 def revert_japanese_encodings(document):
182 " Revert the japanese encodings name changes "
184 "euc": "EUC-JP-pLaTeX",
186 "sjis": "SJIS-pLaTeX"
188 i = find_token(document.header, "\\inputencoding" , 0)
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]
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.")
202 def revert_australian(document):
203 "Set English language variants Australian and Newzealand to English"
205 if document.language == "australian" or document.language == "newzealand":
206 document.language = "english"
207 i = find_token(document.header, "\\language", 0)
209 document.header[i] = "\\language english"
213 j = find_token(document.body, "\\lang australian", j)
215 j = find_token(document.body, "\\lang newzealand", 0)
219 document.body[j] = document.body[j].replace("\\lang newzealand", "\\lang english")
221 document.body[j] = document.body[j].replace("\\lang australian", "\\lang english")
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)
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])
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)
238 document.warning("No \\biblio_style line. Nothing to do.")
241 default_style = get_value(document.header, "\\biblio_style", i)
242 del document.header[i]
244 # We are looking for bibtex insets having the default option
247 i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
250 j = find_end_of_inset(document.body, i)
252 document.warning("Malformed LyX document: Can't find end of bibtex inset at line " + str(i))
255 k = find_token(document.body, "options", i, j)
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)
264 def handle_longtable_captions(document, forward):
267 begin_table = find_token(document.body, '<lyxtabular version=', begin_table)
268 if begin_table == -1:
270 end_table = find_end_of(document.body, begin_table, '<lyxtabular', '</lyxtabular>')
272 document.warning("Malformed LyX document: Could not find end of table.")
275 fline = find_token(document.body, "<features", begin_table, end_table)
277 document.warning("Can't find features for inset at line " + str(begin_table))
280 p = document.body[fline].find("islongtable")
285 numrows = get_option_value(document.body[begin_table], "rows")
287 numrows = int(numrows)
289 document.warning(document.body[begin_table])
290 document.warning("Unable to determine rows!")
291 begin_table = end_table
293 begin_row = begin_table
294 for row in range(numrows):
295 begin_row = find_token(document.body, '<row', begin_row, end_table)
297 document.warning("Can't find row " + str(row + 1))
299 end_row = find_end_of(document.body, begin_row, '<row', '</row>')
301 document.warning("Can't find end of row " + str(row + 1))
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')
320 # since there could be a tabular inside this one, we
321 # cannot jump to end.
325 def convert_longtable_captions(document):
326 "Add a firsthead flag to caption rows"
327 handle_longtable_captions(document, True)
330 def revert_longtable_captions(document):
331 "remove head/foot flag from caption rows"
332 handle_longtable_captions(document, False)
335 def convert_use_packages(document):
336 "use_xxx yyy => use_package xxx yyy"
337 packages = ["amsmath", "esint", "mathdots", "mhchem", "undertilde"]
339 i = find_token(document.header, "\\use_%s" % p , 0)
341 value = get_value(document.header, "\\use_%s" % p , i)
342 document.header[i] = "\\use_package %s %s" % (p, value)
345 def revert_use_packages(document):
346 "use_package xxx yyy => use_xxx yyy"
347 packages = {"amsmath":"1", "esint":"1", "mathdots":"1", "mhchem":"1", "undertilde":"1"}
348 # the order is arbitrary for the use_package version, and not all packages need to be given.
349 # Ensure a complete list and correct order (important for older LyX versions and especially lyx2lyx)
351 for p in packages.keys():
352 regexp = re.compile(r'(\\use_package\s+%s)' % p)
353 i = find_re(document.header, regexp, 0)
355 value = get_value(document.header, "\\use_package" , i).split()[1]
356 del document.header[i]
358 for (p, v) in packages.items():
359 document.header.insert(j, "\\use_%s %s" % (p, value))
363 def convert_use_mathtools(document):
364 "insert use_package mathtools"
365 i = find_token(document.header, "\\use_package", 0)
367 document.warning("Malformed LyX document: Can't find \\use_package.")
369 document.header.insert(i + 1, "\\use_package mathtools 0")
372 def revert_use_mathtools(document):
373 "remove use_package mathtools"
374 regexp = re.compile(r'(\\use_package\s+mathtools)')
375 i = find_re(document.header, regexp, 0)
376 value = "1" # default is auto
378 value = get_value(document.header, "\\use_package" , i).split()[1]
379 del document.header[i]
380 if value == "2": # on
381 add_to_preamble(document, ["\\usepackage{mathtools}"])
382 elif value == "1": # auto
383 commands = ["lgathered", "rgathered", "vcentcolon", "dblcolon", \
384 "coloneqq", "Coloneqq", "coloneq", "Coloneq", "eqqcolon", \
385 "Eqqcolon", "eqcolon", "Eqcolon", "colonapprox", \
386 "Colonapprox", "colonsim", "Colonsim"]
389 i = find_token(document.body, '\\begin_inset Formula', i)
392 j = find_end_of_inset(document.body, i)
394 document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
397 code = "\n".join(document.body[i:j])
399 if code.find("\\%s" % c) != -1:
400 add_to_preamble(document, ["\\usepackage{mathtools}"])
409 supported_versions = ["2.1.0","2.1"]
412 [415, [convert_undertilde]],
414 [417, [convert_japanese_encodings]],
417 [420, [convert_biblio_style]],
418 [421, [convert_longtable_captions]],
419 [422, [convert_use_packages]],
420 [423, [convert_use_mathtools]],
424 [422, [revert_use_mathtools]],
425 [421, [revert_use_packages]],
426 [420, [revert_longtable_captions]],
427 [419, [revert_biblio_style]],
428 [418, [revert_australian]],
429 [417, [revert_justification]],
430 [416, [revert_japanese_encodings]],
431 [415, [revert_negative_space,revert_math_spaces]],
432 [414, [revert_undertilde]],
433 [413, [revert_visible_space]]
437 if __name__ == "__main__":