<s> from __future__ import print_function , unicode_literals <EOL> from itertools import chain <EOL> try : <EOL> from urllib import unquote <EOL> except ImportError : <EOL> from urllib . parse import unquote <EOL> import html5lib <EOL> from isso . compat import map , filter , PY2K <EOL> if PY2K : <EOL> from xml . dom . minidom import NamedNodeMap <EOL> NamedNodeMap . __contains__ = lambda self , key : self . has_key ( key ) <EOL> def thread ( data , default = u"<STR_LIT>" , id = None ) : <EOL> """<STR_LIT>""" <EOL> html = html5lib . parse ( data , treebuilder = "<STR_LIT>" ) <EOL> assert html . lastChild . nodeName == "<STR_LIT:html>" <EOL> html = html . lastChild <EOL> el = list ( filter ( lambda i : i . attributes [ "<STR_LIT:id>" ] . value == "<STR_LIT>" , <EOL> filter ( lambda i : "<STR_LIT:id>" in i . attributes , <EOL> chain ( * map ( html . getElementsByTagName , ( "<STR_LIT>" , "<STR_LIT>" ) ) ) ) ) ) <EOL> if not el : <EOL> return id , default <EOL> el = el [ <NUM_LIT:0> ] <EOL> visited = [ ] <EOL> def recurse ( node ) : <EOL> for child in node . childNodes : <EOL> if child . nodeType != child . ELEMENT_NODE : <EOL> continue <EOL> if child . nodeName . upper ( ) == "<STR_LIT>" : <EOL> return child <EOL> if child not in visited : <EOL> return recurse ( child ) <EOL> def gettext ( rv ) : <EOL> for child in rv . childNodes : <EOL> if child . nodeType == child . TEXT_NODE : <EOL> yield child . nodeValue <EOL> if child . nodeType == child . ELEMENT_NODE : <EOL> for item in gettext ( child ) : <EOL> yield item <EOL> try : <EOL> id = unquote ( el . attributes [ "<STR_LIT>" ] . value ) <EOL> except ( KeyError , AttributeError ) : <EOL> pass <EOL> try : <EOL> return id , unquote ( el . attributes [ "<STR_LIT>" ] . value ) <EOL> except ( KeyError , AttributeError ) : <EOL> pass <EOL> while el is not None : <EOL> visited . append ( el ) <EOL> rv = recurse ( el ) <EOL> if rv : <EOL> return id , '<STR_LIT>' . join ( gettext ( rv ) ) . strip ( ) <EOL> el = el . parentNode <EOL> return id , default </s>
