]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/format_escape.mdwn
web commit by tschwinge: Response.
[ikiwiki.git] / doc / todo / format_escape.mdwn
1 Since some preprocessor directives insert raw HTML, it would be good to 
2 specify, per-format, how to pass HTML so that it goes through the format 
3 OK. With Markdown we cross our fingers; with reST we use the "raw" 
4 directive.
5
6 I added an extra named parameter to the htmlize hook, which feels sort of
7 wrong, since none of the other hooks take parameters. Let me know what 
8 you think. --Ethan
9
10 Seems fairly reasonable, actually. Shouldn't the `$type` come from `$page`
11 instead of `$destpage` though? Only other obvious change is to make the
12 escape parameter optional, and only call it if set. --[[Joey]]
13
14 > I couldn't figure out what to make it from, but thinking it through, 
15 > yeah, it should be $page. Revised patch follows. --Ethan
16
17 >> I've updated the patch some more, but I think it's incomplete. ikiwiki
18 >> emits raw html when expanding WikiLinks too, and it would need to escape
19 >> those. Assuming that escaping html embedded in the middle of a sentence
20 >> works.. --[[Joey]]
21
22 >>> Revised again. I get around this by making another hook, htmlescapelink,
23 >>> which is called to generate links in whatever language. In addition, it 
24 >>> doesn't (can't?) generate
25 >>> spans, and it doesn't handle inlineable image links. If these were 
26 >>> desired, the approach to take would probably be to use substitution
27 >>> definitions, which would require generating two bits of code for each
28 >>> link/html snippet, and putting one at the end of the paragraph (or maybe
29 >>> the document?).
30 >>> To specify that (for example) Discussion links are meant to be HTML and
31 >>> not rst or whatever, I added a "genhtml" parameter to htmllink. It seems
32 >>> to work -- see <http://ikidev.betacantrips.com/blah.html> for an example.
33 >>> --Ethan
34
35 ## Alternative solution
36
37 [Here](http://www.jk.fr.eu.org/ikiwiki/format-escapes-2.diff) is a patch
38 largely inspired from the one below, which is up to date and written with
39 [[todo/multiple_output_formats]] in mind. "htmlize" hooks are generalized
40 to "convert" ones, which can be registered for any pair of filename
41 extensions.
42
43 Preprocessor directives are allowed to return the content to be inserted
44 as a hash, in any format they want, provided they provide htmlize hooks for it.
45 Pseudo filename extensions (such as `"_link"`) can also be introduced,
46 which aren't used as real extensions but provide useful intermediate types.
47
48 --[[JeremieKoenig]]
49
50 > Wow, this is in many ways a beautiful patch. I did notice one problem,
51 > if a link is converted to rst and then from there to a hyperlink, the
52 > styling info usially added to such a link is lost. I wonder if it would
53 > be better to lose _link stuff and just create link html that is fed into
54 > the rst,html converter. Other advantage to doing that is that link
55 > creation has a rather complex interface, with selflink, attrs, url, and
56 > content parameters.
57
58 > --[[Joey]]
59
60 >> Thanks for the compliment. I must confess that I'm not too familiar with
61 >> rst. I am using this todo item somewhat as a pretext to get the conversion
62 >> stuff in, which I need to implement some other stuff. As a result I was
63 >> less careful with the rst plugin than with the rest of the patch.
64 >> I just updated the patch to fix some other problems which I found with
65 >> more testing, and document the current limitations.
66
67 >> Rst cannot embed raw html in the middle of a paragraph, which is why
68 >> "_link" was necessary. Rst links are themselves tricky and can't be made to
69 >> work inside of words without knowledge about the context.
70 >> Both problems could be fixed by inserting marks instead of the html/link,
71 >> which would be replaced at a later stage (htmlize, format), somewhat
72 >> similiar to the way the toc plugin works. When I get more time I will
73 >> try to fix the remaining glitches this way.
74
75 >> Also, I think it would be useful if ikiwiki had an option to export
76 >> the preprocessed source. This way you can use docutils to convert your
77 >> rst documents to other formats. Raw html would be loosed in such a
78 >> process (both with directives and marks), which is another
79 >> argument for `"_link"` and other intermediate forms. I think I can
80 >> come up with a way for rst's convert_link to be used only for export
81 >> purposes, though.
82
83 >> --[[JeremieKoenig]]
84
85 > Another problem with this approach is when there is some html (say a
86 > table), that contains a wikilink. If the link is left up to the markup
87 > lamguage to handle, it will never convert it to a link, since the table
88 > will be processed as a chunk of raw html.
89 > --[[Joey]]
90
91 ## Original patch
92 [[tag patch]]
93
94 <pre>
95 Index: debian/changelog
96 ===================================================================
97 --- debian/changelog    (revision 3197)
98 +++ debian/changelog    (working copy)
99 @@ -24,6 +24,9 @@
100      than just a suggests, since OpenID is enabled by default.
101    * Fix a bug that caused link(foo) to succeed if page foo did not exist.
102    * Fix tags to page names that contain special characters.
103 +  * Based on a patch by Ethan, add a new htmlescape hook, that is called
104 +    when a preprocssor directive emits inline html. The rst plugin uses this
105 +    hook to support inlined raw html.
106  
107    [ Josh Triplett ]
108    * Use pngcrush and optipng on all PNG files.
109 Index: IkiWiki/Render.pm
110 ===================================================================
111 --- IkiWiki/Render.pm   (revision 3197)
112 +++ IkiWiki/Render.pm   (working copy)
113 @@ -96,7 +96,7 @@
114                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
115                    (length $config{cgiurl} ||
116                     exists $links{$page."/".$discussionlink})) {
117 -                       $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
118 +                       $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1, genhtml => 1));
119                         $actions++;
120                 }
121         }
122 Index: IkiWiki/Plugin/rst.pm
123 ===================================================================
124 --- IkiWiki/Plugin/rst.pm       (revision 3197)
125 +++ IkiWiki/Plugin/rst.pm       (working copy)
126 @@ -30,15 +30,36 @@
127  html = publish_string(stdin.read(), writer_name='html', 
128         settings_overrides = { 'halt_level': 6, 
129                                'file_insertion_enabled': 0,
130 -                              'raw_enabled': 0 }
131 +                              'raw_enabled': 1 }
132  );
133  print html[html.find('<body>')+6:html.find('</body>')].strip();
134  ";
135  
136  sub import { #{{{
137         hook(type => "htmlize", id => "rst", call => \&htmlize);
138 +       hook(type => "htmlescape", id => "rst", call => \&htmlescape);
139 +       hook(type => "htmlescapelink", id => "rst", call => \&htmlescapelink);
140  } # }}}
141  
142 +sub htmlescapelink ($$;@) { #{{{
143 +       my $url = shift;
144 +       my $text = shift;
145 +       my %params = @_;
146 +
147 +       if ($params{broken}){
148 +               return "`? <$url>`_\ $text";
149 +       }
150 +       else {
151 +               return "`$text <$url>`_";
152 +       }
153 +} # }}}
154 +
155 +sub htmlescape ($) { #{{{
156 +       my $html=shift;
157 +       $html=~s/^/  /mg;
158 +       return ".. raw:: html\n\n".$html;
159 +} # }}}
160 +
161  sub htmlize (@) { #{{{
162         my %params=@_;
163         my $content=$params{content};
164 Index: doc/plugins/write.mdwn
165 ===================================================================
166 --- doc/plugins/write.mdwn      (revision 3197)
167 +++ doc/plugins/write.mdwn      (working copy)
168 @@ -121,6 +121,26 @@
169  The function is passed named parameters: "page" and "content" and should
170  return the htmlized content.
171  
172 +### htmlescape
173 +
174 +       hook(type => "htmlescape", id => "ext", call => \&htmlescape);
175 +
176 +Some markup languages do not allow raw html to be mixed in with the markup
177 +language, and need it to be escaped in some way. This hook is a companion
178 +to the htmlize hook, and is called when ikiwiki detects that a preprocessor
179 +directive is inserting raw html. It is passed the chunk of html in
180 +question, and should return the escaped chunk.
181 +
182 +### htmlescapelink
183 +
184 +       hook(type => "htmlescapelink", id => "ext", call => \&htmlescapelink);
185 +
186 +Some markup languages have special syntax to link to other pages. This hook
187 +is a companion to the htmlize and htmlescape hooks, and it is called when a
188 +link is inserted. It is passed the target of the link and the text of the 
189 +link, and an optional named parameter "broken" if a broken link is being
190 +generated. It should return the correctly-formatted link.
191 +
192  ### pagetemplate
193  
194         hook(type => "pagetemplate", id => "foo", call => \&pagetemplate);
195 @@ -355,6 +375,7 @@
196  * forcesubpage  - set to force a link to a subpage
197  * linktext - set to force the link text to something
198  * anchor - set to make the link include an anchor
199 +* genhtml - set to generate HTML and not escape for correct format
200  
201  #### `readfile($;$)`
202  
203 Index: doc/plugins/rst.mdwn
204 ===================================================================
205 --- doc/plugins/rst.mdwn        (revision 3197)
206 +++ doc/plugins/rst.mdwn        (working copy)
207 @@ -10,10 +10,8 @@
208  Note that this plugin does not interoperate very well with the rest of
209  ikiwiki. Limitations include:
210  
211 -* reStructuredText does not allow raw html to be inserted into
212 -  documents, but ikiwiki does so in many cases, including
213 -  [[WikiLinks|WikiLink]] and many
214 -  [[PreprocessorDirectives|PreprocessorDirective]].
215 +* Some bits of ikiwiki may still assume that markdown is used or embed html
216 +  in ways that break reStructuredText. (Report bugs if you find any.)
217  * It's slow; it forks a copy of python for each page. While there is a
218    perl version of the reStructuredText processor, it is not being kept in
219    sync with the standard version, so is not used.
220 Index: IkiWiki.pm
221 ===================================================================
222 --- IkiWiki.pm  (revision 3197)
223 +++ IkiWiki.pm  (working copy)
224 @@ -469,6 +469,10 @@
225         my $page=shift; # the page that will contain the link (different for inline)
226         my $link=shift;
227         my %opts=@_;
228 +       # we are processing $lpage and so we need to format things in accordance
229 +       # with the formatting language of $lpage. inline generates HTML so links
230 +       # will be escaped seperately.
231 +       my $type=pagetype($pagesources{$lpage});
232  
233         my $bestlink;
234         if (! $opts{forcesubpage}) {
235 @@ -494,12 +498,17 @@
236         }
237         if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
238                 return $linktext unless length $config{cgiurl};
239 -               return "<span><a href=\"".
240 -                       cgiurl(
241 -                               do => "create",
242 -                               page => pagetitle(lc($link), 1),
243 -                               from => $lpage
244 -                       ).
245 +               my $url = cgiurl(
246 +                                do => "create",
247 +                                page => pagetitle(lc($link), 1),
248 +                                from => $lpage
249 +                               );
250 +
251 +               if ($hooks{htmlescapelink}{$type} && ! $opts{genhtml}){
252 +                       return $hooks{htmlescapelink}{$type}{call}->($url, $linktext,
253 +                                                              broken => 1);
254 +               }
255 +               return "<span><a href=\"". $url.
256                         "\">?</a>$linktext</span>"
257         }
258         
259 @@ -514,6 +523,9 @@
260                 $bestlink.="#".$opts{anchor};
261         }
262  
263 +       if ($hooks{htmlescapelink}{$type} && !$opts{genhtml}) {
264 +         return $hooks{htmlescapelink}{$type}{call}->($bestlink, $linktext);
265 +       }
266         return "<a href=\"$bestlink\">$linktext</a>";
267  } #}}}
268  
269 @@ -628,6 +640,14 @@
270                                 preview => $preprocess_preview,
271                         );
272                         $preprocessing{$page}--;
273 +
274 +                       # Handle escaping html if the htmlizer needs it.
275 +                       if ($ret =~ /[<>]/ && $pagesources{$page}) {
276 +                               my $type=pagetype($pagesources{$page});
277 +                               if ($hooks{htmlescape}{$type}) {
278 +                                       return $hooks{htmlescape}{$type}{call}->($ret);
279 +                               }
280 +                       }
281                         return $ret;
282                 }
283                 else {
284 </pre>