Modify text_to_python() to handle stray whitespace on otherwise blank line
authorNick Downing <nick@ndcode.org>
Sat, 18 Nov 2023 23:06:17 +0000 (10:06 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 18 Nov 2023 23:06:17 +0000 (10:06 +1100)
ndcode/pilex/generate_py.py

index b5dbc3e..742b7b1 100644 (file)
@@ -39,11 +39,12 @@ def text_to_python(text, indent):
   #print(text)
   prefix = lines[0][:j]
   for j in range(len(lines)):
-    if len(lines[j]) == 0:
-      lines[j] = '\n'
-    else:
-      assert lines[j][:len(prefix)] == prefix
-      lines[j] = '{0:s}{1:s}\n'.format(indent, lines[j][len(prefix):])
+    assert lines[j][:len(prefix)] == prefix[:len(lines[j])]
+    lines[j] = (
+      '\n'
+    if len(lines[j]) <= len(prefix) else
+      '{0:s}{1:s}\n'.format(indent, lines[j][len(prefix):])
+    )
   return ''.join(lines)
 
 # note: these routines are literally the same, but conceptually different,