<s> import markdown <EOL> from django . apps import apps <EOL> from django . utils . encoding import smart_text <EOL> class ReferencePattern ( markdown . inlinepatterns . Pattern ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ReferencePattern , self ) . __init__ ( r'<STR_LIT>' % self . MODEL_NAME ) <EOL> def handleMatch ( self , m ) : <EOL> Model = apps . get_model ( * self . MODEL ) <EOL> instance = None <EOL> try : <EOL> instance = Model . objects . get ( pk = m . group ( <NUM_LIT:2> ) ) <EOL> except Model . DoesNotExist : <EOL> pass <EOL> e = instance and self . create_element ( instance ) <EOL> if e is None : <EOL> e = markdown . util . etree . Element ( '<STR_LIT:p>' ) <EOL> e . set ( '<STR_LIT:class>' , '<STR_LIT>' ) <EOL> e . text = '<STR_LIT>' % self . MODEL_NAME <EOL> return e <EOL> class ArticlePattern ( ReferencePattern ) : <EOL> """<STR_LIT>""" <EOL> MODEL_NAME = '<STR_LIT>' <EOL> MODEL = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def create_element ( self , article ) : <EOL> if article . status == article . PUBLISHED : <EOL> e = markdown . util . etree . Element ( '<STR_LIT:a>' ) <EOL> e . set ( '<STR_LIT>' , article . get_absolute_url ( ) ) <EOL> e . text = smart_text ( article ) <EOL> return e <EOL> class ImagePattern ( ReferencePattern ) : <EOL> """<STR_LIT>""" <EOL> MODEL_NAME = '<STR_LIT:image>' <EOL> MODEL = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def create_element ( self , image ) : <EOL> e = markdown . util . etree . Element ( '<STR_LIT>' ) <EOL> e . set ( '<STR_LIT:src>' , image . image . url ) <EOL> e . set ( '<STR_LIT>' , image . caption ) <EOL> e . set ( '<STR_LIT:title>' , image . caption ) <EOL> return e </s>
