]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/todo/Resolve_native_reStructuredText_links_to_ikiwiki_pages.mdwn
response
[git.ikiwiki.info.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:
3 Preview commit: http://github.com/engla/ikiwiki/commit/486fd79e520da1d462f00f40e7a90ab07e9c6fdf  
4 Repository: git://github.com/engla/ikiwiki.git  
6 Design issues of the patch:
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.
10 The page is rST-parsed once in 'scan' and once in 'htmlize' (the first to generate backlinks). Can the parse output be safely reused?
12 > The page content fed to htmlize may be different than that fed to scan,
13 > as directives can change the content. If you cached the input and output
14 > at scan time, you could reuse the cached data at htmlize time for inputs
15 > that are the same -- but that could be a very big cache! --[[Joey]] 
17 Desing issues in general:
19 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.
21 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).
23 Patch follows:
25 ----
26 <pre>
27         From 486fd79e520da1d462f00f40e7a90ab07e9c6fdf Mon Sep 17 00:00:00 2001
28         From: Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
29         Date: Thu, 17 Sep 2009 15:18:50 +0200
30         Subject: [PATCH] rst: Resolve native reStructuredText links to ikiwiki pages
32         Links in rST use syntax `Like This`_ or OneWordLink_, and are
33         generally used for relative or absolue links, with an auxiliary
34         definition:
36         .. _`Like This`: http://ikiwiki.info
37         .. _OneWordLink: relative
39         We can hook into docutils to resolve unresolved links so that rST
40         links without definition can be resolved to wiki pages. This enables
41         WikiLink_ to link to [[WikiLink]] (if no .. _WikiLink is specified).
43         Comparing to Ikiwiki's wikilinks
45         [[blogging|blog]] specifies a link to the page blog, with the name
46         blogging. In rST we should use blogging_
48         .. _blogging: blog
50         *However*, note that this patch does not hook into this. What we
51         resolve in this patch is finding the appropriate "_blogging" if it is
52         not found, not resolving the address 'blog'.
53         ---
54          plugins/rst |   46 +++++++++++++++++++++++++++++++++++++++++-----
55          1 files changed, 41 insertions(+), 5 deletions(-)
57         diff --git a/plugins/rst b/plugins/rst
58         index a2d07eb..a74baa8 100755
59         --- a/plugins/rst
60         +++ b/plugins/rst
61         @@ -6,22 +6,58 @@
62          # based a little bit on rst.pm by Sergio Talens-Oliag, but only a little bit. :)
63          #
64          # Copyright © martin f. krafft <madduck@madduck.net>
65         +# Copyright © Ulrik Sverdrup <ulrik.sverdrup@gmail.com>, 2009
66         +#
67          # Released under the terms of the GNU GPL version 2
68          #
69         +
70          __name__ = 'rst'
71          __description__ = 'xml-rpc-based ikiwiki plugin to process RST files'
72         -__version__ = '0.3'
73         +__version__ = '0.3+'
74          __author__ = 'martin f. krafft <madduck@madduck.net>'
75          __copyright__ = 'Copyright © ' + __author__
76          __licence__ = 'GPLv2'
77          
78          from docutils.core import publish_parts;
79         +from docutils.writers import html4css1
80          from proxy import IkiWikiProcedureProxy
81          
82         -def rst2html(proxy, *kwargs):
83         -    # FIXME arguments should be treated as a hash, the order could change
84         -    # at any time and break this.
85         -    parts = publish_parts(kwargs[3], writer_name='html',
86         +class IkiwikiWriter(html4css1.Writer):
87         +    def resolve_node(self, node):
88         +        refname = node.get('refname', None)
89         +        if not refname:
90         +            return False
91         +
92         +        bestlink = self.proxy.rpc('bestlink', self.page, refname)
93         +
94         +        node.resolved = 1
95         +        node['class'] = 'wiki'
96         +        del node['refname']
97         +
98         +        if not bestlink:
99         +            rel_url = "#"
100         +        else:
101         +            rel_url = self.proxy.rpc('urlto', bestlink, self.page)
102         +            self.proxy.rpc('add_link', self.page, bestlink)
103         +        node['refuri'] = rel_url
104         +        self.proxy.rpc('debug', "Emitting link %s => %s" % (refname, rel_url))
105         +        return True
106         +
107         +    resolve_node.priority = 1
108         +
109         +    def __init__(self, proxy, page):
110         +        html4css1.Writer.__init__(self)
111         +        self.proxy = proxy
112         +        self.page = page
113         +        self.unknown_reference_resolvers = (self.resolve_node, )
114         +
115         +def rst2html(proxy, *args):
116         +    # args is a list paired by key, value, so we turn it into a dict
117         +    kwargs = dict((k, v) for k, v in zip(*[iter(args)]*2))
118         +    page = kwargs['page']
119         +
120         +    parts = publish_parts(kwargs['content'],
121         +                          writer=IkiwikiWriter(proxy, page),
122                                    settings_overrides = { 'halt_level': 6
123                                                         , 'file_insertion_enabled': 0
124                                                         , 'raw_enabled': 1
125         -- 
126         1.6.4
128 </pre>
129 ----