[1707] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # SourceCodePlugin.pm -- source code plugin
|
---|
[1731] | 4 | #
|
---|
[1707] | 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
|
---|
[1731] | 28 |
|
---|
[1707] | 29 | # filename is currently used for Title ( optionally minus some prefix )
|
---|
| 30 |
|
---|
[1731] | 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 |
|
---|
[15872] | 37 | package SourceCodePlugin;
|
---|
[1707] | 38 |
|
---|
[15872] | 39 | use ReadTextFile;
|
---|
[1707] | 40 |
|
---|
[10254] | 41 | use strict;
|
---|
| 42 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 43 |
|
---|
[1707] | 44 | sub BEGIN {
|
---|
[15872] | 45 | @SourceCodePlugin::ISA = ('ReadTextFile');
|
---|
[1707] | 46 | }
|
---|
| 47 |
|
---|
[4744] | 48 | my $arguments =
|
---|
| 49 | [ { 'name' => "process_exp",
|
---|
[16019] | 50 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[6408] | 51 | 'type' => "regexp",
|
---|
[4744] | 52 | 'deft' => &get_default_process_exp(),
|
---|
| 53 | 'reqd' => "no" } ,
|
---|
| 54 | { 'name' => "block_exp",
|
---|
[16019] | 55 | 'desc' => "{BasePlugin.block_exp}",
|
---|
[6408] | 56 | 'type' => "regexp",
|
---|
[4744] | 57 | 'deft' => &get_default_block_exp(),
|
---|
[4873] | 58 | 'reqd' => "no" },
|
---|
[4744] | 59 | { 'name' => "remove_prefix",
|
---|
[15872] | 60 | 'desc' => "{SourceCodePlugin.remove_prefix}",
|
---|
[6408] | 61 | 'type' => "regexp",
|
---|
[4873] | 62 | 'deft' => "^.*[/\\]",
|
---|
[4744] | 63 | 'reqd' => "no" } ];
|
---|
[1707] | 64 |
|
---|
[15872] | 65 | my $options = { 'name' => "SourceCodePlugin",
|
---|
| 66 | 'desc' => "{SourceCodePlugin.desc}",
|
---|
[6408] | 67 | 'abstract' => "no",
|
---|
[4744] | 68 | 'inherits' => "yes",
|
---|
| 69 | 'args' => $arguments };
|
---|
[3540] | 70 |
|
---|
[1707] | 71 |
|
---|
| 72 | sub new {
|
---|
[10218] | 73 | my ($class) = shift (@_);
|
---|
| 74 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 75 | push(@$pluginlist, $class);
|
---|
[1707] | 76 |
|
---|
[15872] | 77 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 78 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10218] | 79 |
|
---|
[15872] | 80 | my $self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 81 |
|
---|
[1707] | 82 | return bless $self, $class;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | sub get_default_block_exp {
|
---|
| 86 | my $self = shift (@_);
|
---|
| 87 |
|
---|
| 88 | return q^(?i)\.(o|obj|a|so|dll)$^;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | sub get_default_process_exp {
|
---|
| 92 | my $self = shift (@_);
|
---|
| 93 |
|
---|
| 94 | return q^(Makefile.*|README.*|(?i)\.(c|cc|cpp|C|h|hpp|pl|pm|sh))$^;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[1731] | 97 |
|
---|
| 98 |
|
---|
[1707] | 99 | # do plugin specific processing of doc_obj
|
---|
| 100 | sub process {
|
---|
| 101 | my $self = shift (@_);
|
---|
[6332] | 102 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
[1707] | 103 | my $outhandle = $self->{'outhandle'};
|
---|
| 104 |
|
---|
| 105 | my $cursection = $doc_obj->get_top_section();
|
---|
| 106 |
|
---|
| 107 | my $filetype="text"; # Makefiles, READMEs, ...
|
---|
| 108 | if ($file =~ /\.(cc|h|cpp|C)$/) {$filetype="C++";} # assume all .h files...
|
---|
| 109 | elsif ($file =~ /\.c$/) {$filetype="C";}
|
---|
| 110 | elsif ($file =~ /\.p(l|m)$/) {$filetype="perl";}
|
---|
| 111 | elsif ($file =~ /\.sh$/) {$filetype="sh";}
|
---|
| 112 |
|
---|
| 113 | # modify '<' and '>' for GML... (even though inside <pre> tags!!)
|
---|
| 114 | $$textref =~ s/</</g;
|
---|
| 115 | $$textref =~ s/>/>/g;
|
---|
[1740] | 116 | $$textref =~ s/_/_/g;
|
---|
[1707] | 117 | # try _escape_text($text) from doc.pm....
|
---|
| 118 |
|
---|
[2085] | 119 | # don't want mg to turn escape chars into actual values
|
---|
| 120 | $$textref =~ s/\\/\\\\/g;
|
---|
[1707] | 121 |
|
---|
| 122 | # use filename (minus any prefix) as the title.
|
---|
| 123 | my $title;
|
---|
| 124 | if ($self->{'remove_prefix' ne ""}) {
|
---|
| 125 | ($title = $file) =~ s/^$self->{'remove_prefix'}//;
|
---|
| 126 | } else {
|
---|
[2657] | 127 | ($title = $file) =~ s@^.*[/\\]@@; # remove pathname by default
|
---|
[1707] | 128 | }
|
---|
| 129 | $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
|
---|
[8121] | 130 | $doc_obj->add_metadata ($cursection, "FileFormat", "SRC");
|
---|
| 131 |
|
---|
[2657] | 132 | # remove the gsdl prefix from the filename
|
---|
| 133 | my $relative_filename=$file;
|
---|
| 134 | $relative_filename =~ s@^.*?gsdl[/\\]@@;
|
---|
| 135 | $doc_obj->add_utf8_metadata ($cursection, "filename", $relative_filename);
|
---|
[1707] | 136 |
|
---|
| 137 | # class information from .h and .cc and .C and .cpp files
|
---|
| 138 | if ($filetype eq "C++")
|
---|
| 139 | {
|
---|
[1731] | 140 | process_c_plus_plus($textref,$pluginfo, $base_dir,
|
---|
[1707] | 141 | $file, $metadata, $doc_obj);
|
---|
[1731] | 142 | } elsif ($filetype eq "C")
|
---|
| 143 | {
|
---|
| 144 | get_includes_metadata($textref, $doc_obj);
|
---|
[1707] | 145 | }
|
---|
[1731] | 146 |
|
---|
| 147 |
|
---|
[1707] | 148 | # default operation...
|
---|
| 149 | # insert preformat tags and add text to document object
|
---|
| 150 | $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
|
---|
| 151 |
|
---|
| 152 | return 1;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 |
|
---|
[1731] | 157 |
|
---|
| 158 | sub get_includes_metadata {
|
---|
| 159 | my ($textref, $doc_obj) = @_;
|
---|
| 160 |
|
---|
[1707] | 161 | my $topsection = $doc_obj->get_top_section();
|
---|
| 162 |
|
---|
| 163 | # Get '#include' directives for metadata
|
---|
[3919] | 164 | if ($$textref !~ /\#\s*include\b/) {
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
[1707] | 167 |
|
---|
[3919] | 168 | my @includes =
|
---|
| 169 | ($$textref =~ m/^\s*\#\s*include\s*(?:\"|<)(.*?)(?:\"|>)/mg);
|
---|
| 170 |
|
---|
[10254] | 171 | my $incs_done_ref=$doc_obj->get_metadata($topsection, "includes");
|
---|
[3919] | 172 | my @incs_done;
|
---|
| 173 | if (defined($incs_done_ref)) {
|
---|
| 174 | @incs_done=@$incs_done_ref;
|
---|
| 175 | } else {
|
---|
| 176 | @incs_done=();
|
---|
| 177 | }
|
---|
[1707] | 178 |
|
---|
[3919] | 179 | foreach my $inc (@includes) {
|
---|
| 180 | # add entries, but only if they don't already exist
|
---|
| 181 | if (!join('', map {$_ eq "$inc"?1:""} @incs_done)) {
|
---|
| 182 | push @incs_done, $inc;
|
---|
| 183 | $doc_obj->add_utf8_metadata($topsection, "includes", $inc);
|
---|
[1707] | 184 | }
|
---|
| 185 | }
|
---|
[1731] | 186 | }
|
---|
[1707] | 187 |
|
---|
| 188 |
|
---|
| 189 |
|
---|
[1731] | 190 | sub process_c_plus_plus {
|
---|
| 191 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
[1707] | 192 |
|
---|
[1731] | 193 | my $topsection = $doc_obj->get_top_section();
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | # Check for include metadata
|
---|
| 197 | get_includes_metadata($textref, $doc_obj);
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
[1707] | 201 | # Get class declarations (but not forward declarations...) as metadata
|
---|
| 202 | if ($$textref =~ /\bclass\b/ ) {
|
---|
| 203 | my $classnames=$$textref;
|
---|
| 204 |
|
---|
| 205 | # remove commented lines
|
---|
| 206 | $classnames =~ s@/\*.*?\*/@@sg;
|
---|
| 207 | $classnames =~ s@//.*$@@mg;
|
---|
| 208 | while ($classnames =~ /\bclass\b/) {
|
---|
| 209 |
|
---|
| 210 | # delete all lines up to the next "class"
|
---|
| 211 | while ($classnames !~ /^[^\n]*\bclass\b[^\n]*\n/)
|
---|
| 212 | {$classnames =~ s/.*\n//;}
|
---|
| 213 |
|
---|
| 214 | # $classnames =~ s/^([^c][^l])*(.)?$//mg; # delete unneccessary lines
|
---|
| 215 |
|
---|
| 216 | # get the line including the next "class" and remove it from
|
---|
| 217 | # our tmp text.
|
---|
| 218 | $classnames =~ s/^(.*\bclass\b.*)$//m;
|
---|
| 219 |
|
---|
| 220 | # don't index if merely a reference/fwd decl. of another class
|
---|
| 221 | if ($1 !~ /(friend\Wclass)|(class\W\w+\W?\;)|(\/\/.*class)/) {
|
---|
| 222 | # $1 is still the whole line - eg:
|
---|
| 223 | # "class StaffSystem: public BaseStaffSystem"
|
---|
| 224 | my $wholeline=$1;
|
---|
| 225 | my $classname=$1;
|
---|
| 226 | $classname =~ s/.*class\W(\w+).*/$1/;
|
---|
[10254] | 227 | my $classes=$doc_obj->get_metadata($topsection, "class");
|
---|
[1707] | 228 | foreach my $elem (@$classes) {
|
---|
| 229 | if ("$elem" eq "$classname") {goto class_done;}
|
---|
| 230 | }
|
---|
| 231 | $doc_obj->add_utf8_metadata($topsection, "class", $classname);
|
---|
| 232 | class_done:
|
---|
| 233 | $doc_obj->add_utf8_metadata($topsection, "classdecl", $wholeline);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | } # end of "class"
|
---|
| 237 |
|
---|
| 238 | return 1;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | 1;
|
---|
| 242 |
|
---|