source: trunk/gsdl/perllib/plugins/SRCPlug.pm@ 8121

Last change on this file since 8121 was 8121, checked in by chi, 20 years ago

Add the "FileFormat" metadata to each of the Plugins.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1###########################################################################
2#
3# SRCPlug.pm -- source code plugin
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 1999 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26# John McPherson Nov 2000
27# originally based on TEXTPlug
28
29# filename is currently used for Title ( optionally minus some prefix )
30
31# Current languages:
32# text: READMEs/Makefiles
33# C/C++ (currently extracts #include statements and C++ class decls)
34# Perl (currently only done as text)
35# Shell (currently only done as text)
36
37# 12/05/02 Added usage datastructure - John Thompson
38
39package SRCPlug;
40
41use BasPlug;
42use parsargv;
43
44sub BEGIN {
45 @ISA = ('BasPlug');
46}
47
48my $arguments =
49 [ { 'name' => "process_exp",
50 'desc' => "{BasPlug.process_exp}",
51 'type' => "regexp",
52 'deft' => &get_default_process_exp(),
53 'reqd' => "no" } ,
54 { 'name' => "block_exp",
55 'desc' => "{BasPlug.block_exp}",
56 'type' => "regexp",
57 'deft' => &get_default_block_exp(),
58 'reqd' => "no" },
59 { 'name' => "remove_prefix",
60 'desc' => "{SRCPlug.remove_prefix}",
61 'type' => "regexp",
62 'deft' => "^.*[/\\]",
63 'reqd' => "no" } ];
64
65my $options = { 'name' => "SRCPlug",
66 'desc' => "{SRCPlug.desc}",
67 'abstract' => "no",
68 'inherits' => "yes",
69 'args' => $arguments };
70
71
72sub new {
73 my ($class) = @_;
74 my $self = new BasPlug ($class, @_);
75 $self->{'plugin_type'} = "SRCPlug";
76 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
77 my $option_list = $self->{'option_list'};
78 push( @{$option_list}, $options );
79
80 if (!parsargv::parse(\@_,
81 q^remove_prefix/(\S+)/^, \$self->{'remove_prefix'},
82 "allow_extra_options"
83 )
84 ) {
85 print STDERR "\nIncorrect options passed to SRCPlug, ";
86 print STDERR "check your collect.cfg configuration file\n";
87 $self->print_txt_usage(""); # Use default resource bundle
88 die "\n";
89 }
90 return bless $self, $class;
91}
92
93sub get_default_block_exp {
94 my $self = shift (@_);
95
96 return q^(?i)\.(o|obj|a|so|dll)$^;
97}
98
99sub get_default_process_exp {
100 my $self = shift (@_);
101
102# return q^(?i)\.te?xt$^;
103 return q^(Makefile.*|README.*|(?i)\.(c|cc|cpp|C|h|hpp|pl|pm|sh))$^;
104}
105
106
107
108# do plugin specific processing of doc_obj
109sub process {
110 my $self = shift (@_);
111 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
112 my $outhandle = $self->{'outhandle'};
113
114 print STDERR "<Processing n='$file' p='SRCPlug'>\n" if ($gli);
115 print $outhandle "SRCPlug: processing $file\n"
116 if $self->{'verbosity'} > 1;
117
118 my $cursection = $doc_obj->get_top_section();
119
120 my $filetype="text"; # Makefiles, READMEs, ...
121 if ($file =~ /\.(cc|h|cpp|C)$/) {$filetype="C++";} # assume all .h files...
122 elsif ($file =~ /\.c$/) {$filetype="C";}
123 elsif ($file =~ /\.p(l|m)$/) {$filetype="perl";}
124 elsif ($file =~ /\.sh$/) {$filetype="sh";}
125
126 # modify '<' and '>' for GML... (even though inside <pre> tags!!)
127 $$textref =~ s/</&lt;/g;
128 $$textref =~ s/>/&gt;/g;
129 $$textref =~ s/_/&#95;/g;
130 # try _escape_text($text) from doc.pm....
131
132 # don't want mg to turn escape chars into actual values
133 $$textref =~ s/\\/\\\\/g;
134
135 # use filename (minus any prefix) as the title.
136 my $title;
137 if ($self->{'remove_prefix' ne ""}) {
138 ($title = $file) =~ s/^$self->{'remove_prefix'}//;
139 } else {
140 ($title = $file) =~ s@^.*[/\\]@@; # remove pathname by default
141 }
142 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
143 $doc_obj->add_metadata ($cursection, "FileFormat", "SRC");
144
145 # remove the gsdl prefix from the filename
146 my $relative_filename=$file;
147 $relative_filename =~ s@^.*?gsdl[/\\]@@;
148 $doc_obj->add_utf8_metadata ($cursection, "filename", $relative_filename);
149
150 # class information from .h and .cc and .C and .cpp files
151 if ($filetype eq "C++")
152 {
153 process_c_plus_plus($textref,$pluginfo, $base_dir,
154 $file, $metadata, $doc_obj);
155 } elsif ($filetype eq "C")
156 {
157 get_includes_metadata($textref, $doc_obj);
158 }
159
160
161 # default operation...
162 # insert preformat tags and add text to document object
163 $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
164
165 return 1;
166}
167
168
169
170
171sub get_includes_metadata {
172 my ($textref, $doc_obj) = @_;
173
174 my $topsection = $doc_obj->get_top_section();
175
176 # Get '#include' directives for metadata
177 if ($$textref !~ /\#\s*include\b/) {
178 return;
179 }
180
181 my @includes =
182 ($$textref =~ m/^\s*\#\s*include\s*(?:\"|&lt;)(.*?)(?:\"|&gt;)/mg);
183
184 my $incs_done_ref=$doc_obj->get_metadata($section, "includes");
185 my @incs_done;
186 if (defined($incs_done_ref)) {
187 @incs_done=@$incs_done_ref;
188 } else {
189 @incs_done=();
190 }
191
192 foreach my $inc (@includes) {
193 # add entries, but only if they don't already exist
194 if (!join('', map {$_ eq "$inc"?1:""} @incs_done)) {
195 push @incs_done, $inc;
196 $doc_obj->add_utf8_metadata($topsection, "includes", $inc);
197 }
198 }
199}
200
201
202
203sub process_c_plus_plus {
204 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
205
206 my $topsection = $doc_obj->get_top_section();
207
208
209 # Check for include metadata
210 get_includes_metadata($textref, $doc_obj);
211
212
213
214 # Get class declarations (but not forward declarations...) as metadata
215 if ($$textref =~ /\bclass\b/ ) {
216 my $classnames=$$textref;
217
218 # remove commented lines
219 $classnames =~ s@/\*.*?\*/@@sg;
220 $classnames =~ s@//.*$@@mg;
221 while ($classnames =~ /\bclass\b/) {
222
223 # delete all lines up to the next "class"
224 while ($classnames !~ /^[^\n]*\bclass\b[^\n]*\n/)
225 {$classnames =~ s/.*\n//;}
226
227# $classnames =~ s/^([^c][^l])*(.)?$//mg; # delete unneccessary lines
228
229 # get the line including the next "class" and remove it from
230 # our tmp text.
231 $classnames =~ s/^(.*\bclass\b.*)$//m;
232
233 # don't index if merely a reference/fwd decl. of another class
234 if ($1 !~ /(friend\Wclass)|(class\W\w+\W?\;)|(\/\/.*class)/) {
235 # $1 is still the whole line - eg:
236 # "class StaffSystem: public BaseStaffSystem"
237 my $wholeline=$1;
238 my $classname=$1;
239 $classname =~ s/.*class\W(\w+).*/$1/;
240 my $classes=$doc_obj->get_metadata($section, "class");
241 foreach my $elem (@$classes) {
242 if ("$elem" eq "$classname") {goto class_done;}
243 }
244 $doc_obj->add_utf8_metadata($topsection, "class", $classname);
245 class_done:
246 $doc_obj->add_utf8_metadata($topsection, "classdecl", $wholeline);
247 }
248 }
249 } # end of "class"
250
251 return 1;
252}
253
2541;
255
Note: See TracBrowser for help on using the repository browser.