# -*- coding: utf-8 -*-
# This file is part of lyx2lyx
# -*- coding: utf-8 -*-
-# Copyright (C) 2010 The LyX team
+# Copyright (C) 2011 The LyX team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
del document.header[i]
+def convert_mhchem(document):
+ "Set mhchem to off for versions older than 1.6.x"
+ if document.start < 277:
+ # LyX 1.5.x and older did never load mhchem.
+ # Therefore we must switch it off: Documents that use mhchem have
+ # a manual \usepackage anyway, and documents not using mhchem but
+ # custom macros with the same names as mhchem commands might get
+ # corrupted if mhchem is automatically loaded.
+ mhchem = 0 # off
+ else:
+ # LyX 1.6.x did always load mhchem automatically.
+ mhchem = 1 # auto
+ i = find_token(document.header, "\\use_esint", 0)
+ if i == -1:
+ # pre-1.5.x document
+ i = find_token(document.header, "\\use_amsmath", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Could not find amsmath os esint setting.")
+ return
+ document.header.insert(i + 1, "\\use_mhchem %d" % mhchem)
+
+
def revert_mhchem(document):
"Revert mhchem loading to preamble code"
i = z
continue
document.body.insert(k + 1, "use_makebox 0")
- i = z + 1
+ i = blay + 1 # not z + 1 (box insets may be nested)
def revert_IEEEtran(document):
def convert_mathdots(document):
" Load mathdots automatically "
- i = find_token(document.header, "\\use_esint" , 0)
+ i = find_token(document.header, "\\use_mhchem" , 0)
+ if i == -1:
+ i = find_token(document.header, "\\use_esint" , 0)
if i != -1:
document.header.insert(i + 1, "\\use_mathdots 1")
# only need to do it once!
return
+chapters = ("amsbook", "book", "docbook-book", "elsart", "extbook", "extreport",
+ "jbook", "jreport", "jsbook", "literate-book", "literate-report", "memoir",
+ "mwbk", "mwrep", "recipebook", "report", "scrbook", "scrreprt", "svmono",
+ "svmult", "tbook", "treport", "tufte-book")
def convert_bibtex_clearpage(document):
" insert a clear(double)page bibliographystyle if bibtotoc option is used "
+ if document.textclass not in chapters:
+ return
+
i = find_token(document.header, '\\papersides', 0)
sides = 0
if i == -1:
document.warning("Can't find end of layout at line " + str(beg))
beg += 1
continue
+ document.warning(str(end))
+
# we are now going to replace newline insets within this layout
# by new instances of this layout. so we have repeated layouts
# instead of newlines.
+
+ # first, though, we need to find out if the paragraph has any
+ # customization, so those can be propogated.
+ custom = []
+ i = beg + 1
+ while document.body[i].startswith("\\"):
+ custom.append(document.body[i])
+ i += 1
+
ns = beg
while True:
ns = find_token(document.body, "\\begin_inset Newline newline", ns, end)
continue
if document.body[ne + 1] == "":
ne += 1
- subst = ["\\end_layout", "", "\\begin_layout " + lay]
+ subst = ["\\end_layout", "", "\\begin_layout " + lay] + custom
document.body[ns:ne + 1] = subst
# now we need to adjust end, in particular, but might as well
# do ns properly, too
- newlines = (ne - ns) - len(subst)
+ newlines = (ne - ns) - len(subst) + len(custom)
ns += newlines + 2
- end += newlines + 1
+ end += newlines + 2
+
# ok, we now want to find out if the next layout is the
# same as this one. if so, we will insert an extra copy of it
didit = False
add_to_preamble(document, ['\\usepackage{subscript}'])
+def convert_use_xetex(document):
+ " convert \\use_xetex to \\use_non_tex_fonts "
+ i = 0
+ i = find_token(document.header, "\\use_xetex", 0)
+ if i == -1:
+ return
+
+ val = get_value(document.header, "\\use_xetex", 0)
+ document.header[i] = "\\use_non_tex_fonts " + val
+
+
+def revert_use_xetex(document):
+ " revert \\use_non_tex_fonts to \\use_xetex "
+ i = 0
+ i = find_token(document.header, "\\use_non_tex_fonts", 0)
+ if i == -1:
+ document.warning("Malformed document. No \\use_non_tex_fonts param!")
+ return
+
+ val = get_value(document.header, "\\use_non_tex_fonts", 0)
+ document.header[i] = "\\use_xetex " + val
+
+
+def revert_labeling(document):
+ koma = ("scrartcl", "scrarticle-beamer", "scrbook", "scrlettr",
+ "scrlttr2", "scrreprt")
+ if document.textclass in koma:
+ return
+ i = 0
+ while True:
+ i = find_token_exact(document.body, "\\begin_layout Labeling", i)
+ if i == -1:
+ return
+ document.body[i] = "\\begin_layout List"
+
+
+def revert_langpack(document):
+ " revert \\language_package parameter "
+ i = 0
+ i = find_token(document.header, "\\language_package", 0)
+ if i == -1:
+ document.warning("Malformed document. No \\language_package param!")
+ return
+
+ del document.header[i]
+
+
+def convert_langpack(document):
+ " Add \\language_package parameter "
+ i = find_token(document.header, "\language" , 0)
+ if i == -1:
+ document.warning("Malformed document. No \\language defined!")
+ return
+
+ document.header.insert(i + 1, "\\language_package default")
+
+
+def revert_tabularwidth(document):
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Tabular", i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Unable to find end of Tabular inset at line " + str(i))
+ i += 1
+ continue
+ i += 1
+ features = find_token(document.body, "<features", i, j)
+ if features == -1:
+ document.warning("Can't find any features in Tabular inset at line " + str(i))
+ i = j
+ continue
+ if document.body[features].find('alignment="tabularwidth"') != -1:
+ remove_option(document.body, features, 'tabularwidth')
+
+
##
# Conversion hub
#
[368, []],
[369, [convert_author_id]],
[370, []],
- [371, []],
+ [371, [convert_mhchem]],
[372, []],
[373, [merge_gbrief]],
[374, []],
[405, []],
[406, [convert_passthru]],
[407, []],
- [408, []]
+ [408, []],
+ [409, [convert_use_xetex]],
+ [410, []],
+ [411, [convert_langpack]],
+ [412, []]
]
-revert = [[407, [revert_script]],
+revert = [[411, [revert_tabularwidth]],
+ [410, [revert_langpack]],
+ [409, [revert_labeling]],
+ [408, [revert_use_xetex]],
+ [407, [revert_script]],
[406, [revert_multirowOffset]],
[405, [revert_passthru]],
[404, []],