import re g_lines = ['a','b','c'] g_lines02 = [] # есть ли начальные пробелы и возращает пару индент и оставш строка def find_indentation(line): return INDENT(re.match(INDENT.regex, line)) def find_indentation02(): global g_lines #del g_lines[2] print ( "find_indentation02" ) for lin in g_lines: print ( lin ) return "" def get_prefixed_lines02( in_body ): global g_lines, g_lines02 # # массив вложенностей global mass_vloz, mass_indent, mass_tail mass_vloz = [] mass_indent = [] mass_tail = [] # # индекс вложенности ind_vloz = 0 # print (" *** get_prefixed_lines02 ", g_lines[1] ) # pack lines in massiv with striping \n g_lines = [line.rstrip('\n') for line in in_body] len_m = len(g_lines) # колич строк # расширение массивов for ii in range( len_m ): g_lines02.append("") mass_vloz.append(0) mass_indent.append(0) mass_tail.append("") print ( g_lines02[0], mass_vloz[1] ) # # # шнуруем массив for lin in g_lines: ind_m = g_lines.index(lin) # текущ индекс массива # # разбивка на индент и остав. строку result = re.match ( '(\s*)(.*)', lin ) vindent = result.group(1) stroka = result.group(2) mass_indent[ind_m] = vindent # колич нач пробелов #print ('len = ', len(vindent)) # # если # if stroka[0] == '#': #print ('^#^') ind_vloz = ind_vloz+1 temp = re.sub( '#', '
' mass_tail[ind_m] = '
' # занесение g_lines02 [ind_m] = [result.group(1), stroka ] # # mass_vloz[ind_m] = ind_vloz # if (ind_m > 0) : if ( (mass_indent[ind_m]) <= (mass_indent[ind_m-1]) ) : #print ( 'mas ind= ', mass_indent[ind_m] ) v_ind = ind_m my_tail = "" while ( ( v_ind >= 0 ) and ( mass_tail[v_ind]=="" ) ): v_ind = v_ind-1 #print ( " & " ) my_tail = mass_tail[v_ind] if ( my_tail != "" ) : print ( g_lines02[v_ind][0]+my_tail ) mass_tail[v_ind] = "" # # print ( result.group(1)+stroka ) # print ( g_lines02[0] ) return "" # main prog # if __name__ == "__main__": # if file name is given convert file, else convert stdin import sys shpaml_text = open("D:/Temp/proba_shpaml.txt") print (shpaml_text) #print (convert_text(shpaml_text) ) get_prefixed_lines02( shpaml_text ) # end