Python cleanup from Thibaut.

This commit is contained in:
Richard Kimberly Heck 2020-05-08 13:31:05 -04:00
parent 8918d5287b
commit 5588c22e77

View File

@ -47,7 +47,7 @@ from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble, insert_to_preamble,
# Private helper functions # Private helper functions
def add_preamble_fonts(document, fontmap): def add_preamble_fonts(document, fontmap):
" Add collected font-packages with their option to user-preamble" """Add collected font-packages with their option to user-preamble"""
for pkg in fontmap: for pkg in fontmap:
if len(fontmap[pkg]) > 0: if len(fontmap[pkg]) > 0:
@ -85,7 +85,7 @@ class fontmapping:
self.pkginmap = dict() # defines, if a map for package exists self.pkginmap = dict() # defines, if a map for package exists
def expandFontMapping(self, font_list, font_type, scale_type, pkg, scaleopt = None, osfopt = None, osfdef = "false"): def expandFontMapping(self, font_list, font_type, scale_type, pkg, scaleopt = None, osfopt = None, osfdef = "false"):
" Expand fontinfo mapping" """Expand fontinfo mapping"""
# #
# fontlist: list of fontnames, each element # fontlist: list of fontnames, each element
# may contain a ','-separated list of needed options # may contain a ','-separated list of needed options
@ -197,8 +197,7 @@ def createFontMapping(fontlist):
return fm return fm
def convert_fonts(document, fm, osfoption = "osf"): def convert_fonts(document, fm, osfoption = "osf"):
" Handle font definition (LaTeX preamble -> native) " """Handle font definition (LaTeX preamble -> native)"""
rpkg = re.compile(r'^\\usepackage(\[([^\]]*)\])?\{([^\}]+)\}') rpkg = re.compile(r'^\\usepackage(\[([^\]]*)\])?\{([^\}]+)\}')
rscaleopt = re.compile(r'^scaled?=(.*)') rscaleopt = re.compile(r'^scaled?=(.*)')
@ -306,9 +305,8 @@ def convert_fonts(document, fm, osfoption = "osf"):
document.header.insert(fo, fotag + " \"" + ",".join(options) + "\"") document.header.insert(fo, fotag + " \"" + ",".join(options) + "\"")
def revert_fonts(document, fm, fontmap, OnlyWithXOpts = False, WithXOpts = False): def revert_fonts(document, fm, fontmap, OnlyWithXOpts = False, WithXOpts = False):
" Revert native font definition to LaTeX " """Revert native font definition to LaTeX"""
# fonlist := list of fonts created from the same package # fonlist := list of fonts created from the same package
# Empty package means that the font-name is the same as the package-name # Empty package means that the font-name is the same as the package-name
# fontmap (key = package, val += found options) will be filled # fontmap (key = package, val += found options) will be filled
@ -390,7 +388,7 @@ def revert_fonts(document, fm, fontmap, OnlyWithXOpts = False, WithXOpts = False
############################################################################### ###############################################################################
def convert_inputencoding_namechange(document): def convert_inputencoding_namechange(document):
" Rename inputencoding settings. " """Rename inputencoding settings."""
i = find_token(document.header, "\\inputencoding", 0) i = find_token(document.header, "\\inputencoding", 0)
if i == -1: if i == -1:
return return
@ -398,7 +396,7 @@ def convert_inputencoding_namechange(document):
document.header[i] = s.replace("default", "auto-legacy-plain") document.header[i] = s.replace("default", "auto-legacy-plain")
def revert_inputencoding_namechange(document): def revert_inputencoding_namechange(document):
" Rename inputencoding settings. " """Rename inputencoding settings."""
i = find_token(document.header, "\\inputencoding", 0) i = find_token(document.header, "\\inputencoding", 0)
if i == -1: if i == -1:
return return
@ -406,14 +404,14 @@ def revert_inputencoding_namechange(document):
document.header[i] = s.replace("auto-legacy", "auto") document.header[i] = s.replace("auto-legacy", "auto")
def convert_notoFonts(document): def convert_notoFonts(document):
" Handle Noto fonts definition to LaTeX " """Handle Noto fonts definition to LaTeX"""
if not get_bool_value(document.header, "\\use_non_tex_fonts"): 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 not get_bool_value(document.header, "\\use_non_tex_fonts"): if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
@ -422,14 +420,14 @@ def revert_notoFonts(document):
add_preamble_fonts(document, fontmap) add_preamble_fonts(document, fontmap)
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 not get_bool_value(document.header, "\\use_non_tex_fonts"): 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 not get_bool_value(document.header, "\\use_non_tex_fonts"): if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
@ -438,14 +436,14 @@ def revert_latexFonts(document):
add_preamble_fonts(document, fontmap) add_preamble_fonts(document, fontmap)
def convert_AdobeFonts(document): def convert_AdobeFonts(document):
" Handle Adobe Source fonts definition to LaTeX " """Handle Adobe Source fonts definition to LaTeX"""
if not get_bool_value(document.header, "\\use_non_tex_fonts"): 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 not get_bool_value(document.header, "\\use_non_tex_fonts"): if not get_bool_value(document.header, "\\use_non_tex_fonts"):
fontmap = dict() fontmap = dict()
@ -454,7 +452,7 @@ def revert_AdobeFonts(document):
add_preamble_fonts(document, fontmap) add_preamble_fonts(document, fontmap)
def removeFrontMatterStyles(document): def removeFrontMatterStyles(document):
" Remove styles Begin/EndFrontmatter" """Remove styles Begin/EndFrontmatter"""
layouts = ['BeginFrontmatter', 'EndFrontmatter'] layouts = ['BeginFrontmatter', 'EndFrontmatter']
tokenend = len('\\begin_layout ') tokenend = len('\\begin_layout ')
@ -475,7 +473,7 @@ def removeFrontMatterStyles(document):
document.body[i:j+1] = [] document.body[i:j+1] = []
def addFrontMatterStyles(document): def addFrontMatterStyles(document):
" Use styles Begin/EndFrontmatter for elsarticle" """Use styles Begin/EndFrontmatter for elsarticle"""
if document.textclass != "elsarticle": if document.textclass != "elsarticle":
return return
@ -522,7 +520,7 @@ def addFrontMatterStyles(document):
def convert_lst_literalparam(document): def convert_lst_literalparam(document):
" Add param literal to include inset " """Add param literal to include inset"""
i = 0 i = 0
while True: while True:
@ -539,7 +537,7 @@ def convert_lst_literalparam(document):
def revert_lst_literalparam(document): def revert_lst_literalparam(document):
" Remove param literal from include inset " """Remove param literal from include inset"""
i = 0 i = 0
while True: while True:
@ -554,7 +552,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 not get_bool_value(document.header, "\\use_non_tex_fonts"): if not get_bool_value(document.header, "\\use_non_tex_fonts"):
preamble = "" preamble = ""
@ -609,7 +607,7 @@ def revert_paratype(document):
def revert_xcharter(document): def revert_xcharter(document):
" Revert XCharter font definitions to LaTeX " """Revert XCharter font definitions to LaTeX"""
i = find_token(document.header, "\\font_roman \"xcharter\"", 0) i = find_token(document.header, "\\font_roman \"xcharter\"", 0)
if i == -1: if i == -1:
@ -633,7 +631,7 @@ def revert_xcharter(document):
def revert_lscape(document): def revert_lscape(document):
" Reverts the landscape environment (Landscape module) to TeX-code " """Reverts the landscape environment (Landscape module) to TeX-code"""
if not "landscape" in document.get_module_list(): if not "landscape" in document.get_module_list():
return return
@ -661,7 +659,7 @@ def revert_lscape(document):
def convert_fontenc(document): def convert_fontenc(document):
" Convert default fontenc setting " """Convert default fontenc setting"""
i = find_token(document.header, "\\fontencoding global", 0) i = find_token(document.header, "\\fontencoding global", 0)
if i == -1: if i == -1:
@ -671,7 +669,7 @@ def convert_fontenc(document):
def revert_fontenc(document): def revert_fontenc(document):
" Revert default fontenc setting " """Revert default fontenc setting"""
i = find_token(document.header, "\\fontencoding auto", 0) i = find_token(document.header, "\\fontencoding auto", 0)
if i == -1: if i == -1:
@ -681,7 +679,7 @@ def revert_fontenc(document):
def revert_nospellcheck(document): def revert_nospellcheck(document):
" Remove nospellcheck font info param " """Remove nospellcheck font info param"""
i = 0 i = 0
while True: while True:
@ -692,7 +690,7 @@ def revert_nospellcheck(document):
def revert_floatpclass(document): def revert_floatpclass(document):
" Remove float placement params 'document' and 'class' " """Remove float placement params 'document' and 'class'"""
del_token(document.header, "\\float_placement class") del_token(document.header, "\\float_placement class")
@ -712,7 +710,7 @@ def revert_floatpclass(document):
def revert_floatalignment(document): def revert_floatalignment(document):
" Remove float alignment params " """Remove float alignment params"""
galignment = get_value(document.header, "\\float_alignment", delete=True) galignment = get_value(document.header, "\\float_alignment", delete=True)
@ -749,7 +747,7 @@ def revert_floatalignment(document):
i = j i = j
def revert_tuftecite(document): def revert_tuftecite(document):
" Revert \cite commands in tufte classes " """Revert \cite commands in tufte classes"""
tufte = ["tufte-book", "tufte-handout"] tufte = ["tufte-book", "tufte-handout"]
if document.textclass not in tufte: if document.textclass not in tufte:
@ -794,7 +792,7 @@ def revert_tuftecite(document):
def revert_stretchcolumn(document): def revert_stretchcolumn(document):
" We remove the column varwidth flags or everything else will become a mess. " """We remove the column varwidth flags or everything else will become a mess."""
i = 0 i = 0
while True: while True:
i = find_token(document.body, "\\begin_inset Tabular", i+1) i = find_token(document.body, "\\begin_inset Tabular", i+1)
@ -811,7 +809,7 @@ def revert_stretchcolumn(document):
def revert_vcolumns(document): def revert_vcolumns(document):
" Revert standard columns with line breaks etc. " """Revert standard columns with line breaks etc."""
i = 0 i = 0
needvarwidth = False needvarwidth = False
needarray = False needarray = False
@ -904,7 +902,7 @@ def revert_vcolumns(document):
def revert_bibencoding(document): def revert_bibencoding(document):
" Revert bibliography encoding " """Revert bibliography encoding"""
# Get cite engine # Get cite engine
engine = "basic" engine = "basic"
@ -1003,7 +1001,7 @@ def revert_bibencoding(document):
def convert_vcsinfo(document): def convert_vcsinfo(document):
" Separate vcs Info inset from buffer Info inset. " """Separate vcs Info inset from buffer Info inset."""
types = { types = {
"vcs-revision" : "revision", "vcs-revision" : "revision",
@ -1034,7 +1032,7 @@ def convert_vcsinfo(document):
def revert_vcsinfo(document): def revert_vcsinfo(document):
" Merge vcs Info inset to buffer Info inset. " """Merge vcs Info inset to buffer Info inset."""
args = ["revision", "tree-revision", "author", "time", "date" ] args = ["revision", "tree-revision", "author", "time", "date" ]
i = 0 i = 0
@ -1080,7 +1078,7 @@ def revert_vcsinfo_rev_abbrev(document):
document.body[arg] = "arg \"revision\"" document.body[arg] = "arg \"revision\""
def revert_dateinfo(document): def revert_dateinfo(document):
" Revert date info insets to static text. " """Revert date info insets to static text."""
# FIXME This currently only considers the main language and uses the system locale # FIXME This currently only considers the main language and uses the system locale
# Ideally, it should honor context languages and switch the locale accordingly. # Ideally, it should honor context languages and switch the locale accordingly.
@ -1255,7 +1253,7 @@ def revert_dateinfo(document):
def revert_timeinfo(document): def revert_timeinfo(document):
" Revert time info insets to static text. " """Revert time info insets to static text."""
# FIXME This currently only considers the main language and uses the system locale # FIXME This currently only considers the main language and uses the system locale
# Ideally, it should honor context languages and switch the locale accordingly. # Ideally, it should honor context languages and switch the locale accordingly.
@ -1370,7 +1368,6 @@ def revert_timeinfo(document):
} }
types = ["time", "fixtime", "modtime" ] types = ["time", "fixtime", "modtime" ]
i = 0
i = find_token(document.header, "\\language", 0) i = find_token(document.header, "\\language", 0)
if i == -1: if i == -1:
# this should not happen # this should not happen
@ -1430,7 +1427,7 @@ def revert_timeinfo(document):
def revert_namenoextinfo(document): def revert_namenoextinfo(document):
" Merge buffer Info inset type name-noext to name. " """Merge buffer Info inset type name-noext to name."""
i = 0 i = 0
while True: while True:
@ -1453,7 +1450,7 @@ def revert_namenoextinfo(document):
def revert_l7ninfo(document): def revert_l7ninfo(document):
" Revert l7n Info inset to text. " """Revert l7n Info inset to text."""
i = 0 i = 0
while True: while True:
@ -1476,7 +1473,7 @@ def revert_l7ninfo(document):
def revert_listpargs(document): def revert_listpargs(document):
" Reverts listpreamble arguments to TeX-code " """Reverts listpreamble arguments to TeX-code"""
i = 0 i = 0
while True: while True:
i = find_token(document.body, "\\begin_inset Argument listpreamble:", i+1) i = find_token(document.body, "\\begin_inset Argument listpreamble:", i+1)
@ -1499,7 +1496,7 @@ def revert_listpargs(document):
def revert_lformatinfo(document): def revert_lformatinfo(document):
" Revert layout format Info inset to text. " """Revert layout format Info inset to text."""
i = 0 i = 0
while True: while True:
@ -1543,19 +1540,19 @@ def convert_hebrew_parentheses(document):
def revert_hebrew_parentheses(document): def revert_hebrew_parentheses(document):
" Store parentheses in Hebrew text reversed" """Store parentheses in Hebrew text reversed"""
# This only exists to keep the convert/revert naming convention # This only exists to keep the convert/revert naming convention
convert_hebrew_parentheses(document) convert_hebrew_parentheses(document)
def revert_malayalam(document): def revert_malayalam(document):
" Set the document language to English but assure Malayalam output " """Set the document language to English but assure Malayalam output"""
revert_language(document, "malayalam", "", "malayalam") revert_language(document, "malayalam", "", "malayalam")
def revert_soul(document): def revert_soul(document):
" Revert soul module flex insets to ERT " """Revert soul module flex insets to ERT"""
flexes = ["Spaceletters", "Strikethrough", "Underline", "Highlight", "Capitalize"] flexes = ["Spaceletters", "Strikethrough", "Underline", "Highlight", "Capitalize"]
@ -1576,16 +1573,15 @@ def revert_soul(document):
def revert_tablestyle(document): def revert_tablestyle(document):
" Remove tablestyle params " """Remove tablestyle params"""
i = 0
i = find_token(document.header, "\\tablestyle") i = find_token(document.header, "\\tablestyle")
if i != -1: if i != -1:
del document.header[i] del document.header[i]
def revert_bibfileencodings(document): def revert_bibfileencodings(document):
" Revert individual Biblatex bibliography encodings " """Revert individual Biblatex bibliography encodings"""
# Get cite engine # Get cite engine
engine = "basic" engine = "basic"
@ -1693,7 +1689,7 @@ def revert_bibfileencodings(document):
def revert_cmidruletrimming(document): def revert_cmidruletrimming(document):
" Remove \\cmidrule trimming " """Remove \\cmidrule trimming"""
# FIXME: Revert to TeX code? # FIXME: Revert to TeX code?
i = 0 i = 0
@ -1750,18 +1746,18 @@ ruby_inset_def = [
] ]
def convert_ruby_module(document): def convert_ruby_module(document):
" Use ruby module instead of local module definition " """Use ruby module instead of local module definition"""
if document.del_local_layout(ruby_inset_def): if document.del_local_layout(ruby_inset_def):
document.add_module("ruby") document.add_module("ruby")
def revert_ruby_module(document): def revert_ruby_module(document):
" Replace ruby module with local module definition " """Replace ruby module with local module definition"""
if document.del_module("ruby"): if document.del_module("ruby"):
document.append_local_layout(ruby_inset_def) document.append_local_layout(ruby_inset_def)
def convert_utf8_japanese(document): def convert_utf8_japanese(document):
" Use generic utf8 with Japanese documents." """Use generic utf8 with Japanese documents."""
lang = get_value(document.header, "\\language") lang = get_value(document.header, "\\language")
if not lang.startswith("japanese"): if not lang.startswith("japanese"):
return return
@ -1771,7 +1767,7 @@ def convert_utf8_japanese(document):
document.set_parameter("inputencoding", "utf8") document.set_parameter("inputencoding", "utf8")
def revert_utf8_japanese(document): def revert_utf8_japanese(document):
" Use Japanese utf8 variants with Japanese documents." """Use Japanese utf8 variants with Japanese documents."""
inputenc = get_value(document.header, "\\inputencoding") inputenc = get_value(document.header, "\\inputencoding")
if inputenc != "utf8": if inputenc != "utf8":
return return
@ -3856,7 +3852,7 @@ revert = [[593, [revert_counter_inset]],
[551, [revert_floatpclass, revert_floatalignment]], [551, [revert_floatpclass, revert_floatalignment]],
[550, [revert_nospellcheck]], [550, [revert_nospellcheck]],
[549, [revert_fontenc]], [549, [revert_fontenc]],
[548, []],# dummy format change [548, []], # dummy format change
[547, [revert_lscape]], [547, [revert_lscape]],
[546, [revert_xcharter]], [546, [revert_xcharter]],
[545, [revert_paratype]], [545, [revert_paratype]],