]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx2lyx_tools.py
Improve the add_to_preamble routine. Now we really check all the lines.
[lyx.git] / lib / lyx2lyx / lyx2lyx_tools.py
index 9e79b2d6fd429d41bd6c78cff5fcb8a16dafbc73..bf14a5bc931a2ee8afeb9b4408bd400994dd3d4d 100644 (file)
@@ -22,10 +22,12 @@ import string
 from parser_tools import find_token
 from unicode_symbols import unicode_reps
 
-# Note that text can be either a list of lines or a single line.
+
+# This will accept either a list of lines or a single line.
+# It is bad practice to pass something with embedded newlines, 
+# though we will handle that.
 def add_to_preamble(document, text):
-    """ Add text to the preamble if it is not already there.
-    Only the first line is checked!"""
+    " Add text to the preamble if it is not already there. "
 
     if not type(text) is list:
       # split on \n just in case
@@ -33,7 +35,20 @@ def add_to_preamble(document, text):
       # if there's no \n, too
       text = text.split('\n')
 
-    if find_token(document.preamble, text[0], 0) != -1:
+    i = 0
+    prelen = len(document.preamble)
+    while True:
+      i = find_token(document.preamble, text[0], i)
+      if i == -1:
+        break
+      # we need a perfect match
+      matched = True
+      for line in text:
+        if i >= prelen or line != document.preamble[i]:
+          matched = False
+          break
+        i += 1
+      if matched:
         return
 
     document.preamble.extend(text)
@@ -352,8 +367,17 @@ def revert_layout_command(document, name, LaTeXname, position):
 
 
 def hex2ratio(s):
-    val = string.atoi(s, 16)
-    if val != 0:
-      val += 1
-    return str(val / 256.0)
+  " Converts an RRGGBB-type hexadecimal string to a float in [0.0,1.0] "
+  try:
+    val = int(s, 16)
+  except:
+    val = 0
+  if val != 0:
+    val += 1
+  return str(val / 256.0)
+
 
+def str2bool(s):
+  "'true' goes to True, case-insensitively, and we strip whitespace."
+  s = s.strip().lower()
+  return s == "true"