* RC2 color plugin

master
http://ptecza.myopenid.com/ 2008-07-27 09:30:55 -04:00 committed by Joey Hess
parent 4b6028a4cb
commit 4c5b83cbcf
1 changed files with 66 additions and 61 deletions

View File

@ -89,12 +89,19 @@ comments are very welcome. --[[Paweł|ptecza]]
>> I don't like that too, but I didn't have better idea :) Thank you for >> 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. >> the hint! I'll take a look at `toggle` plugin.
--- /dev/null 2008-07-24 09:38:19.000000000 +0200 ---
+++ color.pm 2008-07-25 14:43:15.000000000 +0200
@@ -0,0 +1,75 @@ 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 +#!/usr/bin/perl
+# Ikiwiki text colouring plugin +# Ikiwiki text colouring plugin
+# Paweł Tęcza <ptecza@net.icm.edu.pl> +# Paweł Tęcza <ptecza@net.icm.edu.pl>
+package IkiWiki::Plugin::color; +package IkiWiki::Plugin::color;
+ +
+use warnings; +use warnings;
@ -103,101 +110,99 @@ comments are very welcome. --[[Paweł|ptecza]]
+ +
+sub import { #{{{ +sub import { #{{{
+ hook(type => "preprocess", id => "color", call => \&preprocess); + hook(type => "preprocess", id => "color", call => \&preprocess);
+ hook(type => "sanitize", id => "color", call => \&sanitize); + hook(type => "format", id => "color", call => \&format);
+} #}}} +} #}}}
+ +
+sub preserve_style(@) { #{{{ +sub preserve_style($$$) { #{{{
+ my ($colors, $text) = @_; + my $foreground = shift;
+ $colors = '' unless $colors; # foreground and background colors + my $background = shift;
+ $text = '' unless $text; # text + my $text = shift;
+ +
+ # Check colors + $foreground = defined $foreground ? lc($foreground) : '';
+ my ($color1, $color2) = (); + $background = defined $background ? lc($background) : '';
+ $colors = lc($colors); # Regexps on lower case strings are simpler + $text = '' unless (defined $text);
+ if ($colors =~ /,/) { +
+ # Probably defined both foreground and background color
+ ($color1, $color2) = ($colors =~ /(.*),(.*)/);
+ }
+ else {
+ # Probably defined only foreground color
+ ($color1, $color2) = ($colors, '');
+ }
+
+ # Validate colors. Only color name or color code are valid. + # Validate colors. Only color name or color code are valid.
+ my ($fg, $bg) = (); + $foreground = '' unless ($foreground &&
+ $fg = $color1 if ($color1 && + ($foreground =~ /^[a-z]+$/ || $foreground =~ /^#[0-9a-f]{3,6}$/));
+ ($color1 =~ /^[a-z]+$/ || $color1 =~ /^#[0-9a-f]{3,6}$/)); + $background = '' unless ($background &&
+ $bg = $color2 if ($color2 && + ($background =~ /^[a-z]+$/ || $background =~ /^#[0-9a-f]{3,6}$/));
+ ($color2 =~ /^[a-z]+$/ || $color2 =~ /^#[0-9a-f]{3,6}$/));
+ +
+ my $preserved = ''; + my $preserved = '';
+ if ($fg || $bg) { + $preserved .= '<span class="color">';
+ $preserved .= 'COLORS {'; + $preserved .= 'color: '.$foreground if ($foreground);
+ $preserved .= 'color: '.$fg if ($fg); + $preserved .= '; ' if ($foreground && $background);
+ $preserved .= '; ' if ($fg && $bg); + $preserved .= 'background-color: '.$background if ($background);
+ $preserved .= 'background-color: '.$bg if ($bg); + $preserved .= '</span>';
+ $preserved .= '} SROLOC;TEXT {'.$text.'} TXET'; + $preserved .= '<span class="colorend">'.$text.'</span>';
+ }
+ +
+ return $preserved; + return $preserved;
+ +
+} #}}} +} #}}}
+ +
+sub replace_preserved_style(@) { #{{{ +sub replace_preserved_style($) { #{{{
+ my $content = shift; + my $content = shift;
+ +
+ if ($content) { + $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;
+ $content =~ s/COLORS {/<span style="/; + $content =~ s!<span class="colorend">!!g;
+ $content =~ s/} SROLOC;TEXT {/">/;
+ $content =~ s/} TXET/<\/span>/;
+ }
+ +
+ return $content; + return $content;
+} #}}} +} #}}}
+ +
+sub preprocess (@) { #{{{ +sub preprocess(@) { #{{{
+ return preserve_style($_[0], $_[2]); + 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 sanitize (@) { #{{{ +sub format(@) { #{{{
+ my %params = @_; + my %params = @_;
+ +
+ return replace_preserved_style($params{content}) + $params{content} = replace_preserved_style($params{content});
+ if (exists $params{content}) + return $params{content};
+} #}}} +} #}}}
+ +
+1 +1
--- /dev/null 2008-07-24 09:38:19.000000000 +0200 --- /dev/null 2008-06-21 02:02:15.000000000 +0200
+++ color.mdwn 2008-07-25 14:50:19.000000000 +0200 +++ color.mdwn 2008-07-27 15:04:42.000000000 +0200
@@ -0,0 +1,31 @@ @@ -0,0 +1,25 @@
+\[[!template id=plugin name=color core=0 author="[[Paweł Tęcza|ptecza]]"]] +\[[!template id=plugin name=color core=0 author="[[ptecza]]"]]
+ +
+This plugin can be used to color a piece of text on Ikiwiki page. +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. +It's possible setting foreground and/or background color of the text.
+ +
+The plugin syntax is very simple. You only need to type name (e.g. `white`) +You can use name (e.g. `white`) or HTML code of colors (e.g. `#ffffff`)
+or HTML code of colors (e.g. `#ffffff`) and a text you want to color. +to define colors.
+The colors should by separated using a comma character.
+ +
+Below are a few examples: +Below are a few examples:
+ +
+ \[[!color white,#ff0000 "White text on red background"]] + \[[!color foreground=white background=#ff0000 text="White text on red background"]]
+ +
+Foreground color is defined as a word, background color is defined as HTML +Foreground color is defined as a word, background color is defined as HTML
+color code. +color code.
+ +
+ \[[!color white "White text on default color background"]] + \[[!color foreground=white text="White text on default color background"]]
+
+Foreground color is default color if only one color was typed and a comma
+character is missing.
+
+ \[[!color white, "White text on default color background"]]
+ +
+Background color is missing, so the text is displayed on default background. +Background color is missing, so the text is displayed on default background.
+ +
+ \[[!color ,#ff0000 "Default color text on red background"]] + \[[!color background=#ff0000 text="Default color text on red background"]]
+ +
+Foreground is missing, so the text has default color. +Foreground is missing, so the text has default color.
+ +
+This plugin is not enabled by default. You can do that in [[ikiwiki.setup]] +This plugin is not enabled by default. You can do that in [[ikiwiki.setup]]
+file (hint: `add_plugins` variable). +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;
+}