]> git.phdru.name Git - bookmarks_db.git/blob - hotexplode.pl
Fix(Robot): Stop splitting and un-splitting URLs
[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="https://phdru.name/Bookmarks/">main page</A> instead.
32 Any other page in the hierarchy can be renamed, moved or removed 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>\
84 <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=koi8-r\">\
85 <TITLE>$title</TITLE>\
86 </HEAD>\
87 <BODY>\n<CENTER><H1>$title</H1></CENTER>\n $header \n<hr>\n<ul>";
88
89   warn "Creating $prefix...\n" if $opt_v;
90
91   # create the directory, if needed
92   mkdir($prefix, 0755) unless -d $prefix;
93
94   while (<>) {
95     last if (/<\/UL>/);
96
97     if(/<LI> *<A HREF=\"([^\"]*)\"[^\>]*>([^\<]*)<\/A>/) {
98       #
99       #  A URL
100       #
101       local($url,$name) = ($1, $2);
102       $result = $result."<li><a href=\"$url\">$name </a>\n";
103       next;
104     }
105     if(/<LI> (.*)$/) {
106       #
107       #  we've got a live one here...
108       #
109       local($subtitle)=local($filename)=$1;
110       $filename =~ tr/0-9A-Za-z//cd;
111       $filename =~ tr/A-Z/a-z/;
112       <>;  # eat the "<UL>" line.
113       $result .= "<li><b><a href=\"${filename}/index.html\">${subtitle}</a></b>\n";
114       &parse_mosaic_hotlist("${prefix}/${filename}", "${title}:${subtitle}");
115       next;
116     }
117
118   }
119
120   $result = $result . $footer . "</body></html>";
121   # write it to a file
122   open(FILE, ">${prefix}/index.html");
123   print FILE $result;
124   close(FILE);
125 }
126
127 # parse a Netscape bookmarks list
128 # exit when we meet a </DL>
129 # arguments:  pathname of directory in which output is to be placed,
130 #             subtitle
131
132 sub parse_netscape_bookmarks {
133   # we write the file at the very end, because (I think) filehandles do
134   # not have local scope, and this is recursive
135   local($prefix, $title) = @_;
136   local($result) = "<HTML><HEAD>\
137 <META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=koi8-r\">\
138 <TITLE>$title</TITLE>\
139 </HEAD>\
140 <BODY>\n<CENTER><H1>$title</H1></CENTER>\n $header \n<hr>\n<dl>";
141
142   warn "Creating $prefix...\n" if $opt_v;
143
144   # create the directory, if needed
145   mkdir($prefix, 0755) unless -d $prefix;
146
147   while (<>) {
148     last if (/<\/DL>/);
149     if (/<DT><H3[^\>]*>([^\<]*)<\/H3>/) {
150       #
151       #  a nested list
152       #
153       local($subtitle)=$1;
154       local($filename)=$1;
155       $filename =~ tr/0-9A-Za-z//cd;
156       $filename =~ tr/A-Z/a-z/;
157       # parse the description here
158       local($desc)="";
159       while(<>) {
160         last if (/<DL>/);
161         $desc = $desc . $_;
162       }
163       $result = $result . "<dt><b><a href=\"${filename}/index.html\">${subtitle}</a></b>\n";
164       unless("$desc" eq "")  { $result = $result . $desc; }
165       &parse_netscape_bookmarks("${prefix}/${filename}",
166         "${title}:${subtitle}");
167       next;
168     }
169     if (/<DT><A HREF=\"([^\"]*)\"[^\>]*>([^\<]*)<\/A>/) {
170       #
171       #  A URL
172       #
173       local($url, $name) = ($1, $2);
174       $result = $result."<dt><a href=\"$url\">$name </a>\n";
175       next;
176     }
177     $result = $result . $_;
178   }
179   $result = $result . $footer . "</body></html>";
180   # write it to a file
181   open(FILE, ">${prefix}/index.html");
182   print FILE $result;
183   close(FILE);
184 }
185
186