from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
- revert_font_attrs, hex2ratio, str2bool
+ revert_font_attrs, hex2ratio, str2bool, revert_language
####################################################################
# Private helper functions
" Remove preamble code for \\uline font attribute. "
del_complete_lines(document.preamble,
['% Added by lyx2lyx']+ulinelatex_preamble)
- for line in document.preamble:
- print line
def revert_ulinelatex(document):
" Add preamble code for \\uline font attribute in citations. "
del document.header[i]
+mhchem_preamble = [r"\PassOptionsToPackage{version=3}{mhchem}",
+ r"\usepackage{mhchem}"]
+
def convert_mhchem(document):
"Set mhchem to off for versions older than 1.6.x"
if document.initial_format < 277:
# 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.")
+ document.warning("Malformed LyX document: "
+ "Could not find amsmath or esint setting.")
return
document.header.insert(i + 1, "\\use_mhchem %d" % mhchem)
+ # remove LyX-inserted preamble
+ if mhchem != 0:
+ del_complete_lines(document.preamble,
+ ['% Added by lyx2lyx']+mhchem_preamble)
def revert_mhchem(document):
- "Revert mhchem loading to preamble code"
+ "Revert mhchem loading to preamble code."
- mhchem = "off"
- i = find_token(document.header, "\\use_mhchem", 0)
- if i == -1:
- document.warning("Malformed LyX document: Could not find mhchem setting.")
- mhchem = "auto"
- else:
- val = get_value(document.header, "\\use_mhchem", i)
- if val == "1":
- mhchem = "auto"
- elif val == "2":
- mhchem = "on"
- del document.header[i]
-
- if mhchem == "off":
- # don't load case
- return
+ mhchem = get_value(document.header, "\\use_mhchem", delete=True)
+ try:
+ mhchem = int(mhchem)
+ except ValueError:
+ document.warning("Malformed LyX document: "
+ "Could not find mhchem setting.")
+ mhchem = 1 # "auto"
+ # mhchem in {0: "off", 1: "auto", 2: "on"}
- if mhchem == "auto":
+ if mhchem == 1: # "auto"
i = 0
- while True:
+ while i != 1 and mhchem == 1:
i = find_token(document.body, "\\begin_inset Formula", i)
- if i == -1:
- break
- line = document.body[i]
- if line.find("\\ce{") != -1 or line.find("\\cf{") != -1:
- mhchem = "on"
- break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ break
+ if (True for line in document.body[i:j]
+ if r"\ce{" in line or r"\cf{" in line):
+ mhchem = 2
+ break
i += 1
- if mhchem == "on":
- pre = ["\\PassOptionsToPackage{version=3}{mhchem}",
- "\\usepackage{mhchem}"]
- insert_to_preamble(document, pre)
+ if (mhchem == 2 # on
+ and find_token(document.preamble, r"\usepackage{mhchem}") == -1):
+ insert_to_preamble(document, mhchem_preamble)
def revert_fontenc(document):
document.header[i : j + 1] = []
+def convert_includeall(document):
+ " Add maintain_unincluded_children param "
+
+ i = 0
+ i = find_token(document.header, "\\maintain_unincluded_children", 0)
+ if i == -1:
+ i = find_token(document.header, "\\textclass", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\textclass header.")
+ return
+ document.header.insert(i, "\\maintain_unincluded_children false")
+ return
+
+
def revert_includeall(document):
" Remove maintain_unincluded_children param "
del_token(document.header, '\\maintain_unincluded_children', 0)
def revert_turkmen(document):
"Set language Turkmen to English"
- if document.language == "turkmen":
- document.language = "english"
- i = find_token(document.header, "\\language", 0)
- if i != -1:
- document.header[i] = "\\language english"
-
- j = 0
- while True:
- j = find_token(document.body, "\\lang turkmen", j)
- if j == -1:
- return
- document.body[j] = document.body[j].replace("\\lang turkmen", "\\lang english")
- j += 1
+ revert_language(document, "turkmen", "turkmen", "turkmen")
def revert_fontcolor(document):
i += 1
# Make sure it is actually in an inset!
# A normal line could begin with "LatexCommand nameref"!
- val = is_in_inset(document.body, cmdloc, \
- "\\begin_inset CommandInset ref")
- if not val:
+ stins, endins = is_in_inset(document.body, cmdloc,
+ "\\begin_inset CommandInset ref")
+ if endins == -1:
continue
- stins, endins = val
-
# ok, so it is in an InsetRef
refline = find_token(document.body, "reference", stins, endins)
if refline == -1:
break
cmdloc = i
i += 1
-
# Make sure it is actually in an inset!
- val = is_in_inset(document.body, cmdloc, \
- "\\begin_inset CommandInset ref")
+ val = is_in_inset(document.body, cmdloc,
+ "\\begin_inset CommandInset ref", default=False)
if not val:
continue
document.body[cmdloc] = "LatexCommand nameref"
[373, [merge_gbrief]],
[374, []],
[375, []],
- [376, []],
+ [376, [convert_includeall]],
[377, []],
[378, []],
[379, [convert_math_output]],