shosei

make_zsh_urls

May 10th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.29 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # $Id: make-zsh-urls,v 1.4
  4.  
  5. use strict;
  6.  
  7. =head1 NAME
  8.  
  9. make-zsh-urls -- create F<~/.zsh/urls> hierarchy
  10.  
  11. =head1 SYNOPSIS
  12.  
  13. % make-zsh-urls [B<OPTION>] ...
  14.  
  15. =head1 DESCRIPTION
  16.  
  17. make-zsh-urls creates a hierarchy of files and directories under
  18. F<~/.zsh/urls> for use by the _urls completion function in the new
  19. completion system of zsh 3.1.6 and higher.
  20.  
  21. It needs the B<URI::Bookmarks> suite of modules to run, which are
  22. available from CPAN, the Comprehensive Perl Archive Network.
  23. See B<http://www.perl.com/cpan> or L<CPAN> for more information.
  24.  
  25. The following options are available:
  26.  
  27. B<--output-dir>, B<-o>   Specify the output directory for the
  28.                    hierarchy.  Defaults to F<~/.zsh/urls>.
  29.  
  30. B<--input-file>, B<-i>   Specify the input bookmarks file.
  31.                    Defaults to F<~/.netscape/bookmarks.html>.
  32.  
  33. B<--root-node>, B<-r>    Specify which folder contains the
  34.                    bookmarks which the hierarchy will be
  35.                    created from.  Defaults to the root
  36.                    of the bookmark collection tree.
  37.  
  38. =cut
  39.  
  40. use Getopt::Long;
  41. use URI::Bookmarks::Netscape;
  42. use URI;
  43.  
  44. my ($out_dir, $input_file, $root_name, $help);
  45. GetOptions('output-dir|o=s' => \$out_dir,
  46.            'input-file|i=s' => \$input_file,
  47.             'root-node|r=s' => \$root_name,
  48.                    'help|h' => \$help)
  49.   or usage();
  50.  
  51. usage() if $help;
  52.  
  53. $out_dir ||= "$ENV{HOME}/.zsh/urls";
  54. $input_file ||= "$ENV{HOME}/.netscape/bookmarks.html";
  55.  
  56. my $bookmarks =
  57.   new URI::Bookmarks(file => $input_file);
  58.  
  59. my $root = $bookmarks->tree_root();
  60. if ($root_name) {
  61.   my @root_nodes = $bookmarks->name_to_nodes($root_name);
  62.   if (@root_nodes == 0) {
  63.     die "Couldn't find any nodes with name `$root_name'; aborting.\n";
  64.   }
  65.   else {
  66.     if (@root_nodes > 1) {
  67.       warn "Found more than one node with name `$root_name'; " .
  68.            "taking first occurrence.\n";
  69.     }
  70.     $root = $root_nodes[0];
  71.   }
  72. }    
  73.  
  74. my @bookmark_path = ();
  75. $root->walk_down({callback     => \&pre_callback,
  76.                   callbackback => \&post_callback});
  77.  
  78. sub pre_callback {
  79.   my ($node, $options) = @_;
  80.  
  81.   my $depth = $options->{_depth} || 0;
  82.   my $name = $node->name;
  83.   my $type = $node->type;
  84.  
  85.   if ($type eq 'bookmark') {
  86.     my $url = $node->attribute->{'HREF'};
  87.  
  88.     # Type A
  89.     my $full = $url;
  90.     $full =~ s@^(https?|ftp|gopher)://@"\L$1/"@ei;
  91.     $full =~ s@file:@@i;
  92.     my ($path, $file) = $full =~ m@(.+)/(.*)@;
  93.     # This is horribly inefficient but I'm too lazy to reimplement mkdir -p
  94.     # Why isn't there a CPAN module for it?
  95.     system '/bin/mkdir',  '-p', "$out_dir/$path" unless -d "$out_dir/$path";
  96.     system 'touch', "$out_dir/$path" unless $full eq "$path/";
  97.  
  98.     # Type B
  99.     $name =~ s@/@-@g;
  100.     my $bookmark_file = "$out_dir/bookmark/" .
  101.                         (join '/', @bookmark_path) .
  102.                          "/$name";
  103.     open(BOOKMARK, ">$bookmark_file") or die "open >$bookmark_file: $!";
  104.     print BOOKMARK $url, "\n";
  105.     close(BOOKMARK) or die $!;
  106.   }
  107.   elsif ($type eq 'folder' && $depth > 0) {
  108.     print +('  ' x ($depth - 1)), "Processing folder `$name' ...\n";
  109.     push @bookmark_path, $name;
  110.  
  111.     # Type B
  112.     system '/bin/mkdir',
  113.              '-p',
  114.              "$out_dir/bookmark/" .
  115.              (join '/', @bookmark_path);
  116.   }    
  117.  
  118.   return 1;
  119. }
  120.  
  121. sub post_callback {
  122.   my ($node, $options) = @_;
  123.  
  124.   my $type = $node->type;
  125.  
  126.   if ($type eq 'folder') {
  127.     my $name = pop @bookmark_path;
  128.   }    
  129. }
  130.  
  131. sub usage {
  132.   print <<EOF;
  133. Usage: make-zsh-urls [OPTION] ...
  134.   --help, -h         Display this help.
  135.   --output-dir, -o   Specify the output directory for the hierarchy.
  136.                      Defaults to ~/.zsh/urls.
  137.   --input-file, -i   Specify the input bookmarks file.
  138.                      Defaults to ~/.netscape/bookmarks.html.
  139.   --root-node, -r    Specify which folder contains the bookmarks which
  140.                      the hierarchy will be created from.  Defaults to
  141.                      the root of the bookmark collection tree.  
  142. EOF
  143.   exit 0;
  144. }
  145.  
  146.  
  147. =head1 AUTHOR
  148.  
  149.   cyn
  150.  
  151. =head1 COPYRIGHT
  152.  
  153.   Copyright (c) 2007 Cyn <[email protected]>. All rights
  154.   reserved. This program is free software; you can redistribute it and/or
  155.   modify it under the same terms as Perl or zsh.
  156.  
  157. =cut
Advertisement
Add Comment
Please, Sign In to add comment