#NEWLINE# def findSections( filename , lines ) : #NEWLINE# #INDENT# sections = [ ] #NEWLINE# headingOrder = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# orderSeen = 1 #NEWLINE# previousSections = [ ] #NEWLINE# for ( i , line ) in enumerate ( lines ) : #NEWLINE# #INDENT# if ( i == 0 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( headingRe . match ( line ) and subjectRe . match ( lines [ ( i - 1 ) ] ) ) : #NEWLINE# #INDENT# if ( i >= 2 ) : #NEWLINE# #INDENT# topLine = lines [ ( i - 2 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# topLine = '' #NEWLINE# #UNINDENT# if ( len ( line ) < len ( lines [ ( i - 1 ) ] ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# key = line [ 0 ] #NEWLINE# if headingRe . match ( topLine ) : #NEWLINE# #INDENT# if ( topLine != line ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# key = ( key + key ) #NEWLINE# #UNINDENT# if ( key not in headingOrder ) : #NEWLINE# #INDENT# headingOrder [ key ] = orderSeen #NEWLINE# orderSeen += 1 #NEWLINE# #UNINDENT# name = lines [ ( i - 1 ) ] . strip ( ) #NEWLINE# level = headingOrder [ key ] #NEWLINE# previousSections = previousSections [ : ( level - 1 ) ] #NEWLINE# if previousSections : #NEWLINE# #INDENT# parent = previousSections [ ( - 1 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# parent = None #NEWLINE# #UNINDENT# lineNumber = i #NEWLINE# s = Section ( level , name , lines [ ( i - 1 ) ] , lineNumber , filename , parent ) #NEWLINE# previousSections . append ( s ) #NEWLINE# sections . append ( s ) #NEWLINE# lines [ i ] = lines [ ( i - 1 ) ] = '' #NEWLINE# if topLine : #NEWLINE# #INDENT# lines [ ( i - 2 ) ] = '' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return sections