<s> from __future__ import unicode_literals <EOL> import json <EOL> from mkdocs import utils <EOL> try : <EOL> from html . parser import HTMLParser <EOL> except ImportError : <EOL> from HTMLParser import HTMLParser <EOL> class SearchIndex ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _entries = [ ] <EOL> def _find_toc_by_id ( self , toc , id_ ) : <EOL> """<STR_LIT>""" <EOL> for toc_item in toc : <EOL> if toc_item . url [ <NUM_LIT:1> : ] == id_ : <EOL> return toc_item <EOL> toc_item_r = self . _find_toc_by_id ( toc_item . children , id_ ) <EOL> if toc_item_r is not None : <EOL> return toc_item_r <EOL> def _add_entry ( self , title , text , loc ) : <EOL> """<STR_LIT>""" <EOL> self . _entries . append ( { <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT:text>' : utils . text_type ( text . strip ( ) . encode ( '<STR_LIT:utf-8>' ) , encoding = '<STR_LIT:utf-8>' ) , <EOL> '<STR_LIT:location>' : loc <EOL> } ) <EOL> def add_entry_from_context ( self , page , content , toc ) : <EOL> """<STR_LIT>""" <EOL> parser = ContentParser ( ) <EOL> parser . feed ( content ) <EOL> parser . close ( ) <EOL> abs_url = page . abs_url <EOL> self . _add_entry ( <EOL> title = page . title , <EOL> text = self . strip_tags ( content ) . rstrip ( '<STR_LIT:\n>' ) , <EOL> loc = abs_url <EOL> ) <EOL> for section in parser . data : <EOL> self . create_entry_for_section ( section , toc , abs_url ) <EOL> def create_entry_for_section ( self , section , toc , abs_url ) : <EOL> """<STR_LIT>""" <EOL> toc_item = self . _find_toc_by_id ( toc , section . id ) <EOL> if toc_item is not None : <EOL> self . _add_entry ( <EOL> title = toc_item . title , <EOL> text = u"<STR_LIT:U+0020>" . join ( section . text ) , <EOL> loc = abs_url + toc_item . url <EOL> ) <EOL> def generate_search_index ( self ) : <EOL> """<STR_LIT>""" <EOL> page_dicts = { <EOL> '<STR_LIT>' : self . _entries , <EOL> } <EOL> return json . dumps ( page_dicts , sort_keys = True , indent = <NUM_LIT:4> ) <EOL> def strip_tags ( self , html ) : <EOL> """<STR_LIT>""" <EOL> s = HTMLStripper ( ) <EOL> s . feed ( html ) <EOL> return s . get_data ( ) <EOL> class HTMLStripper ( HTMLParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> HTMLParser . __init__ ( self , * args , ** kwargs ) <EOL> self . data = [ ] <EOL> def handle_data ( self , d ) : <EOL> """<STR_LIT>""" <EOL> self . data . append ( d ) <EOL> def get_data ( self ) : <EOL> return '<STR_LIT:\n>' . join ( self . data ) <EOL> class ContentSection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text = None , id_ = None , title = None ) : <EOL> self . text = text or [ ] <EOL> self . id = id_ <EOL> self . title = title <EOL> def __eq__ ( self , other ) : <EOL> return all ( [ <EOL> self . text == other . text , <EOL> self . id == other . id , <EOL> self . title == other . title <EOL> ] ) <EOL> class ContentParser ( HTMLParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> HTMLParser . __init__ ( self , * args , ** kwargs ) <EOL> self . data = [ ] <EOL> self . section = None <EOL> self . is_header_tag = False <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> """<STR_LIT>""" <EOL> if tag not in ( [ "<STR_LIT>" % x for x in range ( <NUM_LIT:1> , <NUM_LIT:7> ) ] ) : <EOL> return <EOL> self . is_header_tag = True <EOL> self . section = ContentSection ( ) <EOL> self . data . append ( self . section ) <EOL> for attr in attrs : <EOL> if attr [ <NUM_LIT:0> ] == "<STR_LIT:id>" : <EOL> self . section . id = attr [ <NUM_LIT:1> ] <EOL> def handle_endtag ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> if tag not in ( [ "<STR_LIT>" % x for x in range ( <NUM_LIT:1> , <NUM_LIT:7> ) ] ) : <EOL> return <EOL> self . is_header_tag = False <EOL> def handle_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . section is None : <EOL> return <EOL> if self . is_header_tag : <EOL> self . section . title = data <EOL> else : <EOL> self . section . text . append ( data . rstrip ( '<STR_LIT:\n>' ) ) </s>
