comparison doc/ext.py @ 912:0354b682c289

Added ext.py from Theano/doc, with some changes
author fsavard
date Thu, 18 Mar 2010 15:31:18 -0400
parents
children 7edb78ba4c5b
comparison
equal deleted inserted replaced
911:fdb63e4e042d 912:0354b682c289
1
2 import sys
3 import re
4 import os
5 from docutils import nodes, utils
6 from docutils.parsers.rst import roles
7 import epydoc.docwriter.xlink as xlink
8
9 def role_fn(name, rawtext, text, lineno, inliner,
10 options={}, content=[]):
11 node = nodes.reference(rawtext, text, refuri = "http://pylearn.org/theano/wiki/%s" % text)
12 return [node], []
13
14
15 _TARGET_RE = re.compile(r'^(.*?)\s*<(?:URI:|URL:)?([^<>]+)>$')
16 def create_api_role(name, problematic):
17 """
18 Create and register a new role to create links for an API documentation.
19
20 Create a role called `name`, which will use the URL resolver registered as
21 ``name`` in `api_register` to create a link for an object.
22
23 :Parameters:
24 `name` : `str`
25 name of the role to create.
26 `problematic` : `bool`
27 if True, the registered role will create problematic nodes in
28 case of failed references. If False, a warning will be raised
29 anyway, but the output will appear as an ordinary literal.
30 """
31 def resolve_api_name(n, rawtext, text, lineno, inliner,
32 options={}, content=[]):
33
34 # Check if there's separate text & targets
35 m = _TARGET_RE.match(text)
36 if m: text, target = m.groups()
37 else: target = text
38
39 # node in monotype font
40 text = utils.unescape(text)
41 node = nodes.literal(rawtext, text, **options)
42
43 # Get the resolver from the register and create an url from it.
44 try:
45 url = xlink.api_register[name].get_url(target)
46 except IndexError, exc:
47 msg = inliner.reporter.warning(str(exc), line=lineno)
48 if problematic:
49 prb = inliner.problematic(rawtext, text, msg)
50 return [prb], [msg]
51 else:
52 return [node], []
53
54 if url is not None:
55 node = nodes.reference(rawtext, '', node, refuri=url, **options)
56 return [node], []
57
58 roles.register_local_role(name, resolve_api_name)
59
60
61 def setup(app):
62
63 try:
64 xlink.set_api_file('api', os.path.join(app.outdir, 'api', 'api-objects.txt'))
65 apiroot = os.getenv('THEANO_API_ROOT')
66 if not apiroot:
67 apiroot = os.path.join(os.path.realpath('api'), '')
68 xlink.set_api_root('api', apiroot)
69 #xlink.create_api_role('api', True)
70 create_api_role('api', True)
71 except IOError:
72 print >>sys.stderr, 'WARNING: Could not find api file! API links will not work.'
73
74 app.add_role("wiki", role_fn)