lyx2lyx refactoring and minor fixes.

This commit is contained in:
Günter Milde 2019-06-03 16:45:05 +02:00
parent b2cee3dcc5
commit f1e7f5267d
3 changed files with 41 additions and 82 deletions

View File

@ -578,63 +578,40 @@ def insert_document_option(document, option):
"Insert _option_ as a document option." "Insert _option_ as a document option."
# Find \options in the header # Find \options in the header
options_line = find_token(document.header, "\\options", 0) i = find_token(document.header, "\\options", 0)
# if the options does not exists add it after the textclass # if the options does not exists add it after the textclass
if options_line == -1: if i == -1:
textclass_line = find_token(document.header, "\\textclass", 0) i = find_token(document.header, "\\textclass", 0) + 1
document.header.insert(textclass_line +1, document.header.insert(i, r"\options %s" % option)
r"\options %s" % option)
return return
# otherwise append to options
# add it to the end of the options if not is_document_option(document, option):
document.header[options_line] += ",%s" % option document.header[i] += ",%s" % option
def remove_document_option(document, option): def remove_document_option(document, option):
""" Remove _option_ as a document option. """ Remove _option_ as a document option."""
It is assumed that option belongs to the \options. i = find_token(document.header, "\\options")
That can be done running is_document_option(document, option).""" options = get_value(document.header, "\\options", i)
options = [op.strip() for op in options.split(',')]
options_line = find_token(document.header, "\\options", 0) # Remove `option` from \options
option_pos = document.header[options_line].find(option) options = [op for op in options if op != option]
# Remove option from \options if options:
comma_before_pos = document.header[options_line].rfind(',', 0, option_pos) document.header[i] = "\\options " + ','.join(options)
comma_after_pos = document.header[options_line].find(',', option_pos) else:
del document.header[i]
# if there are no commas then it is the single option
# and the options line should be removed since it will be empty
if comma_before_pos == comma_after_pos == -1:
del document.header[options_line]
return
# last option
options = document.header[options_line]
if comma_after_pos == -1:
document.header[options_line] = options[:comma_before_pos].rsplit()
return
document.header[options_line] = options[comma_before_pos: comma_after_pos]
def is_document_option(document, option): def is_document_option(document, option):
"Find if _option_ is a document option" "Find if _option_ is a document option"
# Find \options in the header options = get_value(document.header, "\\options")
options_line = find_token(document.header, "\\options", 0) options = [op.strip() for op in options.split(',')]
return option in options
# \options is not present in the header
if options_line == -1:
return False
option_pos = document.header[options_line].find(option)
# option is not present in the \options
if option_pos == -1:
return False
return True
singlepar_insets = [s.strip() for s in singlepar_insets = [s.strip() for s in
u"Argument, Caption Above, Caption Below, Caption Bicaption," u"Argument, Caption Above, Caption Below, Caption Bicaption,"

View File

@ -32,7 +32,8 @@ from parser_tools import (del_token, del_value, del_complete_lines,
# find_tokens, find_token_exact, check_token, get_option_value # find_tokens, find_token_exact, check_token, get_option_value
from lyx2lyx_tools import (add_to_preamble, put_cmd_in_ert, revert_font_attrs, from lyx2lyx_tools import (add_to_preamble, put_cmd_in_ert, revert_font_attrs,
insert_to_preamble, latex_length, revert_language) insert_to_preamble, latex_length, is_document_option,
insert_document_option, remove_document_option, revert_language)
#################################################################### ####################################################################
# Private helper functions # Private helper functions
@ -149,9 +150,8 @@ def revert_ibranches(document):
if j == -1: if j == -1:
document.warning("Malformed LyX document! Can't find end of branch " + old) document.warning("Malformed LyX document! Can't find end of branch " + old)
continue continue
# ourbranches[old] - 1 inverts the selection status of the old branch
lines = ["\\branch " + new, lines = ["\\branch " + new,
"\\selected " + str(ourbranches[old] - 1)] "\\selected %d" % (not ourbranches[old])]
# these are the old lines telling us color, etc. # these are the old lines telling us color, etc.
lines += document.header[i+2 : j+1] lines += document.header[i+2 : j+1]
document.header[i:i] = lines document.header[i:i] = lines
@ -1873,35 +1873,23 @@ def revert_allowbreak(document):
def convert_mathnumberpos(document): def convert_mathnumberpos(document):
" add the \\math_number_before tag " " add the \\math_number_before tag "
i = find_token(document.header, "\\quotes_style")
# check if the document uses the class option "leqno" # check if the document uses the class option "leqno"
i = find_token(document.header, "\\options") if is_document_option(document, "leqno"):
k = find_token(document.header, "\\quotes_style") remove_document_option(document, "leqno")
if 'leqno' in document.header[i]: document.header.insert(i, "\\math_number_before 1")
document.header.insert(k, "\\math_number_before 1")
# delete the found option
document.header[i] = document.header[i].replace(",leqno", "")
document.header[i] = document.header[i].replace(", leqno", "")
document.header[i] = document.header[i].replace("leqno,", "")
if 'leqno' in document.header[i]:
# then we have leqno as the only option
del document.header[i]
else: else:
document.header.insert(k, "\\math_number_before 0") document.header.insert(i, "\\math_number_before 0")
def revert_mathnumberpos(document): def revert_mathnumberpos(document):
"""Remove \\math_number_before tag, """Remove \\math_number_before tag,
add the document class option leqno if required. add the document class option leqno if required.
""" """
math_number_before = get_bool_value(document.header, math_number_before = get_bool_value(document.header, '\\math_number_before',
'\\math_number_before', delete=True) delete=True)
if math_number_before: if math_number_before:
i = find_token(document.header, "\\options") insert_document_option(document, "leqno")
if i != -1 and 'leqno' not in document.header[i]:
document.header[i] = document.header[i].replace("\\options", "\\options leqno,")
else:
i = find_token(document.header, "\\use_default_options")
document.header.insert(i, "\\options leqno")
def convert_mathnumberingname(document): def convert_mathnumberingname(document):

View File

@ -428,25 +428,21 @@ def get_quoted_value(lines, token, start=0, end=0, default="", delete=False):
return default return default
return val.strip('"') return val.strip('"')
bool_values = {True: ("true", "1"),
False: ("false", "0")} bool_values = {"true": True, "1": True,
"false": False, "0": False}
def get_bool_value(lines, token, start=0, end=0, default=None, delete=False): def get_bool_value(lines, token, start=0, end=0, default=None, delete=False):
""" get_bool_value(lines, token, start[[, end], default]) -> string """ get_bool_value(lines, token, start[[, end], default]) -> string
Find the next line that looks like: Find the next line that looks like:
token <bool_value> `token` <bool_value>
Return True if <bool_value> is 1 or "true", False if bool_value Return True if <bool_value> is 1 or "true", False if <bool_value>
is 0 or "false", else `default`. is 0 or "false", else `default`.
""" """
val = get_quoted_value(lines, token, start, end, default, delete) val = get_quoted_value(lines, token, start, end, default, delete)
if val in bool_values[True]: return bool_values.get(val, default)
return True
if val in bool_values[False]:
return False
return default
def set_bool_value(lines, token, value, start=0, end=0): def set_bool_value(lines, token, value, start=0, end=0):
@ -462,13 +458,11 @@ def set_bool_value(lines, token, value, start=0, end=0):
oldvalue = get_bool_value(lines, token, i, i+1) oldvalue = get_bool_value(lines, token, i, i+1)
if oldvalue is value: if oldvalue is value:
return oldvalue return oldvalue
# Use 0/1 or true/false?
if get_quoted_value(lines, token, i, i+1) in ('0', '1'):
value_string = bool_values[value][1]
else:
value_string = bool_values[value][0]
# set to new value # set to new value
lines[i] = "%s %s" % (token, value_string) if get_quoted_value(lines, token, i, i+1) in ('0', '1'):
lines[i] = "%s %d" % (token, value)
else:
lines[i] = "%s %s" % (token, str(value).lower())
return oldvalue return oldvalue