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

Last change on this file since 1917 was 1787, checked in by jrm21, 23 years ago

"allow_extra_options" missing, to get inherited options

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 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
38package SRCPlug;
39
40use BasPlug;
41use parsargv;
42
43sub BEGIN {
44 @ISA = ('BasPlug');
45}
46
47
48sub print_usage {
49 print STDERR "\n usage: plugin SRCPlug [options]\n";
50 print STDERR "Try to import C and C++ source code. Adds \"class\"";
51 print STDERR " metadata.\n\n";
52 print STDERR " options:\n";
53 print STDERR " -remove_prefix <pattern> Remove this leading pattern from the filename\n";
54 print STDERR " (eg -remove_prefix /tmp/XX/src/). The default is to\n";
55 print STDERR " remove the whole path from the filename.\n";
56 print STDERR "\n";
57}
58
59sub new {
60 my ($class) = @_;
61 my $self = new BasPlug ($class, @_);
62
63 if (!parsargv::parse(\@_,
64 q^remove_prefix/(\S+)/^, \$self->{'remove_prefix'},
65 "allow_extra_options"
66 )
67 ) {
68 print STDERR "\nIncorrect options passed to SRCPlug, ";
69 print STDERR "check your collect.cfg configuration file\n";
70 &print_usage();
71 die "\n";
72 }
73 return bless $self, $class;
74}
75
76sub get_default_block_exp {
77 my $self = shift (@_);
78
79 return q^(?i)\.(o|obj|a|so|dll)$^;
80}
81
82sub get_default_process_exp {
83 my $self = shift (@_);
84
85# return q^(?i)\.te?xt$^;
86 return q^(Makefile.*|README.*|(?i)\.(c|cc|cpp|C|h|hpp|pl|pm|sh))$^;
87}
88
89
90
91# do plugin specific processing of doc_obj
92sub process {
93 my $self = shift (@_);
94 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
95 my $outhandle = $self->{'outhandle'};
96
97 print $outhandle "SRCPlug: processing $file\n"
98 if $self->{'verbosity'} > 1;
99
100 my $cursection = $doc_obj->get_top_section();
101
102 my $filetype="text"; # Makefiles, READMEs, ...
103 if ($file =~ /\.(cc|h|cpp|C)$/) {$filetype="C++";} # assume all .h files...
104 elsif ($file =~ /\.c$/) {$filetype="C";}
105 elsif ($file =~ /\.p(l|m)$/) {$filetype="perl";}
106 elsif ($file =~ /\.sh$/) {$filetype="sh";}
107
108 # modify '<' and '>' for GML... (even though inside <pre> tags!!)
109 $$textref =~ s/</&lt;/g;
110 $$textref =~ s/>/&gt;/g;
111 $$textref =~ s/_/&#95;/g;
112 # try _escape_text($text) from doc.pm....
113
114
115 # use filename (minus any prefix) as the title.
116 my $title;
117 if ($self->{'remove_prefix' ne ""}) {
118 ($title = $file) =~ s/^$self->{'remove_prefix'}//;
119 } else {
120 ($title = $file) =~ s@^.*(/|\\)@@; # remove pathname by default
121 }
122 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
123 $doc_obj->add_utf8_metadata ($cursection, "filename", $file);
124
125 # class information from .h and .cc and .C and .cpp files
126 if ($filetype eq "C++")
127 {
128 process_c_plus_plus($textref,$pluginfo, $base_dir,
129 $file, $metadata, $doc_obj);
130 } elsif ($filetype eq "C")
131 {
132 get_includes_metadata($textref, $doc_obj);
133 }
134
135
136 # default operation...
137 # insert preformat tags and add text to document object
138 $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
139
140 return 1;
141}
142
143
144
145
146sub get_includes_metadata {
147 my ($textref, $doc_obj) = @_;
148
149 my $topsection = $doc_obj->get_top_section();
150
151 # Get '#include' directives for metadata
152 if ($$textref =~ /\#\W?include\b/) {
153 my $includes=$$textref;
154
155 # remove commented lines
156 $includes =~ s@/\*.*?\*/@@sg; # treat string as single line
157 # ? means match smallest instead of longest !!!
158 $includes =~ s@//.*$@@mg; # treat string as multiple lines
159
160 # remove non- include lines (well, lines without a '#')
161 $includes =~ s/^[^\#]*$//mg;
162
163 # lines don't always start '#include "'.... we have to allow (eg)
164 # '# include ...'
165 # ' #include ...'
166 while ($includes =~ /\#\W?include/) {
167 $includes =~ s/^.*?include.*?(\"|&lt;)(.*)(\"|&gt;).*$//m;
168 # $1 is now the filename between the "s.
169 my $include=$2;
170 # remove leading pathname
171 $include =~ s@^.*(/|\\)@@;
172
173 my $incs_done=$doc_obj->get_metadata($section, "includes");
174 foreach my $elem (@$incs_done) {
175 if ("$elem" eq "$include") {goto header_done;}
176 }
177 $doc_obj->add_utf8_metadata($topsection, "includes", $include);
178 header_done:
179 }
180 }
181}
182
183
184
185sub process_c_plus_plus {
186 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
187
188 my $topsection = $doc_obj->get_top_section();
189
190
191 # Check for include metadata
192 get_includes_metadata($textref, $doc_obj);
193
194
195
196 # Get class declarations (but not forward declarations...) as metadata
197 if ($$textref =~ /\bclass\b/ ) {
198 my $classnames=$$textref;
199
200 # remove commented lines
201 $classnames =~ s@/\*.*?\*/@@sg;
202 $classnames =~ s@//.*$@@mg;
203 while ($classnames =~ /\bclass\b/) {
204
205 # delete all lines up to the next "class"
206 while ($classnames !~ /^[^\n]*\bclass\b[^\n]*\n/)
207 {$classnames =~ s/.*\n//;}
208
209# $classnames =~ s/^([^c][^l])*(.)?$//mg; # delete unneccessary lines
210
211 # get the line including the next "class" and remove it from
212 # our tmp text.
213 $classnames =~ s/^(.*\bclass\b.*)$//m;
214
215 # don't index if merely a reference/fwd decl. of another class
216 if ($1 !~ /(friend\Wclass)|(class\W\w+\W?\;)|(\/\/.*class)/) {
217 # $1 is still the whole line - eg:
218 # "class StaffSystem: public BaseStaffSystem"
219 my $wholeline=$1;
220 my $classname=$1;
221 $classname =~ s/.*class\W(\w+).*/$1/;
222 my $classes=$doc_obj->get_metadata($section, "class");
223 foreach my $elem (@$classes) {
224 if ("$elem" eq "$classname") {goto class_done;}
225 }
226 $doc_obj->add_utf8_metadata($topsection, "class", $classname);
227 class_done:
228 $doc_obj->add_utf8_metadata($topsection, "classdecl", $wholeline);
229 }
230 }
231 } # end of "class"
232
233 return 1;
234}
235
2361;
237
Note: See TracBrowser for help on using the repository browser.