def _modify_tagids(source, add=True):
 output = []
 tagcount = 0
 if (not isinstance(source, HtmlPage)):
  source = HtmlPage(body=source)
 for element in source.parsed_body:
  if _must_add_tagid(element):
   if add:
    element.attributes[TAGID] = str(tagcount)
    tagcount += 1
   else:
    element.attributes.pop(TAGID, None)
   output.append(serialize_tag(element))
  else:
   output.append(source.body[element.start:element.end])
 return u''.join(output)

def _modify_tagids ( source , add = True ) : output = [ ] tagcount = 0 if ( not isinstance ( source , HtmlPage ) ) : source = HtmlPage ( body = source ) for element in source . parsed_body : if _must_add_tagid ( element ) : if add : element . attributes [ TAGID ] = str ( tagcount ) tagcount += 1 else : element . attributes . pop ( TAGID , None ) output . append ( serialize_tag ( element ) ) else : output . append ( source . body [ element . start : element . end ] ) return u'' . join ( output )
add or remove tags ids to/from html document .