<s> from __future__ import division , unicode_literals <EOL> from . messages import * <EOL> from . htmlhelpers import * <EOL> from . config import simplifyText <EOL> def processHeadings ( doc , scope = "<STR_LIT>" ) : <EOL> for el in findAll ( '<STR_LIT>' , doc ) : <EOL> addClass ( el , '<STR_LIT>' ) <EOL> headings = [ ] <EOL> for el in findAll ( "<STR_LIT>" , doc ) : <EOL> if scope == "<STR_LIT>" and treeAttr ( el , "<STR_LIT>" ) : <EOL> continue <EOL> headings . append ( el ) <EOL> resetHeadings ( doc , headings ) <EOL> determineHeadingLevels ( doc , headings ) <EOL> addHeadingIds ( doc , headings ) <EOL> addHeadingAlgorithms ( doc , headings ) <EOL> fixupIDs ( doc , headings ) <EOL> addHeadingBonuses ( doc , headings ) <EOL> for el in headings : <EOL> addClass ( el , '<STR_LIT>' ) <EOL> if scope == "<STR_LIT:all>" and doc . md . groupIsW3C : <EOL> checkPrivacySecurityHeadings ( findAll ( "<STR_LIT>" , doc ) ) <EOL> def resetHeadings ( doc , headings ) : <EOL> for header in headings : <EOL> if find ( "<STR_LIT>" , header ) is not None : <EOL> content = find ( "<STR_LIT>" , header ) <EOL> moveContents ( header , content ) <EOL> content = E . span ( { "<STR_LIT:class>" : "<STR_LIT:content>" } ) <EOL> moveContents ( content , header ) <EOL> appendChild ( header , content ) <EOL> def addHeadingIds ( doc , headings ) : <EOL> neededIds = set ( ) <EOL> for header in headings : <EOL> if header . get ( '<STR_LIT:id>' ) is None : <EOL> if header . get ( "<STR_LIT>" ) is None : <EOL> neededIds . add ( header ) <EOL> header . set ( '<STR_LIT:id>' , simplifyText ( textContent ( find ( "<STR_LIT>" , header ) ) ) ) <EOL> if header . get ( "<STR_LIT>" ) : <EOL> oldIDs = [ h . strip ( ) for h in header . get ( "<STR_LIT>" ) . strip ( ) . split ( "<STR_LIT:U+002C>" ) ] <EOL> for oldID in oldIDs : <EOL> appendChild ( header , E . span ( { "<STR_LIT:id>" : oldID } ) ) <EOL> if len ( neededIds ) > <NUM_LIT:0> : <EOL> warn ( "<STR_LIT>" , <EOL> "<STR_LIT:\n>" . join ( "<STR_LIT:U+0020>" + outerHTML ( el ) for el in neededIds ) ) <EOL> def checkPrivacySecurityHeadings ( headings ) : <EOL> security = False <EOL> privacy = False <EOL> for header in headings : <EOL> text = simplifyText ( textContent ( find ( "<STR_LIT>" , header ) ) ) <EOL> if text == "<STR_LIT>" : <EOL> security = True <EOL> if text == "<STR_LIT>" : <EOL> privacy = True <EOL> if text == "<STR_LIT>" or text == "<STR_LIT>" : <EOL> security = True <EOL> privacy = True <EOL> if not security and not privacy : <EOL> warn ( "<STR_LIT>" ) <EOL> elif not security : <EOL> warn ( "<STR_LIT>" ) <EOL> elif not privacy : <EOL> warn ( "<STR_LIT>" ) <EOL> def addHeadingAlgorithms ( doc , headings ) : <EOL> for header in headings : <EOL> if header . get ( '<STR_LIT>' ) == "<STR_LIT>" : <EOL> header . set ( '<STR_LIT>' , textContent ( header ) . strip ( ) ) <EOL> def determineHeadingLevels ( doc , headings ) : <EOL> headerLevel = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> def incrementLevel ( level ) : <EOL> headerLevel [ level - <NUM_LIT:2> ] += <NUM_LIT:1> <EOL> for i in range ( level - <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> headerLevel [ i ] = <NUM_LIT:0> <EOL> def printLevel ( ) : <EOL> return '<STR_LIT:.>' . join ( unicode ( x ) for x in headerLevel if x > <NUM_LIT:0> ) <EOL> skipLevel = float ( '<STR_LIT>' ) <EOL> for header in headings : <EOL> level = int ( header . tag [ - <NUM_LIT:1> ] ) <EOL> if ( header . get ( '<STR_LIT>' ) ) : <EOL> del header . attrib [ '<STR_LIT>' ] <EOL> if hasClass ( header , "<STR_LIT>" ) or textContent ( header ) [ <NUM_LIT:0> : <NUM_LIT:9> ] . lower ( ) == "<STR_LIT>" : <EOL> skipLevel = min ( level , skipLevel ) <EOL> continue <EOL> if skipLevel < level : <EOL> continue <EOL> else : <EOL> skipLevel = float ( '<STR_LIT>' ) <EOL> incrementLevel ( level ) <EOL> header . set ( '<STR_LIT>' , printLevel ( ) ) <EOL> def addHeadingBonuses ( doc , headings ) : <EOL> for header in headings : <EOL> if header . get ( "<STR_LIT>" ) is not None : <EOL> secno = E . span ( { "<STR_LIT:class>" : "<STR_LIT>" } , header . get ( '<STR_LIT>' ) + '<STR_LIT>' ) <EOL> header . insert ( <NUM_LIT:0> , secno ) </s>
