]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/format_escape.mdwn
update
[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 ## Original patch
61 [[tag patch]]
62
63 <pre>
64 Index: debian/changelog
65 ===================================================================
66 --- debian/changelog    (revision 3197)
67 +++ debian/changelog    (working copy)
68 @@ -24,6 +24,9 @@
69      than just a suggests, since OpenID is enabled by default.
70    * Fix a bug that caused link(foo) to succeed if page foo did not exist.
71    * Fix tags to page names that contain special characters.
72 +  * Based on a patch by Ethan, add a new htmlescape hook, that is called
73 +    when a preprocssor directive emits inline html. The rst plugin uses this
74 +    hook to support inlined raw html.
75  
76    [ Josh Triplett ]
77    * Use pngcrush and optipng on all PNG files.
78 Index: IkiWiki/Render.pm
79 ===================================================================
80 --- IkiWiki/Render.pm   (revision 3197)
81 +++ IkiWiki/Render.pm   (working copy)
82 @@ -96,7 +96,7 @@
83                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
84                    (length $config{cgiurl} ||
85                     exists $links{$page."/".$discussionlink})) {
86 -                       $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
87 +                       $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1, genhtml => 1));
88                         $actions++;
89                 }
90         }
91 Index: IkiWiki/Plugin/rst.pm
92 ===================================================================
93 --- IkiWiki/Plugin/rst.pm       (revision 3197)
94 +++ IkiWiki/Plugin/rst.pm       (working copy)
95 @@ -30,15 +30,36 @@
96  html = publish_string(stdin.read(), writer_name='html', 
97         settings_overrides = { 'halt_level': 6, 
98                                'file_insertion_enabled': 0,
99 -                              'raw_enabled': 0 }
100 +                              'raw_enabled': 1 }
101  );
102  print html[html.find('<body>')+6:html.find('</body>')].strip();
103  ";
104  
105  sub import { #{{{
106         hook(type => "htmlize", id => "rst", call => \&htmlize);
107 +       hook(type => "htmlescape", id => "rst", call => \&htmlescape);
108 +       hook(type => "htmlescapelink", id => "rst", call => \&htmlescapelink);
109  } # }}}
110  
111 +sub htmlescapelink ($$;@) { #{{{
112 +       my $url = shift;
113 +       my $text = shift;
114 +       my %params = @_;
115 +
116 +       if ($params{broken}){
117 +               return "`? <$url>`_\ $text";
118 +       }
119 +       else {
120 +               return "`$text <$url>`_";
121 +       }
122 +} # }}}
123 +
124 +sub htmlescape ($) { #{{{
125 +       my $html=shift;
126 +       $html=~s/^/  /mg;
127 +       return ".. raw:: html\n\n".$html;
128 +} # }}}
129 +
130  sub htmlize (@) { #{{{
131         my %params=@_;
132         my $content=$params{content};
133 Index: doc/plugins/write.mdwn
134 ===================================================================
135 --- doc/plugins/write.mdwn      (revision 3197)
136 +++ doc/plugins/write.mdwn      (working copy)
137 @@ -121,6 +121,26 @@
138  The function is passed named parameters: "page" and "content" and should
139  return the htmlized content.
140  
141 +### htmlescape
142 +
143 +       hook(type => "htmlescape", id => "ext", call => \&htmlescape);
144 +
145 +Some markup languages do not allow raw html to be mixed in with the markup
146 +language, and need it to be escaped in some way. This hook is a companion
147 +to the htmlize hook, and is called when ikiwiki detects that a preprocessor
148 +directive is inserting raw html. It is passed the chunk of html in
149 +question, and should return the escaped chunk.
150 +
151 +### htmlescapelink
152 +
153 +       hook(type => "htmlescapelink", id => "ext", call => \&htmlescapelink);
154 +
155 +Some markup languages have special syntax to link to other pages. This hook
156 +is a companion to the htmlize and htmlescape hooks, and it is called when a
157 +link is inserted. It is passed the target of the link and the text of the 
158 +link, and an optional named parameter "broken" if a broken link is being
159 +generated. It should return the correctly-formatted link.
160 +
161  ### pagetemplate
162  
163         hook(type => "pagetemplate", id => "foo", call => \&pagetemplate);
164 @@ -355,6 +375,7 @@
165  * forcesubpage  - set to force a link to a subpage
166  * linktext - set to force the link text to something
167  * anchor - set to make the link include an anchor
168 +* genhtml - set to generate HTML and not escape for correct format
169  
170  #### `readfile($;$)`
171  
172 Index: doc/plugins/rst.mdwn
173 ===================================================================
174 --- doc/plugins/rst.mdwn        (revision 3197)
175 +++ doc/plugins/rst.mdwn        (working copy)
176 @@ -10,10 +10,8 @@
177  Note that this plugin does not interoperate very well with the rest of
178  ikiwiki. Limitations include:
179  
180 -* reStructuredText does not allow raw html to be inserted into
181 -  documents, but ikiwiki does so in many cases, including
182 -  [[WikiLinks|WikiLink]] and many
183 -  [[PreprocessorDirectives|PreprocessorDirective]].
184 +* Some bits of ikiwiki may still assume that markdown is used or embed html
185 +  in ways that break reStructuredText. (Report bugs if you find any.)
186  * It's slow; it forks a copy of python for each page. While there is a
187    perl version of the reStructuredText processor, it is not being kept in
188    sync with the standard version, so is not used.
189 Index: IkiWiki.pm
190 ===================================================================
191 --- IkiWiki.pm  (revision 3197)
192 +++ IkiWiki.pm  (working copy)
193 @@ -469,6 +469,10 @@
194         my $page=shift; # the page that will contain the link (different for inline)
195         my $link=shift;
196         my %opts=@_;
197 +       # we are processing $lpage and so we need to format things in accordance
198 +       # with the formatting language of $lpage. inline generates HTML so links
199 +       # will be escaped seperately.
200 +       my $type=pagetype($pagesources{$lpage});
201  
202         my $bestlink;
203         if (! $opts{forcesubpage}) {
204 @@ -494,12 +498,17 @@
205         }
206         if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
207                 return $linktext unless length $config{cgiurl};
208 -               return "<span><a href=\"".
209 -                       cgiurl(
210 -                               do => "create",
211 -                               page => pagetitle(lc($link), 1),
212 -                               from => $lpage
213 -                       ).
214 +               my $url = cgiurl(
215 +                                do => "create",
216 +                                page => pagetitle(lc($link), 1),
217 +                                from => $lpage
218 +                               );
219 +
220 +               if ($hooks{htmlescapelink}{$type} && ! $opts{genhtml}){
221 +                       return $hooks{htmlescapelink}{$type}{call}->($url, $linktext,
222 +                                                              broken => 1);
223 +               }
224 +               return "<span><a href=\"". $url.
225                         "\">?</a>$linktext</span>"
226         }
227         
228 @@ -514,6 +523,9 @@
229                 $bestlink.="#".$opts{anchor};
230         }
231  
232 +       if ($hooks{htmlescapelink}{$type} && !$opts{genhtml}) {
233 +         return $hooks{htmlescapelink}{$type}{call}->($bestlink, $linktext);
234 +       }
235         return "<a href=\"$bestlink\">$linktext</a>";
236  } #}}}
237  
238 @@ -628,6 +640,14 @@
239                                 preview => $preprocess_preview,
240                         );
241                         $preprocessing{$page}--;
242 +
243 +                       # Handle escaping html if the htmlizer needs it.
244 +                       if ($ret =~ /[<>]/ && $pagesources{$page}) {
245 +                               my $type=pagetype($pagesources{$page});
246 +                               if ($hooks{htmlescape}{$type}) {
247 +                                       return $hooks{htmlescape}{$type}{call}->($ret);
248 +                               }
249 +                       }
250                         return $ret;
251                 }
252                 else {
253 </pre>