<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import urlparse <EOL> import urllib2 <EOL> from dateutil import parser <EOL> import jinja2 <EOL> from h . _compat import text_type <EOL> def _format_document_link ( href , title , link_text , host_or_filename ) : <EOL> """<STR_LIT>""" <EOL> if href and host_or_filename and host_or_filename in link_text : <EOL> host_or_filename = "<STR_LIT>" <EOL> elif not href and title == host_or_filename : <EOL> title = "<STR_LIT>" <EOL> def truncate ( content , length = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if len ( content ) <= length : <EOL> return content <EOL> else : <EOL> return content [ : length ] + jinja2 . Markup ( "<STR_LIT>" ) <EOL> host_or_filename = truncate ( host_or_filename ) <EOL> link_text = truncate ( link_text ) <EOL> if href and host_or_filename : <EOL> link = '<STR_LIT>' <EOL> elif href : <EOL> link = '<STR_LIT>' <EOL> else : <EOL> link = '<STR_LIT>' <EOL> if host_or_filename : <EOL> link += '<STR_LIT>' <EOL> link = link . format ( <EOL> href = jinja2 . escape ( href ) , <EOL> title = jinja2 . escape ( title ) , <EOL> link_text = jinja2 . escape ( link_text ) , <EOL> host_or_filename = jinja2 . escape ( host_or_filename ) ) <EOL> return jinja2 . Markup ( link ) <EOL> class AnnotationHTMLPresenter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , annotation ) : <EOL> self . annotation = annotation <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . annotation , attr ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . annotation [ key ] <EOL> @ property <EOL> def uri ( self ) : <EOL> return jinja2 . escape ( self . annotation . uri ) <EOL> @ property <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . uri . lower ( ) . startswith ( "<STR_LIT>" ) : <EOL> return jinja2 . escape ( self . uri . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def hostname_or_filename ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . filename : <EOL> return jinja2 . escape ( urllib2 . unquote ( self . filename ) ) <EOL> else : <EOL> hostname = urlparse . urlparse ( self . uri ) . hostname <EOL> hostname = hostname or "<STR_LIT>" <EOL> return jinja2 . escape ( hostname ) <EOL> @ property <EOL> def title ( self ) : <EOL> """<STR_LIT>""" <EOL> document_ = self . annotation . document <EOL> if document_ : <EOL> title = document_ . title <EOL> if title : <EOL> title = text_type ( title ) <EOL> return jinja2 . escape ( title ) <EOL> if self . filename : <EOL> return jinja2 . escape ( urllib2 . unquote ( self . filename ) ) <EOL> else : <EOL> return jinja2 . escape ( urllib2 . unquote ( self . uri ) ) <EOL> @ property <EOL> def href ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . uri <EOL> if ( uri . lower ( ) . startswith ( "<STR_LIT>" ) or <EOL> uri . lower ( ) . startswith ( "<STR_LIT>" ) ) : <EOL> return jinja2 . escape ( uri ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def link_text ( self ) : <EOL> """<STR_LIT>""" <EOL> title = jinja2 . escape ( self . title ) <EOL> lower = title . lower ( ) <EOL> if lower . startswith ( "<STR_LIT>" ) or lower . startswith ( "<STR_LIT>" ) : <EOL> parts = urlparse . urlparse ( title ) <EOL> return urllib2 . unquote ( parts . netloc + parts . path ) <EOL> else : <EOL> return title <EOL> @ property <EOL> def document_link ( self ) : <EOL> """<STR_LIT>""" <EOL> return _format_document_link ( <EOL> self . href , self . title , self . link_text , self . hostname_or_filename ) <EOL> @ property <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> def get_selection ( ) : <EOL> targets = self . annotation . get ( "<STR_LIT:target>" ) <EOL> if not isinstance ( targets , list ) : <EOL> return <EOL> for target in targets : <EOL> if not isinstance ( target , dict ) : <EOL> continue <EOL> selectors = target . get ( "<STR_LIT>" ) <EOL> if not isinstance ( selectors , list ) : <EOL> continue <EOL> for selector in selectors : <EOL> if not isinstance ( selector , dict ) : <EOL> continue <EOL> if "<STR_LIT>" in selector : <EOL> return selector [ "<STR_LIT>" ] <EOL> description = "<STR_LIT>" <EOL> selection = get_selection ( ) <EOL> if selection : <EOL> selection = jinja2 . escape ( selection ) <EOL> description += "<STR_LIT>" . format ( <EOL> selection = selection ) <EOL> text = self . annotation . get ( "<STR_LIT:text>" ) <EOL> if text : <EOL> text = jinja2 . escape ( text ) <EOL> description += "<STR_LIT>" . format ( text = text ) <EOL> return description <EOL> @ property <EOL> def created_day_string ( self ) : <EOL> """<STR_LIT>""" <EOL> created_string = jinja2 . escape ( self . annotation [ "<STR_LIT>" ] ) <EOL> return parser . parse ( created_string ) . strftime ( "<STR_LIT>" ) </s>
