<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> from docutils import nodes , utils <EOL> from docutils . parsers . rst import directives <EOL> from docutils . parsers . rst . languages import en as _fallback_language_module <EOL> DEFAULT_INTERPRETED_ROLE = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> _role_registry = { } <EOL> """<STR_LIT>""" <EOL> _roles = { } <EOL> """<STR_LIT>""" <EOL> def role ( role_name , language_module , lineno , reporter ) : <EOL> """<STR_LIT>""" <EOL> normname = role_name . lower ( ) <EOL> messages = [ ] <EOL> msg_text = [ ] <EOL> if normname in _roles : <EOL> return _roles [ normname ] , messages <EOL> if role_name : <EOL> canonicalname = None <EOL> try : <EOL> canonicalname = language_module . roles [ normname ] <EOL> except AttributeError , error : <EOL> msg_text . append ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( language_module , error ) ) <EOL> except KeyError : <EOL> msg_text . append ( '<STR_LIT>' <EOL> % ( role_name , language_module . __name__ ) ) <EOL> else : <EOL> canonicalname = DEFAULT_INTERPRETED_ROLE <EOL> if not canonicalname : <EOL> try : <EOL> canonicalname = _fallback_language_module . roles [ normname ] <EOL> msg_text . append ( '<STR_LIT>' <EOL> % role_name ) <EOL> except KeyError : <EOL> msg_text . append ( '<STR_LIT>' <EOL> % role_name ) <EOL> canonicalname = normname <EOL> if msg_text : <EOL> message = reporter . info ( '<STR_LIT:\n>' . join ( msg_text ) , line = lineno ) <EOL> messages . append ( message ) <EOL> if canonicalname in _role_registry : <EOL> role_fn = _role_registry [ canonicalname ] <EOL> register_local_role ( normname , role_fn ) <EOL> return role_fn , messages <EOL> else : <EOL> return None , messages <EOL> def register_canonical_role ( name , role_fn ) : <EOL> """<STR_LIT>""" <EOL> set_implicit_options ( role_fn ) <EOL> _role_registry [ name ] = role_fn <EOL> def register_local_role ( name , role_fn ) : <EOL> """<STR_LIT>""" <EOL> set_implicit_options ( role_fn ) <EOL> _roles [ name ] = role_fn <EOL> def set_implicit_options ( role_fn ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( role_fn , '<STR_LIT>' ) or role_fn . options is None : <EOL> role_fn . options = { '<STR_LIT:class>' : directives . class_option } <EOL> elif '<STR_LIT:class>' not in role_fn . options : <EOL> role_fn . options [ '<STR_LIT:class>' ] = directives . class_option <EOL> def register_generic_role ( canonical_name , node_class ) : <EOL> """<STR_LIT>""" <EOL> role = GenericRole ( canonical_name , node_class ) <EOL> register_canonical_role ( canonical_name , role ) <EOL> class GenericRole : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , role_name , node_class ) : <EOL> self . name = role_name <EOL> self . node_class = node_class <EOL> def __call__ ( self , role , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> set_classes ( options ) <EOL> return [ self . node_class ( rawtext , utils . unescape ( text ) , ** options ) ] , [ ] <EOL> class CustomRole : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , role_name , base_role , options = { } , content = [ ] ) : <EOL> self . name = role_name <EOL> self . base_role = base_role <EOL> self . options = None <EOL> if hasattr ( base_role , '<STR_LIT>' ) : <EOL> self . options = base_role . options <EOL> self . content = None <EOL> if hasattr ( base_role , '<STR_LIT:content>' ) : <EOL> self . content = base_role . content <EOL> self . supplied_options = options <EOL> self . supplied_content = content <EOL> def __call__ ( self , role , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> opts = self . supplied_options . copy ( ) <EOL> opts . update ( options ) <EOL> cont = list ( self . supplied_content ) <EOL> if cont and content : <EOL> cont += '<STR_LIT:\n>' <EOL> cont . extend ( content ) <EOL> return self . base_role ( role , rawtext , text , lineno , inliner , <EOL> options = opts , content = cont ) <EOL> def generic_custom_role ( role , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> """<STR_LIT>""" <EOL> set_classes ( options ) <EOL> return [ nodes . inline ( rawtext , utils . unescape ( text ) , ** options ) ] , [ ] <EOL> generic_custom_role . options = { '<STR_LIT:class>' : directives . class_option } <EOL> register_generic_role ( '<STR_LIT>' , nodes . abbreviation ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . acronym ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . emphasis ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . literal ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . strong ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . subscript ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . superscript ) <EOL> register_generic_role ( '<STR_LIT>' , nodes . title_reference ) <EOL> def pep_reference_role ( role , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> try : <EOL> pepnum = int ( text ) <EOL> if pepnum < <NUM_LIT:0> or pepnum > <NUM_LIT> : <EOL> raise ValueError <EOL> except ValueError : <EOL> msg = inliner . reporter . error ( <EOL> '<STR_LIT>' <EOL> % text , line = lineno ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> ref = ( inliner . document . settings . pep_base_url <EOL> + inliner . document . settings . pep_file_url_template % pepnum ) <EOL> set_classes ( options ) <EOL> return [ nodes . reference ( rawtext , '<STR_LIT>' + utils . unescape ( text ) , refuri = ref , <EOL> ** options ) ] , [ ] <EOL> register_canonical_role ( '<STR_LIT>' , pep_reference_role ) <EOL> def rfc_reference_role ( role , rawtext , text , lineno , inliner , <EOL> options = { } , content = [ ] ) : <EOL> try : <EOL> rfcnum = int ( text ) <EOL> if rfcnum <= <NUM_LIT:0> : <EOL> raise ValueError <EOL> except ValueError : <EOL> msg = inliner . reporter . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % text , line = lineno ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> ref = inliner . document . settings . rfc_base_url + inliner . rfc_url % rfcnum <EOL> set_classes ( options ) <EOL> node = nodes . reference ( rawtext , '<STR_LIT>' + utils . unescape ( text ) , refuri = ref , <EOL> ** options ) <EOL> return [ node ] , [ ] <EOL> register_canonical_role ( '<STR_LIT>' , rfc_reference_role ) <EOL> def raw_role ( role , rawtext , text , lineno , inliner , options = { } , content = [ ] ) : <EOL> if not inliner . document . settings . raw_enabled : <EOL> msg = inliner . reporter . warning ( '<STR_LIT>' ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> if '<STR_LIT>' not in options : <EOL> msg = inliner . reporter . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % role , line = lineno ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> set_classes ( options ) <EOL> node = nodes . raw ( rawtext , utils . unescape ( text , <NUM_LIT:1> ) , ** options ) <EOL> return [ node ] , [ ] <EOL> raw_role . options = { '<STR_LIT>' : directives . unchanged } <EOL> register_canonical_role ( '<STR_LIT>' , raw_role ) <EOL> def math_role ( role , rawtext , text , lineno , inliner , options = { } , content = [ ] ) : <EOL> i = rawtext . find ( '<STR_LIT>' ) <EOL> text = rawtext . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> node = nodes . math ( rawtext , text ) <EOL> return [ node ] , [ ] <EOL> register_canonical_role ( '<STR_LIT>' , math_role ) <EOL> def unimplemented_role ( role , rawtext , text , lineno , inliner , attributes = { } ) : <EOL> msg = inliner . reporter . error ( <EOL> '<STR_LIT>' % role , line = lineno ) <EOL> prb = inliner . problematic ( rawtext , rawtext , msg ) <EOL> return [ prb ] , [ msg ] <EOL> register_canonical_role ( '<STR_LIT:index>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT:target>' , unimplemented_role ) <EOL> register_canonical_role ( '<STR_LIT>' , <EOL> unimplemented_role ) <EOL> def set_classes ( options ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:class>' in options : <EOL> assert '<STR_LIT>' not in options <EOL> options [ '<STR_LIT>' ] = options [ '<STR_LIT:class>' ] <EOL> del options [ '<STR_LIT:class>' ] </s>
