def anchorArticles(txt): # find all textnodes starting with Article, wrapping this in a named <a> and prepending a hoverable link to this anchor aregex=re.compile('^\s*Article\s+[0-9][0-9.,]*', re.I) nsoup = BeautifulSoup(txt) node=nsoup.find(text=aregex) while node: nodeidx=node.parent.contents.index(node) match=str(re.match(aregex,node).group()) # create named <a> name=match.replace(' ','_') a=Tag(nsoup,'a',[('name',name)]) a.insert(0,match) # create a link that is displayed if the <a> is hovered link=Tag(nsoup,'a', [('class',"anchorLink"), ('href','#'+name)]) link.insert(0,"#") # create a container for the a and the link hover=Tag(nsoup,'span',[('class','hover')]) hover.insert(0,a) hover.insert(0,link) node.parent.insert(nodeidx,hover) # cut the newly wrapped from the original node. newNode=NavigableString(node[len(match):]) node.replaceWith(newNode) node=newNode.findNext(text=aregex) return str(nsoup)