-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmsort.pl
133 lines (113 loc) · 2.92 KB
/
bmsort.pl
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
#!/usr/bin/perl -s
#
# File:
# bmsort.pl
#
# Version:
# 1.0 - September 2003
#
# Author:
# Herb Wolfe, Jr
# hwolfe@inetnebr.com
# http://incolor.inetnebr.com/hwolfe
# http://incolor.inetnebr.com/hwolfe/computer/mysoftware/bmsort
#
# Description:
# This program sorts a Netscape/Mozilla bookmark file, creating a
# backup copy. Bookmarks are sorted, case insensitive, with links before
# folders and by web page title by default. Links with the same title
# are sorted by url. Separators are kept in place, ie, links after the
# separator are kept after it.
#
# Options:
# $f = folders sorted first
# $U = sort by urls instead of web page title
# Initialization
$file = "bookmarks.html";
$bakfile = "bookmarks.bak";
# <hr> string key, to keep groupings together
$HR = "_HR_";
# </dl> string key, for end of folder
$dlstr = "_3_";
# Strings to add to keys to sort with folders first, or urls first
if ($f) {
$fstr = "_1_";
$ustr = "_2_";
} else {
$fstr = "_2_";
$ustr = "_1_";
}
open (IN, $file) ||
die "can't open bookmarks.html\n";
print "Processing bookmarks\n";
# Read heading
do {
$_ = <IN>;
$header .= $_;
if (/<H1>/) {
($folder) = />([^<]+)</;
}
} until /<DL>/;
# $end is the last line in bookmark file, usually </dl><p>
$end = &ProcessFolder($folder);
close (IN);
rename ($file, $bakfile) ||
die "can't rename $file\n";
open (OUT, ">$file") ||
die "can't open $file for writing\n";
print OUT $header;
# Sort keys, ignoring case, printing out values
print "Writing output to $file\n";
foreach $keys (sort {uc($a) cmp uc($b)} (keys %bm ) ) {
print OUT "$bm{$keys}";
}
print OUT $end;
close (OUT);
#-------------------------------------------------
sub ProcessFolder {
local ($key) = @_;
local ($tempkey, $value);
$tempkey = $key;
while (<IN>) {
if (/<H3/) {
# Create entry for folder & process it
$bm{$tempkey} = $value unless $value eq "";
$value = $_;
($folder) = /<H3[^>]+>([^<]+)</;
$tempkey = join("", $key, $fstr, $folder);
do {
$_ = <IN>;
$value .= $_;
} until /<DL>/;
$bm{$tempkey} = $value unless $value eq "";
$value = &ProcessFolder($tempkey);
$tempkey .= $dlstr;
$bm{$tempkey} = $value unless $value eq "";
$tempkey = $key;
$value = "";
} elsif (/<HR>/) {
# Create entry for separator
$bm{$tempkey} = $value unless $value eq "";
$key .= $HR;
$tempkey = $key;
$value = $_;
$bm{$tempkey} = $value unless $value eq "";
$value = "";
} elsif (/<A HREF=/) {
# Create entry for link
$bm{$tempkey} = $value unless $value eq "";
($url, $title) = /<A HREF="([^"]+)"[^>]+>([^<]+)</ ;
$tempkey = join ("", $key, $ustr, ($U ? $url : $title.$url));
$value = $_;
$bm{$tempkey} = $value unless $value eq "";
} elsif (m#</DL>#) {
# End of folder, add and return
$bm{$tempkey} = $value unless $value eq "";
return ($_);
} else {
# Everything else, append to current value
# should just be bookmark descriptions
$value .= $_;
}
}
}