Commit 8dc0c578 authored by john's avatar john

fixed runner bug in ST.py

parent 675ef134
...@@ -97,7 +97,6 @@ def indention(str): ...@@ -97,7 +97,6 @@ def indention(str):
if str == '\n': if str == '\n':
return -1 return -1
#str = expandtabs(str) # covert tabs into spaces
front = re.compile('( *)') front = re.compile('( *)')
m = front.match(str) m = front.match(str)
...@@ -109,46 +108,28 @@ def indention(str): ...@@ -109,46 +108,28 @@ def indention(str):
def runner(struct,top,level,numbers): def runner(struct,top,level,numbers):
""" tmp = []
returns run which is a pointer to for x in numbers:
struct to be appended to if level > x:
""" tmp.append(x)
numbers = tmp
i = 3 numbers.append(level)
run = struct[top][1]
if level == 0:
return struct
if level == 1:
return run
if level == 2:
a = numbers[level-2]
return run[a-1][1]
a = numbers[level-2]
run = run[a-1][1]
while i <= level: if len(numbers) == 1:
a = numbers[i-2] return (struct,numbers)
run = run[a-1][1]
i = i + 1
return run
def find_level(indent, levels):
"""
when the indention is less,
find which level it is a
sublevel of
"""
if levels.has_key(indent): run = struct[top][1]
return levels[indent] if level == numbers[len(numbers)-1]:
i = 1
for key in levels.keys(): while i < level:
if key > indent: run = run[len(run)-1][1]
return levels[key] i = i + 1
else:
i = 1
while i <= level:
run = run[len(run)-1][1]
i = i + 1
return run,numbers
def split_paragraphs(paragraphs): def split_paragraphs(paragraphs):
...@@ -164,7 +145,6 @@ def split_paragraphs(paragraphs): ...@@ -164,7 +145,6 @@ def split_paragraphs(paragraphs):
if type(paragraphs).__name__ == "list": if type(paragraphs).__name__ == "list":
for paragraph in paragraphs: for paragraph in paragraphs:
tmp = tmp + expandtabs(paragraph) tmp = tmp + expandtabs(paragraph)
paragraphs = par.split(tmp) paragraphs = par.split(tmp)
elif type(paragraphs).__name__ == "string": elif type(paragraphs).__name__ == "string":
paragraphs = par.split(expandtabs(paragraphs)) paragraphs = par.split(expandtabs(paragraphs))
...@@ -177,6 +157,21 @@ def split_paragraphs(paragraphs): ...@@ -177,6 +157,21 @@ def split_paragraphs(paragraphs):
return paragraphs return paragraphs
def parent_level(levels, level):
K = levels.keys()
greatest = 0
for key in K:
if key > greatest and key < level:
greatest = key
return greatest
def find_level(levels,indention):
K = levels.keys()
for key in K:
if levels[key] == indention:
return key
return -1
def StructuredText(paragraphs): def StructuredText(paragraphs):
""" """
...@@ -188,75 +183,71 @@ def StructuredText(paragraphs): ...@@ -188,75 +183,71 @@ def StructuredText(paragraphs):
current_level = 0 current_level = 0
current_indent = 0 current_indent = 0
levels = {0:0} # what level levels = {0:0}
ind = [] # structure based on indention levels ind = [] # structure based on indention levels
top = -1 # which header are we under top = -1 # which header are we under
numbers = {0:0} # how many sub-paragraphs already at a level numbers = [0] # Which levels have paragraphs
struct = [] # the structure to be returned struct = [] # the structure to be returned
paragraphs = split_paragraphs(paragraphs) paragraphs = split_paragraphs(paragraphs)
if not paragraphs: if not paragraphs:
result = ["",[]] result = ["",[]]
return result return result
for paragraph in paragraphs: for paragraph in paragraphs:
if paragraph == '\n': if paragraph == '\n':
ind.append([-1, paragraph]) ind.append([-1, paragraph])
else : else :
ind.append([indention(paragraph), strip(paragraph)+"\n"]) ind.append([indention(paragraph), strip(paragraph)+"\n"])
for indent,paragraph in ind : current_indent = indention(paragraphs[0])
levels[0] = current_indent
if indent > -1:
if indent == 0:
"""
a new top header, start over, everything underneath is
a sub-paragraph
"""
current_level = 0
current_indent = 0
top = top + 1
levels = {0:0}
numbers = {0:0}
struct.append([paragraph,[]])
elif indent > current_indent:
current_indent = indent
current_level = current_level + 1
levels[current_indent] = current_level
if not numbers.has_key(current_level-1): for indent,paragraph in ind :
numbers[current_level-1] = 0 if indent == 0:
struct.append([paragraph,[]])
run = runner(struct,top,current_level,numbers) current_level = 0
run.append([paragraph,[]]) current_indent = 0
numbers[current_level-1] = numbers[current_level-1] + 1 numbers = [0]
levels = {0:0}
elif (indent == current_indent): top = top + 1
if not numbers.has_key(current_level-1): elif indent == current_indent:
numbers[current_level-1] = 0 run,numbers = runner(struct,top,current_level,numbers)
run.append([paragraph,[]])
run = runner(struct,top,current_level,numbers) elif indent > current_indent:
run.append([paragraph,[]]) current_level = current_level + 1
numbers[current_level-1] = numbers[current_level-1] + 1 current_indent = indent
levels[current_level] = indent
elif indent < current_indent : run,numbers = runner(struct,top,current_level,numbers)
run.append([paragraph,[]])
current_level = find_level(indent,levels) levels[current_level] = indent
elif indent < current_indent:
l = parent_level(levels,current_level)
if indent > 0 and indent < levels[0]:
levels[0] = indent
current_indent = indent current_indent = indent
run,numbers = runner(struct,top,current_level,numbers)
if not numbers.has_key(current_level-1): elif find_level(levels,indent) != -1:
numbers[current_level-1] = 0 current_level = find_level(levels,indent)
current_indent = indent
run = runner(struct,top,current_level,numbers) run,numbers = runner(struct,top,current_level,numbers)
run.append([paragraph,[]]) elif levels[current_level] > indent and levels[l] < indent:
numbers[current_level-1] = numbers[current_level-1] + 1 levels[current_level] = indent
current_indent = indent
run,numbers = runner(struct,top,current_level,numbers)
current_level = l
else:
tmp = {}
for i in range(current_level):
if indent > levels[i]:
tmp[i] = levels[i]
elif indent == levels[i]:
current_level = i
current_indent = indent
run,numbers = runner(struct,top,current_level,numbers)
levels = tmp
run.append([paragraph,[]])
return struct return struct
class doc_text: class doc_text:
...@@ -592,7 +583,7 @@ class doc_underline: ...@@ -592,7 +583,7 @@ class doc_underline:
class doc_href1: class doc_href1:
def __init__(self,str=''): def __init__(self,str=''):
self.expr = re.compile('\"[ a-zA-Z0-9.:/;,]+\":[a-zA-Z0-9.:/;,]+(?=(\s+|\.|\!|\?))').search self.expr = re.compile('\"[ a-zA-Z0-9.:/;,\n]+\":[a-zA-Z0-9.:/;,\n]+(?=(\s+|\.|\!|\?))').search
self.str = [str] self.str = [str]
self.typ = "href1" self.typ = "href1"
...@@ -788,7 +779,6 @@ class DOC: ...@@ -788,7 +779,6 @@ class DOC:
instance. '-underline **this**' would be stored as an underline instance. '-underline **this**' would be stored as an underline
instance with a strong instance stored in its string instance with a strong instance stored in its string
""" """
def __init__(self): def __init__(self):
self.types = [doc_unorder_list(), self.types = [doc_unorder_list(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment