+def revert_lineno(document):
+ " Replace lineno setting with user-preamble code."
+
+ options = get_quoted_value(document.header, "\\lineno_options",
+ delete=True)
+ if not get_bool_value(document.header, "\\use_lineno", delete=True):
+ return
+ if options:
+ options = "[" + options + "]"
+ add_to_preamble(document, ["\\usepackage%s{lineno}" % options,
+ "\\linenumbers"])
+
+def convert_lineno(document):
+ " Replace user-preamble code with native lineno support."
+ use_lineno = 0
+ options = ""
+ i = find_token(document.preamble, "\\linenumbers", 1)
+ if i > -1:
+ usepkg = re.match(r"\\usepackage(.*){lineno}", document.preamble[i-1])
+ if usepkg:
+ use_lineno = 1
+ options = usepkg.group(1).strip("[]")
+ del(document.preamble[i-1:i+1])
+ del_token(document.preamble, "% Added by lyx2lyx", i-2, i-1)
+
+ k = find_token(document.header, "\\index ")
+ if options == "":
+ document.header[k:k] = ["\\use_lineno %d" % use_lineno]
+ else:
+ document.header[k:k] = ["\\use_lineno %d" % use_lineno,
+ "\\lineno_options %s" % options]
+
+
+def revert_new_languages(document):
+ """Emulate support for Azerbaijani, Bengali, Church Slavonic, Korean,
+ and Russian (Petrine orthography)."""
+
+ # lyxname: (babelname, polyglossianame)
+ new_languages = {"azerbaijani": ("azerbaijani", ""),
+ "bengali": ("", "bengali"),
+ "churchslavonic": ("", "churchslavonic"),
+ "oldrussian": ("", "russian"),
+ "korean": ("", "korean"),
+ }
+ used_languages = set()
+ if document.language in new_languages:
+ used_languages.add(document.language)
+ i = 0
+ while True:
+ i = find_token(document.body, "\\lang", i+1)
+ if i == -1:
+ break
+ if document.body[i][6:].strip() in new_languages:
+ used_languages.add(document.language)
+
+ # Korean is already supported via CJK, so leave as-is for Babel
+ if ("korean" in used_languages
+ and get_bool_value(document.header, "\\use_non_tex_fonts")
+ and get_value(document.header, "\\language_package") in ("default", "auto")):
+ revert_language(document, "korean", "", "korean")
+ used_languages.discard("korean")
+
+ for lang in used_languages:
+ revert(lang, *new_languages[lang])
+
+
+gloss_inset_def = [
+ r'### Inserted by lyx2lyx (deprecated ling glosses) ###',
+ r'InsetLayout Flex:Glosse',
+ r' LyXType custom',
+ r' LabelString "Gloss (old version)"',
+ r' MenuString "Gloss (old version)"',
+ r' LatexType environment',
+ r' LatexName linggloss',
+ r' Decoration minimalistic',
+ r' LabelFont',
+ r' Size Small',
+ r' EndFont',
+ r' MultiPar true',
+ r' CustomPars false',
+ r' ForcePlain true',
+ r' ParbreakIsNewline true',
+ r' FreeSpacing true',
+ r' Requires covington',
+ r' Preamble',
+ r' \def\glosstr{}',
+ r' \@ifundefined{linggloss}{%',
+ r' \newenvironment{linggloss}[2][]{',
+ r' \def\glosstr{\glt #1}%',
+ r' \gll #2}',
+ r' {\glosstr\glend}}{}',
+ r' EndPreamble',
+ r' InToc true',
+ r' ResetsFont true',
+ r' Argument 1',
+ r' Decoration conglomerate',
+ r' LabelString "Translation"',
+ r' MenuString "Glosse Translation|s"',
+ r' Tooltip "Add a translation for the glosse"',
+ r' EndArgument',
+ r'End'
+]
+
+glosss_inset_def = [
+ r'### Inserted by lyx2lyx (deprecated ling glosses) ###',
+ r'InsetLayout Flex:Tri-Glosse',
+ r' LyXType custom',
+ r' LabelString "Tri-Gloss (old version)"',
+ r' MenuString "Tri-Gloss (old version)"',
+ r' LatexType environment',
+ r' LatexName lingglosss',
+ r' Decoration minimalistic',
+ r' LabelFont',
+ r' Size Small',
+ r' EndFont',
+ r' MultiPar true',
+ r' CustomPars false',
+ r' ForcePlain true',
+ r' ParbreakIsNewline true',
+ r' FreeSpacing true',
+ r' InToc true',
+ r' Requires covington',
+ r' Preamble',
+ r' \def\glosstr{}',
+ r' \@ifundefined{lingglosss}{%',
+ r' \newenvironment{lingglosss}[2][]{',
+ r' \def\glosstr{\glt #1}%',
+ r' \glll #2}',
+ r' {\glosstr\glend}}{}',
+ r' EndPreamble',
+ r' ResetsFont true',
+ r' Argument 1',
+ r' Decoration conglomerate',
+ r' LabelString "Translation"',
+ r' MenuString "Glosse Translation|s"',
+ r' Tooltip "Add a translation for the glosse"',
+ r' EndArgument',
+ r'End'
+]
+
+def convert_linggloss(document):
+ " Move old ling glosses to local layout "
+ if find_token(document.body, '\\begin_inset Flex Glosse', 0) != -1:
+ document.append_local_layout(gloss_inset_def)
+ if find_token(document.body, '\\begin_inset Flex Tri-Glosse', 0) != -1:
+ document.append_local_layout(glosss_inset_def)
+
+def revert_linggloss(document):
+ " Revert to old ling gloss definitions "
+ if not "linguistics" in document.get_module_list():
+ return
+ document.del_local_layout(gloss_inset_def)
+ document.del_local_layout(glosss_inset_def)
+
+ cov_req = False
+ glosses = ["\\begin_inset Flex Interlinear Gloss (2 Lines)", "\\begin_inset Flex Interlinear Gloss (3 Lines)"]
+ for glosse in glosses:
+ i = 0
+ while True:
+ i = find_token(document.body, glosse, 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 Gloss inset")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument 1", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ optargcontent = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find optarg plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ optargcontent = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ arg = find_token(document.body, "\\begin_inset Argument post:1", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ marg1content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find arg 1 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ marg1content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ arg = find_token(document.body, "\\begin_inset Argument post:2", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ marg2content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find arg 2 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ marg2content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ arg = find_token(document.body, "\\begin_inset Argument post:3", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ marg3content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find arg 3 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ marg3content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ cmd = "\\digloss"
+ if glosse == "\\begin_inset Flex Interlinear Gloss (3 Lines)":
+ cmd = "\\trigloss"
+
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
+ endInset = find_end_of_inset(document.body, i)
+ endPlain = find_end_of_layout(document.body, beginPlain)
+ precontent = put_cmd_in_ert(cmd)
+ if len(optargcontent) > 0:
+ precontent += put_cmd_in_ert("[") + optargcontent + put_cmd_in_ert("]")
+ precontent += put_cmd_in_ert("{")
+
+ postcontent = put_cmd_in_ert("}{") + marg1content + put_cmd_in_ert("}{") + marg2content
+ if cmd == "\\trigloss":
+ postcontent += put_cmd_in_ert("}{") + marg3content
+ postcontent += put_cmd_in_ert("}")
+
+ document.body[endPlain:endInset + 1] = postcontent
+ document.body[beginPlain + 1:beginPlain] = precontent
+ del document.body[i : beginPlain + 1]
+ if not cov_req:
+ document.append_local_layout("Requires covington")
+ cov_req = True
+ i = beginPlain
+
+
+def revert_subexarg(document):
+ " Revert linguistic subexamples with argument to ERT "
+
+ if not "linguistics" in document.get_module_list():
+ return
+
+ cov_req = False
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_layout Subexample", i+1)
+ if i == -1:
+ break
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of Subexample layout")
+ continue
+ while True:
+ # check for consecutive layouts
+ k = find_token(document.body, "\\begin_layout", j)
+ if k == -1 or document.body[k] != "\\begin_layout Subexample":
+ break
+ j = find_end_of_layout(document.body, k)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of Subexample layout")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument 1", i, j)
+ if arg == -1:
+ continue
+
+ endarg = find_end_of_inset(document.body, arg)
+ optargcontent = ""
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find optarg plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ optargcontent = lyx2latex(document, document.body[argbeginPlain + 1 : argendPlain - 2])
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ cmd = put_cmd_in_ert("\\begin{subexamples}[" + optargcontent + "]")
+
+ # re-find end of layout
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of Subexample layout")
+ continue
+ while True:
+ # check for consecutive layouts
+ k = find_token(document.body, "\\begin_layout", j)
+ if k == -1 or document.body[k] != "\\begin_layout Subexample":
+ break
+ document.body[k : k + 1] = ["\\begin_layout Standard"] + put_cmd_in_ert("\\item ")
+ j = find_end_of_layout(document.body, k)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of Subexample layout")
+ continue
+
+ endev = put_cmd_in_ert("\\end{subexamples}")
+
+ document.body[j : j] = ["\\end_layout", "", "\\begin_layout Standard"] + endev
+ document.body[i : i + 1] = ["\\begin_layout Standard"] + cmd \
+ + ["\\end_layout", "", "\\begin_layout Standard"] + put_cmd_in_ert("\\item ")
+ if not cov_req:
+ document.append_local_layout("Requires covington")
+ cov_req = True
+
+
+def revert_drs(document):
+ " Revert DRS insets (linguistics) to ERT "
+
+ if not "linguistics" in document.get_module_list():
+ return
+
+ cov_req = False
+ drses = ["\\begin_inset Flex DRS", "\\begin_inset Flex DRS*",
+ "\\begin_inset Flex IfThen-DRS", "\\begin_inset Flex Cond-DRS",
+ "\\begin_inset Flex QDRS", "\\begin_inset Flex NegDRS",
+ "\\begin_inset Flex SDRS"]
+ for drs in drses:
+ i = 0
+ while True:
+ i = find_token(document.body, drs, 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 DRS inset")
+ continue
+
+ # Check for arguments
+ arg = find_token(document.body, "\\begin_inset Argument 1", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ prearg1content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find Argument 1 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ prearg1content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ # re-find inset end
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of DRS inset")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument 2", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ prearg2content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find Argument 2 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ prearg2content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ # re-find inset end
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of DRS inset")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument post:1", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ postarg1content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find Argument post:1 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ postarg1content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ # re-find inset end
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of DRS inset")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument post:2", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ postarg2content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find Argument post:2 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ postarg2content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ # re-find inset end
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of DRS inset")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument post:3", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ postarg3content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find Argument post:3 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ postarg3content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ # re-find inset end
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of DRS inset")
+ continue
+
+ arg = find_token(document.body, "\\begin_inset Argument post:4", i, j)
+ endarg = find_end_of_inset(document.body, arg)
+ postarg4content = []
+ if arg != -1:
+ argbeginPlain = find_token(document.body, "\\begin_layout Plain Layout", arg, endarg)
+ if argbeginPlain == -1:
+ document.warning("Malformed LyX document: Can't find Argument post:4 plain Layout")
+ continue
+ argendPlain = find_end_of_inset(document.body, argbeginPlain)
+ postarg4content = document.body[argbeginPlain + 1 : argendPlain - 2]
+
+ # remove Arg insets and paragraph, if it only contains this inset
+ if document.body[arg - 1] == "\\begin_layout Plain Layout" and find_end_of_layout(document.body, arg - 1) == endarg + 3:
+ del document.body[arg - 1 : endarg + 4]
+ else:
+ del document.body[arg : endarg + 1]
+
+ # The respective LaTeX command
+ cmd = "\\drs"
+ if drs == "\\begin_inset Flex DRS*":
+ cmd = "\\drs*"
+ elif drs == "\\begin_inset Flex IfThen-DRS":
+ cmd = "\\ifdrs"
+ elif drs == "\\begin_inset Flex Cond-DRS":
+ cmd = "\\condrs"
+ elif drs == "\\begin_inset Flex QDRS":
+ cmd = "\\qdrs"
+ elif drs == "\\begin_inset Flex NegDRS":
+ cmd = "\\negdrs"
+ elif drs == "\\begin_inset Flex SDRS":
+ cmd = "\\sdrs"
+
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
+ endInset = find_end_of_inset(document.body, i)
+ endPlain = find_token_backwards(document.body, "\\end_layout", endInset)
+ precontent = put_cmd_in_ert(cmd)
+ precontent += put_cmd_in_ert("{") + prearg1content + put_cmd_in_ert("}")
+ if drs == "\\begin_inset Flex SDRS":
+ precontent += put_cmd_in_ert("{") + prearg2content + put_cmd_in_ert("}")
+ precontent += put_cmd_in_ert("{")
+
+ postcontent = []
+ if cmd == "\\qdrs" or cmd == "\\condrs" or cmd == "\\ifdrs":
+ postcontent = put_cmd_in_ert("}{") + postarg1content + put_cmd_in_ert("}{") + postarg2content + put_cmd_in_ert("}")
+ if cmd == "\\condrs" or cmd == "\\qdrs":
+ postcontent += put_cmd_in_ert("{") + postarg3content + put_cmd_in_ert("}")
+ if cmd == "\\qdrs":
+ postcontent += put_cmd_in_ert("{") + postarg4content + put_cmd_in_ert("}")
+ else:
+ postcontent = put_cmd_in_ert("}")
+
+ document.body[endPlain:endInset + 1] = postcontent
+ document.body[beginPlain + 1:beginPlain] = precontent
+ del document.body[i : beginPlain + 1]
+ if not cov_req:
+ document.append_local_layout("Provides covington 1")
+ add_to_preamble(document, ["\\usepackage{drs,covington}"])
+ cov_req = True
+ i = beginPlain
+
+
+
+def revert_babelfont(document):
+ " Reverts the use of \\babelfont to user preamble "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if not str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+ i = find_token(document.header, '\\language_package', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\language_package.")
+ return
+ if get_value(document.header, "\\language_package", 0) != "babel":
+ return
+
+ # check font settings
+ # defaults
+ roman = sans = typew = "default"
+ osf = False
+ sf_scale = tt_scale = 100.0
+
+ j = find_token(document.header, "\\font_roman", 0)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing \\font_roman.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[j])
+ roman = romanfont[2].strip('"')
+ romanfont[2] = '"default"'
+ document.header[j] = " ".join(romanfont)
+
+ j = find_token(document.header, "\\font_sans", 0)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing \\font_sans.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ sansfont = re.findall(r'[^"\s]\S*|".+?"', document.header[j])
+ sans = sansfont[2].strip('"')
+ sansfont[2] = '"default"'
+ document.header[j] = " ".join(sansfont)
+
+ j = find_token(document.header, "\\font_typewriter", 0)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing \\font_typewriter.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ ttfont = re.findall(r'[^"\s]\S*|".+?"', document.header[j])
+ typew = ttfont[2].strip('"')
+ ttfont[2] = '"default"'
+ document.header[j] = " ".join(ttfont)
+
+ i = find_token(document.header, "\\font_osf", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_osf.")
+ else:
+ osf = str2bool(get_value(document.header, "\\font_osf", i))
+
+ j = find_token(document.header, "\\font_sf_scale", 0)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing \\font_sf_scale.")
+ else:
+ sfscale = document.header[j].split()
+ val = sfscale[2]
+ sfscale[2] = "100"
+ document.header[j] = " ".join(sfscale)
+ try:
+ # float() can throw
+ sf_scale = float(val)
+ except:
+ document.warning("Invalid font_sf_scale value: " + val)
+
+ j = find_token(document.header, "\\font_tt_scale", 0)
+ if j == -1:
+ document.warning("Malformed LyX document: Missing \\font_tt_scale.")
+ else:
+ ttscale = document.header[j].split()
+ val = ttscale[2]
+ ttscale[2] = "100"
+ document.header[j] = " ".join(ttscale)
+ try:
+ # float() can throw
+ tt_scale = float(val)
+ except:
+ document.warning("Invalid font_tt_scale value: " + val)
+
+ # set preamble stuff
+ pretext = ['%% This document must be processed with xelatex or lualatex!']
+ pretext.append('\\AtBeginDocument{%')
+ if roman != "default":
+ pretext.append('\\babelfont{rm}[Mapping=tex-text]{' + roman + '}')
+ if sans != "default":
+ sf = '\\babelfont{sf}['
+ if sf_scale != 100.0:
+ sf += 'Scale=' + str(sf_scale / 100.0) + ','
+ sf += 'Mapping=tex-text]{' + sans + '}'
+ pretext.append(sf)
+ if typew != "default":
+ tw = '\\babelfont{tt}'
+ if tt_scale != 100.0:
+ tw += '[Scale=' + str(tt_scale / 100.0) + ']'
+ tw += '{' + typew + '}'
+ pretext.append(tw)
+ if osf:
+ pretext.append('\\defaultfontfeatures{Numbers=OldStyle}')
+ pretext.append('}')
+ insert_to_preamble(document, pretext)
+
+
+def revert_minionpro(document):
+ " Revert native MinionPro font definition (with extra options) to LaTeX "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+
+ regexp = re.compile(r'(\\font_roman_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x == -1:
+ return
+
+ # We need to use this regex since split() does not handle quote protection
+ romanopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ opts = romanopts[1].strip('"')
+
+ i = find_token(document.header, "\\font_roman", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_roman.")
+ return
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ roman = romanfont[1].strip('"')
+ if roman != "minionpro":
+ return
+ romanfont[1] = '"default"'
+ document.header[i] = " ".join(romanfont)
+ osf = False
+ j = find_token(document.header, "\\font_osf true", 0)
+ if j != -1:
+ osf = True
+ preamble = "\\usepackage["
+ if osf:
+ document.header[j] = "\\font_osf false"
+ else:
+ preamble += "lf,"
+ preamble += opts
+ preamble += "]{MinionPro}"
+ add_to_preamble(document, [preamble])
+ del document.header[x]
+
+
+def revert_font_opts(document):
+ " revert font options by outputting \\setxxxfont or \\babelfont to the preamble "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ NonTeXFonts = str2bool(get_value(document.header, "\\use_non_tex_fonts", i))
+ i = find_token(document.header, '\\language_package', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\language_package.")
+ return
+ Babel = (get_value(document.header, "\\language_package", 0) == "babel")
+
+ # 1. Roman
+ regexp = re.compile(r'(\\font_roman_opts)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ # We need to use this regex since split() does not handle quote protection
+ romanopts = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ opts = romanopts[1].strip('"')
+ del document.header[i]
+ if NonTeXFonts:
+ regexp = re.compile(r'(\\font_roman)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ font = romanfont[2].strip('"')
+ romanfont[2] = '"default"'
+ document.header[i] = " ".join(romanfont)
+ if font != "default":
+ if Babel:
+ preamble = "\\babelfont{rm}["
+ else:
+ preamble = "\\setmainfont["
+ preamble += opts
+ preamble += ","
+ preamble += "Mapping=tex-text]{"
+ preamble += font
+ preamble += "}"
+ add_to_preamble(document, [preamble])
+
+ # 2. Sans
+ regexp = re.compile(r'(\\font_sans_opts)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ scaleval = 100
+ # We need to use this regex since split() does not handle quote protection
+ sfopts = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ opts = sfopts[1].strip('"')
+ del document.header[i]
+ if NonTeXFonts:
+ regexp = re.compile(r'(\\font_sf_scale)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ scaleval = get_value(document.header, "\\font_sf_scale" , i).split()[1]
+ regexp = re.compile(r'(\\font_sans)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ # We need to use this regex since split() does not handle quote protection
+ sffont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ font = sffont[2].strip('"')
+ sffont[2] = '"default"'
+ document.header[i] = " ".join(sffont)
+ if font != "default":
+ if Babel:
+ preamble = "\\babelfont{sf}["
+ else:
+ preamble = "\\setsansfont["
+ preamble += opts
+ preamble += ","
+ if scaleval != 100:
+ preamble += "Scale=0."
+ preamble += scaleval
+ preamble += ","
+ preamble += "Mapping=tex-text]{"
+ preamble += font
+ preamble += "}"
+ add_to_preamble(document, [preamble])
+
+ # 3. Typewriter
+ regexp = re.compile(r'(\\font_typewriter_opts)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ scaleval = 100
+ # We need to use this regex since split() does not handle quote protection
+ ttopts = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ opts = ttopts[1].strip('"')
+ del document.header[i]
+ if NonTeXFonts:
+ regexp = re.compile(r'(\\font_tt_scale)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ scaleval = get_value(document.header, "\\font_tt_scale" , i).split()[1]
+ regexp = re.compile(r'(\\font_typewriter)')
+ i = find_re(document.header, regexp, 0)
+ if i != -1:
+ # We need to use this regex since split() does not handle quote protection
+ ttfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ font = ttfont[2].strip('"')
+ ttfont[2] = '"default"'
+ document.header[i] = " ".join(ttfont)
+ if font != "default":
+ if Babel:
+ preamble = "\\babelfont{tt}["
+ else:
+ preamble = "\\setmonofont["
+ preamble += opts
+ preamble += ","
+ if scaleval != 100:
+ preamble += "Scale=0."
+ preamble += scaleval
+ preamble += ","
+ preamble += "Mapping=tex-text]{"
+ preamble += font
+ preamble += "}"
+ add_to_preamble(document, [preamble])
+
+
+def revert_plainNotoFonts_xopts(document):
+ " Revert native (straight) Noto font definition (with extra options) to LaTeX "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+
+ osf = False
+ y = find_token(document.header, "\\font_osf true", 0)
+ if y != -1:
+ osf = True
+
+ regexp = re.compile(r'(\\font_roman_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x == -1 and not osf:
+ return
+
+ opts = ""
+ if x != -1:
+ # We need to use this regex since split() does not handle quote protection
+ romanopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ opts = romanopts[1].strip('"')
+ if osf:
+ if opts != "":
+ opts += ", "
+ opts += "osf"
+
+ i = find_token(document.header, "\\font_roman", 0)
+ if i == -1:
+ return
+
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ roman = romanfont[1].strip('"')
+ if roman != "NotoSerif-TLF":
+ return
+
+ j = find_token(document.header, "\\font_sans", 0)
+ if j == -1:
+ return
+
+ # We need to use this regex since split() does not handle quote protection
+ sffont = re.findall(r'[^"\s]\S*|".+?"', document.header[j])
+ sf = sffont[1].strip('"')
+ if sf != "default":
+ return
+
+ j = find_token(document.header, "\\font_typewriter", 0)
+ if j == -1:
+ return
+
+ # We need to use this regex since split() does not handle quote protection
+ ttfont = re.findall(r'[^"\s]\S*|".+?"', document.header[j])
+ tt = ttfont[1].strip('"')
+ if tt != "default":
+ return
+
+ # So we have noto as "complete font"
+ romanfont[1] = '"default"'
+ document.header[i] = " ".join(romanfont)
+
+ preamble = "\\usepackage["
+ preamble += opts
+ preamble += "]{noto}"
+ add_to_preamble(document, [preamble])
+ if osf:
+ document.header[y] = "\\font_osf false"
+ if x != -1:
+ del document.header[x]
+
+
+def revert_notoFonts_xopts(document):
+ " Revert native (extended) Noto font definition (with extra options) to LaTeX "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+
+ fontmap = dict()
+ fm = createFontMapping(['Noto'])
+ if revert_fonts(document, fm, fontmap, True):
+ add_preamble_fonts(document, fontmap)
+
+
+def revert_IBMFonts_xopts(document):
+ " Revert native IBM font definition (with extra options) to LaTeX "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+
+ fontmap = dict()
+ fm = createFontMapping(['IBM'])
+ ft = ""
+ if revert_fonts(document, fm, fontmap, True):
+ add_preamble_fonts(document, fontmap)
+
+
+def revert_AdobeFonts_xopts(document):
+ " Revert native Adobe font definition (with extra options) to LaTeX "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+
+ fontmap = dict()
+ fm = createFontMapping(['Adobe'])
+ ft = ""
+ if revert_fonts(document, fm, fontmap, True):
+ add_preamble_fonts(document, fontmap)
+
+
+def convert_osf(document):
+ " Convert \\font_osf param to new format "
+
+ NonTeXFonts = False
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ else:
+ NonTeXFonts = str2bool(get_value(document.header, "\\use_non_tex_fonts", i))
+
+ i = find_token(document.header, '\\font_osf', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_osf.")
+ return
+
+ osfsf = ["biolinum", "ADOBESourceSansPro", "NotoSansRegular", "NotoSansMedium", "NotoSansThin", "NotoSansLight", "NotoSansExtralight" ]
+ osftt = ["ADOBESourceCodePro", "NotoMonoRegular" ]
+
+ osfval = str2bool(get_value(document.header, "\\font_osf", i))
+ document.header[i] = document.header[i].replace("\\font_osf", "\\font_roman_osf")
+
+ if NonTeXFonts:
+ document.header.insert(i, "\\font_sans_osf false")
+ document.header.insert(i + 1, "\\font_typewriter_osf false")
+ return
+
+ if osfval:
+ x = find_token(document.header, "\\font_sans", 0)
+ if x == -1:
+ document.warning("Malformed LyX document: Missing \\font_sans.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ sffont = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ sf = sffont[1].strip('"')
+ if sf in osfsf:
+ document.header.insert(i, "\\font_sans_osf true")
+ else:
+ document.header.insert(i, "\\font_sans_osf false")
+
+ x = find_token(document.header, "\\font_typewriter", 0)
+ if x == -1:
+ document.warning("Malformed LyX document: Missing \\font_typewriter.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ ttfont = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ tt = ttfont[1].strip('"')
+ if tt in osftt:
+ document.header.insert(i + 1, "\\font_typewriter_osf true")
+ else:
+ document.header.insert(i + 1, "\\font_typewriter_osf false")
+
+ else:
+ document.header.insert(i, "\\font_sans_osf false")
+ document.header.insert(i + 1, "\\font_typewriter_osf false")
+
+
+def revert_osf(document):
+ " Revert \\font_*_osf params "
+
+ NonTeXFonts = False
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ else:
+ NonTeXFonts = str2bool(get_value(document.header, "\\use_non_tex_fonts", i))
+
+ i = find_token(document.header, '\\font_roman_osf', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_roman_osf.")
+ return
+
+ osfval = str2bool(get_value(document.header, "\\font_roman_osf", i))
+ document.header[i] = document.header[i].replace("\\font_roman_osf", "\\font_osf")
+
+ i = find_token(document.header, '\\font_sans_osf', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_sans_osf.")
+ return
+
+ osfval = str2bool(get_value(document.header, "\\font_sans_osf", i))
+ del document.header[i]
+
+ i = find_token(document.header, '\\font_typewriter_osf', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_typewriter_osf.")
+ return
+
+ osfval |= str2bool(get_value(document.header, "\\font_typewriter_osf", i))
+ del document.header[i]
+
+ if osfval:
+ i = find_token(document.header, '\\font_osf', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_osf.")
+ return
+ document.header[i] = "\\font_osf true"
+
+
+def revert_texfontopts(document):
+ " Revert native TeX font definitions (with extra options) to LaTeX "
+
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ return
+ if str2bool(get_value(document.header, "\\use_non_tex_fonts", i)):
+ return
+
+ rmfonts = ["ccfonts", "cochineal", "utopia", "garamondx", "libertine", "lmodern", "palatino", "times", "xcharter" ]
+
+ # First the sf (biolinum only)
+ regexp = re.compile(r'(\\font_sans_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x != -1:
+ # We need to use this regex since split() does not handle quote protection
+ sfopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ opts = sfopts[1].strip('"')
+ i = find_token(document.header, "\\font_sans", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_sans.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ sffont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ sans = sffont[1].strip('"')
+ if sans == "biolinum":
+ sf_scale = 100.0
+ sffont[1] = '"default"'
+ document.header[i] = " ".join(sffont)
+ osf = False
+ j = find_token(document.header, "\\font_sans_osf true", 0)
+ if j != -1:
+ osf = True
+ k = find_token(document.header, "\\font_sf_scale", 0)
+ if k == -1:
+ document.warning("Malformed LyX document: Missing \\font_sf_scale.")
+ else:
+ sfscale = document.header[k].split()
+ val = sfscale[1]
+ sfscale[1] = "100"
+ document.header[k] = " ".join(sfscale)
+ try:
+ # float() can throw
+ sf_scale = float(val)
+ except:
+ document.warning("Invalid font_sf_scale value: " + val)
+ preamble = "\\usepackage["
+ if osf:
+ document.header[j] = "\\font_sans_osf false"
+ preamble += "osf,"
+ if sf_scale != 100.0:
+ preamble += 'scaled=' + str(sf_scale / 100.0) + ','
+ preamble += opts
+ preamble += "]{biolinum}"
+ add_to_preamble(document, [preamble])
+ del document.header[x]
+
+ regexp = re.compile(r'(\\font_roman_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x == -1:
+ return
+
+ # We need to use this regex since split() does not handle quote protection
+ romanopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ opts = romanopts[1].strip('"')
+
+ i = find_token(document.header, "\\font_roman", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_roman.")
+ return
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ roman = romanfont[1].strip('"')
+ if not roman in rmfonts:
+ return
+ romanfont[1] = '"default"'
+ document.header[i] = " ".join(romanfont)
+ package = roman
+ if roman == "utopia":
+ package = "fourier"
+ elif roman == "palatino":
+ package = "mathpazo"
+ elif roman == "times":
+ package = "mathptmx"
+ elif roman == "xcharter":
+ package = "XCharter"
+ osf = ""
+ j = find_token(document.header, "\\font_roman_osf true", 0)
+ if j != -1:
+ if roman == "cochineal":
+ osf = "proportional,osf,"
+ elif roman == "utopia":
+ osf = "oldstyle,"
+ elif roman == "garamondx":
+ osf = "osfI,"
+ elif roman == "libertine":
+ osf = "osf,"
+ elif roman == "palatino":
+ osf = "osf,"
+ elif roman == "xcharter":
+ osf = "osf,"
+ document.header[j] = "\\font_roman_osf false"
+ k = find_token(document.header, "\\font_sc true", 0)
+ if k != -1:
+ if roman == "utopia":
+ osf += "expert,"
+ if roman == "palatino" and osf == "":
+ osf = "sc,"
+ document.header[k] = "\\font_sc false"
+ preamble = "\\usepackage["
+ preamble += osf
+ preamble += opts
+ preamble += "]{" + package + "}"
+ add_to_preamble(document, [preamble])
+ del document.header[x]
+
+
+def convert_CantarellFont(document):
+ " Handle Cantarell font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fm = createFontMapping(['Cantarell'])
+ convert_fonts(document, fm, "oldstyle")
+
+def revert_CantarellFont(document):
+ " Revert native Cantarell font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fontmap = dict()
+ fm = createFontMapping(['Cantarell'])
+ if revert_fonts(document, fm, fontmap, False, True):
+ add_preamble_fonts(document, fontmap)
+
+def convert_ChivoFont(document):
+ " Handle Chivo font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fm = createFontMapping(['Chivo'])
+ convert_fonts(document, fm, "oldstyle")
+
+def revert_ChivoFont(document):
+ " Revert native Chivo font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fontmap = dict()
+ fm = createFontMapping(['Chivo'])
+ if revert_fonts(document, fm, fontmap, False, True):
+ add_preamble_fonts(document, fontmap)
+
+
+def convert_FiraFont(document):
+ " Handle Fira font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fm = createFontMapping(['Fira'])
+ convert_fonts(document, fm, "lf")
+
+def revert_FiraFont(document):
+ " Revert native Fira font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fontmap = dict()
+ fm = createFontMapping(['Fira'])
+ if revert_fonts(document, fm, fontmap, False, True):
+ add_preamble_fonts(document, fontmap)
+
+
+def convert_Semibolds(document):
+ " Move semibold options to extraopts "
+
+ NonTeXFonts = False
+ i = find_token(document.header, '\\use_non_tex_fonts', 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\use_non_tex_fonts.")
+ else:
+ NonTeXFonts = str2bool(get_value(document.header, "\\use_non_tex_fonts", i))
+
+ i = find_token(document.header, "\\font_roman", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_roman.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ roman = romanfont[1].strip('"')
+ if roman == "IBMPlexSerifSemibold":
+ romanfont[1] = '"IBMPlexSerif"'
+ document.header[i] = " ".join(romanfont)
+
+ if NonTeXFonts == False:
+ regexp = re.compile(r'(\\font_roman_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x == -1:
+ # Sensible place to insert tag
+ fo = find_token(document.header, "\\font_sf_scale")
+ if fo == -1:
+ document.warning("Malformed LyX document! Missing \\font_sf_scale")
+ else:
+ document.header.insert(fo, "\\font_roman_opts \"semibold\"")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ romanopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ document.header[x] = "\\font_roman_opts \"semibold, " + romanopts[1].strip('"') + "\""
+
+ i = find_token(document.header, "\\font_sans", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_sans.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ sffont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ sf = sffont[1].strip('"')
+ if sf == "IBMPlexSansSemibold":
+ sffont[1] = '"IBMPlexSans"'
+ document.header[i] = " ".join(sffont)
+
+ if NonTeXFonts == False:
+ regexp = re.compile(r'(\\font_sans_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x == -1:
+ # Sensible place to insert tag
+ fo = find_token(document.header, "\\font_sf_scale")
+ if fo == -1:
+ document.warning("Malformed LyX document! Missing \\font_sf_scale")
+ else:
+ document.header.insert(fo, "\\font_sans_opts \"semibold\"")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ sfopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ document.header[x] = "\\font_sans_opts \"semibold, " + sfopts[1].strip('"') + "\""
+
+ i = find_token(document.header, "\\font_typewriter", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_typewriter.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ ttfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ tt = ttfont[1].strip('"')
+ if tt == "IBMPlexMonoSemibold":
+ ttfont[1] = '"IBMPlexMono"'
+ document.header[i] = " ".join(ttfont)
+
+ if NonTeXFonts == False:
+ regexp = re.compile(r'(\\font_typewriter_opts)')
+ x = find_re(document.header, regexp, 0)
+ if x == -1:
+ # Sensible place to insert tag
+ fo = find_token(document.header, "\\font_tt_scale")
+ if fo == -1:
+ document.warning("Malformed LyX document! Missing \\font_tt_scale")
+ else:
+ document.header.insert(fo, "\\font_typewriter_opts \"semibold\"")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ ttopts = re.findall(r'[^"\s]\S*|".+?"', document.header[x])
+ document.header[x] = "\\font_typewriter_opts \"semibold, " + sfopts[1].strip('"') + "\""
+
+
+def convert_NotoRegulars(document):
+ " Merge diverse noto reagular fonts "
+
+ i = find_token(document.header, "\\font_roman", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_roman.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ romanfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ roman = romanfont[1].strip('"')
+ if roman == "NotoSerif-TLF":
+ romanfont[1] = '"NotoSerifRegular"'
+ document.header[i] = " ".join(romanfont)
+
+ i = find_token(document.header, "\\font_sans", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_sans.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ sffont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ sf = sffont[1].strip('"')
+ if sf == "NotoSans-TLF":
+ sffont[1] = '"NotoSansRegular"'
+ document.header[i] = " ".join(sffont)
+
+ i = find_token(document.header, "\\font_typewriter", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing \\font_typewriter.")
+ else:
+ # We need to use this regex since split() does not handle quote protection
+ ttfont = re.findall(r'[^"\s]\S*|".+?"', document.header[i])
+ tt = ttfont[1].strip('"')
+ if tt == "NotoMono-TLF":
+ ttfont[1] = '"NotoMonoRegular"'
+ document.header[i] = " ".join(ttfont)
+
+
+def convert_CrimsonProFont(document):
+ " Handle CrimsonPro font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fm = createFontMapping(['CrimsonPro'])
+ convert_fonts(document, fm, "lf")
+
+def revert_CrimsonProFont(document):
+ " Revert native CrimsonPro font definition to LaTeX "
+
+ if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+ fontmap = dict()
+ fm = createFontMapping(['CrimsonPro'])
+ if revert_fonts(document, fm, fontmap, False, True):
+ add_preamble_fonts(document, fontmap)
+
+
+def revert_pagesizes(document):
+ " Revert new page sizes in memoir and KOMA to options "
+
+ if document.textclass != "memoir" and document.textclass[:2] != "scr":
+ return
+
+ i = find_token(document.header, "\\use_geometry true", 0)
+ if i != -1:
+ return
+
+ defsizes = ["default", "custom", "letterpaper", "legalpaper", "executivepaper", "a4paper", "a5paper", "b5paper"]
+
+ i = find_token(document.header, "\\papersize", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\papersize header.")
+ return
+ val = get_value(document.header, "\\papersize", i)
+ if val in defsizes:
+ # nothing to do
+ return
+
+ document.header[i] = "\\papersize default"
+
+ i = find_token(document.header, "\\options", 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, "\\options " + val)
+ return
+ document.header[i] = document.header[i] + "," + val
+
+
+def convert_pagesizes(document):
+ " Convert to new page sizes in memoir and KOMA to options "
+
+ if document.textclass != "memoir" and document.textclass[:3] != "scr":
+ return
+
+ i = find_token(document.header, "\\use_geometry true", 0)
+ if i != -1:
+ return
+
+ defsizes = ["default", "custom", "letterpaper", "legalpaper", "executivepaper", "a4paper", "a5paper", "b5paper"]
+
+ i = find_token(document.header, "\\papersize", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\papersize header.")
+ return
+ val = get_value(document.header, "\\papersize", i)
+ if val in defsizes:
+ # nothing to do
+ return
+
+ i = find_token(document.header, "\\use_geometry false", 0)
+ if i != -1:
+ # Maintain use of geometry
+ document.header[1] = "\\use_geometry true"
+
+def revert_komafontsizes(document):
+ " Revert new font sizes in KOMA to options "
+
+ if document.textclass[:3] != "scr":
+ return
+
+ i = find_token(document.header, "\\paperfontsize", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\paperfontsize header.")
+ return
+
+ defsizes = ["default", "10", "11", "12"]
+
+ val = get_value(document.header, "\\paperfontsize", i)
+ if val in defsizes:
+ # nothing to do
+ return
+
+ document.header[i] = "\\paperfontsize default"
+
+ fsize = "fontsize=" + val
+
+ i = find_token(document.header, "\\options", 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, "\\options " + fsize)
+ return
+ document.header[i] = document.header[i] + "," + fsize
+
+
+def revert_dupqualicites(document):
+ " Revert qualified citation list commands with duplicate keys to ERT "
+
+ # LyX 2.3 only supports qualified citation lists with unique keys. Thus,
+ # we need to revert those with multiple uses of the same key.
+
+ # Get cite engine
+ engine = "basic"
+ i = find_token(document.header, "\\cite_engine", 0)
+ if i == -1:
+ document.warning("Malformed document! Missing \\cite_engine")
+ else:
+ engine = get_value(document.header, "\\cite_engine", i)
+
+ if not engine in ["biblatex", "biblatex-natbib"]:
+ return
+
+ # Citation insets that support qualified lists, with their LaTeX code
+ ql_citations = {
+ "cite" : "cites",
+ "Cite" : "Cites",
+ "citet" : "textcites",
+ "Citet" : "Textcites",
+ "citep" : "parencites",
+ "Citep" : "Parencites",
+ "Footcite" : "Smartcites",
+ "footcite" : "smartcites",
+ "Autocite" : "Autocites",
+ "autocite" : "autocites",
+ }
+
+ i = 0
+ while (True):
+ i = find_token(document.body, "\\begin_inset CommandInset citation", i)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Can't find end of citation inset at line %d!!" %(i))
+ i += 1
+ continue
+
+ k = find_token(document.body, "LatexCommand", i, j)
+ if k == -1:
+ document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
+ i = j + 1
+ continue
+
+ cmd = get_value(document.body, "LatexCommand", k)
+ if not cmd in list(ql_citations.keys()):
+ i = j + 1
+ continue
+
+ pres = find_token(document.body, "pretextlist", i, j)
+ posts = find_token(document.body, "posttextlist", i, j)
+ if pres == -1 and posts == -1:
+ # nothing to do.
+ i = j + 1
+ continue
+
+ key = get_quoted_value(document.body, "key", i, j)
+ if not key:
+ document.warning("Citation inset at line %d does not have a key!" %(i))
+ i = j + 1
+ continue
+
+ keys = key.split(",")
+ ukeys = list(set(keys))
+ if len(keys) == len(ukeys):
+ # no duplicates.
+ i = j + 1
+ continue
+
+ pretexts = get_quoted_value(document.body, "pretextlist", pres)
+ posttexts = get_quoted_value(document.body, "posttextlist", posts)
+
+ pre = get_quoted_value(document.body, "before", i, j)
+ post = get_quoted_value(document.body, "after", i, j)
+ prelist = pretexts.split("\t")
+ premap = dict()
+ for pp in prelist:
+ ppp = pp.split(" ", 1)
+ val = ""
+ if len(ppp) > 1:
+ val = ppp[1]
+ else:
+ val = ""
+ if ppp[0] in premap:
+ premap[ppp[0]] = premap[ppp[0]] + "\t" + val
+ else:
+ premap[ppp[0]] = val
+ postlist = posttexts.split("\t")
+ postmap = dict()
+ num = 1
+ for pp in postlist:
+ ppp = pp.split(" ", 1)
+ val = ""
+ if len(ppp) > 1:
+ val = ppp[1]
+ else:
+ val = ""
+ if ppp[0] in postmap:
+ postmap[ppp[0]] = postmap[ppp[0]] + "\t" + val
+ else:
+ postmap[ppp[0]] = val
+ # Replace known new commands with ERT
+ if "(" in pre or ")" in pre:
+ pre = "{" + pre + "}"
+ if "(" in post or ")" in post:
+ post = "{" + post + "}"
+ res = "\\" + ql_citations[cmd]
+ if pre:
+ res += "(" + pre + ")"
+ if post:
+ res += "(" + post + ")"
+ elif pre:
+ res += "()"
+ for kk in keys:
+ if premap.get(kk, "") != "":
+ akeys = premap[kk].split("\t", 1)
+ akey = akeys[0]
+ if akey != "":
+ res += "[" + akey + "]"
+ if len(akeys) > 1:
+ premap[kk] = "\t".join(akeys[1:])
+ else:
+ premap[kk] = ""
+ if postmap.get(kk, "") != "":
+ akeys = postmap[kk].split("\t", 1)
+ akey = akeys[0]
+ if akey != "":
+ res += "[" + akey + "]"
+ if len(akeys) > 1:
+ postmap[kk] = "\t".join(akeys[1:])
+ else:
+ postmap[kk] = ""
+ elif premap.get(kk, "") != "":
+ res += "[]"
+ res += "{" + kk + "}"
+ document.body[i:j+1] = put_cmd_in_ert([res])
+
+
+def convert_pagesizenames(document):
+ " Convert LyX page sizes names "
+
+ i = find_token(document.header, "\\papersize", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\papersize header.")
+ return
+ oldnames = ["letterpaper", "legalpaper", "executivepaper", \
+ "a0paper", "a1paper", "a2paper", "a3paper", "a4paper", "a5paper", "a6paper", \
+ "b0paper", "b1paper", "b2paper", "b3paper", "b4paper", "b5paper", "b6paper", \
+ "c0paper", "c1paper", "c2paper", "c3paper", "c4paper", "c5paper", "c6paper"]
+ val = get_value(document.header, "\\papersize", i)
+ if val in oldnames:
+ newval = val.replace("paper", "")
+ document.header[i] = "\\papersize " + newval
+
+def revert_pagesizenames(document):
+ " Convert LyX page sizes names "
+
+ i = find_token(document.header, "\\papersize", 0)
+ if i == -1:
+ document.warning("Malformed LyX document! Missing \\papersize header.")
+ return
+ newnames = ["letter", "legal", "executive", \
+ "a0", "a1", "a2", "a3", "a4", "a5", "a6", \
+ "b0", "b1", "b2", "b3", "b4", "b5", "b6", \
+ "c0", "c1", "c2", "c3", "c4", "c5", "c6"]
+ val = get_value(document.header, "\\papersize", i)
+ if val in newnames:
+ newval = val + "paper"
+ document.header[i] = "\\papersize " + newval
+
+
+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 != "":
+ document.body.insert(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
+