Improve the file format upgrade and downgrade for microtype

Add get_bool_value to parser_tools to be used in other places.
Make the upgrade convertion seamless from lyx writing the file (regarding
microtypes, there are still other dragons to chase. :-)
This commit is contained in:
José Matos 2016-07-13 15:01:17 +01:00
parent 2b6f822bad
commit cca93dacae
2 changed files with 28 additions and 7 deletions

View File

@ -28,9 +28,10 @@ import sys, os
#from parser_tools import find_token, find_end_of, find_tokens, \
# find_token_exact, find_end_of_inset, find_end_of_layout, \
# find_token_backwards, is_in_inset, get_value, get_quoted_value, \
# del_token, check_token, get_option_value
# del_token, check_token, get_option_value, get_bool_value
from parser_tools import find_token, find_end_of_inset, get_value
from parser_tools import find_token, find_end_of_inset, get_value, \
get_bool_value
#from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert, lyx2latex, \
# lyx2verbatim, length_in_bp, convert_info_insets
@ -55,12 +56,13 @@ def convert_microtype(document):
i = find_token(document.header, "\\font_tt_scale" , 0)
if i == -1:
document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
return;
i = len(document.header) - 1
j = find_token(document.preamble, "\\usepackage{microtype}", 0)
if j == -1:
document.header.insert(i + 1, "\\use_microtype 0")
document.header.insert(i + 1, "\\use_microtype false")
else:
document.header.insert(i + 1, "\\use_microtype 1")
document.header.insert(i + 1, "\\use_microtype true")
del document.preamble[j]
@ -69,9 +71,9 @@ def revert_microtype(document):
i = find_token(document.header, "\\use_microtype", 0)
if i == -1:
return
value = get_value(document.header, "\\use_microtype" , i).split()[0]
use_microtype = get_bool_value(document.header, "\\use_microtype" , i)
del document.header[i]
if value == "1":
if use_microtype:
add_to_preamble(document, ["\\usepackage{microtype}"])

View File

@ -315,6 +315,25 @@ def get_quoted_value(lines, token, start, end = 0, default = ""):
return val.strip('"')
def get_bool_value(lines, token, start, end = 0, default = None):
""" get_value(lines, token, start[[, end], default]) -> string
Find the next line that looks like:
token bool_value
Returns True if bool_value is 1 or true and
False if bool_value is 0 or false
"""
val = get_value(lines, token, start, end, "")
if val == "1" or val == "true":
return True
if val == "0" or val == "false":
return False
return default
def get_option_value(line, option):
rx = option + '\s*=\s*"([^"]+)"'
rx = re.compile(rx)