~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Plugins/WysiwygPlugin/Constants.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#package Foswiki::Plugins::WysiwygPlugin::Constants;
 
2
# Use s simpler-named namespace for constants to improve code readability
 
3
package Foswiki::Plugins::WysiwygPlugin::Constants;
 
4
 
 
5
use strict;
 
6
 
 
7
our (%ALWAYS_BLOCK, $ALWAYS_BLOCK_S, $STARTWW, $ENDWW, $PROTOCOL);
 
8
 
 
9
# HTML elements that are strictly block type, as defined by
 
10
# http://www.htmlhelp.com/reference/html40/block.html.
 
11
# Block type elements do not require
 
12
# <br /> to be generated for newlines on the boundary - see WC::isInline.
 
13
%ALWAYS_BLOCK = map { $_ => 1 }
 
14
  qw( ADDRESS BLOCKQUOTE CENTER DIR DIV DL FIELDSET FORM H1 H2 H3 H4 H5 H6
 
15
      HR ISINDEX MENU NOFRAMES NOSCRIPT OL P PRE TABLE UL );
 
16
$ALWAYS_BLOCK_S = join('|', keys %ALWAYS_BLOCK);
 
17
 
 
18
$STARTWW  = qr/^|(?<=[ \t\n\(\!])/om;
 
19
$ENDWW    = qr/$|(?=[ \t\n\,\.\;\:\!\?\)])/om;
 
20
$PROTOCOL = qr/^(file|ftp|gopher|https?|irc|news|nntp|telnet|mailto):/;
 
21
 
 
22
our (%KNOWN_COLOUR);
 
23
 
 
24
# Colours with colour settings in DefaultPreferences. WTF does Foswiki see
 
25
# fit to *redefine* the standard colors? e.g. ORANGE below is *not* orange.
 
26
# For goodness sakes!
 
27
%KNOWN_COLOUR = (
 
28
    BLACK => 'BLACK',
 
29
    '#000000' => 'BLACK',
 
30
    MAROON => 'MAROON',
 
31
    '#800000' => 'MAROON',
 
32
    PURPLE => 'PURPLE',
 
33
    '#800080' => 'PURPLE',
 
34
    PINK => 'PINK',
 
35
    '#FF00FF' => 'PINK',
 
36
    RED => 'RED',
 
37
    '#FF0000' => 'RED',
 
38
    ORANGE => 'ORANGE',
 
39
    '#FF6600' => 'ORANGE',
 
40
    '#FFA500' => 'ORANGE', # HTML standard
 
41
    YELLOW => 'YELLOW',
 
42
    '#FFFF00' => 'YELLOW',
 
43
    LIME => 'LIME',
 
44
    '#00FF00' => 'LIME',
 
45
    AQUA => 'AQUA',
 
46
    AQUAMARINE => 'AQUA',
 
47
    '#00FFFF' => 'AQUA',
 
48
    GREEN => 'GREEN',
 
49
    '#008000' => 'GREEN',
 
50
    OLIVE => 'OLIVE',
 
51
    '#808000' => 'OLIVE',
 
52
    BROWN => 'BROWN',
 
53
    '#996633' => 'BROWN',
 
54
    '#A52A2A' => 'BROWN', # HTML standard
 
55
    NAVY => 'NAVY',
 
56
    '#000080' => 'NAVY',
 
57
    TEAL => 'TEAL',
 
58
    '#008080' => 'TEAL',
 
59
    BLUE => 'BLUE',
 
60
    '#0000FF' => 'BLUE',
 
61
    GRAY => 'GRAY',
 
62
    '#808080' => 'GRAY',
 
63
    SILVER => 'SILVER',
 
64
    '#C0C0C0' => 'SILVER',
 
65
    WHITE => 'WHITE',
 
66
    '#FFFFFF' => 'WHITE',
 
67
);
 
68
 
 
69
# Genuine HTML colors as follows:
 
70
# '#4682B4' => 'steelblue',
 
71
# '#041690' => 'royalblue',
 
72
# '#6495ED' => 'cornflowerblue',
 
73
# '#B0C4DE' => 'lightsteelblue',
 
74
# '#7B68EE' => 'mediumslateblue',
 
75
# '#6A5ACD' => 'slateblue',
 
76
# '#483D8B' => 'darkslateblue',
 
77
# '#191970' => 'midnightblue',
 
78
# '#000080' => 'navy',
 
79
# '#00008B' => 'darkblue',
 
80
# '#0000CD' => 'mediumblue',
 
81
# '#0000FF' => 'blue',
 
82
# '#1E90FF' => 'dodgerblue',
 
83
# '#00BFFF' => 'deepskyblue',
 
84
# '#87CEFA' => 'lightskyblue',
 
85
# '#87CEEB' => 'skyblue',
 
86
# '#ADD8E6' => 'lightblue',
 
87
# '#B0E0E6' => 'powderblue',
 
88
# '#F0FFFF' => 'azure',
 
89
# '#E0FFFF' => 'lightcyan',
 
90
# '#AFEEEE' => 'paleturquoise',
 
91
# '#48D1CC' => 'mediumturquoise',
 
92
# '#20B2AA' => 'lightseagreen',
 
93
# '#008B8B' => 'darkcyan',
 
94
# '#008080' => 'teal',
 
95
# '#5F9EA0' => 'cadetblue',
 
96
# '#00CED1' => 'darkturquoise',
 
97
# '#00FFFF' => 'aqua',
 
98
# '#00FFFF' => 'cyan',
 
99
# '#40E0D0' => 'turquoise',
 
100
# '#7FFFD4' => 'aquamarine',
 
101
# '#66CDAA' => 'mediumaquamarine',
 
102
# '#8FBC8F' => 'darkseagreen',
 
103
# '#3CB371' => 'mediumseagreen',
 
104
# '#2E8B57' => 'seagreen',
 
105
# '#006400' => 'darkgreen',
 
106
# '#008000' => 'green',
 
107
# '#228B22' => 'forestgreen',
 
108
# '#32CD32' => 'limegreen',
 
109
# '#00FF00' => 'lime',
 
110
# '#7FFF00' => 'chartreuse',
 
111
# '#7CFC00' => 'lawngreen',
 
112
# '#ADFF2F' => 'greenyellow',
 
113
# '#9ACD32' => 'yellowgreen',
 
114
# '#98FB98' => 'palegreen',
 
115
# '#90EE90' => 'lightgreen',
 
116
# '#00FF7F' => 'springgreen',
 
117
# '#00FA9A' => 'mediumspringgreen',
 
118
# '#556B2F' => 'darkolivegreen',
 
119
# '#6B8E23' => 'olivedrab',
 
120
# '#808000' => 'olive',
 
121
# '#BDB76B' => 'darkkhaki',
 
122
# '#B8860B' => 'darkgoldenrod',
 
123
# '#DAA520' => 'goldenrod',
 
124
# '#FFD700' => 'gold',
 
125
# '#FFFF00' => 'yellow',
 
126
# '#F0E68C' => 'khaki',
 
127
# '#EEE8AA' => 'palegoldenrod',
 
128
# '#FFEBCD' => 'blanchedalmond',
 
129
# '#FFE4B5' => 'moccasin',
 
130
# '#F5DEB3' => 'wheat',
 
131
# '#FFDEAD' => 'navajowhite',
 
132
# '#DEB887' => 'burlywood',
 
133
# '#D2B48C' => 'tan',
 
134
# '#BC8F8F' => 'rosybrown',
 
135
# '#A0522D' => 'sienna',
 
136
# '#8B4513' => 'saddlebrown',
 
137
# '#D2691E' => 'chocolate',
 
138
# '#CD853F' => 'peru',
 
139
# '#F4A460' => 'sandybrown',
 
140
# '#8B0000' => 'darkred',
 
141
# '#800000' => 'maroon',
 
142
# '#A52A2A' => 'brown',
 
143
# '#B22222' => 'firebrick',
 
144
# '#CD5C5C' => 'indianred',
 
145
# '#F08080' => 'lightcoral',
 
146
# '#FA8072' => 'salmon',
 
147
# '#E9967A' => 'darksalmon',
 
148
# '#FFA07A' => 'lightsalmon',
 
149
# '#FF7F50' => 'coral',
 
150
# '#FF6347' => 'tomato',
 
151
# '#FF8C00' => 'darkorange',
 
152
# '#FFA500' => 'orange',
 
153
# '#FF4500' => 'orangered',
 
154
# '#DC143C' => 'crimson',
 
155
# '#FF0000' => 'red',
 
156
# '#FF1493' => 'deeppink',
 
157
# '#FF00FF' => 'fuchsia',
 
158
# '#FF00FF' => 'magenta',
 
159
# '#FF69B4' => 'hotpink',
 
160
# '#FFB6C1' => 'lightpink',
 
161
# '#FFC0CB' => 'pink',
 
162
# '#DB7093' => 'palevioletred',
 
163
# '#C71585' => 'mediumvioletred',
 
164
# '#800080' => 'purple',
 
165
# '#8B008B' => 'darkmagenta',
 
166
# '#9370DB' => 'mediumpurple',
 
167
# '#8A2BE2' => 'blueviolet',
 
168
# '#4B0082' => 'indigo',
 
169
# '#9400D3' => 'darkviolet',
 
170
# '#9932CC' => 'darkorchid',
 
171
# '#BA55D3' => 'mediumorchid',
 
172
# '#DA70D6' => 'orchid',
 
173
# '#EE82EE' => 'violet',
 
174
# '#DDA0DD' => 'plum',
 
175
# '#D8BFD8' => 'thistle',
 
176
# '#E6E6FA' => 'lavender',
 
177
# '#F8F8FF' => 'ghostwhite',
 
178
# '#F0F8FF' => 'aliceblue',
 
179
# '#F5FFFA' => 'mintcream',
 
180
# '#F0FFF0' => 'honeydew',
 
181
# '#FAFAD2' => 'lightgoldenrodyellow',
 
182
# '#FFFACD' => 'lemonchiffon',
 
183
# '#FFF8DC' => 'cornsilk',
 
184
# '#FFFFE0' => 'lightyellow',
 
185
# '#FFFFF0' => 'ivory',
 
186
# '#FFFAF0' => 'floralwhite',
 
187
# '#FAF0E6' => 'linen',
 
188
# '#FDF5E6' => 'oldlace',
 
189
# '#FAEBD7' => 'antiquewhite',
 
190
# '#FFE4C4' => 'bisque',
 
191
# '#FFDAB9' => 'peachpuff',
 
192
# '#FFEFD5' => 'papayawhip',
 
193
# '#F5F5DC' => 'beige',
 
194
# '#FFF5EE' => 'seashell',
 
195
# '#FFF0F5' => 'lavenderblush',
 
196
# '#FFE4E1' => 'mistyrose',
 
197
# '#FFFAFA' => 'snow',
 
198
# '#FFFFFF' => 'white',
 
199
# '#F5F5F5' => 'whitesmoke',
 
200
# '#DCDCDC' => 'gainsboro',
 
201
# '#D3D3D3' => 'lightgrey',
 
202
 
 
203
############ Encodings ###############
 
204
 
 
205
# Mapping high-bit characters from unicode back to iso-8859-1
 
206
# (a.k.a Windows 1252 a.k.a "ANSI") - http://www.alanwood.net/demos/ansi.html
 
207
our %unicode2HighBit = (
 
208
        chr(8364) => chr(128),  chr(8218) => chr(130),  chr(402)  => chr(131),
 
209
        chr(8222) => chr(132),  chr(8230) => chr(133),  chr(8224) => chr(134),
 
210
        chr(8225) => chr(135),  chr(710)  => chr(136),  chr(8240) => chr(137),
 
211
    chr(352)  => chr(138),      chr(8249) => chr(139),  chr(338)  => chr(140),
 
212
    chr(381)  => chr(142),      chr(8216) => chr(145),  chr(8217) => chr(146),
 
213
        chr(8220) => chr(147),  chr(8221) => chr(148),  chr(8226) => chr(149),
 
214
        chr(8211) => chr(150),  chr(8212) => chr(151),  chr(732)  => chr(152),
 
215
        chr(8482) => chr(153),  chr(353)  => chr(154),  chr(8250) => chr(155),
 
216
    chr(339)  => chr(156),  chr(382)  => chr(158),  chr(376)  => chr(159),
 
217
);
 
218
 
 
219
# Reverse mapping
 
220
our %highBit2Unicode = map { $unicode2HighBit{$_} => $_ } keys %unicode2HighBit;
 
221
 
 
222
our $unicode2HighBitChars = join('', keys %unicode2HighBit);
 
223
our $highBit2UnicodeChars = join('', keys %highBit2Unicode);
 
224
our $encoding;
 
225
 
 
226
sub encoding {
 
227
    unless ($encoding) {
 
228
        $encoding = Encode::resolve_alias(
 
229
            $Foswiki::cfg{Site}{CharSet} || 'iso-8859-1');
 
230
    }
 
231
    return $encoding;
 
232
}
 
233
 
 
234
# Map selected unicode characters back to high-bit chars if
 
235
# iso-8859-1 is selected. This is required because the same characters
 
236
# have different code points in unicode and iso-8859-1. For example,
 
237
# &euro; is 128 in iso-8859-1 and 8364 in unicode.
 
238
sub mapUnicode2HighBit {
 
239
    if (encoding() eq 'iso-8859-1') {
 
240
        # Map unicode back to iso-8859 high-bit chars
 
241
        $_[0] =~ s/([$unicode2HighBitChars])/$unicode2HighBit{$1}/ge;
 
242
    }
 
243
}
 
244
 
 
245
# Map selected high-bit chars to unicode if
 
246
# iso-8859-1 is selected.
 
247
sub mapHighBit2Unicode {
 
248
    if (encoding() eq 'iso-8859-1') {
 
249
        # Map unicode back to iso-8859 high-bit chars
 
250
        $_[0] =~ s/([$highBit2UnicodeChars])/$highBit2Unicode{$1}/ge;
 
251
    }
 
252
}
 
253
 
 
254
# Named entities that we want to convert back to characters, rather
 
255
# than leaving them as HTML entities.
 
256
our @safeEntities = qw(
 
257
    euro   iexcl  cent   pound  curren yen    brvbar sect
 
258
    uml    copy   ordf   laquo  not    shy    reg    macr
 
259
    deg    plusmn sup2   sup3   acute  micro  para   middot
 
260
    cedil  sup1   ordm   raquo  frac14 frac12 frac34 iquest
 
261
    Agrave Aacute Acirc  Atilde Auml   Aring  AElig  Ccedil
 
262
    Egrave Eacute Ecirc  Euml   Igrave Iacute Icirc  Iuml
 
263
    ETH    Ntilde Ograve Oacute Ocirc  Otilde Ouml   times
 
264
    Oslash Ugrave Uacute Ucirc  Uuml   Yacute THORN  szlig
 
265
    agrave aacute acirc  atilde auml   aring  aelig  ccedil
 
266
    egrave eacute ecirc  uml    igrave iacute icirc  iuml
 
267
    eth    ntilde ograve oacute ocirc  otilde ouml   divide
 
268
    oslash ugrave uacute ucirc  uuml   yacute thorn  yuml
 
269
);
 
270
 
 
271
# Mapping from entity names to characters
 
272
our $safe_entities;
 
273
 
 
274
# Get a hash that maps the safe entities values to characters
 
275
# in the site charset.
 
276
sub safeEntities {
 
277
    unless ($safe_entities) {
 
278
        foreach my $entity (@safeEntities) {
 
279
            # Decode the entity name to unicode
 
280
            my $unicode = HTML::Entities::decode_entities("&$entity;");
 
281
            # Map unicode back to iso-8859 high-bit chars if required
 
282
            mapUnicode2HighBit($unicode);
 
283
            $safe_entities->{$entity} = Encode::encode(encoding(), $unicode);
 
284
        }
 
285
    }
 
286
    return $safe_entities;
 
287
}
 
288
 
 
289
# Debug
 
290
sub chCodes {
 
291
    my $text = shift;
 
292
    my $s = "";
 
293
    for (my $i = 0; $i < length($text); $i++) {
 
294
        my $ch = substr($text, $i, 1);
 
295
        if (ord($ch) < 32 || ord($ch) > 127) {
 
296
            $s = $s . '#' . ord($ch) . ';';
 
297
        } else {
 
298
            $s .= $ch;
 
299
        }
 
300
    }
 
301
    return $s;
 
302
}
 
303
 
 
304
# Create shorter alias for other modules
 
305
no strict 'refs';
 
306
*{'WC::'} = \*{'Foswiki::Plugins::WysiwygPlugin::Constants::'};
 
307
 
 
308
1;