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 |
|
---|
29 | # 12/05/02 Added usage datastructure - John Thompson
|
---|
30 |
|
---|
31 | package TEXTPlug;
|
---|
32 |
|
---|
33 | use BasPlug;
|
---|
34 | use parsargv;
|
---|
35 |
|
---|
36 |
|
---|
37 | sub BEGIN {
|
---|
38 | @ISA = ('BasPlug');
|
---|
39 | }
|
---|
40 |
|
---|
41 | my $arguments =
|
---|
42 | [ { 'name' => "process_exp",
|
---|
43 | 'desc' => "{BasPlug.process_exp}",
|
---|
44 | 'type' => "regexp",
|
---|
45 | 'deft' => &get_default_process_exp(),
|
---|
46 | 'reqd' => "no" } ,
|
---|
47 | { 'name' => "title_sub",
|
---|
48 | 'desc' => "{TEXTPlug.title_sub}",
|
---|
49 | 'type' => "regexp",
|
---|
50 | 'deft' => "",
|
---|
51 | 'reqd' => "no" } ];
|
---|
52 |
|
---|
53 | my $options = { 'name' => "TEXTPlug",
|
---|
54 | 'desc' => "{TEXTPlug.desc}",
|
---|
55 | 'abstract' => "no",
|
---|
56 | 'inherits' => "yes",
|
---|
57 | 'args' => $arguments };
|
---|
58 |
|
---|
59 |
|
---|
60 | sub new {
|
---|
61 | my ($class) = @_;
|
---|
62 | my $self = new BasPlug ($class, @_);
|
---|
63 | $self->{'plugin_type'} = "TEXTPlug";
|
---|
64 | # 14-05-02 To allow for proper inheritance of arguments - John Thompson
|
---|
65 | my $option_list = $self->{'option_list'};
|
---|
66 | push( @{$option_list}, $options );
|
---|
67 |
|
---|
68 | if (!parsargv::parse(\@_,
|
---|
69 | q^title_sub/.*/^, \$self->{'title_sub'},
|
---|
70 | "allow_extra_options")) {
|
---|
71 | print STDERR "\nIncorrect options passed to TEXTPlug, check your collect.cfg configuration file\n";
|
---|
72 | $self->print_txt_usage(""); # Use default resource bundle
|
---|
73 | die "\n";
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | return bless $self, $class;
|
---|
78 | }
|
---|
79 |
|
---|
80 | sub get_default_process_exp {
|
---|
81 | my $self = shift (@_);
|
---|
82 |
|
---|
83 | return q^(?i)\.te?xt$^;
|
---|
84 | }
|
---|
85 |
|
---|
86 | # do plugin specific processing of doc_obj
|
---|
87 | sub process {
|
---|
88 | my $self = shift (@_);
|
---|
89 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
90 | my $outhandle = $self->{'outhandle'};
|
---|
91 |
|
---|
92 | print STDERR "<Processing n='$file' p='TEXTPlug'>\n" if ($gli);
|
---|
93 | print $outhandle "TEXTPlug: processing $file\n"
|
---|
94 | if $self->{'verbosity'} > 1;
|
---|
95 |
|
---|
96 | my $cursection = $doc_obj->get_top_section();
|
---|
97 | # get title metadata
|
---|
98 | # (don't need to get title if it has been passed
|
---|
99 | # in from another plugin)
|
---|
100 | if (!defined $metadata->{'Title'}) {
|
---|
101 | my ($title) = $$textref;
|
---|
102 | $title =~ /^\s+/s;
|
---|
103 | if (defined $self->{'title_sub'} &&
|
---|
104 | $self->{'title_sub'}) {$title =~ s/$self->{'title_sub'}//;}
|
---|
105 | $title =~ /^\s*([^\n]*)/s; $title=$1;
|
---|
106 | if (length($title) > 100) {
|
---|
107 | $title = substr ($title, 0, 100) . "...";
|
---|
108 | }
|
---|
109 | $title =~ s/\[/[/g;
|
---|
110 | $title =~ s/\[/]/g;
|
---|
111 | $title =~ s/\</</g;
|
---|
112 | $title =~ s/\>/>/g;
|
---|
113 | $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
|
---|
114 | }
|
---|
115 | # Add FileFormat metadata
|
---|
116 | $doc_obj->add_metadata($cursection, "FileFormat", "TEXT");
|
---|
117 |
|
---|
118 |
|
---|
119 | # we need to escape the escape character, or else mg will convert into
|
---|
120 | # eg literal newlines, instead of leaving the text as '\n'
|
---|
121 | $$textref =~ s/\\/\\\\/g; # macro language
|
---|
122 | $$textref =~ s/_/\\_/g; # macro language
|
---|
123 | $$textref =~ s/</</g;
|
---|
124 | $$textref =~ s/>/>/g;
|
---|
125 |
|
---|
126 | # insert preformat tags and add text to document object
|
---|
127 | $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
|
---|
128 |
|
---|
129 | return 1;
|
---|
130 | }
|
---|
131 |
|
---|
132 | 1;
|
---|
133 |
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|