Refactor lyx2lyx code.

Use helper functions for shorter, self-documenting code.

Drop optional arguments that equal the default value.

Remove hard TABs and trailing whitespace.
This commit is contained in:
Günter Milde 2020-03-03 23:44:08 +01:00
parent 0b5ce4b5ba
commit 91bdcbbb82
2 changed files with 51 additions and 105 deletions

View File

@ -73,10 +73,10 @@ def main():
parser.add_argument("-n", "--try-hard", action="store_true", parser.add_argument("-n", "--try-hard", action="store_true",
help = "Try hard (ignore any conversion errors).") help = "Try hard (ignore any conversion errors).")
parser.add_argument("-s", "--systemlyxdir", type=cmd_arg, dest= "systemlyxdir", parser.add_argument("-s", "--systemlyxdir", type=cmd_arg, dest= "systemlyxdir",
help= "LyX system directory for conversion from" help= "LyX system directory for conversion from"
" version 489 or older.") " version 489 or older.")
parser.add_argument('--version', action='version', version="""lyx2lyx, version %s parser.add_argument('--version', action='version', version="""lyx2lyx, version %s
Copyright (C) 2011 The LyX Team, José Matos and Dekel Tsur""" % LyX.version__) Copyright (C) 2011 The LyX Team, José Matos and Dekel Tsur""" % LyX.version__)
parser.add_argument("input", nargs='?', type=cmd_arg, default=None) parser.add_argument("input", nargs='?', type=cmd_arg, default=None)
options = parser.parse_args() options = parser.parse_args()

View File

@ -206,7 +206,7 @@ def convert_fonts(document, fm, osfoption = "osf"):
haveFontOpts = document.end_format > 580 haveFontOpts = document.end_format > 580
i = 0 i = 0
while i < len(document.preamble): while True:
i = find_re(document.preamble, rpkg, i+1) i = find_re(document.preamble, rpkg, i+1)
if i == -1: if i == -1:
return return
@ -408,14 +408,14 @@ def revert_inputencoding_namechange(document):
def convert_notoFonts(document): def convert_notoFonts(document):
" Handle Noto fonts definition to LaTeX " " Handle Noto fonts definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['Noto']) fm = createFontMapping(['Noto'])
convert_fonts(document, fm) convert_fonts(document, fm)
def revert_notoFonts(document): def revert_notoFonts(document):
" Revert native Noto font definition to LaTeX " " Revert native Noto font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['Noto']) fm = createFontMapping(['Noto'])
if revert_fonts(document, fm, fontmap): if revert_fonts(document, fm, fontmap):
@ -424,14 +424,14 @@ def revert_notoFonts(document):
def convert_latexFonts(document): def convert_latexFonts(document):
" Handle DejaVu and IBMPlex fonts definition to LaTeX " " Handle DejaVu and IBMPlex fonts definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['DejaVu', 'IBM']) fm = createFontMapping(['DejaVu', 'IBM'])
convert_fonts(document, fm) convert_fonts(document, fm)
def revert_latexFonts(document): def revert_latexFonts(document):
" Revert native DejaVu font definition to LaTeX " " Revert native DejaVu font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['DejaVu', 'IBM']) fm = createFontMapping(['DejaVu', 'IBM'])
if revert_fonts(document, fm, fontmap): if revert_fonts(document, fm, fontmap):
@ -440,14 +440,14 @@ def revert_latexFonts(document):
def convert_AdobeFonts(document): def convert_AdobeFonts(document):
" Handle Adobe Source fonts definition to LaTeX " " Handle Adobe Source fonts definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['Adobe']) fm = createFontMapping(['Adobe'])
convert_fonts(document, fm) convert_fonts(document, fm)
def revert_AdobeFonts(document): def revert_AdobeFonts(document):
" Revert Adobe Source font definition to LaTeX " " Revert Adobe Source font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['Adobe']) fm = createFontMapping(['Adobe'])
if revert_fonts(document, fm, fontmap): if revert_fonts(document, fm, fontmap):
@ -556,7 +556,7 @@ def revert_lst_literalparam(document):
def revert_paratype(document): def revert_paratype(document):
" Revert ParaType font definitions to LaTeX " " Revert ParaType font definitions to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
preamble = "" preamble = ""
i1 = find_token(document.header, "\\font_roman \"PTSerif-TLF\"", 0) i1 = find_token(document.header, "\\font_roman \"PTSerif-TLF\"", 0)
i2 = find_token(document.header, "\\font_sans \"default\"", 0) i2 = find_token(document.header, "\\font_sans \"default\"", 0)
@ -1821,22 +1821,20 @@ def convert_aaencoding(document):
if document.textclass != "aa": if document.textclass != "aa":
return return
i = 0 i = find_token(document.header, "\\use_default_options true")
i = find_token(document.header, "\\use_default_options true", i)
if i == -1: if i == -1:
return return
j = find_token(document.header, "\\inputencoding", 0) val = get_value(document.header, "\\inputencoding")
if j == -1: if not val:
document.warning("Malformed LyX Document! Missing \\inputencoding header.") document.warning("Malformed LyX Document! Missing '\\inputencoding' header.")
return return
val = get_value(document.header, "\\inputencoding", j)
if val == "auto-legacy" or val == "latin9": if val == "auto-legacy" or val == "latin9":
document.header[i] = "\\use_default_options false" document.header[i] = "\\use_default_options false"
k = find_token(document.header, "\\options", 0) k = find_token(document.header, "\\options")
if k == -1: if k == -1:
document.header.insert(i, "\\options latin9") document.header.insert(i, "\\options latin9")
else: else:
document.header[k] = document.header[k] + ",latin9" document.header[k] += ",latin9"
def revert_aaencoding(document): def revert_aaencoding(document):
@ -1845,15 +1843,13 @@ def revert_aaencoding(document):
if document.textclass != "aa": if document.textclass != "aa":
return return
i = 0 i = find_token(document.header, "\\use_default_options true")
i = find_token(document.header, "\\use_default_options true", i)
if i == -1: if i == -1:
return return
j = find_token(document.header, "\\inputencoding", 0) val = get_value(document.header, "\\inputencoding")
if j == -1: if not val:
document.warning("Malformed LyX Document! Missing \\inputencoding header.") document.warning("Malformed LyX Document! Missing \\inputencoding header.")
return return
val = get_value(document.header, "\\inputencoding", j)
if val == "utf8": if val == "utf8":
document.header[i] = "\\use_default_options false" document.header[i] = "\\use_default_options false"
k = find_token(document.header, "\\options", 0) k = find_token(document.header, "\\options", 0)
@ -1874,9 +1870,10 @@ def revert_new_languages(document):
"oldrussian": ("", "russian"), "oldrussian": ("", "russian"),
"korean": ("", "korean"), "korean": ("", "korean"),
} }
used_languages = set()
if document.language in new_languages: if document.language in new_languages:
used_languages.add(document.language) used_languages = set((document.language, ))
else:
used_languages = set()
i = 0 i = 0
while True: while True:
i = find_token(document.body, "\\lang", i+1) i = find_token(document.body, "\\lang", i+1)
@ -1888,13 +1885,12 @@ def revert_new_languages(document):
# Korean is already supported via CJK, so leave as-is for Babel # Korean is already supported via CJK, so leave as-is for Babel
if ("korean" in used_languages if ("korean" in used_languages
and get_bool_value(document.header, "\\use_non_tex_fonts") and (not get_bool_value(document.header, "\\use_non_tex_fonts")
and get_value(document.header, "\\language_package") in ("default", "auto")): or get_value(document.header, "\\language_package") == "babel")):
revert_language(document, "korean", "", "korean") used_languages.discard("korean")
used_languages.discard("korean")
for lang in used_languages: for lang in used_languages:
revert_language(document, lang, new_languages[lang][0], new_languages[lang][1]) revert_language(document, lang, *new_languages[lang])
gloss_inset_def = [ gloss_inset_def = [
@ -2368,12 +2364,9 @@ def revert_drs(document):
def revert_babelfont(document): def revert_babelfont(document):
" Reverts the use of \\babelfont to user preamble " " Reverts the use of \\babelfont to user preamble "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if not get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
i = find_token(document.header, '\\language_package', 0) i = find_token(document.header, '\\language_package', 0)
if i == -1: if i == -1:
document.warning("Malformed LyX document: Missing \\language_package.") document.warning("Malformed LyX document: Missing \\language_package.")
@ -2477,11 +2470,7 @@ def revert_babelfont(document):
def revert_minionpro(document): def revert_minionpro(document):
" Revert native MinionPro font definition (with extra options) to LaTeX " " Revert native MinionPro font definition (with extra options) to LaTeX "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
regexp = re.compile(r'(\\font_roman_opts)') regexp = re.compile(r'(\\font_roman_opts)')
@ -2523,16 +2512,8 @@ def revert_minionpro(document):
def revert_font_opts(document): def revert_font_opts(document):
" revert font options by outputting \\setxxxfont or \\babelfont to the preamble " " revert font options by outputting \\setxxxfont or \\babelfont to the preamble "
i = find_token(document.header, '\\use_non_tex_fonts', 0) NonTeXFonts = get_bool_value(document.header, "\\use_non_tex_fonts")
if i == -1: Babel = (get_value(document.header, "\\language_package") == "babel")
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 # 1. Roman
regexp = re.compile(r'(\\font_roman_opts)') regexp = re.compile(r'(\\font_roman_opts)')
@ -2643,11 +2624,7 @@ def revert_font_opts(document):
def revert_plainNotoFonts_xopts(document): def revert_plainNotoFonts_xopts(document):
" Revert native (straight) Noto font definition (with extra options) to LaTeX " " Revert native (straight) Noto font definition (with extra options) to LaTeX "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
osf = False osf = False
@ -2717,11 +2694,7 @@ def revert_plainNotoFonts_xopts(document):
def revert_notoFonts_xopts(document): def revert_notoFonts_xopts(document):
" Revert native (extended) Noto font definition (with extra options) to LaTeX " " Revert native (extended) Noto font definition (with extra options) to LaTeX "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
fontmap = dict() fontmap = dict()
@ -2733,11 +2706,7 @@ def revert_notoFonts_xopts(document):
def revert_IBMFonts_xopts(document): def revert_IBMFonts_xopts(document):
" Revert native IBM font definition (with extra options) to LaTeX " " Revert native IBM font definition (with extra options) to LaTeX "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
fontmap = dict() fontmap = dict()
@ -2750,11 +2719,7 @@ def revert_IBMFonts_xopts(document):
def revert_AdobeFonts_xopts(document): def revert_AdobeFonts_xopts(document):
" Revert native Adobe font definition (with extra options) to LaTeX " " Revert native Adobe font definition (with extra options) to LaTeX "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
fontmap = dict() fontmap = dict()
@ -2767,12 +2732,7 @@ def revert_AdobeFonts_xopts(document):
def convert_osf(document): def convert_osf(document):
" Convert \\font_osf param to new format " " Convert \\font_osf param to new format "
NonTeXFonts = False NonTeXFonts = get_bool_value(document.header, "\\use_non_tex_fonts")
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) i = find_token(document.header, '\\font_osf', 0)
if i == -1: if i == -1:
@ -2823,12 +2783,7 @@ def convert_osf(document):
def revert_osf(document): def revert_osf(document):
" Revert \\font_*_osf params " " Revert \\font_*_osf params "
NonTeXFonts = False NonTeXFonts = get_bool_value(document.header, "\\use_non_tex_fonts")
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) i = find_token(document.header, '\\font_roman_osf', 0)
if i == -1: if i == -1:
@ -2865,11 +2820,7 @@ def revert_osf(document):
def revert_texfontopts(document): def revert_texfontopts(document):
" Revert native TeX font definitions (with extra options) to LaTeX " " Revert native TeX font definitions (with extra options) to LaTeX "
i = find_token(document.header, '\\use_non_tex_fonts', 0) if get_bool_value(document.header, "\\use_non_tex_fonts"):
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 return
rmfonts = ["ccfonts", "cochineal", "utopia", "garamondx", "libertine", "lmodern", "palatino", "times", "xcharter" ] rmfonts = ["ccfonts", "cochineal", "utopia", "garamondx", "libertine", "lmodern", "palatino", "times", "xcharter" ]
@ -2984,14 +2935,14 @@ def revert_texfontopts(document):
def convert_CantarellFont(document): def convert_CantarellFont(document):
" Handle Cantarell font definition to LaTeX " " Handle Cantarell font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['Cantarell']) fm = createFontMapping(['Cantarell'])
convert_fonts(document, fm, "oldstyle") convert_fonts(document, fm, "oldstyle")
def revert_CantarellFont(document): def revert_CantarellFont(document):
" Revert native Cantarell font definition to LaTeX " " Revert native Cantarell font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['Cantarell']) fm = createFontMapping(['Cantarell'])
if revert_fonts(document, fm, fontmap, False, True): if revert_fonts(document, fm, fontmap, False, True):
@ -3000,14 +2951,14 @@ def revert_CantarellFont(document):
def convert_ChivoFont(document): def convert_ChivoFont(document):
" Handle Chivo font definition to LaTeX " " Handle Chivo font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['Chivo']) fm = createFontMapping(['Chivo'])
convert_fonts(document, fm, "oldstyle") convert_fonts(document, fm, "oldstyle")
def revert_ChivoFont(document): def revert_ChivoFont(document):
" Revert native Chivo font definition to LaTeX " " Revert native Chivo font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['Chivo']) fm = createFontMapping(['Chivo'])
if revert_fonts(document, fm, fontmap, False, True): if revert_fonts(document, fm, fontmap, False, True):
@ -3017,14 +2968,14 @@ def revert_ChivoFont(document):
def convert_FiraFont(document): def convert_FiraFont(document):
" Handle Fira font definition to LaTeX " " Handle Fira font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['Fira']) fm = createFontMapping(['Fira'])
convert_fonts(document, fm, "lf") convert_fonts(document, fm, "lf")
def revert_FiraFont(document): def revert_FiraFont(document):
" Revert native Fira font definition to LaTeX " " Revert native Fira font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['Fira']) fm = createFontMapping(['Fira'])
if revert_fonts(document, fm, fontmap, False, True): if revert_fonts(document, fm, fontmap, False, True):
@ -3034,12 +2985,7 @@ def revert_FiraFont(document):
def convert_Semibolds(document): def convert_Semibolds(document):
" Move semibold options to extraopts " " Move semibold options to extraopts "
NonTeXFonts = False NonTeXFonts = get_bool_value(document.header, "\\use_non_tex_fonts")
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) i = find_token(document.header, "\\font_roman", 0)
if i == -1: if i == -1:
@ -3160,14 +3106,14 @@ def convert_NotoRegulars(document):
def convert_CrimsonProFont(document): def convert_CrimsonProFont(document):
" Handle CrimsonPro font definition to LaTeX " " Handle CrimsonPro font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fm = createFontMapping(['CrimsonPro']) fm = createFontMapping(['CrimsonPro'])
convert_fonts(document, fm, "lf") convert_fonts(document, fm, "lf")
def revert_CrimsonProFont(document): def revert_CrimsonProFont(document):
" Revert native CrimsonPro font definition to LaTeX " " Revert native CrimsonPro font definition to LaTeX "
if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
fm = createFontMapping(['CrimsonPro']) fm = createFontMapping(['CrimsonPro'])
if revert_fonts(document, fm, fontmap, False, True): if revert_fonts(document, fm, fontmap, False, True):
@ -3567,7 +3513,7 @@ def revert_totalheight(document):
val = val + "," + special val = val + "," + special
document.body[k] = "\tspecial " + "totalheight=" + val document.body[k] = "\tspecial " + "totalheight=" + val
else: else:
document.body.insert(kk, "\tspecial totalheight=" + val) document.body.insert(kk, "\tspecial totalheight=" + val)
if oldheight != "": if oldheight != "":
document.body[kk] = m.group(1) + oldheight document.body[kk] = m.group(1) + oldheight
else: else:
@ -3626,13 +3572,13 @@ def convert_totalheight(document):
val = val + "," + special val = val + "," + special
document.body[k] = "\tspecial " + "height=" + val document.body[k] = "\tspecial " + "height=" + val
else: else:
document.body.insert(kk + 1, "\tspecial height=" + val) document.body.insert(kk + 1, "\tspecial height=" + val)
if newheight != "": if newheight != "":
document.body[kk] = m.group(1) + newheight document.body[kk] = m.group(1) + newheight
else: else:
del document.body[kk] del document.body[kk]
elif newheight != "": elif newheight != "":
document.body.insert(k, "\theight " + newheight) document.body.insert(k, "\theight " + newheight)
i = j + 1 i = j + 1