<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import re <EOL> import sys <EOL> from docutils import nodes , utils <EOL> from docutils . transforms import TransformError , Transform <EOL> class SectNum ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> """<STR_LIT>""" <EOL> def apply ( self ) : <EOL> self . maxdepth = self . startnode . details . get ( '<STR_LIT>' , None ) <EOL> self . startvalue = self . startnode . details . get ( '<STR_LIT:start>' , <NUM_LIT:1> ) <EOL> self . prefix = self . startnode . details . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . suffix = self . startnode . details . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . startnode . parent . remove ( self . startnode ) <EOL> if self . document . settings . sectnum_xform : <EOL> if self . maxdepth is None : <EOL> self . maxdepth = sys . maxsize <EOL> self . update_section_numbers ( self . document ) <EOL> else : <EOL> self . document . settings . sectnum_depth = self . maxdepth <EOL> self . document . settings . sectnum_start = self . startvalue <EOL> self . document . settings . sectnum_prefix = self . prefix <EOL> self . document . settings . sectnum_suffix = self . suffix <EOL> def update_section_numbers ( self , node , prefix = ( ) , depth = <NUM_LIT:0> ) : <EOL> depth += <NUM_LIT:1> <EOL> if prefix : <EOL> sectnum = <NUM_LIT:1> <EOL> else : <EOL> sectnum = self . startvalue <EOL> for child in node : <EOL> if isinstance ( child , nodes . section ) : <EOL> numbers = prefix + ( str ( sectnum ) , ) <EOL> title = child [ <NUM_LIT:0> ] <EOL> generated = nodes . generated ( <EOL> '<STR_LIT>' , ( self . prefix + '<STR_LIT:.>' . join ( numbers ) + self . suffix <EOL> + '<STR_LIT>' * <NUM_LIT:3> ) , <EOL> classes = [ '<STR_LIT>' ] ) <EOL> title . insert ( <NUM_LIT:0> , generated ) <EOL> title [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if depth < self . maxdepth : <EOL> self . update_section_numbers ( child , numbers , depth ) <EOL> sectnum += <NUM_LIT:1> <EOL> class Contents ( Transform ) : <EOL> """<STR_LIT>""" <EOL> default_priority = <NUM_LIT> <EOL> def apply ( self ) : <EOL> try : <EOL> toc_by_writer = self . document . settings . use_latex_toc <EOL> except AttributeError : <EOL> toc_by_writer = False <EOL> details = self . startnode . details <EOL> if '<STR_LIT>' in details : <EOL> startnode = self . startnode . parent . parent <EOL> while not ( isinstance ( startnode , nodes . section ) <EOL> or isinstance ( startnode , nodes . document ) ) : <EOL> startnode = startnode . parent <EOL> else : <EOL> startnode = self . document <EOL> self . toc_id = self . startnode . parent [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' in details : <EOL> self . backlinks = details [ '<STR_LIT>' ] <EOL> else : <EOL> self . backlinks = self . document . settings . toc_backlinks <EOL> if toc_by_writer : <EOL> self . startnode . parent . attributes . update ( details ) <EOL> self . startnode . parent . remove ( self . startnode ) <EOL> else : <EOL> contents = self . build_contents ( startnode ) <EOL> if len ( contents ) : <EOL> self . startnode . replace_self ( contents ) <EOL> else : <EOL> self . startnode . parent . parent . remove ( self . startnode . parent ) <EOL> def build_contents ( self , node , level = <NUM_LIT:0> ) : <EOL> level += <NUM_LIT:1> <EOL> sections = [ sect for sect in node if isinstance ( sect , nodes . section ) ] <EOL> entries = [ ] <EOL> autonum = <NUM_LIT:0> <EOL> depth = self . startnode . details . get ( '<STR_LIT>' , sys . maxsize ) <EOL> for section in sections : <EOL> title = section [ <NUM_LIT:0> ] <EOL> auto = title . get ( '<STR_LIT>' ) <EOL> entrytext = self . copy_and_filter ( title ) <EOL> reference = nodes . reference ( '<STR_LIT>' , '<STR_LIT>' , refid = section [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> * entrytext ) <EOL> ref_id = self . document . set_id ( reference ) <EOL> entry = nodes . paragraph ( '<STR_LIT>' , '<STR_LIT>' , reference ) <EOL> item = nodes . list_item ( '<STR_LIT>' , entry ) <EOL> if ( self . backlinks in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> and title . next_node ( nodes . reference ) is None ) : <EOL> if self . backlinks == '<STR_LIT>' : <EOL> title [ '<STR_LIT>' ] = ref_id <EOL> elif self . backlinks == '<STR_LIT>' : <EOL> title [ '<STR_LIT>' ] = self . toc_id <EOL> if level < depth : <EOL> subsects = self . build_contents ( section , level ) <EOL> item += subsects <EOL> entries . append ( item ) <EOL> if entries : <EOL> contents = nodes . bullet_list ( '<STR_LIT>' , * entries ) <EOL> if auto : <EOL> contents [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> return contents <EOL> else : <EOL> return [ ] <EOL> def copy_and_filter ( self , node ) : <EOL> """<STR_LIT>""" <EOL> visitor = ContentsFilter ( self . document ) <EOL> node . walkabout ( visitor ) <EOL> return visitor . get_entry_text ( ) <EOL> class ContentsFilter ( nodes . TreeCopyVisitor ) : <EOL> def get_entry_text ( self ) : <EOL> return self . get_tree_copy ( ) . children <EOL> def visit_citation_reference ( self , node ) : <EOL> raise nodes . SkipNode <EOL> def visit_footnote_reference ( self , node ) : <EOL> raise nodes . SkipNode <EOL> def visit_image ( self , node ) : <EOL> if node . hasattr ( '<STR_LIT>' ) : <EOL> self . parent . append ( nodes . Text ( node [ '<STR_LIT>' ] ) ) <EOL> raise nodes . SkipNode <EOL> def ignore_node_but_process_children ( self , node ) : <EOL> raise nodes . SkipDeparture <EOL> visit_interpreted = ignore_node_but_process_children <EOL> visit_problematic = ignore_node_but_process_children <EOL> visit_reference = ignore_node_but_process_children <EOL> visit_target = ignore_node_but_process_children </s>
