+
+
+def revert_theendnotes(document):
+ " Reverts native support of \\theendnotes to TeX-code "
+
+ if not "endnotes" in document.get_module_list() and not "foottoend" in document.get_module_list():
+ return
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset FloatList endnote", i + 1)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of FloatList inset")
+ continue
+
+ document.body[i : j + 1] = put_cmd_in_ert("\\theendnotes")
+
+
+def revert_enotez(document):
+ " Reverts native support of enotez package to TeX-code "
+
+ if not "enotez" in document.get_module_list() and not "foottoenotez" in document.get_module_list():
+ return
+
+ use = False
+ if find_token(document.body, "\\begin_inset Flex Endnote", 0) != -1:
+ use = True
+
+ revert_flex_inset(document.body, "Endnote", "\\endnote")
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset FloatList endnote", i + 1)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of FloatList inset")
+ continue
+
+ use = True
+ document.body[i : j + 1] = put_cmd_in_ert("\\printendnotes")
+
+ if use:
+ add_to_preamble(document, ["\\usepackage{enotez}"])
+ document.del_module("enotez")
+ document.del_module("foottoenotez")
+
+
+def revert_memoir_endnotes(document):
+ " Reverts native support of memoir endnotes to TeX-code "
+
+ if document.textclass != "memoir":
+ return
+
+ encommand = "\\pagenote"
+ modules = document.get_module_list()
+ if "enotez" in modules or "foottoenotez" in modules or "endnotes" in modules or "foottoend" in modules:
+ encommand = "\\endnote"
+
+ revert_flex_inset(document.body, "Endnote", encommand)
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset FloatList pagenote", i + 1)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of FloatList inset")
+ continue
+
+ if document.body[i] == "\\begin_inset FloatList pagenote*":
+ document.body[i : j + 1] = put_cmd_in_ert("\\printpagenotes*")
+ else:
+ document.body[i : j + 1] = put_cmd_in_ert("\\printpagenotes")
+ add_to_preamble(document, ["\\makepagenote"])
+
+
+def revert_totalheight(document):
+ " Reverts graphics height parameter from totalheight to height "
+
+ i = 0
+ while (True):
+ i = find_token(document.body, "\\begin_inset Graphics", i)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Can't find end of graphics inset at line %d!!" %(i))
+ i += 1
+ continue
+
+ rx = re.compile(r'\s*special\s*(\S+)$')
+ k = find_re(document.body, rx, i, j)
+ special = ""
+ oldheight = ""
+ if k != -1:
+ m = rx.match(document.body[k])
+ if m:
+ special = m.group(1)
+ mspecial = special.split(',')
+ for spc in mspecial:
+ if spc[:7] == "height=":
+ oldheight = spc.split('=')[1]
+ mspecial.remove(spc)
+ break
+ if len(mspecial) > 0:
+ special = ",".join(mspecial)
+ else:
+ special = ""
+
+ rx = re.compile(r'(\s*height\s*)(\S+)$')
+ kk = find_re(document.body, rx, i, j)
+ if kk != -1:
+ m = rx.match(document.body[kk])
+ val = ""
+ if m:
+ val = m.group(2)
+ if k != -1:
+ if special != "":
+ val = val + "," + special
+ document.body[k] = "\tspecial " + "totalheight=" + val
+ else:
+ document.body.insert(kk, "\tspecial totalheight=" + val)
+ if oldheight != "":
+ document.body[kk] = m.group(1) + oldheight
+ else:
+ del document.body[kk]
+ elif oldheight != "":
+ if special != "":
+ document.body[k] = "\tspecial " + special
+ document.body.insert(k, "\theight " + oldheight)
+ else:
+ document.body[k] = "\theight " + oldheight
+ i = j + 1
+
+
+def convert_totalheight(document):
+ " Converts graphics height parameter from totalheight to height "
+
+ i = 0
+ while (True):
+ i = find_token(document.body, "\\begin_inset Graphics", i)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Can't find end of graphics inset at line %d!!" %(i))
+ i += 1
+ continue
+
+ rx = re.compile(r'\s*special\s*(\S+)$')
+ k = find_re(document.body, rx, i, j)
+ special = ""
+ newheight = ""
+ if k != -1:
+ m = rx.match(document.body[k])
+ if m:
+ special = m.group(1)
+ mspecial = special.split(',')
+ for spc in mspecial:
+ if spc[:12] == "totalheight=":
+ newheight = spc.split('=')[1]
+ mspecial.remove(spc)
+ break
+ if len(mspecial) > 0:
+ special = ",".join(mspecial)
+ else:
+ special = ""
+
+ rx = re.compile(r'(\s*height\s*)(\S+)$')
+ kk = find_re(document.body, rx, i, j)
+ if kk != -1:
+ m = rx.match(document.body[kk])
+ val = ""
+ if m:
+ val = m.group(2)
+ if k != -1:
+ if special != "":
+ val = val + "," + special
+ document.body[k] = "\tspecial " + "height=" + val
+ else:
+ document.body.insert(kk + 1, "\tspecial height=" + val)
+ if newheight != "":
+ document.body[kk] = m.group(1) + newheight
+ else:
+ del document.body[kk]
+ elif newheight != "":
+ document.body.insert(k, "\theight " + newheight)
+ i = j + 1
+
+
+def convert_changebars(document):
+ " Converts the changebars module to native solution "
+
+ if not "changebars" in document.get_module_list():
+ return
+
+ i = find_token(document.header, "\\output_changes", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\output_changes header.")
+ document.del_module("changebars")
+ return
+
+ document.header.insert(i, "\\change_bars true")
+ document.del_module("changebars")
+
+
+def revert_changebars(document):
+ " Converts native changebar param to module "
+
+ i = find_token(document.header, "\\change_bars", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\change_bars header.")
+ return
+
+ val = get_value(document.header, "\\change_bars", i)
+
+ if val == "true":
+ document.add_module("changebars")
+
+ del document.header[i]
+
+
+def convert_postpone_fragile(document):
+ " Adds false \\postpone_fragile_content buffer param "
+
+ i = find_token(document.header, "\\output_changes", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\output_changes header.")
+ return
+ # Set this to false for old documents (see #2154)
+ document.header.insert(i, "\\postpone_fragile_content false")
+
+
+def revert_postpone_fragile(document):
+ " Remove \\postpone_fragile_content buffer param "
+
+ i = find_token(document.header, "\\postpone_fragile_content", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\postpone_fragile_content.")
+ return
+
+ del document.header[i]
+
+
+def revert_colrow_tracking(document):
+ " Remove change tag from tabular columns/rows "
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Tabular", i+1)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i+1)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of tabular.")
+ continue
+ for k in range(i, j):
+ m = re.search('^<column.*change="([^"]+)".*>$', document.body[k])
+ if m:
+ document.body[k] = document.body[k].replace(' change="' + m.group(1) + '"', '')
+ m = re.search('^<row.*change="([^"]+)".*>$', document.body[k])
+ if m:
+ document.body[k] = document.body[k].replace(' change="' + m.group(1) + '"', '')
+
+
+def convert_counter_maintenance(document):
+ " Convert \\maintain_unincluded_children buffer param from boolean value tro tristate "
+
+ i = find_token(document.header, "\\maintain_unincluded_children", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\maintain_unincluded_children.")
+ return
+
+ val = get_value(document.header, "\\maintain_unincluded_children", i)
+
+ if val == "true":
+ document.header[i] = "\\maintain_unincluded_children strict"
+ else:
+ document.header[i] = "\\maintain_unincluded_children no"
+
+
+def revert_counter_maintenance(document):
+ " Revert \\maintain_unincluded_children buffer param to previous boolean value "
+
+ i = find_token(document.header, "\\maintain_unincluded_children", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\maintain_unincluded_children.")
+ return
+
+ val = get_value(document.header, "\\maintain_unincluded_children", i)
+
+ if val == "no":
+ document.header[i] = "\\maintain_unincluded_children false"
+ else:
+ document.header[i] = "\\maintain_unincluded_children true"
+
+
+def revert_counter_inset(document):
+ " Revert counter inset to ERT, where possible"
+ i = 0
+ needed_counters = {}
+ while True:
+ i = find_token(document.body, "\\begin_inset CommandInset counter", i)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Can't find end of counter inset at line %d!" % i)
+ i += 1
+ continue
+ lyx = get_quoted_value(document.body, "lyxonly", i, j)
+ if lyx == "true":
+ # there is nothing we can do to affect the LyX counters
+ document.body[i : j + 1] = []
+ i = j + 1
+ continue
+ cnt = get_quoted_value(document.body, "counter", i, j)
+ if not cnt:
+ document.warning("No counter given for inset at line %d!" % i)
+ i = j + 1
+ continue
+
+ cmd = get_quoted_value(document.body, "LatexCommand", i, j)
+ document.warning(cmd)
+ ert = ""
+ if cmd == "set":
+ val = get_quoted_value(document.body, "value", i, j)
+ if not val:
+ document.warning("Can't convert counter inset at line %d!" % i)
+ else:
+ ert = put_cmd_in_ert("\\setcounter{%s}{%s}" % (cnt, val))
+ elif cmd == "addto":
+ val = get_quoted_value(document.body, "value", i, j)
+ if not val:
+ document.warning("Can't convert counter inset at line %d!" % i)
+ else:
+ ert = put_cmd_in_ert("\\addtocounter{%s}{%s}" % (cnt, val))
+ elif cmd == "reset":
+ ert = put_cmd_in_ert("\\setcounter{%s}{0}" % (cnt))
+ elif cmd == "save":
+ needed_counters[cnt] = 1
+ savecnt = "LyXSave" + cnt
+ ert = put_cmd_in_ert("\\setcounter{%s}{\\value{%s}}" % (savecnt, cnt))
+ elif cmd == "restore":
+ needed_counters[cnt] = 1
+ savecnt = "LyXSave" + cnt
+ ert = put_cmd_in_ert("\\setcounter{%s}{\\value{%s}}" % (cnt, savecnt))
+ else:
+ document.warning("Unknown counter command `%s' in inset at line %d!" % (cnt, i))
+
+ if ert:
+ document.body[i : j + 1] = ert
+ i += 1
+ continue
+
+ pretext = []
+ for cnt in needed_counters:
+ pretext.append("\\newcounter{LyXSave%s}" % (cnt))
+ if pretext:
+ add_to_preamble(document, pretext)
+
+
+def revert_ams_spaces(document):
+ "Revert InsetSpace medspace and thickspace into their TeX-code counterparts"
+ Found = False
+ insets = ["\\medspace{}", "\\thickspace{}"]
+ for inset in insets:
+ i = 0
+ j = 0
+ i = find_token(document.body, "\\begin_inset space " + inset, i)
+ if i == -1:
+ continue
+ end = find_end_of_inset(document.body, i)
+ subst = put_cmd_in_ert(inset)
+ document.body[i : end + 1] = subst
+ Found = True
+
+ if Found == True:
+ # load amsmath in the preamble if not already loaded
+ i = find_token(document.header, "\\use_package amsmath 2", 0)
+ if i == -1:
+ add_to_preamble(document, ["\\@ifundefined{thickspace}{\\usepackage{amsmath}}{}"])
+ return
+
+
+def convert_parskip(document):
+ " Move old parskip settings to preamble "
+
+ i = find_token(document.header, "\\paragraph_separation skip", 0)
+ if i == -1:
+ return
+
+ j = find_token(document.header, "\\defskip", 0)
+ if j == -1:
+ document.warning("Malformed LyX document! Missing \\defskip.")
+ return
+
+ val = get_value(document.header, "\\defskip", j)
+
+ skipval = "\\medskipamount"
+ if val == "smallskip" or val == "medskip" or val == "bigskip":
+ skipval = "\\" + val + "amount"
+ else:
+ skipval = val
+
+ add_to_preamble(document, ["\\setlength{\\parskip}{" + skipval + "}", "\\setlength{\\parindent}{0pt}"])
+
+ document.header[i] = "\\paragraph_separation indent"
+ document.header[j] = "\\paragraph_indentation default"
+
+
+def revert_parskip(document):
+ " Revert new parskip settings to preamble "
+
+ i = find_token(document.header, "\\paragraph_separation skip", 0)
+ if i == -1:
+ return
+
+ j = find_token(document.header, "\\defskip", 0)
+ if j == -1:
+ document.warning("Malformed LyX document! Missing \\defskip.")
+ return
+
+ val = get_value(document.header, "\\defskip", j)
+
+ skipval = ""
+ if val == "smallskip" or val == "medskip" or val == "bigskip":
+ skipval = "[skip=\\" + val + "amount]"
+ elif val == "fullline":
+ skipval = "[skip=\\baselineskip]"
+ elif val != "halfline":
+ skipval = "[skip={" + val + "}]"
+
+ add_to_preamble(document, ["\\usepackage" + skipval + "{parskip}"])