]> git.phdru.name Git - bookmarks_db.git/blob - hotexplode.pl
Minor changes in the warning message.
[bookmarks_db.git] / hotexplode.pl
1 #!/usr/bin/perl
2
3 # hotexplode --  a program for "exploding" a xmosaic hotlist or Netscape 
4 # bookmark file into a hierarchial multi-page structure.
5 # acb  60 Chs 3162
6
7 # revision history:
8 # v1.0:   1-3-1996:     initial version
9
10 $date = `date`;
11
12 # customise below
13
14 # header:  some arbitrary HTML text which is appended below the title and 
15 #          above the hotlist data
16
17 $header = <<FOO;
18 <hr width="50%">
19 <blockquote>
20 This hotlist was generated with 
21 <a href="http://www.zikzak.net/~acb/hacks/hotexplode.html">hotexplode</a>
22 on $date.
23 <p>
24 <b>WARNING:</b>  The inclusion of a link to a page on 
25 this hotlist is not an indication of the maintainer's 
26 approval of or agreement with its content.
27 </blockquote>
28 <hr width="50%">
29 <blockquote>
30 Please <b>DO NOT</b> bookmark this page. Bookmark the
31 <a href="http://phd.pp.ru/Bookmarks/">main page</A> instead.
32 Any other page in the hierarchy can disappear at any time.
33 </blockquote>
34 FOO
35
36 $footer = <<FOO;
37 <hr>
38 FOO
39
40 # which directory shall contain the hotlist?
41
42 $outdir = "hotlist";
43
44
45 #  end of customisable portion
46
47 require "getopts.pl";
48
49 &Getopts("o:t:v");
50
51 $outdir = $opt_o if $opt_o;
52
53
54 # seek forward to the title
55 while (<>) {
56   if (/<TITLE>([^\<\>]*)<\/TITLE>/) {
57     $title = $1;
58     last;
59   }
60 }
61
62 $title = $opt_t if $opt_t;
63
64 # seek forward to the start of the list
65
66
67 while (<>) {
68   if(/<UL>/) { warn "Detected xmosaic hotlist format\n" if $opt_v; 
69                &parse_mosaic_hotlist($outdir, $title); last; }
70   if(/<DL>/) { warn "Detected Netscape bookmark format\n" if $opt_v;
71                &parse_netscape_bookmarks($outdir, $title); last; }
72 }
73
74 # parse an xmosaic hotlist
75 # exit when we meet a </UL>
76 # arguments:  pathname of directory in which output is to be placed,
77 #             title
78
79 sub parse_mosaic_hotlist {
80   # we write the file at the very end, because (I think) filehandles do
81   # not have local scope, and this is recursive
82   local($prefix, $title) = @_;
83   local($result) = "<HTML><HEAD><TITLE>$title </TITLE></HEAD>\
84 <BODY>\n<CENTER><H1>$title </H1></CENTER>\n $header \n<hr>\n<ul>";
85
86   warn "Creating $prefix...\n" if $opt_v;
87   
88   # create the directory, if needed
89   mkdir($prefix, 0755) unless -d $prefix;
90
91   while (<>) {
92     last if (/<\/UL>/);
93
94     if(/<LI> *<A HREF=\"([^\"]*)\"[^\>]*>([^\<]*)<\/A>/) {
95       #
96       #  A URL
97       #
98       local($url,$name) = ($1, $2);
99       $result = $result."<li><a href=\"$url\">$name </a>\n";
100       next;
101     }
102     if(/<LI> (.*)$/) {
103       #
104       #  we've got a live one here...
105       #
106       local($subtitle)=local($filename)=$1;
107       $filename =~ tr/0-9A-Za-z//cd;
108       $filename =~ tr/A-Z/a-z/;
109       <>;  # eat the "<UL>" line.
110       $result .= "<li><b><a href=\"${filename}/index.html\">${subtitle}</a></b>\n";
111       &parse_mosaic_hotlist("${prefix}/${filename}", "${title}:${subtitle}");
112       next;
113     }
114     
115   }
116
117   $result = $result . $footer . "</body></html>";
118   # write it to a file
119   open(FILE, ">${prefix}/index.html");
120   print FILE $result;
121   close(FILE);
122 }
123
124 # parse a Netscape bookmarks list
125 # exit when we meet a </DL>
126 # arguments:  pathname of directory in which output is to be placed,
127 #             subtitle
128
129 sub parse_netscape_bookmarks {
130   # we write the file at the very end, because (I think) filehandles do
131   # not have local scope, and this is recursive
132   local($prefix, $title) = @_;
133   local($result) = "<HTML><HEAD><TITLE>$title </TITLE></HEAD>\
134 <BODY>\n<CENTER><H1>$title </H1></CENTER>\n $header \n<hr>\n<dl>";
135
136   warn "Creating $prefix...\n" if $opt_v;
137   
138   # create the directory, if needed
139   mkdir($prefix, 0755) unless -d $prefix;
140
141   while (<>) {
142     last if (/<\/DL>/);
143     if (/<DT><H3[^\>]*>([^\<]*)<\/H3>/) {
144       #
145       #  a nested list
146       #
147       local($subtitle)=$1;
148       local($filename)=$1; 
149       $filename =~ tr/0-9A-Za-z//cd;
150       $filename =~ tr/A-Z/a-z/;
151       # parse the description here
152       local($desc)="";
153       while(<>) {
154         last if (/<DL>/);
155         $desc = $desc . $_;
156       }
157       $result = $result . "<dt><b><a href=\"${filename}/index.html\">${subtitle}</a></b>\n";
158       unless("$desc" eq "")  { $result = $result . $desc; }
159       &parse_netscape_bookmarks("${prefix}/${filename}", 
160         "${title}:${subtitle}");
161       next;
162     }
163     if (/<DT><A HREF=\"([^\"]*)\"[^\>]*>([^\<]*)<\/A>/) {
164       #
165       #  A URL
166       #
167       local($url, $name) = ($1, $2);
168       $result = $result."<dt><a href=\"$url\">$name </a>\n";
169       next;
170     }
171     $result = $result . $_;
172   }
173   $result = $result . $footer . "</body></html>";
174   # write it to a file
175   open(FILE, ">${prefix}/index.html");
176   print FILE $result;
177   close(FILE);
178 }
179
180