#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
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]
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}"])
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)