]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/color.pm
Fix color and format plugins to appear in the websetup interface.
[ikiwiki.git] / IkiWiki / Plugin / color.pm
1 #!/usr/bin/perl
2 # Ikiwiki text colouring plugin
3 # Paweł‚ Tęcza <ptecza@net.icm.edu.pl>
4 package IkiWiki::Plugin::color;
5
6 use warnings;
7 use strict;
8 use IkiWiki 3.00;
9
10 sub import {
11         hook(type => "preprocess", id => "color", call => \&preprocess);
12         hook(type => "format",     id => "color", call => \&format);
13         hook(type => "getsetup",   id => "color", call => \&getsetup);
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                 },
22 }
23
24 sub preserve_style ($$$) {
25         my $foreground = shift;
26         my $background = shift;
27         my $text       = shift;
28
29         $foreground = defined $foreground ? lc($foreground) : '';
30         $background = defined $background ? lc($background) : '';
31         $text       = '' unless (defined $text);
32
33         # Validate colors. Only color name or color code are valid.
34         $foreground = '' unless ($foreground &&
35                                 ($foreground =~ /^[a-z]+$/ || $foreground =~ /^#[0-9a-f]{3,6}$/));
36         $background = '' unless ($background &&
37                                 ($background =~ /^[a-z]+$/ || $background =~ /^#[0-9a-f]{3,6}$/));
38
39         my $preserved = '';
40         $preserved .= '<span class="color">';
41         $preserved .= 'color: '.$foreground if ($foreground);
42         $preserved .= '; ' if ($foreground && $background);
43         $preserved .= 'background-color: '.$background if ($background);
44         $preserved .= '</span>';
45         $preserved .= '<span class="colorend">'.$text.'</span>';
46         
47         return $preserved;
48
49 }
50
51 sub replace_preserved_style ($) {
52         my $content = shift;
53
54         $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;
55         $content =~ s!<span class="colorend">!!g;
56
57         return $content;
58 }
59
60 sub preprocess (@) {
61         my %params = @_;
62
63         # Preprocess the text to expand any preprocessor directives
64         # embedded inside it.
65         $params{text} = IkiWiki::preprocess($params{page}, $params{destpage},
66                                 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
67
68         return preserve_style($params{foreground}, $params{background}, $params{text});
69 }
70
71 sub format (@) {
72         my %params = @_;
73
74         $params{content} = replace_preserved_style($params{content});
75         return $params{content};        
76 }
77
78 1