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