File: Filter.html

package info (click to toggle)
swish-e 2.4.3-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,308 kB
  • ctags: 7,642
  • sloc: ansic: 47,402; sh: 8,508; perl: 5,281; makefile: 723; xml: 9
file content (1225 lines) | stat: -rw-r--r-- 32,077 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
  <head>
   <title>SWISH-Enhanced:  SWISH::Filter - Perl extension for filtering documents with Swish-e </title>
   <link href="./style.css" rel=stylesheet type="text/css" title="refstyle">
  </head>
  <body>

    <h1 class="banner">
        <a href="http://swish-e.org"><img border=0 src="images/swish.gif" alt="Swish-E Logo"></a><br>
        <img src="images/swishbanner1.gif"><br>
        <img src="images/dotrule1.gif"><br>
         SWISH::Filter - Perl extension for filtering documents with Swish-e 
    </h1>

    <hr>

    <p>
    <div class="navbar">
      <a href="./spider.html">Prev</a> |
      <a href="./index.html">Contents</a> |
      
    </div>
    <p>

    <div class="toc">
      
<A NAME="toc"></A>
<P><B>Table of Contents:</B></P>

<UL>

	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#METHODS">METHODS</A>
	<UL>

		<LI><A HREF="#SWISH_Filter_new_Options">SWISH::Filter-&gt;new Options</A>
	</UL>

	<LI><A HREF="#WRITING_FILTERS">WRITING FILTERS</A>
	<LI><A HREF="#SWISH_Filter_document_Methods">SWISH::Filter::document Methods</A>
	<UL>

		<LI><A HREF="#Methods_used_by_end_users_and_filter_authors">Methods used by end-users and filter authors</A>
		<LI><A HREF="#Methods_used_by_filter_authors">Methods used by filter authors</A>
	</UL>

	<LI><A HREF="#SWISH_Filters_BASE">SWISH::Filters::_BASE</A>
	<LI><A HREF="#TESTING">TESTING</A>
	<LI><A HREF="#SUPPORT">SUPPORT</A>
	<LI><A HREF="#Bugs_todo_items_and_other_notes">Bugs, todo items, and other notes</A>
	<LI><A HREF="#AUTHOR">AUTHOR</A>
	<LI><A HREF="#COPYRIGHT">COPYRIGHT</A>
</UL>

    </div>

    

	    [ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>

<P>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  use SWISH::Filter;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # load available filters into memory
  my $filter = SWISH::Filter-&gt;new;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # convert a document</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  my $doc = $filter-&gt;convert(
        document     =&gt; \$scalar_ref,  # path or ref to a doc
        content_type =&gt; $content_type, # content type if doc reference
        name         =&gt; $real_path,    # optional name for this file (useful for debugging)
        user_data    =&gt; $whatever,     # optional data to make available to filters
   );</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  return unless $doc;  # empty doc, zero size, or no filters installed</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # Was the document converted by a filter?
  my $was_filtered = $doc-&gt;was_filtered;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # Skip if the file is not text
  return if $doc-&gt;is_binary;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # Print out the doc
  my $doc_ref = $doc-&gt;fetch_doc;
  print $$doc_ref;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # Fetch the final content type of the document
  my $content_type = $doc-&gt;content_type;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  # Fetch Swish-e parser type (TXT*, XML*, HTML*, or undefined)
  my $doc_type = $doc-&gt;swish_parser_type;</pre>
        </td>
	    
      </tr>
    </table>
    <P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
SWISH::Filter provides a unified way to convert documents into a type that
Swish-e can index. Individual filters are installed as separate perl
modules. For example, there might be a filter that converts from PDF format
to HTML format.

<P>
Note that this is just a framework for filtering documents. Additional
helper programs or Perl module may need to be installed to use
SWISH::Filter to filter documents. For example, to filter PDF documents you
must install the Xpdf package.

<P>
The filters are automatically loaded when <CODE>SWISH::Filters-&gt;new()</CODE> is called. Filters define a type and priority that determines the
processing order of the filter. Filters are processed in this sort order
until a filter accepts the document for filtering. The filter uses the
document's content type to determine if the filter should handle the
current document. The content-type is determined by the files suffix if not
supplied by the calling program.

<P>
The individual filters are not designed to be used as separate modules. All
access to the filters is through this SWISH::Filter module.

<P>
Normally, once a document is filtered processing stops. Filters can filter
the document and then set a flag saying that filtering should continue (for
example a filter that uncompresses a MS Word document before passing on to
the filter that converts from MS Word to text). All this should be
transparent to the end user. So, filters can be pipe-lined.

<P>
The idea of SWISH::Filter is that new filters can be created, and then
downloaded and installed to provide new filtering capabilities. For
example, if you needed to index MS Excel documents you might be able to
download a filter from the Swish-e site and magically next time you run
indexing MS Excel docs would be indexed.

<P>
The SWISH::Filter setup can be used with -S prog or -S http. It works best
with the -S prog method because the filter modules only need to be loaded
and compiled one time. The -S prog program <EM>spider.pl</EM> will automatically use SWISH::Filter when spidering with default settings
(using &quot;default&quot; as the first parameter to spider.pl).

<P>
The -S http indexing method uses a Perl helper script called <EM>swishspider</EM>.
<EM>swishspider</EM> has been updated to work with SWISH::Filter, but (unlike spider.pl) does
not contain a &quot;use lib&quot; line to point to the location of
SWISH::Filter. This means that by default <EM>swishspider</EM> will <STRONG>not</STRONG> use SWISH::Filter for filtering. The reason for this is because <EM>swishspider</EM>
runs for every URL fetched, and loading the Filters for each document can
be slow. The recommended way of spidering is using -S prog with spider.pl,
but if -S http is desired the way to enable SWISH::Filter is to set
PERL5LIB before running swish so that <EM>swishspider</EM> will be able to locate the SWISH::Filter module. Here's one way to set the
PERL5LIB with the bash shell:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>  $ export PERL5LIB=`swish-filter-test -path`</pre>
        </td>
	    
      </tr>
    </table>
    <P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H1><A NAME="METHODS">METHODS</A></H1>
<DL>
<P><DT><STRONG><A NAME="item__filter">$filter = SWISH::Filter-&gt;new()</A></STRONG><DD>
<P>
This creates a SWISH::Filter object. You may pass in options as a list or a
hash reference.

</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="SWISH_Filter_new_Options">SWISH::Filter-&gt;new Options</A></H2>
<P>
There is currently only one option that can be passed in to
<CODE>new():</CODE>

<DL>
<P><DT><STRONG><A NAME="item_ignore_filters">ignore_filters</A></STRONG><DD>
<P>
Pass in a reference to a list of filter names to ignore. For example, if
you have two filters installed &quot;Pdf2HTML&quot; and &quot;Pdf2XML&quot;
and want to avoid using &quot;Pdf2XML&quot;:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    my $filter = SWISH::Filter-&gt;new( ignore_filters =&gt; ['Pdf2XML'];</pre>
        </td>
	    
      </tr>
    </table>
    <P><DT><STRONG><A NAME="item__doc_object">$doc_object = $filter-&gt;convert();</A></STRONG><DD>
<P>
This method filters a document. Returns an object of the class
SWISH::Filter::document or undefined if passed in an empty document, a
filename that cannot be read off disk, or if no filters have been loaded.

<P>
SWISH::Filter::document methods listed below can be called on the object
to, for example, check if the document was filtered and to fetch the
document content (filtered or not).

<P>
You must pass in a hash (or hash reference) of parameters to the
<CODE>convert()</CODE> method. The possible parameters are:

<DL>
<P><DT><STRONG><A NAME="item_document">document</A></STRONG><DD>
<P>
This can be either a path to a file, or a scalar reference to a document in
memory. This is required.

<P><DT><STRONG><A NAME="item_content_type">content_type</A></STRONG><DD>
<P>
The MIME type of the document. This is only required when passing in a
scalar reference to a document. The content type string is what the filters
use to match a document type.

<P>
When passing in a file name and <A HREF="#item_content_type">content_type</A> is not set, then the content type will be determined from the file's
extension by using the MIME::Types Perl module (available on CPAN).

<P><DT><STRONG><A NAME="item_name">name</A></STRONG><DD>
<P>
Optional name to pass in to filters that will be used in error and warning
messages.

<P><DT><STRONG><A NAME="item_user_data">user_data</A></STRONG><DD>
<P>
Optional data structure that all filters may access. This can be fetched in
a filter by:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    my $user_data = $doc_object-&gt;user_data;</pre>
        </td>
	    
      </tr>
    </table>
    <P>
And used in the filter as:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    if ( ref $user_data &amp;&amp; $user_data-&gt;{pdf2html}{title} ) {
       ...
    }</pre>
        </td>
	    
      </tr>
    </table>
    <P>
It's up to the filter author to use a unique first-level hash key for a
given filter.

</DL>
<P>
Example of using the <CODE>convert()</CODE> method:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    $doc_object = $filter-&gt;convert(
        document     =&gt; $doc_ref,
        content-type =&gt; 'application/pdf',
    );</pre>
        </td>
	    
      </tr>
    </table>
    <P><DT><STRONG><A NAME="item__filter_mywarn_">$filter-&gt;mywarn()</A></STRONG><DD>
<P>
Internal function used for writing warning messages to STDERR if
$ENV{FILTER_DEBUG} is set. Set the environment variable FILTER_DEBUG before
running to see extra messages while processing.

<P><DT><STRONG><A NAME="item__filters">@filters = $filter-&gt;filter_list;</A></STRONG><DD>
<P>
Returns a list of filter objects installed.

<P><DT><STRONG><A NAME="item__filter">@filter = $filter-&gt;can_filter( $content_type );</A></STRONG><DD>
<P>
This is useful for testing to see if a mimetype might be handled by
SWISH::Filter wihtout having to pass in a document. Helpful if doing HEAD
requests.

<P>
Returns an array of filters that can handle this type of document

<H1><A NAME="WRITING_FILTERS">WRITING FILTERS</A></H1>
<P>
Filters are standard perl modules that are installed into the
SWISH::Filters name space. Filters are not complicated -- see the existing
filters for examples.

<P>
Each filter defines the content-types (or mimetypes) that it can handle.
These are specified as a list of regular expressions to match against the
document's content-type. If one of the mimetypes of a filter match the
incoming document's content-type the filter is called. The filter can then
either filter the content or return undefined indicating that it decided
not to filter the document for some reason. If the document is converted
the filter returns either a reference to a scalar of the content or a file
name where the content is stored. The filter also must change the
content-type of the document to reflect the new document.

<P>
Filters typically use external programs or modules to do that actual work
of converting a document from one type to another. For example, programs in
the Xpdf packages are used for converting PDF files. The filter can (and
should) test for those programs in its <CODE>new()</CODE> method.

<P>
Filters also can define a type and priority. These attributes are used to
set the order filters are tested for a content-type match. This allows you
to have more than one filter that can work on the same content-type.

<P>
If a filter calls <CODE>die()</CODE> then the filter is removed from the
chain and will not be called again <EM>during the same run</EM>. Calling die when running with -S http or -S fs has no effect since the
program is run once per document.

<P>
Once a filter returns something other than undef no more filters will be
called. If the filter calls $filter-&gt;set_continue then processing will
continue as if the file was not filtered. For example, a filter can
uncompress data and then set $filter-&gt;set_continue and let other filters
process the document.

<P>
This is the list of methods the filter should or may define (as
specificed):

<DL>
<P><DT><STRONG><A NAME="item_new">new()  * required *</A></STRONG><DD>
<P>
This method returns either an object which provides access to the filter,
or undefined if the filter is not to be used.

<P>
The <CODE>new()</CODE> method is a good place to check for required modules
or helper programs. Returning undefined prevents the filter from being
included in the filter chain.

<P>
The new method must return a blessed hash reference. The only required
attribute is <STRONG>mimetypes</STRONG>. This attribute must contain a reference to an array of regular
expressions used for matching the content-type of the document passed in.

<P>
Example:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    sub new {
        my ( $class ) = @_;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        # List of regular expressions
        my @mimetypes = (
            qr[application/(x-)?msword],
            qr[application/worddoc],
        );</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        my %settings = (
            mimetypes   =&gt; \@mimetypes,</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>            # Optional settings
            priority    =&gt; 20,
            type        =&gt; 2,
        );</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        return bless \%settings, $class;
    }</pre>
        </td>
	    
      </tr>
    </table>
    <P>
The attribute &quot;mimetypes&quot; returns an array reference to a list of
regular expressions. Those patterns are matched against each document's
content type.

<P><DT><STRONG><A NAME="item_filter">filter() * required *</A></STRONG><DD>
<P>
This is the function that does the work of converting a document from one
content type to another. The function is passed the document object. See
document object methods listed below for what methods may be called on a
document.

<P>
The function can return undefined (or any false value) to indicate that the
filter did not want to process the document. Other filters will then be
tested for a content type match.

<P>
If the document is filtered then the filter must set the new document's
content type (if it changed) and return either a file name where the
document can be found or a reference to a scalar containing the document.

<P><DT><STRONG><A NAME="item_type">type()</A></STRONG><DD>
<P>
Returns a number. Filters are sorted (for processing in a specific order)
and this number is simply the primary key used in sorting. If not specified
the filter's type used for sorting is 2.

<P>
This is an optional method. You can also set the type in your
<CODE>new()</CODE> constructor as shown above.

<P><DT><STRONG><A NAME="item_priority">priority()</A></STRONG><DD>
<P>
Returns a number. Filters are sorted (for processing in a specific order)
and this number is simply the secondary key used in sorting. If not
specified the filter's priority is 50.

<P>
This is an optional method. You can also set the priority in your
<CODE>new()</CODE> constructor as shown above.

</DL>
<P>
Again, the point of the <CODE>type()</CODE> and <CODE>priority()</CODE>
methods is to allow setting the sort order of the filters. Useful if you
have two filters for filtering the same content-type, but prefer to use one
over the other. Neither are required.

<P>
Here's a module to convert MS Word documents using the program
&quot;catdoc&quot;:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    package SWISH::Filters::Doc2txt;
    use vars qw/ $VERSION /;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    $VERSION = '0.02';</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    sub new {
        my ( $class ) = @_;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        my $self = bless {
            mimetypes   =&gt; [ qr!application/(x-)?msword! ],
            priority    =&gt; 50,
        }, $class;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        # check for helpers
        return $self-&gt;set_programs( 'catdoc' );</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    }</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    sub filter {
        my ( $self, $doc ) = @_;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        my $content = $self-&gt;run_catdoc( $doc-&gt;fetch_filename ) || return;</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        # update the document's content type
        $filter-&gt;set_content_type( 'text/plain' );</pre>
        </td>
	    
      </tr>
    </table>
    <P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>        # return the document
        return \$content;
    }
    1;</pre>
        </td>
	    
      </tr>
    </table>
    <P>
The <CODE>new()</CODE> constructor creates a blessed hash which contains an
array reference of mimetypes patterns that this filter accepts. The
priority sets this filter to run after any other filters that might handle
the same type of content. The <EM>set_programs()</EM> function says that we need to call a program called &quot;catdoc&quot;. The
function either returns <CODE>$self</CODE> or undefined if catdoc could not
be found. The <EM>set_programs()</EM> function creates a new method for running catdoc.

<P>
The filter function runs catdoc passing in the name of the file (if the
file is in memory a temporary file is created). That <EM>run_catdoc()</EM> function was created by the
<EM>set_programs()</EM> call above.

<H1><A NAME="SWISH_Filter_document_Methods">SWISH::Filter::document Methods</A></H1>
<P>
These methods are available to Filter authors, and also provide access to
the document after calling the <CODE>convert()</CODE> method to end-users
of SWISH::Filter.

<P>
End users of SWISH::Filter will use a subset of these methods. Mostly:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>   $doc_object-&gt;fetch_doc      # and alias for fetch_document_reference()
   $doc_object-&gt;was_filtered   # true the document was filtered
   $doc_object-&gt;content_type   # document's current content type (mime type)
   $doc_object-&gt;swish_parser_type # returns a parser type to use with Swish-e -S prog method
   $doc_object-&gt;is_binary      # returns $content_type !~ m[^text/];</pre>
        </td>
	    
      </tr>
    </table>
    <P>
These methods are also available to the individual filter modules. The
filter's &quot;filter&quot; function is also passed a
SWISH::Filter::document object. Method calls may be made on this object to
check the document's current content type, or to fetch the document as
either a file name or a reference to a scalar containing the document
content.

<H2><A NAME="Methods_used_by_end_users_and_filter_authors">Methods used by end-users and filter authors</A></H2>
<DL>
<P><DT><STRONG><A NAME="item__doc_ref">$doc_ref = $doc_object-&gt;fetch_doc_reference;</A></STRONG><DD>
<P>
Returns a scalar reference to the document. This can be used when the
filter can operate on the document in memory (or if an external program
expects the input to be from standard input).

<P>
If the file is currently on disk then it will be read into memory. If the
file was stored in a temporary file on disk the file will be deleted once
read into memory. The file will be read in binmode if $doc-&gt;is_binary is
true.

<P>
Note that $doc_object-&gt;fetch_doc is an alias.

<P><DT><STRONG><A NAME="item__was_filtered">$was_filtered = $doc_object-&gt;was_filtered</A></STRONG><DD>
<P>
Returns true if some filter processed the document

<P><DT><STRONG><A NAME="item__content_type">$content_type = $doc_object-&gt;content_type;</A></STRONG><DD>
<P>
Fetches the current content type for the document.

<P>
Example:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    return unless $filter-&gt;content_type =~ m!application/pdf!;</pre>
        </td>
	    
      </tr>
    </table>
    <P><DT><STRONG><A NAME="item__type">$type = $doc_object-&gt;swish_parser_type</A></STRONG><DD>
<P>
Returns a parser type based on the content type

<P><DT><STRONG><A NAME="item__doc_object_is_binary">$doc_object-&gt;is_binary</A></STRONG><DD>
<P>
Returns true if the document's content-type does not match
&quot;text/&quot;.

</DL>
<H2><A NAME="Methods_used_by_filter_authors">Methods used by filter authors</A></H2>
<DL>
<P><DT><STRONG><A NAME="item__file_name">$file_name = $doc_object-&gt;fetch_filename;</A></STRONG><DD>
<P>
Returns a path to the document as stored on disk. This name can be passed
to external programs (e.g. catdoc) that expect input as a file name.

<P>
If the document is currently in memory then a temporary file will be
created. Do not expect the file name passed to be the real path of the
document.

<P>
The file will be written in binmode if $doc-&gt;is_binary is true.

<P>
This method is not normally used by end-users of SWISH::Filter.

<P><DT><STRONG><A NAME="item__doc_object_set_continue_">$doc_object-&gt;set_continue;</A></STRONG><DD>
<P>
Processing will continue to the next filter if this is set to a true value.
This should be set for filters that change encodings or uncompress
documents.

<P><DT><STRONG><A NAME="item__doc_object_set_content_type_">$doc_object-&gt;set_content_type( $type );</A></STRONG><DD>
<P>
Sets the content type for a document.

<P><DT><STRONG><A NAME="item__doc_object_name">$doc_object-&gt;name</A></STRONG><DD>
<P>
Fetches the name of the current file. This is useful for printing out the
name of the file in an error message. This is the name passed in to the
SWISH::Filter-&gt;convert method. It is optional and thus may not always be
set.

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    my $name = $doc_object-&gt;name || 'Unknown name';
    warn &quot;File '$name': failed to convert -- file may be corrupt\n&quot;;</pre>
        </td>
	    
      </tr>
    </table>
    <P><DT><STRONG><A NAME="item__doc_object_user_data">$doc_object-&gt;user_data</A></STRONG><DD>
<P>
Fetches the the user_data passed in to the filter. This can be any data or
data structure passed into SWISH::Filter-&gt;new.

<P>
This is an easy way to pass special parameters into your filters.

<P>
Example:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    my $data = $doc_object-&gt;user_data;
    # see if a choice for the &lt;title&gt; was passed in
    if ( ref $data eq 'HASH' &amp;&amp; $data-&gt;{pdf2html}{title_field}  {
       ...
       ...
    }</pre>
        </td>
	    
      </tr>
    </table>
    <H1><A NAME="SWISH_Filters_BASE">SWISH::Filters::_BASE</A></H1>
<P>
Each filter is a subclass of SWISH::Filters::_BASE. A number of methods are
available by default (and some can be overridden). Others are useful when
writing your <CODE>new()</CODE> constructor.

<DL>
<P><DT><STRONG><A NAME="item__self_type">$self-&gt;type</A></STRONG><DD>
<P>
This method fetches the type of the filter. The value returned sets the
primary sort key for sorting the filters. You can override this in your
filter, or just set it as an attribute in your object. The default is 2.

<P>
The idea of the &quot;type&quot; is to create groups of filters, if needed.
For example, you might have a set of filters that are used for
uncompressing some documents before passing on to another group for
filtering.

<P><DT><STRONG><A NAME="item__self_priority">$self-&gt;priority</A></STRONG><DD>
<P>
This method fetches the priority of the filter. The value returned sets the
secondary sort key for sorting the filters. You can override this in your
filter, or just set it as an attribute in your object. The default method
returns 50.

<P>
The priority is useful if you have multiple filters for the same content
type that use different methods for filtering (say one uses wvWare and
another uses catdoc for filtering MS Word files). You might give the wvWare
filter a lower priority number so it runs before the catdoc filter if both
wvWare AND catdoc happen to be installed at the same time.

<P><DT><STRONG><A NAME="item__types">@types = $self-&gt;mimetypes</A></STRONG><DD>
<P>
Returns the list of mimetypes (as regular expressions) set for the filter.

<P><DT><STRONG><A NAME="item__pattern">$pattern = $self-&gt;can_filter_mimetype( $content_type )</A></STRONG><DD>
<P>
Returns true if passed in content type matches one of the filter's
mimetypes Returns the pattern that matched.

<P><DT><STRONG><A NAME="item_mywarn">mywarn( $message )</A></STRONG><DD>
<P>
method for printing out message if debugging is available

<P><DT><STRONG><A NAME="item__boolean">$boolean = $self-&gt;set_programs( @program_list );</A></STRONG><DD>
<P>
Returns true if all the programs listed in <CODE>@program_list</CODE> are
found and can be executed as the current user. Creates a method for each
program with the &quot;run_&quot; prefix. Returns false is ANY program
cannot be found.

<P>
Actually, it returns $self, so you can make it the last statement in your
constructor.

<P>
So in your constructor you might do:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    return $self-&gt;set_programs( qw/ pdftotext pdfinfo / );</pre>
        </td>
	    
      </tr>
    </table>
    <P>
Then in your <CODE>filter()</CODE> method:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    my $content = $self-&gt;run_pdfinfo( $doc-&gt;fetch_filename, [options] );</pre>
        </td>
	    
      </tr>
    </table>
    <P><DT><STRONG><A NAME="item__path">$path = $self-&gt;find_binary( $prog );</A></STRONG><DD>
<P>
Use in a filter's <CODE>new()</CODE> method to test for a necesary program
located in $PATH. Returns the path to the program or undefined if not found
or does not pass the -x file test.

<P><DT><STRONG><A NAME="item__bool">$bool = $self-&gt;use_modules( @module_list );</A></STRONG><DD>
<P>
Attempts to load each of the module listed and calls its
<CODE>import()</CODE> method.

<P>
Use to test and load required modules within a filter without aborting.

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>    return unless $self-&gt;use_modules( qw/ Spreadsheet::ParseExcel  HTML::Entities / );</pre>
        </td>
	    
      </tr>
    </table>
    <P>
A warning message is displayed if the FILTER_DEBUG environment variable is
true. Returns <CODE>$self</CODE> if no error.

</DL>
<P><DT><STRONG>$doc_ref = $self-&gt;run_program( $program, @args );</STRONG><DD>
<P>
Runs <CODE>$program</CODE> with @args. Must pass in @args.

<P>
Under Windows calls IPC::Open2, which may pass data through the shell.
Double-quotes are escaped (backslashed) and each parameter is wrapped in
double-quotes.

<P>
On other platforms a fork and exec is used to avoid passing any data
through the shell. Returns a reference to a scalar containing the output
from your program, or dies.

<P>
This method is intended to read output from a program that converts one
format into text. The output is read back in text mode -- on systems like
Windows this means \r\n (CRLF) will be convertet to \n.

</DL>
<H1><A NAME="TESTING">TESTING</A></H1>
<P>
Filters can be tested with the <EM>swish-filter-test</EM> program. Run:

<P>

    <table>
      <tr>

	<td bgcolor="#eeeeee" width="1">
	  &nbsp;
        </td>

	<td>
	  <pre>   swish-filter-test -man</pre>
        </td>
	    
      </tr>
    </table>
    <P>
for documentation.

<H1><A NAME="SUPPORT">SUPPORT</A></H1>
<P>
Please contact the Swish-e discussion list. <A
HREF="http://swish-e.org">http://swish-e.org</A>

<H1><A NAME="Bugs_todo_items_and_other_notes">Bugs, todo items, and other notes</A></H1>
<P>
TBD

<H1><A NAME="AUTHOR">AUTHOR</A></H1>
<P>
Bill Moseley

<H1><A NAME="COPYRIGHT">COPYRIGHT</A></H1>
<P>
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

</DL>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>



    <p>
    <div class="navbar">
      <a href="./spider.html">Prev</a> |
      <a href="./index.html">Contents</a> |
      
    </div>
    <p>

    <P ALIGN="CENTER">
    <IMG ALT="" WIDTH="470" HEIGHT="10" SRC="images/dotrule1.gif"></P>
    <P ALIGN="CENTER">

    <div class="footer">
        <BR>SWISH-E is distributed with <B>no warranty</B> under the terms of the
        <A HREF="http://www.fsf.org/copyleft/gpl.html">GNU Public License</A>,<BR>
        Free Software Foundation, Inc., 
        59 Temple Place - Suite 330, Boston, MA  02111-1307, USA<BR> 
        Public questions may be posted to 
        the <A HREF="http://swish-e.org/Discussion/">SWISH-E Discussion</A>.
    </div>

</body>
</html>