]> git.lyx.org Git - lyx.git/commitdiff
Improve the file format upgrade and downgrade for microtype
authorJosé Matos <jamatos@lyx.org>
Wed, 13 Jul 2016 14:01:17 +0000 (15:01 +0100)
committerJosé Matos <jamatos@lyx.org>
Wed, 13 Jul 2016 14:01:17 +0000 (15:01 +0100)
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. :-)

lib/lyx2lyx/lyx_2_3.py
lib/lyx2lyx/parser_tools.py

index fca850831f89a3e8197abf93473efbdf68e9e1dc..37386a3865001134bfe11a907ae1557d765952f2 100644 (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}"])
 
 
index 85e2b6ae8c969c1e98de55fb3f169b4ab737069b..50ad3a5812308d5c08d7db2f823fcad4833d5e9e 100644 (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)