also tag 'patch/core', considering that over half of the changes are there
[ikiwiki] / doc / todo / color_plugin.mdwn
1 Recently I've wanted to colour some piece of text on my Ikiwiki page.
2 It seems that Markdown can do it only using HTML tags, so I used
3 `<span class="color">foo bar baz</span>`.
4
5 However, in my opinion mixing Markdown syntax and HTML tags is rather ugly,
6 so maybe we should create a new color plugin to add more color to Ikiwiki ;)
7 I know that another Wikis have similar plugin, for example
8 [WikiDot](http://www.wikidot.com/).
9
10 I've noticed that htmlscrubber plugin strips `style` attribute, because of
11 security, so probably we need to use `class` attribute of HTML. But then
12 we have to customize our `local.css` file to add all color we want to use.
13 It's not as easy in usage like color name or definition as plugin argument,
14 but I don't have a better idea right now.
15
16 What do you think about it? --[[Paweł|ptecza]]
17
18 > Making a plugin preserve style attributes can be done, it just has to add
19 > them after the sanitize step, which strips them. The general method is
20 > adding placeholders first, and replacing them with the real html later.
21
22 > The hard thing to me seems to be finding a syntax that is better than a
23 > `<span>`. A preprocessor directive is not really any less ugly than html
24 > tags, though at least it could play nicely with nested markdown: --[[Joey]]
25
26 >       \[[!color red,green """
27 >       Xmas-colored markdown here
28 >       """]]
29
30 >> I'm glad you like that idea. In my opinion your syntax looks good.
31 >> Out of curiosity, why did you used 2 colors in your example? What is HTML
32 >> result for it? ;)
33
34 >>> I was thinking one would be foreground, the other background. Don't
35 >>> know if setting the background makes sense or not.
36
37 >> I can try to create that plugin, if you are too busy now. I'm not Perl
38 >> hacker, but I wrote a lot of Perl scripts in my life and color plugin
39 >> doesn't seem to be very hard task. --[[Paweł|ptecza]]
40
41 >> Yes, it's a good intro plugin, have at it! --[[Joey]]
42
43 ---
44
45 This is a RC1 of my `color` plugin. It works for me well, but all your
46 comments are very welcome. --[[Paweł|ptecza]]
47
48 > Sure, I have a couple.
49
50 >> Great! Thank you very much! --[[Paweł|ptecza]]
51
52 > The preprocess function is passed named parameters. The hack you have of
53 > hardcoding use of `$_[0]` and `$_[2]` can fail at any time.
54
55 >> But the problem is that arguments of my plugin don't have a name.
56 >> How can I identify them in `params` hash?
57
58 >> Similar hardcoded method I've found in `img` plugin :) But only one
59 >> argument is not named there (image path).
60
61 >>> I think I hadn't realized what you were doing there. The order
62 >>> for unnamed parameters can in fact be relied on. 
63 >>> 
64 >>> --[[Joey]]
65
66 >> Maybe I shouldn't use so simple plugin syntax? For following syntax
67 >> I wouldn't have that problem:
68
69 >>     \[[!color fg=white bg=red text="White text on red background"]]
70
71 > `replace_preserved_style` is passed a single parameter, so its prototype
72 > should be `($)`, not `(@)`.  Ditt `preserve_style`, it should have
73 > `($$)`.
74
75 >> OK, it will be fixed.
76
77 > The sanitize hook is always passed `$params{content}`, so there should be
78 > no reason to check that it exists. Also, it shouldn't be done in a
79 > sanitize hook, since html sanitization could run _after_ that santize
80 > hook. It should use a format hook.
81
82 >> Probably you're right. It was rather paranoid checking ;) Thanks for
83 >> the hook hint!
84
85 > The preprocess hook needs to call `IkiWiki::preprocess` on the content
86 > passed into it if you want to support nesting other preprocessor
87 > directives inside the color directive. See `preprocess_toggleable` in the
88 > toggle plugin, for example.
89
90 > I'm not a big fan of the dummy text `COLORS { ... } SROLOC;TEXT { ... TXET }`
91 > The method used by toggle of using two real `<div>`s seems slightly
92 > better. --[[Joey]]
93
94 >> I don't like that too, but I didn't have better idea :) Thank you for
95 >> the hint! I'll take a look at `toggle` plugin.
96
97 ---
98
99 And here is RC2 of that plugin. I've changed a plugin syntax, because the old
100 seems to be too enigmatic and it was hard to me to handle unnamed parameters
101 in not hardcoded way. I hope that my changes are acceptable for you.
102 Of course, I'm open for discussion or exchange of ideas :) --[[Paweł|ptecza]]
103
104 > One question, why the 2px padding for span.color? --[[Joey]]
105
106 >> Sorry for a long silence, but I had Internet free summer holiday :)
107 >> I did that, because backgrounded text without any padding looks
108 >> strange for me ;) You can remove it if you don't like that padding.
109 >> --[[Paweł|ptecza]]
110
111 >>> Joey, will you add that plugin to Ikiwiki 2.61? :) --[[Paweł|ptecza]]
112
113 >>>> I also had a long net-free summer holiday. :-) The [[patch]] is
114 >>>> ready for integration (made a few minor changes). Is this GPL 2?
115 >>>> --[[Joey]]
116
117 >>>>> No problem. I guessed it, because I've not seen your commits
118 >>>>> at [[RecentChanges]] page in last days and I subscribe your
119 >>>>> [blog](http://kitenet.net/~joey/blog/entry/vacation/) :D
120 >>>>> It's GPL-2+ like your Ikiwiki and the most external plugins.
121 >>>>> --[[Paweł|ptecza]]
122
123         --- /dev/null   2008-06-21 02:02:15.000000000 +0200
124         +++ color.pm    2008-07-27 14:58:12.000000000 +0200
125         @@ -0,0 +1,69 @@
126         +#!/usr/bin/perl
127         +# Ikiwiki text colouring plugin
128         +# Paweł‚ Tęcza <ptecza@net.icm.edu.pl>
129         +package IkiWiki::Plugin::color;
130         +
131         +use warnings;
132         +use strict;
133         +use IkiWiki 2.00;
134         +
135         +sub import {
136         +       hook(type => "preprocess", id => "color", call => \&preprocess);
137         +       hook(type => "format",     id => "color", call => \&format);
138         +}
139         +
140         +sub preserve_style ($$$) {
141         +       my $foreground = shift;
142         +       my $background = shift;
143         +       my $text       = shift;
144         +
145         +       $foreground = defined $foreground ? lc($foreground) : '';
146         +       $background = defined $background ? lc($background) : '';
147         +       $text       = '' unless (defined $text);
148         +
149         +       # Validate colors. Only color name or color code are valid.
150         +       $foreground = '' unless ($foreground &&
151         +                               ($foreground =~ /^[a-z]+$/ || $foreground =~ /^#[0-9a-f]{3,6}$/));
152         +       $background = '' unless ($background &&
153         +                               ($background =~ /^[a-z]+$/ || $background =~ /^#[0-9a-f]{3,6}$/));
154         +
155         +       my $preserved = '';
156         +       $preserved .= '<span class="color">';
157         +       $preserved .= 'color: '.$foreground if ($foreground);
158         +       $preserved .= '; ' if ($foreground && $background);
159         +       $preserved .= 'background-color: '.$background if ($background);
160         +       $preserved .= '</span>';
161         +       $preserved .= '<span class="colorend">'.$text.'</span>';
162         +       
163         +       return $preserved;
164         +
165         +}
166         +
167         +sub replace_preserved_style ($) {
168         +       my $content = shift;
169         +
170         +       $content =~ s!<span class="color">((color: ([a-z]+|\#[0-9a-f]{3,6})?)?((; )?(background-color: ([a-z]+|\#[0-9a-f]{3,6})?)?)?)</span>!<span class="color" style="$1">!g;
171         +       $content =~ s!<span class="colorend">!!g;
172         +
173         +       return $content;
174         +}
175         +
176         +sub preprocess (@) {
177         +       my %params = @_;
178         +
179         +       # Preprocess the text to expand any preprocessor directives
180         +       # embedded inside it.
181         +       $params{text} = IkiWiki::preprocess($params{page}, $params{destpage},
182         +                               IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
183         +
184         +       return preserve_style($params{foreground}, $params{background}, $params{text});
185         +}
186         +
187         +sub format (@) {
188         +       my %params = @_;
189         +
190         +       $params{content} = replace_preserved_style($params{content});
191         +       return $params{content};        
192         +}
193         +
194         +1
195         --- /dev/null   2008-06-21 02:02:15.000000000 +0200
196         +++ color.mdwn  2008-07-27 15:04:42.000000000 +0200
197         @@ -0,0 +1,25 @@
198         +\[[!template id=plugin name=color core=0 author="[[ptecza]]"]]
199         +
200         +This plugin can be used to color a piece of text on a page.
201         +It can be used to set the foreground and/or background color of the text.
202         +
203         +You can use a color name (e.g. `white`) or HTML code (e.g. `#ffffff`)
204         +to define colors. 
205         +
206         +Below are a few examples:
207         +
208         +    \[[!color foreground=white background=#ff0000 text="White text on red background"]]
209         +
210         +In the above example, the foreground color is defined as a word, while the background color is defined as a HTML
211         +color code.
212         +
213         +    \[[!color foreground=white text="White text on default color background"]]
214         +
215         +The background color is missing, so the text is displayed on default background.
216         +
217         +    \[[!color background=#ff0000 text="Default color text on red background"]]
218         +
219         +The foreground is missing, so the text has the default foreground color.
220         --- style.css-orig      2008-07-27 15:12:39.000000000 +0200
221         +++ style.css   2008-07-27 15:15:06.000000000 +0200
222         @@ -333,3 +333,7 @@
223                 background: #eee; 
224                 color: black !important;
225          }
226         +
227         +span.color {
228         +       padding: 2px;
229         +}
230
231 [[done]]