/
css.tag
206 lines (146 loc) · 4.16 KB
/
css.tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
UserTag css Order name
UserTag css addAttr
UserTag css Routine <<EOR
sub {
my ($name, $opt) = @_;
use vars qw/$Tag/;
=head1 NAME
css -- ITL tag to build css files for <link>
=head1 SYNOPSIS
[css name=CSS_VAR (options)]
=head1 DESCRIPTION
Builds a CSS file from a Variable (or other source) and generates a
link to it.
In the simplest case:
[css THEME_CSS]
it looks for the file C<images/them_css.css>, and if it exists generates
a <C<link rel=stylesheet href="/foundation/images/theme_css.css">> HTML
tag to call it.
=head2 OPTIONS
=over 4
=item basefile
If the Variable being used is dynamic via DirConfig, this should be the
file that it is contained in. The file will be checked for mod time, and
if it is newer than the CSS file the CSS will be rebuilt.
=item imagedir
An image prefix to use instead of the default (the ImageDir directive).
=item literal
The literal CSS to use instead of a Variable. Normally, you would do:
[set my_css]
BODY { }
TD { font-size: 11 pt}
[/set]
Then call with:
[css literal="[scratch my_css]"]
=item media
If you need a media code for the <C<link>> tag, you can set it here. In
other words:
[css name=THEME_CSS media=PRINT]
will generate:
<link rel="stylesheet" media="PRINT" href="/found/images/theme_css.css">
=item mode
The mode (in octal) of the file to be created.
=item output-dir
The output directory to place the generated CSS file in, by default "images".
Obviously you must make the ImageDir match this.
=item relative
Makes the generated CSS file be relative to the directory the IC page is
in. If the current page is "info/index", and the CSS tag is called,
it will write the output to <images/info/theme_css.css> and generate:
<link rel="stylesheet" media="PRINT" href="/found/images/info/theme_css.css">
=item timed
Regenerates the file on a timed basis. Default is the number of minutes,
but you can pass any standard Interchange interval (i.e. seconds, minutes,
days, weeks).
=back
=head1 AUTHOR
Mike Heins, Perusion <mikeh@perusion.com>
=cut
return unless $name;
my $bn = lc $name;
$bn .= '.css';
my $dir = $opt->{output_dir} ||= 'images';
my $add_imagedir = ! $opt->{no_imagedir};
my $id = $opt->{imagedir} || $Vend::Cfg->{ImageDir};
$id =~ s:/*$:/:;
$dir =~ s:/+$::;
if($opt->{relative}) {
my @dirs = split m{/}, $Global::Variable->{MV_PAGE};
pop @dirs;
if(@dirs) {
$id .= join "/", @dirs, '';
$dir = join "/", $dir, @dirs;
}
}
my $sourcetime;
if($opt->{basefile}) {
$sourcetime = (stat($opt->{basefile}))[9];
#::logDebug("basefile=$opt->{basefile} sourcetime=$sourcetime");
}
my $url = "$id$bn";
my $fn = "$dir/$bn";
my $write;
my $success;
my @stat = stat($fn);
my $writable;
if(@stat) {
$writable = -w _;
if($opt->{basefile}) {
if($sourcetime > $stat[9]) {
#::logDebug("Found a basefile, out of date at modtime=$stat[9]");
$write = 1;
}
else {
#::logDebug("Found a basefile, in date at modtime=$stat[9]");
$success = 1;
}
}
elsif($opt->{timed}) {
my $now = time();
$opt->{timed} .= ' min' if $opt->{timed} =~ /^\d+$/;
my $secs = Vend::Config::time_to_seconds($opt->{timed});
#::logDebug("timed seconds = $secs");
my $fliptime = $stat[9] + $secs;
#::logDebug("fliptime=$fliptime now=$now");
if ($fliptime <= $now) {
$write = 1;
}
else {
$success = 1;
}
}
else {
$success = 1;
}
}
else {
$writable = -w $dir;
$write = 1;
}
my $extra = '';
$extra .= qq{ media="$opt->{media}"} if $opt->{media};
my $css;
WRITE: {
last WRITE unless $write;
if(! $writable) {
if(@stat) {
logError("CSS file %s has no write permission.", $fn);
}
else {
logError("CSS dir %s has no write permission.", $dir);
}
last WRITE;
}
my $mode = $opt->{mode} ? oct($opt->{mode}) : 0644;
$css = length($opt->{literal})
? $opt->{literal}
: interpolate_html($Tag->var($name));
$css =~ s/^\s*<style.*?>\s*//si;
$css =~ s:\s*</style>\s*$:\n:i;
$success = $Tag->write_relative_file($fn, $css) && chmod($mode, $fn)
or logError("Error writing CSS file %s, returning in page", $fn);
}
return qq{<link rel="stylesheet" href="$url">} if $success;
return qq{<style type="text/css">\n$css</style>};
}
EOR