<s> from docutils . parsers . rst import Directive , directives <EOL> from docutils import nodes <EOL> from acrylamid . lib . requests import get , HTTPError , URLError <EOL> from acrylamid import log <EOL> class Gist ( Directive ) : <EOL> """<STR_LIT>""" <EOL> required_arguments = <NUM_LIT:1> <EOL> optional_arguments = <NUM_LIT:1> <EOL> option_spec = { '<STR_LIT:file>' : directives . unchanged } <EOL> final_argument_whitespace = True <EOL> has_content = False <EOL> def get_raw_gist_with_filename ( self , gistID , filename ) : <EOL> url = "<STR_LIT>" % ( gistID , filename ) <EOL> try : <EOL> return get ( url ) . read ( ) <EOL> except ( URLError , HTTPError ) as e : <EOL> log . exception ( '<STR_LIT>' % ( url , e ) ) <EOL> return '<STR_LIT>' <EOL> def get_raw_gist ( self , gistID ) : <EOL> url = "<STR_LIT>" % ( gistID ) <EOL> try : <EOL> return get ( url ) . read ( ) <EOL> except ( URLError , HTTPError ) as e : <EOL> log . exception ( '<STR_LIT>' % ( url , e ) ) <EOL> return '<STR_LIT>' <EOL> def run ( self ) : <EOL> gistID = self . arguments [ <NUM_LIT:0> ] . strip ( ) <EOL> if '<STR_LIT:file>' in self . options : <EOL> filename = self . options [ '<STR_LIT:file>' ] <EOL> rawGist = ( self . get_raw_gist_with_filename ( gistID , filename ) ) <EOL> embedHTML = '<STR_LIT>' % ( gistID , filename ) <EOL> else : <EOL> rawGist = ( self . get_raw_gist ( gistID ) ) <EOL> embedHTML = '<STR_LIT>' % gistID <EOL> return [ nodes . raw ( '<STR_LIT>' , embedHTML , format = '<STR_LIT:html>' ) , <EOL> nodes . raw ( '<STR_LIT>' , '<STR_LIT>' , format = '<STR_LIT:html>' ) , <EOL> nodes . literal_block ( '<STR_LIT>' , rawGist ) , <EOL> nodes . raw ( '<STR_LIT>' , '<STR_LIT>' , format = '<STR_LIT:html>' ) ] <EOL> def register ( roles , directives ) : <EOL> directives . register_directive ( '<STR_LIT>' , Gist ) </s>
