Changeset 11121


Ignore:
Timestamp:
2006-01-26T17:09:45+13:00 (18 years ago)
Author:
mdewsnip
Message:

Updated expand_macros.pl script for super cool macro browsing, by Jens Wille.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/bin/script/expand_macros.pl

    r10704 r11121  
    33##################################################################################
    44#                                                                                #
    5 # expand_macros.pl -- recursively expand greenstone macros / 1080805 - 1050905   #
     5# expand_macros.pl -- recursively expand greenstone macros / 1080805 - 1230106   #
    66#                                                                                #
    7 # Copyright (C) 2005 Jens Wille <j_wille at gmx.net>                             #
     7# Copyright (C) 2005,2006 Jens Wille <j_wille at gmx.net>                        #
    88#                                                                                #
    99# This program is free software; you can redistribute it and/or                  #
     
    6060# - handle macro options better (v, c, l, ...?)
    6161#
    62 #
    63 # KNOWN BUGS:
     62# - implement some kind of "persistent" macro db (so that it doesn't need to be
     63#   built each and every time)
     64#
     65#
     66# KNOWN ISSUES:
    6467#
    6568# - for a sufficiently large file (> 12288 bytes == 12 k) (paged-)read will quit
     
    7477
    7578use File::Basename        qw(basename dirname);
    76 use File::Spec::Functions qw(catdir catfile tmpdir);
     79use File::Spec::Functions qw(catdir catfile curdir);
    7780
    7881use IO::Handle            qw(autoflush);
     
    8588
    8689my $NAME    = basename $0;
    87 my $VERSION = '0.21';
     90my $VERSION = '0.22';
    8891
    8992
     
    116119my $DEFINITION_START    = '\{';
    117120
    118 # end of macro definition: closing curly bracket ('{'), not escaped,
     121# end of macro definition: closing curly bracket ('}'), not escaped,
    119122# i.e. not preceded by a backslash ('\')
    120123# (need to double-escape backslash here!)
     
    137140  'paged'       => 0,
    138141  'pager'       => $ENV{'PAGER'} || 'less',
    139   'histfile'    => catfile($ENV{'GSDLTMPDIR'} || tmpdir, 'expand_macros.hist'),
     142  'histfile'    => catfile($ENV{'HOME'} || curdir, '.expand_macros.history'),
    140143  'histsize'    => 100,
    141144  'macro_dirs'  => [catdir($ENV{'GSDLHOME'}, 'macros')],
     
    195198
    196199    (you can restrict your macro name query to a certain package by prepending the macro name with '<package-name>:')
     200
     201    EXAMPLES:
     202
     203      get definition of the 'pagescriptextra' macro
     204      > $NAME _pagescriptextra_
     205
     206      get definition of the 'pagescriptextra' macro, package 'query' only
     207      > $NAME _query:pagescriptextra_
     208
     209      get definition of the 'pagescriptextra' macro, package 'query' only -- and recursively get definitions of
     210      macros used within that definition (up to level 2)
     211      > $NAME -d 2 _query:pagescriptextra_
     212
     213      get all the macros that use the 'pagescriptextra' macro (names only)
     214      > $NAME -r -s _pagescriptextra_
    197215
    198216
     
    217235HERE_HELP
    218236
    219 my $INTERACTIVE_HELP = <<HERE_HELP;
     237my $HELP_INTERACTIVE = <<HERE_HELP;
    220238$NAME: expand greenstone macros in ***interactive browse mode*** (v$VERSION)
    221239
     
    224242
    225243    - commands are equal to command line options, except that they start with a dot '.'
    226       (NOTE: not all command line options are available as command, see list below)
     244      (NOTE: not all command line options are available as commands, see list below)
    227245
    228246    - commands that take an optional argument ([...]) will print their current value if
     
    283301
    284302
    285 ### some sanity checks
     303### some sanity checks (dunno which one to check first ;-)
    286304
    287305# need one of our "actions": batch, query or interactive
    288306# ("batch" requiring at least one macro name or regexp specified)
    289 die "$USAGE\n"
    290   unless @{$ARG{'args'}} || $ARG{'interactive'};
     307unless (@{$ARG{'args'}} || $ARG{'interactive'}) {
     308  warn "$USAGE";
     309
     310  warn <<HERE_WARN unless $ENV{'GSDLHOME'};
     311
     312GSDLHOME not set!
     313
     314for this script to run your greenstone environment needs to be set up. please
     315change into the directory where greenstone has been installed and run/source
     316the appropriate setup script.
     317HERE_WARN
     318
     319  die "\n";
     320}
    291321
    292322# need GSDLHOME for default macro directory
     
    295325  unless $ENV{'GSDLHOME'};
    296326
     327## need one of our "actions": batch, query or interactive
     328# ("batch" requiring at least one macro name or regexp specified)
     329#die "$USAGE\n"
     330#  unless @{$ARG{'args'}} || $ARG{'interactive'};
     331
    297332
    298333### action!
     
    327362
    328363        # now recurse those macros
    329         foreach my $m (@refs) {
    330           get_macro($m);
    331         }
     364        get_macro($_)
     365          foreach @refs;
    332366      }
    333367    }
     
    343377
    344378      # now print those macros
    345       foreach my $m (@macros) {
    346         get_macro($m);
    347       }
     379      get_macro($_)
     380        foreach @macros;
    348381    }
    349382
     
    360393  $SIG{'PIPE'} = 'IGNORE';
    361394
    362   # get the pager executable
    363   # no need to test if not in "paged" mode
     395  # get the pager executable (no need to test if not in "paged" mode)
    364396  get_pager()
    365397    if $ARG{'paged'};
     
    459491      next unless length $line;      # skip empty lines
    460492      next if     $line =~ /^\s*$/;  # skip "empty" lines
    461       next if     $line =~ /^\s*#/;  # skip comments (i hope this doesn't
    462                                      # affect cases we actually wanted to keep)
     493      next if     $line =~ /^\s*#/;  # skip comments (hope this doesn't affect
     494                                     # cases we actually wanted to keep)
    463495
    464496      if    ($line =~ /^$PACKAGE_DECLARATION\s*($PACKAGE_NAME)/) {
     
    476508        # don't include unnecessary version, unless we're interactive (where version may change during session)
    477509        next if $ARG{'version'} ne '-1' && $version ne $ARG{'version'} && ! $ARG{'interactive'};
    478 
    479         # is the macro definition already finished?
    480         $contd = ($content =~ s/\s*$DEFINITION_END.*//) ? 0 : 1;
    481510
    482511        if (exists $macro_db{$name}->{$curpkg}->{$version}) {
     
    496525        $macro_db{$name}->{$curpkg}->{$version}->{'line'}    = $.;
    497526        $macro_db{$name}->{$curpkg}->{$version}->{'content'} = [$content] if length $content;
     527
     528        # is the macro definition already finished?
     529        $contd = ($content =~ s/\s*$DEFINITION_END.*//) ? 0 : 1;
    498530      }
    499531      elsif ($contd) {
    500532        # continuation of macro definition
    501533
     534        # store additional content
     535        push(@{$macro_db{$name}->{$curpkg}->{$version}->{'content'}} => $line);
     536
    502537        # is the macro definition already finished?
    503538        $contd = ($line =~ s/\s*$DEFINITION_END.*//) ? 0 : 1;
    504 
    505         # store additional content
    506         push(@{$macro_db{$name}->{$curpkg}->{$version}->{'content'}} => $line);
    507539      }
    508540      else {
     
    675707                           # we want to skip some macros that have no content anyway (defined
    676708                           # from within the server) - unless we're doing a "reverse" search
    677     next if $seen{$m}++ || ($m =~ /^(cgiarg.*|if|httpimg|gwcgi|(decoded)?compressedoptions)$/i
     709    next if $seen{$m}++ || ($m =~ /^(cgiarg.*|histvalue\d+|if|httpimg|gwcgi|(decoded)?compressedoptions)$/i
    678710                            && ! $ARG{'reverse'});
    679711
     
    702734# usage:
    703735#   @macros = get_r_macros($macro)
    704 #   @macros = get_r_macros($query)
     736#   @macros = get_r_macros($query, $is_query)
    705737#
    706738#   macro:    macro name
    707739#   query:    query string (regular expression)
     740#   is_query: boolean value to indicate whether arg is a query or a macro
    708741#
    709742#   => macros: list of macros
     
    947980#
    948981# usage:
    949 #   $line = prompt([$prompt])
     982#   $reply = prompt([$prompt])
    950983#
    951984#   prompt:  optional prompt (default: '> ')
    952985#
    953 #   => line: user input
     986#   => reply: user input
    954987#
    955988sub prompt {
     
    958991
    959992  # read user input
    960   my $line = $term->readline($prompt);
    961 
    962   if (defined $line) {
     993  my $reply = $term->readline($prompt);
     994
     995  if (defined $reply) {
    963996    # add input to history, unless it's just a number
    964     $term->addhistory($line)
    965       if $line =~ /[[:alpha:]]/;
    966 
    967     if ($line =~ s/^\s*["']*\s*\././) {
     997    $term->addhistory($reply)
     998      if $reply =~ /[[:alpha:]]/;
     999
     1000    if ($reply =~ s/^\s*["']*\s*\././) {
    9681001      # execute command
    969       my $return = parse_command($line);
     1002      my $return = parse_command($reply);
    9701003
    9711004      return $return if defined $return;
    9721005    }
    9731006    else {
    974       return $line;
     1007      return $reply;
    9751008    }
    9761009  }
     
    9981031  if ($ARG{'paged'}) {
    9991032    # pass output to pager
    1000     open(LESS, "| $ARG{'pager'}")
     1033    open(PAGER, "| $ARG{'pager'}")
    10011034      or die "can't open pipe to '$ARG{'pager'}': $!";
    10021035
    1003     print LESS "$output";
    1004 
    1005     close LESS;
     1036    print PAGER "$output";
     1037
     1038    close PAGER;
    10061039  }
    10071040  else {
     
    10241057#
    10251058sub wait_for_user {
    1026   print "[press <enter> to continue]";
     1059  print "[press key to continue]";
    10271060  print "\n" if <STDIN>;
    10281061}
     
    10531086
    10541087    if    ($command =~ /^(h|\?|help)$/) {
    1055       print "$INTERACTIVE_HELP\n";
     1088      print "$HELP_INTERACTIVE\n";
    10561089
    10571090      # wait for user reaction to continue
Note: See TracChangeset for help on using the changeset viewer.