root/trunk/gsdl/perllib/plugins/TEXTPlug.pm @ 2450

Revision 2450, 3.2 KB (checked in by jrm21, 19 years ago)

now accepts the "-title_sub" option, a regexp to remove when automatically
extracting title from text.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1###########################################################################
2#
3# TEXTPlug.pm -- simple text plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# creates simple single-level document. Adds Title metadata
27# of first line of text (up to 100 characters long).
28
29package TEXTPlug;
30
31use BasPlug;
32use parsargv;
33
34
35sub BEGIN {
36    @ISA = ('BasPlug');
37}
38
39sub print_usage {
40    print STDERR "\n  usage: plugin TEXTPlug [options]\n\n";
41    print STDERR "  options:\n";
42    print STDERR "   -title_sub\t     Substitution expression to modify string stored as Title.\n";
43    print STDERR "\t\t     Used by, for example, PSPlug to remove \"Page 1\" etc from\n";
44    print STDERR "\t\t     text used as the title.\n";
45
46    print STDERR "\n";
47}
48
49sub new {
50    my ($class) = @_;
51    my $self = new BasPlug ($class, @_);
52
53    if (!parsargv::parse(\@_,
54             q^title_sub/.*/^, \$self->{'title_sub'},
55             "allow_extra_options")) {
56    print STDERR "\nIncorrect options passed to TEXTPlug, check your collect.cfg configuration file\n";
57    &print_usage();
58        die "\n";
59    }
60
61
62    return bless $self, $class;
63}
64
65sub get_default_process_exp {
66    my $self = shift (@_);
67
68    return q^(?i)\.te?xt$^;
69}
70
71# do plugin specific processing of doc_obj
72sub process {
73    my $self = shift (@_);
74    my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
75    my $outhandle = $self->{'outhandle'};
76
77    print $outhandle "TEXTPlug: processing $file\n"
78    if $self->{'verbosity'} > 1;
79   
80    my $cursection = $doc_obj->get_top_section();
81   
82    # get title metadata
83    # (don't need to get title if it has been passed
84    # in from another plugin)
85    if (!defined $metadata->{'Title'}) {
86    my ($title) = $$textref;
87    $title =~ /^\s+/s;
88    if (defined $self->{'title_sub'}) {$title =~ s/$self->{'title_sub'}//;}
89    $title =~ /^\s*([^\n]*)/s; $title=$1;
90    if (length($title) > 100) {
91        $title = substr ($title, 0, 100) . "...";
92    }
93    $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
94    }
95
96    # we need to escape the escape character, or else mg will convert into
97    # eg literal newlines, instead of leaving the text as '\n'
98    $$textref =~ s/\\/\\\\/g;
99
100    # insert preformat tags and add text to document object
101    $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
102
103    return 1;
104}
105
1061;
107
108
109
110
111
112
113
114
115
116
117
Note: See TracBrowser for help on using the browser.