File Coverage

blib/lib/Apache/AxKit/Provider/File/Syntax.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # $Id: Syntax.pm,v 1.7 2004/07/16 04:40:56 nachbaur Exp $
2              
3             package Apache::AxKit::Provider::File::Syntax;
4 1     1   876 use strict;
  1         2  
  1         50  
5 1     1   5 use vars qw/@ISA/;
  1         3  
  1         67  
6 1     1   1721 use Apache::AxKit::Provider::File;
  0            
  0            
7             @ISA = ('Apache::AxKit::Provider::File');
8              
9             our $VERSION = 0.06;
10             our $noMimeInfo = 0; # unless told otherwise, we use File::MimeInfo::Magic
11              
12             use Apache;
13             use Apache::Log;
14             use Apache::Constants qw(HTTP_OK);
15             use Apache::AxKit::Exception;
16             use Apache::AxKit::Provider;
17             use Text::VimColor;
18             use AxKit;
19             use File::Spec;
20             use Fcntl qw(O_RDONLY LOCK_SH);
21              
22             # see if we can use File::MimeInfo::Magic
23             eval "use File::MimeInfo::Magic qw( mimetype )";
24             $noMimeInfo = 1 if $@;
25              
26             #
27             # We can't output a filehandle, so throw the necessary exception
28             sub get_fh {
29             throw Apache::AxKit::Exception::IO( -text => "Can't get fh for Syntax" );
30             }
31              
32             #
33             # perform the necessary mime-type processing magic
34             sub get_strref {
35             my $self = shift;
36              
37             # Return the XML data if we've already computed it
38             return \$$self{xml} if ($self->{xml});
39              
40             # Let the superclass A:A:P:File handle the request if
41             # this is a directory
42             if ($self->_is_dir()) {
43             return $self->SUPER::get_strref();
44             }
45              
46             # Process the file with Text::VimColor
47             my $filetype = '';
48             $filetype = $self->_resolve_type unless $noMimeInfo;
49             my $syntax = undef;
50             if ($filetype) {
51             $syntax = new Text::VimColor(
52             file => $self->{file},
53             filetype => $filetype,
54             xml_root_element => 0, # We'll add the root ourselves
55             );
56             } else {
57             # either the filetype is empty or set to 'plain'
58             # in both cases, we let VimColor take care of
59             # figuring out what kind of file this is
60             $syntax = new Text::VimColor(
61             file => $self->{file},
62             xml_root_element => 0, # We'll add the root ourselves
63             );
64             }
65              
66             # Fetch the XML and return it
67             my $data = $syntax->xml;
68              
69             # Trim off the > that Text::VimColor always seems to add
70             $data =~ s/>>/>/;
71              
72             # Add tags, since it would be nice to have
73             my $filename = $self->{file};
74             $self->{data} = qq{\n
75             $self->{data} .= qq{ type="$filetype"} if ($filetype);
76             $self->{data} .= qq{ filename="$filename">};
77             my $line_number = 0;
78             foreach my $line (split(/\n/, $data)) {
79             $line_number++;
80             $self->{data} .= qq{$line$1};
81             }
82             $self->{data} .= qq{};
83              
84             return \$$self{data};
85             }
86              
87             sub _resolve_type {
88             my ($self) = shift;
89             # Figure out the mime-type, and rip it apart to determine
90             # what VIM syntax file this should use
91             my $mimetype = mimetype($self->{file});
92             $mimetype = '' if $mimetype eq 'text/plain'; # I don't believe you!
93             if ($mimetype) {
94             AxKit::Debug(8, "MimeInfo::Magic recognized file as '$mimetype'");
95             }
96             # reformating for VimColor
97             my $filetype = $mimetype;
98             $filetype =~ s/^(?:application|text)\/(?:x\-)?(.*)$/$1/;
99             $filetype = 'xml' if $filetype eq 'rdf';
100             return $filetype;
101             }
102              
103             1;
104             __END__