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:
- document.warning("You should pass a list to add_to_preamble!")
# split on \n just in case
# it'll give us the one element list we want
# 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)
""" Insert text to the preamble at a given line"""
if not type(text) is list:
- document.warning("You should pass a list to insert_to_preamble!")
# split on \n just in case
# it'll give us the one element list we want
# if there's no \n, too