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