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