line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
## Emacs: -*- tab-width: 4; -*- |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
662
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Apache::PrettyText; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); $VERSION = '1.08'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
91
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=pod |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Apache::PrettyText - Simple mod_perl PerlHandler for text files |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
## In httpd.conf: |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Apache::PrettyText; |
21
|
|
|
|
|
|
|
Perl> ## <-- Omit the space if you copy this example. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
SetHandler perl-script |
25
|
|
|
|
|
|
|
PerlHandler Apache::PrettyText |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
To modify your Apache server to dynamically format .txt files so they |
29
|
|
|
|
|
|
|
look nicer in the client's browser, put the following directives into |
30
|
|
|
|
|
|
|
httpd.conf, or in any VirtualHost section and restart the server. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Optional: Insert a section that changes |
33
|
|
|
|
|
|
|
$Apache::PrettyText::TabWidth to your site's standard or set to 0 to |
34
|
|
|
|
|
|
|
disable detabbing. If you don't set it, the default is 4. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
## In httpd.conf: |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$Apache::PrettyText::TabWidth = 4; |
39
|
|
|
|
|
|
|
Perl> ## <-- Omit the space if you copy this example. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
You must be using mod_perl. See http://perl.apache.org for details. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This is a simple Apache handler written in Perl that converts text |
46
|
|
|
|
|
|
|
files on the fly into a basic HTML format: |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=over 4 |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item * |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
surrounded by tags |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item * |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
tabs converted to spaces (optional) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item * |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
white background |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item * |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
hilited URLs |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item * |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
first line of text file = |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=back |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Also serves as a good template to help you write your own simple |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
handler. I wrote this as an exercise because I found no good |
75
|
|
|
|
|
|
|
examples. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 INSTALLATION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Using CPAN module: |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
perl -MCPAN -e 'install Apache::PrettyText' |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Or manually: |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
tar xzvf Apache-PrettyText*gz |
86
|
|
|
|
|
|
|
cd Apache-PrettyText-1.?? |
87
|
|
|
|
|
|
|
perl Makefile.PL |
88
|
|
|
|
|
|
|
make |
89
|
|
|
|
|
|
|
make test |
90
|
|
|
|
|
|
|
make install |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
If you're reading this in pod or man, it's already installed. If |
93
|
|
|
|
|
|
|
you're reading the source code in PrettyText.pm, you can copy this |
94
|
|
|
|
|
|
|
file under the name "PrettyText.pm" into this location: |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
/usr/lib/perl5/site_perl/Apache/ |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
... or its equivalent on your computer. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
A helpful tip: you can include the entire contents of the |
102
|
|
|
|
|
|
|
PrettyText.pm file or of your own version of it into a section |
103
|
|
|
|
|
|
|
within httpd.conf. This can be very helpful if you'd like to use this |
104
|
|
|
|
|
|
|
module as a template for your own. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
To syntax-check your code under those circumstances, use: |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
perl -cx httpd.conf |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
... which will read just the perl code between #!...perl and __END__ |
111
|
|
|
|
|
|
|
in the httpd.conf file. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 AUTHOR |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Chris Thorman |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Copyright (c) 1995-2002 Chris Thorman. All rights reserved. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
120
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 SEE ALSO |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Apache(3), mod_perl, http://perl.apache.org/src/contrib |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The Apache::PrettyText home page: |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
http://christhorman.com/projects/perl/Apache-PrettyText/ |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
The implementation in PrettyText.pm. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 THANKS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Thanks to Vivek Khera, Doug MacEachern, Jeffrey William Baker for |
135
|
|
|
|
|
|
|
suggestions and corrections. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut {}; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
## use Apache::Constants ':common'; ## for OK (200) and NOT_FOUND (304) |
140
|
1
|
|
|
1
|
|
5
|
use constant OK => 200; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
77
|
|
141
|
1
|
|
|
1
|
|
5
|
use constant NOT_FOUND => 304; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
925
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub handler |
144
|
|
|
|
|
|
|
{ |
145
|
0
|
|
|
0
|
0
|
|
my ($r) = @_; |
146
|
0
|
|
|
|
|
|
my $Status = NOT_FOUND; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
## Get a unique file handle; register a routine to guarantee it |
149
|
|
|
|
|
|
|
## gets closed. |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $fh = $r->gensym; |
152
|
0
|
|
|
0
|
|
|
$r->register_cleanup(sub {close $fh}); |
|
0
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
## Open the requested file. |
155
|
0
|
|
|
|
|
|
my $FileName = $r->filename; |
156
|
0
|
0
|
0
|
|
|
|
$r->log_error("Apache::PrettyText: $FileName not found."), goto done |
|
|
|
0
|
|
|
|
|
157
|
|
|
|
|
|
|
unless ((-f $FileName) && (-r $FileName) && (open $fh, "<$FileName")); |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
## Read entire file into memory. |
160
|
0
|
|
|
|
|
|
local $/; undef $/; |
|
0
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $contents = <$fh>; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
## Tab width for de-tabbing; 0 means don't detab. Override the |
164
|
|
|
|
|
|
|
## default setting of 4 by setting $Apache::PrettyText::TabWidth |
165
|
|
|
|
|
|
|
## in httpd.conf. |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
my $TW = $Apache::PrettyText::TabWidth; |
168
|
0
|
0
|
|
|
|
|
$TW = 4 unless defined($TW); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
## Detab |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
$contents =~ s{^(.*)} |
173
|
0
|
|
|
|
|
|
{my $X=$1; |
174
|
0
|
|
|
|
|
|
while($X=~s/^(.*?)\t/$1.' 'x($TW-length($1)%$TW)/e){}; $X |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
}meg if $TW > 1; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
## Quote <, > and & characters, allowing HTML markups to appear as |
178
|
|
|
|
|
|
|
## such. |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
$contents =~ s/&/&/g; |
181
|
0
|
|
|
|
|
|
$contents =~ s/</g; |
182
|
0
|
|
|
|
|
|
$contents =~ s/>/>/g; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
## Make URLS into links |
185
|
0
|
|
|
|
|
|
$contents =~ s{\b((s?https?|ftp|gopher|news|telnet|wais|mailto): |
186
|
|
|
|
|
|
|
(//[-a-zA-Z0-9_\.]+:[0-9]*)? |
187
|
|
|
|
|
|
|
[-a-zA-Z0-9_=?#\$@~`%&:;*+|\/\.,]* |
188
|
|
|
|
|
|
|
[-a-zA-Z0-9_=#\$@~`%&:;*+|\/])} |
189
|
|
|
|
|
|
|
{$1}igx; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
## Fix any A HREFs to unquote the quoted stuff... |
192
|
0
|
|
|
|
|
|
$contents =~ s{(A HREF=")(.*?)(">.*?)}{my ($x,$y,$z)=($1,$2,$3); |
|
0
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
$y=~s{>}{>}g; |
194
|
0
|
|
|
|
|
|
$y=~s{<}{<}g; |
195
|
0
|
|
|
|
|
|
$y=~s{&}{&}g; |
196
|
0
|
|
|
|
|
|
"$x$y$z"}eigx; |
197
|
|
|
|
|
|
|
|
198
|
0
|
0
|
|
|
|
|
&$Apache::PrettyText::TextCleanHook(\$contents) if ref($Apache::PrettyText::TextCleanHook) eq 'CODE'; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
## Wrap in a simple HTML page |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
my ($title) = ($contents =~ /(\w.*)/); |
203
|
0
|
|
|
|
|
|
$contents = "$title |
204
|
|
|
|
|
|
|
\n$contents\n "; |
205
|
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
$r->content_type("text/html"); |
207
|
0
|
|
|
|
|
|
$r->send_http_header; |
208
|
0
|
|
|
|
|
|
$r->print($contents); |
209
|
0
|
|
|
|
|
|
$Status = OK; |
210
|
|
|
|
|
|
|
|
211
|
0
|
|
|
|
|
|
done: |
212
|
|
|
|
|
|
|
return $Status; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
1; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|