X-Git-Url: https://sipb.mit.edu/gitweb.cgi/ikiwiki.git/blobdiff_plain/9721801af0345ebbdf6f527ff3c555c7112a0a3e..4c5b83cbcf4d668353252312653214bc30a018aa:/doc/todo/color_plugin.mdwn diff --git a/doc/todo/color_plugin.mdwn b/doc/todo/color_plugin.mdwn index 069e2d4c2..1e1fb174e 100644 --- a/doc/todo/color_plugin.mdwn +++ b/doc/todo/color_plugin.mdwn @@ -23,7 +23,7 @@ What do you think about it? --[[Paweł|ptecza]] > ``. A preprocessor directive is not really any less ugly than html > tags, though at least it could play nicely with nested markdown: --[[Joey]] > -> \[[color red,green """ +> \[[!color red,green """ > Xmas-colored markdown here > """]] @@ -39,3 +39,170 @@ What do you think about it? --[[Paweł|ptecza]] >> doesn't seem to be very hard task. --[[Paweł|ptecza]] >> Yes, it's a good intro plugin, have at it! --[[Joey]] + +--- + +This is a RC1 of my `color` plugin. It works for me well, but all your +comments are very welcome. --[[Paweł|ptecza]] + +> Sure, I have a couple. + +>> Great! Thank you very much! --[[Paweł|ptecza]] + +> The preprocess function is passed named parameters. The hack you have of +> hardcoding use of `$_[0]` and `$_[2]` can fail at any time. + +>> But the problem is that arguments of my plugin don't have a name. +>> How can I identify them in `params` hash? + +>> Similar hardcoded method I've found in `img` plugin :) But only one +>> argument is not named there (image path). + +>> Maybe I shouldn't use so simple plugin syntax? For following syntax +>> I wouldn't have that problem: + +>> \[[!color fg=white bg=red text="White text on red background"]] + +> `replace_preserved_style` is passed a single parameter, so its prototype +> should be `($)`, not `(@)`. Ditt `preserve_style`, it should have +> `($$)`. + +>> OK, it will be fixed. + +> The sanitize hook is always passed `$params{content}`, so there should be +> no reason to check that it exists. Also, it shouldn't be done in a +> sanitize hook, since html sanitization could run _after_ that santize +> hook. It should use a format hook. + +>> Probably you're right. It was rather paranoid checking ;) Thanks for +>> the hook hint! + +> The preprocess hook needs to call `IkiWiki::preprocess` on the content +> passed into it if you want to support nesting other preprocessor +> directives inside the color directive. See `preprocess_toggleable` in the +> toggle plugin, for example. +> +> I'm not a big fan of the dummy text `COLORS { ... } SROLOC;TEXT { ... TXET }` +> The method used by toggle of using two real `
`s seems slightly +> better. --[[Joey]] + +>> I don't like that too, but I didn't have better idea :) Thank you for +>> the hint! I'll take a look at `toggle` plugin. + +--- + +And here is RC2 of that plugin. I've changed a plugin syntax, because the old +seems to be too enigmatic and it was hard to me to handle unnamed parameters +in not hardcoded way. I hope that my changes are acceptable for you. +Of course, I'm open for discussion or exchange of ideas :) --[[Paweł|ptecza]] + + --- /dev/null 2008-06-21 02:02:15.000000000 +0200 + +++ color.pm 2008-07-27 14:58:12.000000000 +0200 + @@ -0,0 +1,69 @@ + +#!/usr/bin/perl + +# Ikiwiki text colouring plugin + +# Paweł‚ Tęcza + +package IkiWiki::Plugin::color; + + + +use warnings; + +use strict; + +use IkiWiki 2.00; + + + +sub import { #{{{ + + hook(type => "preprocess", id => "color", call => \&preprocess); + + hook(type => "format", id => "color", call => \&format); + +} #}}} + + + +sub preserve_style($$$) { #{{{ + + my $foreground = shift; + + my $background = shift; + + my $text = shift; + + + + $foreground = defined $foreground ? lc($foreground) : ''; + + $background = defined $background ? lc($background) : ''; + + $text = '' unless (defined $text); + + + + # Validate colors. Only color name or color code are valid. + + $foreground = '' unless ($foreground && + + ($foreground =~ /^[a-z]+$/ || $foreground =~ /^#[0-9a-f]{3,6}$/)); + + $background = '' unless ($background && + + ($background =~ /^[a-z]+$/ || $background =~ /^#[0-9a-f]{3,6}$/)); + + + + my $preserved = ''; + + $preserved .= ''; + + $preserved .= 'color: '.$foreground if ($foreground); + + $preserved .= '; ' if ($foreground && $background); + + $preserved .= 'background-color: '.$background if ($background); + + $preserved .= ''; + + $preserved .= ''.$text.''; + + + + return $preserved; + + + +} #}}} + + + +sub replace_preserved_style($) { #{{{ + + my $content = shift; + + + + $content =~ s!((color: ([a-z]+|\#[0-9a-f]{3,6})?)?((; )?(background-color: ([a-z]+|\#[0-9a-f]{3,6})?)?)?)!!g; + + $content =~ s!!!g; + + + + return $content; + +} #}}} + + + +sub preprocess(@) { #{{{ + + my %params = @_; + + + + # Preprocess the text to expand any preprocessor directives + + # embedded inside it. + + $params{text} = IkiWiki::preprocess($params{page}, $params{destpage}, + + IkiWiki::filter($params{page}, $params{destpage}, $params{text})); + + + + return preserve_style($params{foreground}, $params{background}, $params{text}); + +} #}}} + + + +sub format(@) { #{{{ + + my %params = @_; + + + + $params{content} = replace_preserved_style($params{content}); + + return $params{content}; + +} #}}} + + + +1 + --- /dev/null 2008-06-21 02:02:15.000000000 +0200 + +++ color.mdwn 2008-07-27 15:04:42.000000000 +0200 + @@ -0,0 +1,25 @@ + +\[[!template id=plugin name=color core=0 author="[[ptecza]]"]] + + + +This plugin can be used to color a piece of text on Ikiwiki page. + +It's possible setting foreground and/or background color of the text. + + + +You can use name (e.g. `white`) or HTML code of colors (e.g. `#ffffff`) + +to define colors. + + + +Below are a few examples: + + + + \[[!color foreground=white background=#ff0000 text="White text on red background"]] + + + +Foreground color is defined as a word, background color is defined as HTML + +color code. + + + + \[[!color foreground=white text="White text on default color background"]] + + + +Background color is missing, so the text is displayed on default background. + + + + \[[!color background=#ff0000 text="Default color text on red background"]] + + + +Foreground is missing, so the text has default color. + + + +This plugin is not enabled by default. You can do that in [[ikiwiki.setup]] + +file (hint: `add_plugins` variable). + --- style.css-orig 2008-07-27 15:12:39.000000000 +0200 + +++ style.css 2008-07-27 15:15:06.000000000 +0200 + @@ -333,3 +333,7 @@ + background: #eee; + color: black !important; + } + + + +span.color { + + padding: 2px; + +}