| 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 |
| | 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], [] |
| 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. |
| 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) |