everything in src for packaging purposes
[tiramisu.git] / doc / rst2html.py
1 #!/usr/bin/python
2 # unproudly borrowed from David Goodger's rst2html.py
3
4 """
5 A minimal front end to the Docutils Publisher, producing HTML.
6 """
7
8 try:
9     import locale
10     locale.setlocale(locale.LC_ALL, '')
11 except:
12     pass
13
14 from docutils.core import publish_cmdline, default_description
15 # ____________________________________________________________
16 from docutils import nodes, utils
17 from docutils.parsers.rst import roles
18
19 """
20 description of the new roles:
21
22 `:api:` : link to the code
23
24 - code.py becomes api/code.html
25 - code.Code.code_test becomes api/code.Code.code_test.html
26 - code.Code() becomes api/code.Code.html
27
28 `:doc:`a link to an internal file
29 example become example.html
30
31 ref: link with anchor as in an external file 
32
33 :ref:`toto#titi` becomes toto.html#titi 
34 """
35 from os.path import splitext
36
37 def api_reference_role(role, rawtext, text, lineno, inliner,
38                     options={}, content=[]):
39     basename = text
40     if "(" in text:
41         basename = text.split("(")[0]
42     if ".py" in text:
43         basename = splitext(text)[0]                
44     if "test_" in text:
45         refuri = "api/" + "tiramisu.test." + basename + '.html'
46     else:
47         refuri = "api/" + "tiramisu." + basename + '.html'
48     roles.set_classes(options)
49     node = nodes.reference(rawtext, utils.unescape(text), refuri=refuri,
50                         **options)
51     return [node], []
52
53 roles.register_local_role('api', api_reference_role)
54
55 def doc_reference_role(role, rawtext, text, lineno, inliner,
56                     options={}, content=[]):
57     refuri = text + '.html'
58     roles.set_classes(options)
59     node = nodes.reference(rawtext, utils.unescape(text), refuri=refuri,
60                         **options)
61     return [node], []
62
63 roles.register_local_role('doc', doc_reference_role)
64
65 def ref_reference_role(role, rawtext, text, lineno, inliner,
66                     options={}, content=[]):
67     fname, anchor = text.split('#')
68     refuri = fname + '.html#' + anchor 
69     roles.set_classes(options)
70     node = nodes.reference(rawtext, utils.unescape(anchor), refuri=refuri,
71                         **options)
72     return [node], []
73
74 roles.register_local_role('ref', ref_reference_role)
75
76 # ____________________________________________________________
77
78 description = ('Generates (X)HTML documents from standalone reStructuredText '
79                'sources.  ' + default_description)
80
81 publish_cmdline(writer_name='html', description=description)
82