]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/Resolve_native_reStructuredText_links_to_ikiwiki_pages.mdwn
c90261dc32139b2afa0d2ec34324aeec2ed35303
[ikiwiki.git] / doc / todo / Resolve_native_reStructuredText_links_to_ikiwiki_pages.mdwn
1 I have a working minimal implementation letting the rst renderer resolve undefined native rST links to ikiwiki pages. I have posted it as one patch at:
2
3 Preview commit: http://github.com/engla/ikiwiki/commit/486fd79e520da1d462f00f40e7a90ab07e9c6fdf  
4 Repository: git://github.com/engla/ikiwiki.git  
5
6 Design issues of the patch:
7
8 Right now it changes rendering so that undefined pages (previous errors) are resolved to either ikiwiki pages or link to "#". It could be changed (trivially) so that undefined pages give the same error as before. Since it only resolves links that would previously error out, impact on current installations should be minimal.
9
10 I don't know why backlinks don't show up with the patch as it is; they are registered, but not rendered on the linked-to page.
11
12 Desing issues in general:
13
14 We resolve rST links without definition, we don't help resolving defined relative links, so we don't support specifying link name and target separately.
15
16 Many other issues with rST are of course unresolved, but some might be solved by implementing custom rST directives (which is a supported extension mechanism).
17
18 Patch follows:
19
20 ----
21 <pre>
22         From 486fd79e520da1d462f00f40e7a90ab07e9c6fdf Mon Sep 17 00:00:00 2001
23         From: Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
24         Date: Thu, 17 Sep 2009 15:18:50 +0200
25         Subject: [PATCH] rst: Resolve native reStructuredText links to ikiwiki pages
26
27         Links in rST use syntax `Like This`_ or OneWordLink_, and are
28         generally used for relative or absolue links, with an auxiliary
29         definition:
30
31         .. _`Like This`: http://ikiwiki.info
32         .. _OneWordLink: relative
33
34         We can hook into docutils to resolve unresolved links so that rST
35         links without definition can be resolved to wiki pages. This enables
36         WikiLink_ to link to [[WikiLink]] (if no .. _WikiLink is specified).
37
38         Comparing to Ikiwiki's wikilinks
39
40         [[blogging|blog]] specifies a link to the page blog, with the name
41         blogging. In rST we should use blogging_
42
43         .. _blogging: blog
44
45         *However*, note that this patch does not hook into this. What we
46         resolve in this patch is finding the appropriate "_blogging" if it is
47         not found, not resolving the address 'blog'.
48         ---
49          plugins/rst |   46 +++++++++++++++++++++++++++++++++++++++++-----
50          1 files changed, 41 insertions(+), 5 deletions(-)
51
52         diff --git a/plugins/rst b/plugins/rst
53         index a2d07eb..a74baa8 100755
54         --- a/plugins/rst
55         +++ b/plugins/rst
56         @@ -6,22 +6,58 @@
57          # based a little bit on rst.pm by Sergio Talens-Oliag, but only a little bit. :)
58          #
59          # Copyright © martin f. krafft <madduck@madduck.net>
60         +# Copyright © Ulrik Sverdrup <ulrik.sverdrup@gmail.com>, 2009
61         +#
62          # Released under the terms of the GNU GPL version 2
63          #
64         +
65          __name__ = 'rst'
66          __description__ = 'xml-rpc-based ikiwiki plugin to process RST files'
67         -__version__ = '0.3'
68         +__version__ = '0.3+'
69          __author__ = 'martin f. krafft <madduck@madduck.net>'
70          __copyright__ = 'Copyright © ' + __author__
71          __licence__ = 'GPLv2'
72          
73          from docutils.core import publish_parts;
74         +from docutils.writers import html4css1
75          from proxy import IkiWikiProcedureProxy
76          
77         -def rst2html(proxy, *kwargs):
78         -    # FIXME arguments should be treated as a hash, the order could change
79         -    # at any time and break this.
80         -    parts = publish_parts(kwargs[3], writer_name='html',
81         +class IkiwikiWriter(html4css1.Writer):
82         +    def resolve_node(self, node):
83         +        refname = node.get('refname', None)
84         +        if not refname:
85         +            return False
86         +
87         +        bestlink = self.proxy.rpc('bestlink', self.page, refname)
88         +
89         +        node.resolved = 1
90         +        node['class'] = 'wiki'
91         +        del node['refname']
92         +
93         +        if not bestlink:
94         +            rel_url = "#"
95         +        else:
96         +            rel_url = self.proxy.rpc('urlto', bestlink, self.page)
97         +            self.proxy.rpc('add_link', self.page, bestlink)
98         +        node['refuri'] = rel_url
99         +        self.proxy.rpc('debug', "Emitting link %s => %s" % (refname, rel_url))
100         +        return True
101         +
102         +    resolve_node.priority = 1
103         +
104         +    def __init__(self, proxy, page):
105         +        html4css1.Writer.__init__(self)
106         +        self.proxy = proxy
107         +        self.page = page
108         +        self.unknown_reference_resolvers = (self.resolve_node, )
109         +
110         +def rst2html(proxy, *args):
111         +    # args is a list paired by key, value, so we turn it into a dict
112         +    kwargs = dict((k, v) for k, v in zip(*[iter(args)]*2))
113         +    page = kwargs['page']
114         +
115         +    parts = publish_parts(kwargs['content'],
116         +                          writer=IkiwikiWriter(proxy, page),
117                                    settings_overrides = { 'halt_level': 6
118                                                         , 'file_insertion_enabled': 0
119                                                         , 'raw_enabled': 1
120         -- 
121         1.6.4
122
123 </pre>
124 ----