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."
# 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 options_line == -1:
textclass_line = find_token(document.header, "\\textclass", 0)
document.header.insert(textclass_line +1,
r"\options %s" % option)
if i == -1:
i = find_token(document.header, "\\textclass", 0) + 1
document.header.insert(i, r"\options %s" % option)
return
# add it to the end of the options
document.header[options_line] += ",%s" % option
# otherwise append to options
if not is_document_option(document, option):
document.header[i] += ",%s" % 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.
That can be done running is_document_option(document, option)."""
i = find_token(document.header, "\\options")
options = get_value(document.header, "\\options", i)
options = [op.strip() for op in options.split(',')]
options_line = find_token(document.header, "\\options", 0)
option_pos = document.header[options_line].find(option)
# Remove `option` from \options
options = [op for op in options if op != option]
# Remove option from \options
comma_before_pos = document.header[options_line].rfind(',', 0, option_pos)
comma_after_pos = document.header[options_line].find(',', option_pos)
# 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]
if options:
document.header[i] = "\\options " + ','.join(options)
else:
del document.header[i]
def is_document_option(document, option):
"Find if _option_ is a document option"
# Find \options in the header
options_line = find_token(document.header, "\\options", 0)
options = get_value(document.header, "\\options")
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
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
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
@ -149,9 +150,8 @@ def revert_ibranches(document):
if j == -1:
document.warning("Malformed LyX document! Can't find end of branch " + old)
continue
# ourbranches[old] - 1 inverts the selection status of the old branch
lines = ["\\branch " + new,
"\\selected " + str(ourbranches[old] - 1)]
"\\selected %d" % (not ourbranches[old])]
# these are the old lines telling us color, etc.
lines += document.header[i+2 : j+1]
document.header[i:i] = lines
@ -1873,35 +1873,23 @@ def revert_allowbreak(document):
def convert_mathnumberpos(document):
" add the \\math_number_before tag "
i = find_token(document.header, "\\quotes_style")
# check if the document uses the class option "leqno"
i = find_token(document.header, "\\options")
k = find_token(document.header, "\\quotes_style")
if 'leqno' in document.header[i]:
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]
if is_document_option(document, "leqno"):
remove_document_option(document, "leqno")
document.header.insert(i, "\\math_number_before 1")
else:
document.header.insert(k, "\\math_number_before 0")
document.header.insert(i, "\\math_number_before 0")
def revert_mathnumberpos(document):
"""Remove \\math_number_before tag,
add the document class option leqno if required.
"""
math_number_before = get_bool_value(document.header,
'\\math_number_before', delete=True)
math_number_before = get_bool_value(document.header, '\\math_number_before',
delete=True)
if math_number_before:
i = find_token(document.header, "\\options")
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")
insert_document_option(document, "leqno")
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 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):
""" get_bool_value(lines, token, start[[, end], default]) -> string
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`.
"""
val = get_quoted_value(lines, token, start, end, default, delete)
if val in bool_values[True]:
return True
if val in bool_values[False]:
return False
return default
return bool_values.get(val, default)
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)
if oldvalue is value:
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
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