blib/lib/Apache/AxKit/Provider/File/Syntax.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw/@ISA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Apache::AxKit::Provider::File;
Subroutine BEGIN: called CC=1 SCAR=2.6
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 <syn:line> tags, since it would be nice to have
73 my $filename = $self->{file};
74 $self->{data} = qq{<?xml version="1.0"?>\n<syn:syntax xmlns:syn="http://ns.laxan.com/text-vimcolor/1"};
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{<syn:Line number="$line_number">$line$1</syn:Line>};
81 }
82 $self->{data} .= qq{</syn:syntax>};
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__
↑ Summary