def ensure_headings_linkable(soups):
 for soup in soups.values():
  existing_anchors = find_existing_anchors(soup)
  count = 100
  for tag in soup.find_all(_heading_re):
   if (not (tag.has_attr(u'id') or tag.has_attr(u'name'))):
    snippet = u''.join([c for c in tag.text if c.isalpha()])[:20]
    while True:
     count += 1
     candidate_id = u'heading_{0}_{1}'.format(snippet, count).lower()
     if (not (candidate_id in existing_anchors)):
      existing_anchors.add(candidate_id)
      tag[u'id'] = candidate_id
      break

def ensure_headings_linkable ( soups ) : for soup in soups . values ( ) : existing_anchors = find_existing_anchors ( soup ) count = 100 for tag in soup . find_all ( _heading_re ) : if ( not ( tag . has_attr ( u'id' ) or tag . has_attr ( u'name' ) ) ) : snippet = u'' . join ( [ c for c in tag . text if c . isalpha ( ) ] ) [ : 20 ] while True : count += 1 candidate_id = u'heading_{0}_{1}' . format ( snippet , count ) . lower ( ) if ( not ( candidate_id in existing_anchors ) ) : existing_anchors . add ( candidate_id ) tag [ u'id' ] = candidate_id break
foreach soup .