From: Adam Spiers <adam@thelonious.new.ox.ac.uk>
To: Kevin Sullivan <kevins@mediaone.net>
Cc: zsh workers mailing list <zsh-workers@sunsite.auc.dk>
Subject: PATCH: automatically generate ~/.zsh/urls for use with _urls
Date: Tue, 9 Nov 1999 02:13:48 +0000 [thread overview]
Message-ID: <19991109021348.A26905@thelonious.new.ox.ac.uk> (raw)
In-Reply-To: <14332.42410.2764.422238@kevins>
Kevin Sullivan (kevins@mediaone.net) wrote:
> Adam Spiers writes:
> > Incidentally, I've recently written a Perl module which parses
> > Netscape bookmark files, and a program which harnesses this to
> > generate the ~/.zsh/urls hierarchy. It will be more freely available
> > when I get around to CPANifying the module, but for now if anyone's
> > interested in it, drop me a mail.
>
> I am certainly very interested...
Apologies for the delay on this. I've finally CPANified the module
(about to upload it to PAUSE now, but you can get it from
http://www.new.ox.ac.uk/~adam/computing/URI-Bookmarks/ in the
mean-time).
This patch addes the program which creates the ~/.zsh/urls hierarchy.
I hope that license is OK; if not I'll change it to something that is.
Index: Misc/make-zsh-urls
===================================================================
+++ Misc/make-zsh-urls Tue Nov 9 02:03:33 1999
@@ -0,0 +1,157 @@
+#!/usr/bin/perl -w
+#
+# $Id: make-zsh-urls,v 1.4 1999/11/09 02:02:29 localadams Exp $
+
+use strict;
+
+=head1 NAME
+
+make-zsh-urls -- create F<~/.zsh/urls> hierarchy
+
+=head1 SYNOPSIS
+
+% make-zsh-urls [B<OPTION>] ...
+
+=head1 DESCRIPTION
+
+make-zsh-urls creates a hierarchy of files and directories under
+F<~/.zsh/urls> for use by the _urls completion function in the new
+completion system of zsh 3.1.6 and higher.
+
+It needs the B<URI::Bookmarks> suite of modules to run, which are
+available from CPAN, the Comprehensive Perl Archive Network.
+See B<http://www.perl.com/cpan> or L<CPAN> for more information.
+
+The following options are available:
+
+B<--output-dir>, B<-o> Specify the output directory for the
+ hierarchy. Defaults to F<~/.zsh/urls>.
+
+B<--input-file>, B<-i> Specify the input bookmarks file.
+ Defaults to F<~/.netscape/bookmarks.html>.
+
+B<--root-node>, B<-r> Specify which folder contains the
+ bookmarks which the hierarchy will be
+ created from. Defaults to the root
+ of the bookmark collection tree.
+
+=cut
+
+use Getopt::Long;
+use URI::Bookmarks::Netscape;
+use URI;
+
+my ($out_dir, $input_file, $root_name, $help);
+GetOptions('output-dir|o=s' => \$out_dir,
+ 'input-file|i=s' => \$input_file,
+ 'root-node|r=s' => \$root_name,
+ 'help|h' => \$help)
+ or usage();
+
+usage() if $help;
+
+$out_dir ||= "$ENV{HOME}/.zsh/urls";
+$input_file ||= "$ENV{HOME}/.netscape/bookmarks.html";
+
+my $bookmarks =
+ new URI::Bookmarks(file => $input_file);
+
+my $root = $bookmarks->tree_root();
+if ($root_name) {
+ my @root_nodes = $bookmarks->name_to_nodes($root_name);
+ if (@root_nodes == 0) {
+ die "Couldn't find any nodes with name `$root_name'; aborting.\n";
+ }
+ else {
+ if (@root_nodes > 1) {
+ warn "Found more than one node with name `$root_name'; " .
+ "taking first occurrence.\n";
+ }
+ $root = $root_nodes[0];
+ }
+}
+
+my @bookmark_path = ();
+$root->walk_down({callback => \&pre_callback,
+ callbackback => \&post_callback});
+
+sub pre_callback {
+ my ($node, $options) = @_;
+
+ my $depth = $options->{_depth} || 0;
+ my $name = $node->name;
+ my $type = $node->type;
+
+ if ($type eq 'bookmark') {
+ my $url = $node->attribute->{'HREF'};
+
+ # Type A
+ my $full = $url;
+ $full =~ s@^(https?|ftp|gopher)://@"\L$1/"@ei;
+ $full =~ s@file:@@i;
+ my ($path, $file) = $full =~ m@(.+)/(.*)@;
+ # This is horribly inefficient but I'm too lazy to reimplement mkdir -p
+ # Why isn't there a CPAN module for it?
+ system '/bin/mkdir', '-p', "$out_dir/$path" unless -d "$out_dir/$path";
+ system 'touch', "$out_dir/$path" unless $full eq "$path/";
+
+ # Type B
+ $name =~ s@/@-@g;
+ my $bookmark_file = "$out_dir/bookmark/" .
+ (join '/', @bookmark_path) .
+ "/$name";
+ open(BOOKMARK, ">$bookmark_file") or die "open >$bookmark_file: $!";
+ print BOOKMARK $url, "\n";
+ close(BOOKMARK) or die $!;
+ }
+ elsif ($type eq 'folder' && $depth > 0) {
+ print +(' ' x ($depth - 1)), "Processing folder `$name' ...\n";
+ push @bookmark_path, $name;
+
+ # Type B
+ system '/bin/mkdir',
+ '-p',
+ "$out_dir/bookmark/" .
+ (join '/', @bookmark_path);
+ }
+
+ return 1;
+}
+
+sub post_callback {
+ my ($node, $options) = @_;
+
+ my $type = $node->type;
+
+ if ($type eq 'folder') {
+ my $name = pop @bookmark_path;
+ }
+}
+
+sub usage {
+ print <<EOF;
+Usage: make-zsh-urls [OPTION] ...
+ --help, -h Display this help.
+ --output-dir, -o Specify the output directory for the hierarchy.
+ Defaults to ~/.zsh/urls.
+ --input-file, -i Specify the input bookmarks file.
+ Defaults to ~/.netscape/bookmarks.html.
+ --root-node, -r Specify which folder contains the bookmarks which
+ the hierarchy will be created from. Defaults to
+ the root of the bookmark collection tree.
+EOF
+ exit 0;
+}
+
+
+=head1 AUTHOR
+
+ Adam Spiers <adam@spiers.net>
+
+=head1 COPYRIGHT
+
+ Copyright (c) 1999 Adam Spiers <adam@spiers.net>. All rights
+ reserved. This program is free software; you can redistribute it and/or
+ modify it under the same terms as Perl or zsh.
+
+=cut
prev parent reply other threads:[~1999-11-09 2:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-10-07 11:42 PATCH: Support file url in _urls Oliver Kiddle
1999-10-07 11:47 ` Zefram
1999-10-07 13:21 ` Tanaka Akira
1999-10-07 13:26 ` Adam Spiers
[not found] ` <14332.42410.2764.422238@kevins>
1999-11-09 2:13 ` Adam Spiers [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=19991109021348.A26905@thelonious.new.ox.ac.uk \
--to=adam@thelonious.new.ox.ac.uk \
--cc=adam@spiers.net \
--cc=kevins@mediaone.net \
--cc=zsh-workers@sunsite.auc.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).