Show
Ignore:
Timestamp:
08/31/07 10:43:27 (15 months ago)
Author:
asterix
Message:

[sgala and I] better support of sizes in XHTML, simplify rst generator

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/gajim_0.11.1/src/common/rst_xhtml_generator.py

    r7940 r8665  
    2222        from docutils.parsers.rst.roles import set_classes 
    2323except: 
     24        print "Requires docutils 0.4 for set_classes to be available" 
    2425        def create_xhtml(text): 
    2526                return None 
    2627else: 
    27         def jep_reference_role(role, rawtext, text, lineno, inliner, 
    28                 options={}, content=[]): 
    29                 '''Role to make handy references to Jabber Enhancement Proposals (JEP). 
     28        def pos_int_validator(text): 
     29                """Validates that text can be evaluated as a positive integer.""" 
     30                result = int(text) 
     31                if result < 0: 
     32                        raise ValueError("Error: value '%(text)s' " 
     33                                                        "must be a positive integer") 
     34                return result 
    3035 
    31                 Use as :JEP:`71` (or jep, or jep-reference). 
    32                 Modeled after the sample in docutils documentation. 
     36        def generate_uri_role( role_name, aliases, 
     37                                        anchor_text, base_url, 
     38                                        interpret_url, validator): 
     39                '''Creates and register a uri based "interpreted role". 
     40 
     41                Those are similar to the RFC, and PEP ones, and take 
     42                role_name: 
     43                        name that will be registered 
     44                aliases: 
     45                        list of alternate names 
     46                anchor_text: 
     47                        text that will be used, together with the role 
     48                base_url: 
     49                        base url for the link 
     50                interpret_url: 
     51                        this, modulo the validated text, will be added to it 
     52                validator: 
     53                        should return the validated text, or raise ValueError  
    3354                ''' 
     55                def uri_reference_role(role, rawtext, text, lineno, inliner, 
     56                        options={}, content=[]): 
     57                        try: 
     58                                valid_text = validator(text) 
     59                        except ValueError, e: 
     60                                msg = inliner.reporter.error( e.message % dict(text=text), line=lineno) 
     61                                prb = inliner.problematic(rawtext, rawtext, msg) 
     62                                return [prb], [msg] 
     63                        ref = base_url + interpret_url % valid_text 
     64                        set_classes(options) 
     65                        node = nodes.reference(rawtext, anchor_text + utils.unescape(text), refuri=ref, 
     66                                        **options) 
     67                        return [node], [] 
    3468 
    35                 jep_base_url = 'http://www.jabber.org/jeps/' 
    36                 jep_url = 'jep-%04d.html' 
    37                 try: 
    38                         jepnum = int(text) 
    39                         if jepnum <= 0: 
    40                                 raise ValueError 
    41                 except ValueError: 
    42                         msg = inliner.reporter.error( 
    43                         'JEP number must be a number greater than or equal to 1; ' 
    44                         '"%s" is invalid.' % text, line=lineno) 
    45                         prb = inliner.problematic(rawtext, rawtext, msg) 
    46                         return [prb], [msg] 
    47                 ref = jep_base_url + jep_url % jepnum 
    48                 set_classes(options) 
    49                 node = nodes.reference(rawtext, 'JEP ' + utils.unescape(text), refuri=ref, 
    50                         **options) 
    51                 return [node], [] 
     69                uri_reference_role.__doc__ = """Role to make handy references to URIs. 
    5270 
    53         roles.register_canonical_role('jep-reference', jep_reference_role) 
    54         from docutils.parsers.rst.languages.en import roles 
    55         roles['jep-reference'] = 'jep-reference' 
    56         roles['jep'] = 'jep-reference' 
     71                        Use as :%(role_name)s:`71` (or any of %(aliases)s). 
     72                        It will use %(base_url)s+%(interpret_url)s 
     73                        validator should throw a ValueError, containing optionally 
     74                        a %%(text)s format, if the interpreted text is not valid. 
     75                        """ % locals() 
     76                roles.register_canonical_role(role_name, uri_reference_role) 
     77                from docutils.parsers.rst.languages import en 
     78                en.roles[role_name] = role_name 
     79                for alias in aliases: 
     80                        en.roles[alias] = role_name 
     81 
     82        generate_uri_role('xep-reference', ('jep', 'xep'), 
     83                                'XEP #', 'http://www.xmpp.org/extensions/', 'xep-%04d.html', 
     84                                pos_int_validator) 
     85        generate_uri_role('gajim-ticket-reference', ('ticket','gtrack'), 
     86                                'Gajim Ticket #', 'http://trac.gajim.org/ticket/', '%d', 
     87                                pos_int_validator) 
    5788 
    5889        class HTMLGenerator: 
     
    109140 
    110141if __name__ == '__main__': 
    111         print Generator.create_xhtml(''' 
     142        print "test 1\n", Generator.create_xhtml(""" 
    112143test:: 
    113144 
     
    119150this `` should    trigger`` should trigger the &nbsp; problem. 
    120151 
    121 ''') 
    122         print Generator.create_xhtml(''' 
     152""") 
     153        print "test 2\n", Generator.create_xhtml(""" 
    123154*test1 
    124155 
    125156test2_ 
    126 ''') 
     157""") 
     158        print "test 3\n", Generator.create_xhtml(""":ticket:`316` implements :xep:`71`""")