]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/marc2html
utility for turning MARC into HTML for easy viewing
[Evergreen.git] / Open-ILS / src / extras / marc2html
1 #!/usr/bin/perl
2
3 use Error;
4 use MARC::Batch;
5 use MARC::File::XML;
6 use XML::LibXSLT;
7 use XML::LibXML;
8 use Unicode::Normalize;
9 use Getopt::Long;
10
11 my ($split,$enc,$marc,$out) = (100);
12 GetOptions(
13         'split=i' => \$split,
14         'marc=s'  => \$marc,
15         'encoding=s'  => \$enc,
16         'out_dir=s'  => \$out,
17 );
18
19 if ($enc) {
20         MARC::Charset->ignore_errors(1);
21         MARC::Charset->assume_encoding($enc);
22 }
23
24 die "gimme some marc!\n" unless $marc;
25 die "gimme somewhere to put it!\n" unless $out;
26
27 my $xsl = join('',(<DATA>));
28
29 my $parser = XML::LibXML->new();
30 my $xslt = XML::LibXSLT->new();
31
32 $stylesheet = $xslt->parse_stylesheet( $parser->parse_string($xsl) );
33
34
35 my $xml = '';
36 my $current = 1;
37 my $prev = 0;
38 my $next = 2;
39
40 my $marc = MARC::Batch->new( USMARC => $marc );
41 $marc->strict_off;
42 $marc->warnings_off;
43
44 while (my $r = $marc->next) {
45         $xml .= entityize(MARC::File::XML::record($r));
46
47         unless ($current % $split) {
48                 $xml = <<"              XML";
49                         <collection xmlns="http://www.loc.gov/MARC21/slim">
50                                 $xml
51                         </collection>
52                 XML
53
54                 my $doc = $parser->parse_string($xml);
55                 $xml = '';
56
57                 my $results = $stylesheet->transform($doc, prev => "'$prev'", next => "'$next'");
58                 $prev++;
59                 $next++;
60
61                 open OUT, ">$out/$prev.html";
62                 print OUT $results->toString;
63                 close OUT;
64         }
65         $current++;
66 }
67
68 my $doc = $parser->parse_string(<<XML);
69 <marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">
70         $xml
71 </marc:collection>
72 XML
73
74 my $results = $stylesheet->transform($doc, prev => "'$prev'", next => "'0'");
75 $prev++;
76
77 $stylesheet->output_file($results, "$out/$prev.html");
78
79
80 sub entityize {
81         my $stuff = shift;
82         my $form = shift; 
83         
84         if ($form eq 'D') {
85                 $stuff = NFD($stuff);
86         } else {
87                 $stuff = NFC($stuff);
88         }
89         
90         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
91         $stuff =~ s/([\x00-\x19])//sgoe;
92         return $stuff;
93 }
94
95
96
97 __DATA__
98 <?xml version="1.0" encoding="UTF-8"?>
99 <xsl:stylesheet version="1.0" xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
100         <xsl:output method="html"/>
101         
102         <xsl:template match="/">
103                 <html>
104                         <head>
105
106                                 <style>
107
108                                         .marc_table {}
109                                         .marc_tag_row {}
110                                         .marc_tag_data {}
111                                         .marc_tag_col {}
112                                         .marc_tag_ind {}
113                                         .marc_subfields {}
114                                         .marc_subfield_code { 
115                                                 color: blue; 
116                                                 padding-left: 5px;
117                                                 padding-right: 5px; 
118                                         }
119
120                                 </style>
121
122                                 <link href='/css/opac_marc.css' rel='stylesheet' type='text/css'></link>
123                         </head>
124                         <body>
125                                 <xsl:if test="$prev &gt; 0">
126                                         <a>
127                                                 <xsl:attribute name="href">
128                                                         <xsl:value-of select="concat( $prev, '.html')"/>
129                                                 </xsl:attribute>
130                                                 <xsl:text>Previous page</xsl:text>
131                                         </a>
132                                 </xsl:if>
133                                 <span> | </span>
134                                 <xsl:if test="$next &gt; 0">
135                                         <a>
136                                                 <xsl:attribute name="href">
137                                                         <xsl:value-of select="concat( $next, '.html')"/>
138                                                 </xsl:attribute>
139                                                 <xsl:text>Next page</xsl:text>
140                                         </a>
141                                 </xsl:if>
142                                 <hr/>
143                                 <xsl:apply-templates select="//marc:record"/>
144                                 <xsl:if test="$prev &gt; 0">
145                                         <a>
146                                                 <xsl:attribute name="href">
147                                                         <xsl:value-of select="concat( $prev, '.html')"/>
148                                                 </xsl:attribute>
149                                                 <xsl:text>Previous page</xsl:text>
150                                         </a>
151                                 </xsl:if>
152                                 <span> | </span>
153                                 <xsl:if test="$next &gt; 0">
154                                         <a>
155                                                 <xsl:attribute name="href">
156                                                         <xsl:value-of select="concat( $next, '.html')"/>
157                                                 </xsl:attribute>
158                                                 <xsl:text>Next page</xsl:text>
159                                         </a>
160                                 </xsl:if>
161                         </body>
162                 </html>
163         </xsl:template>
164         
165         <xsl:template match="marc:record">
166                 <table class='marc_table'>
167                         <tr class='marc_tag_row'>
168                                 <th class='marc_tag_col' NOWRAP="TRUE" ALIGN="RIGHT" VALIGN="middle">
169                                         LDR
170                                 </th>
171                                 <td class='marc_tag_data' COLSPAN='3'>
172                                         <xsl:value-of select="marc:leader"/>
173                                 </td>
174                         </tr>
175                         <xsl:apply-templates select="marc:datafield|marc:controlfield"/>
176                 </table>
177                 <hr/>
178         </xsl:template>
179         
180         <xsl:template match="marc:controlfield">
181                 <tr class='marc_tag_row'>
182                         <th class='marc_tag_col' NOWRAP="TRUE" ALIGN="RIGHT" VALIGN="middle">
183                                 <xsl:value-of select="@tag"/>
184                         </th>
185                         <td class='marc_tag_data' COLSPAN='3'>
186                                 <xsl:value-of select="."/>
187                         </td>
188                 </tr>
189         </xsl:template>
190         
191         <xsl:template match="marc:datafield">
192                 <tr class='marc_tag_row'>
193                         <th class='marc_tag_col' NOWRAP="TRUE" ALIGN="RIGHT" VALIGN="middle">
194                                 <xsl:value-of select="@tag"/>
195                         </th>
196                         <td class='marc_tag_ind'>
197                                 <xsl:value-of select="@ind1"/>
198                         </td>
199
200                         <td class='marc_tag_ind' style='border-left: 1px solid #A0A0A0; padding-left: 3px;'>
201                                 <xsl:value-of select="@ind2"/>
202                                 <span style='color:#FFF'>.</span> 
203                         </td>
204
205                         <td class='marc_subfields'>
206                                 <xsl:apply-templates select="marc:subfield"/>
207                         </td>
208                 </tr>
209         </xsl:template>
210         
211         <xsl:template match="marc:subfield">
212                 <span class='marc_subfield_code' > 
213                         &#8225;<xsl:value-of select="@code"/>
214                 </span><xsl:value-of select="."/>       
215         </xsl:template>
216
217 </xsl:stylesheet>
218