#NEWLINE# def run( self , doc ) : #NEWLINE# #INDENT# div = etree . Element ( 'div' ) #NEWLINE# div . attrib [ 'class' ] = 'toc' #NEWLINE# header_rgx = re . compile ( '[Hh][123456]' ) #NEWLINE# self . use_anchors = ( self . config [ 'anchorlink' ] in [ 1 , '1' , True , 'True' , 'true' ] ) #NEWLINE# used_ids = set ( ) #NEWLINE# for c in doc . getiterator ( ) : #NEWLINE# #INDENT# if ( 'id' in c . attrib ) : #NEWLINE# #INDENT# used_ids . add ( c . attrib [ 'id' ] ) #NEWLINE# #UNINDENT# #UNINDENT# toc_list = [ ] #NEWLINE# marker_found = False #NEWLINE# for ( p , c ) in self . iterparent ( doc ) : #NEWLINE# #INDENT# text = '' . join ( itertext ( c ) ) . strip ( ) #NEWLINE# if ( not text ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( c . text and ( c . text . strip ( ) == self . config [ 'marker' ] ) and ( not header_rgx . match ( c . tag ) ) and ( c . tag not in [ 'pre' , 'code' ] ) ) : #NEWLINE# #INDENT# for i in range ( len ( p ) ) : #NEWLINE# #INDENT# if ( p [ i ] == c ) : #NEWLINE# #INDENT# p [ i ] = div #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# marker_found = True #NEWLINE# #UNINDENT# if header_rgx . match ( c . tag ) : #NEWLINE# #INDENT# if ( not ( 'id' in c . attrib ) ) : #NEWLINE# #INDENT# elem_id = unique ( self . config [ 'slugify' ] ( text , '-' ) , used_ids ) #NEWLINE# c . attrib [ 'id' ] = elem_id #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# elem_id = c . attrib [ 'id' ] #NEWLINE# #UNINDENT# tag_level = int ( c . tag [ ( - 1 ) ] ) #NEWLINE# toc_list . append ( { #NEWLINE# #INDENT# 'level' : tag_level , #NEWLINE# 'id' : elem_id , #NEWLINE# 'name' : text , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . add_anchor ( c , elem_id ) #NEWLINE# #UNINDENT# #UNINDENT# toc_list_nested = order_toc_list ( toc_list ) #NEWLINE# self . build_toc_etree ( div , toc_list_nested ) #NEWLINE# prettify = self . markdown . treeprocessors . get ( 'prettify' ) #NEWLINE# if prettify : #NEWLINE# #INDENT# prettify . run ( div ) #NEWLINE# #UNINDENT# if ( not marker_found ) : #NEWLINE# #INDENT# toc = self . markdown . serializer ( div ) #NEWLINE# for pp in self . markdown . postprocessors . values ( ) : #NEWLINE# #INDENT# toc = pp . run ( toc ) #NEWLINE# #UNINDENT# self . markdown . toc = toc