1 | # plugin which process an HTML book directory
|
---|
2 |
|
---|
3 | package GMLPlug;
|
---|
4 |
|
---|
5 | use html;
|
---|
6 | use BasPlug;
|
---|
7 | use util;
|
---|
8 | use doc;
|
---|
9 |
|
---|
10 |
|
---|
11 | sub BEGIN {
|
---|
12 | @ISA = ('BasPlug');
|
---|
13 | }
|
---|
14 |
|
---|
15 | sub new {
|
---|
16 | my ($class) = @_;
|
---|
17 | $self = new BasPlug ();
|
---|
18 |
|
---|
19 | return bless $self, $class;
|
---|
20 | }
|
---|
21 |
|
---|
22 | sub is_recursive {
|
---|
23 | my $self = shift (@_);
|
---|
24 |
|
---|
25 | return 0; # this is not a recursive plugin
|
---|
26 | }
|
---|
27 |
|
---|
28 | # return 1 if processed, 0 if not processed
|
---|
29 | # Note that $base_dir might be "" and that $file might
|
---|
30 | # include directories
|
---|
31 | sub read {
|
---|
32 | my $self = shift (@_);
|
---|
33 | my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
|
---|
34 | my $fullname = &util::filename_cat ($base_dir, $file);
|
---|
35 |
|
---|
36 | # see if this is a gml book
|
---|
37 | return 0 unless (-f $fullname && $fullname =~ /\.gml$/i);
|
---|
38 |
|
---|
39 | my ($parent_dir) = $fullname =~ /^(.*)\/[^\/]+.gml$/;
|
---|
40 |
|
---|
41 | # create a new document
|
---|
42 | my $doc_obj = new doc ();
|
---|
43 | my $section = $doc_obj->get_top_section();
|
---|
44 |
|
---|
45 | print STDERR "GMLPlug: processing $file\n";
|
---|
46 |
|
---|
47 | # read in the document
|
---|
48 | my $gml = "";
|
---|
49 | my $line = "";
|
---|
50 | if (!open (INFILE, $fullname)) {
|
---|
51 | print STDERR "GMLPlug::read - couldn't read $fullname\n";
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 | while (defined ($line = <INFILE>)) {
|
---|
55 | $gml .= $line;
|
---|
56 | }
|
---|
57 | close (INFILE);
|
---|
58 |
|
---|
59 | # process the document
|
---|
60 | my $firstsection = 1;
|
---|
61 | while ($gml =~ /\S/) {
|
---|
62 | if ($gml =~ s/^\s*<gsdlsection([^>]*)>(.*?)(<\/?gsdlsection)/$3/is) {
|
---|
63 | my $tags = $1;
|
---|
64 | $tags = "" unless defined $tags;
|
---|
65 | my $text = $2;
|
---|
66 |
|
---|
67 | # create the section (unless this is the first section)
|
---|
68 | if (!$firstsection) {
|
---|
69 | $tags =~ s/gsdlnum\s*=\s*\"?(\d+)\"?//i;
|
---|
70 | if (defined $1) {
|
---|
71 | $section .= ".$1";
|
---|
72 | $doc_obj->create_named_section($section);
|
---|
73 | } else {
|
---|
74 | $section = $doc_obj->insert_section($doc_obj->get_end_child($section));
|
---|
75 | }
|
---|
76 | }
|
---|
77 | $firstsection = 0;
|
---|
78 |
|
---|
79 | # add the tags
|
---|
80 | while ((defined $tags) && ($tags =~ s/^\s*(\w+)\s*=\s*\"([^\"]*)\"//)) {
|
---|
81 | $doc_obj->add_metadata($section, $1 , $2) if (defined $1 and defined $2);
|
---|
82 | }
|
---|
83 |
|
---|
84 | # add the text
|
---|
85 | $doc_obj->add_text($section, $text) if ((defined $text) && ($text ne ""));
|
---|
86 |
|
---|
87 | } elsif ($gml =~ s/^\s*<\/gsdlsection>//) {
|
---|
88 | $section = $doc_obj->get_parent_section ($section);
|
---|
89 |
|
---|
90 | } else {
|
---|
91 | print STDERR "GMLPlug::read - error in file $fullname\n";
|
---|
92 | print STDERR "text: \"$gml\"\n";
|
---|
93 | last;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | # add the associated files
|
---|
98 | $assoc_files = $doc_obj->get_metadata($doc_obj->get_top_section(), "gsdlassocfile");
|
---|
99 | my ($assoc_file_info);
|
---|
100 | foreach $assoc_file_info (@$assoc_files) {
|
---|
101 | my ($assoc_file, $mime_type) = split (":", $assoc_file_info);
|
---|
102 | $doc_obj->associate_file(&util::filename_cat($parent_dir, $assoc_file),
|
---|
103 | $assoc_file, $mime_type);
|
---|
104 |
|
---|
105 | }
|
---|
106 | $doc_obj->delete_metadata($doc_obj->get_top_section(), "gsdlassocfile");
|
---|
107 |
|
---|
108 | # assume the document has an OID
|
---|
109 |
|
---|
110 | # process the document
|
---|
111 | $processor->process($doc_obj);
|
---|
112 |
|
---|
113 | return 1; # processed the file
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | 1;
|
---|