Changeset 11121 for trunk/gsdl/bin/script
- Timestamp:
- 2006-01-26T17:09:45+13:00 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/bin/script/expand_macros.pl
r10704 r11121 3 3 ################################################################################## 4 4 # # 5 # expand_macros.pl -- recursively expand greenstone macros / 1080805 - 1 050905#5 # expand_macros.pl -- recursively expand greenstone macros / 1080805 - 1230106 # 6 6 # # 7 # Copyright (C) 2005 Jens Wille <j_wille at gmx.net>#7 # Copyright (C) 2005,2006 Jens Wille <j_wille at gmx.net> # 8 8 # # 9 9 # This program is free software; you can redistribute it and/or # … … 60 60 # - handle macro options better (v, c, l, ...?) 61 61 # 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: 64 67 # 65 68 # - for a sufficiently large file (> 12288 bytes == 12 k) (paged-)read will quit … … 74 77 75 78 use File::Basename qw(basename dirname); 76 use File::Spec::Functions qw(catdir catfile tmpdir);79 use File::Spec::Functions qw(catdir catfile curdir); 77 80 78 81 use IO::Handle qw(autoflush); … … 85 88 86 89 my $NAME = basename $0; 87 my $VERSION = '0.2 1';90 my $VERSION = '0.22'; 88 91 89 92 … … 116 119 my $DEFINITION_START = '\{'; 117 120 118 # end of macro definition: closing curly bracket (' {'), not escaped,121 # end of macro definition: closing curly bracket ('}'), not escaped, 119 122 # i.e. not preceded by a backslash ('\') 120 123 # (need to double-escape backslash here!) … … 137 140 'paged' => 0, 138 141 'pager' => $ENV{'PAGER'} || 'less', 139 'histfile' => catfile($ENV{' GSDLTMPDIR'} || tmpdir, 'expand_macros.hist'),142 'histfile' => catfile($ENV{'HOME'} || curdir, '.expand_macros.history'), 140 143 'histsize' => 100, 141 144 'macro_dirs' => [catdir($ENV{'GSDLHOME'}, 'macros')], … … 195 198 196 199 (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_ 197 215 198 216 … … 217 235 HERE_HELP 218 236 219 my $ INTERACTIVE_HELP= <<HERE_HELP;237 my $HELP_INTERACTIVE = <<HERE_HELP; 220 238 $NAME: expand greenstone macros in ***interactive browse mode*** (v$VERSION) 221 239 … … 224 242 225 243 - 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) 227 245 228 246 - commands that take an optional argument ([...]) will print their current value if … … 283 301 284 302 285 ### some sanity checks 303 ### some sanity checks (dunno which one to check first ;-) 286 304 287 305 # need one of our "actions": batch, query or interactive 288 306 # ("batch" requiring at least one macro name or regexp specified) 289 die "$USAGE\n" 290 unless @{$ARG{'args'}} || $ARG{'interactive'}; 307 unless (@{$ARG{'args'}} || $ARG{'interactive'}) { 308 warn "$USAGE"; 309 310 warn <<HERE_WARN unless $ENV{'GSDLHOME'}; 311 312 GSDLHOME not set! 313 314 for this script to run your greenstone environment needs to be set up. please 315 change into the directory where greenstone has been installed and run/source 316 the appropriate setup script. 317 HERE_WARN 318 319 die "\n"; 320 } 291 321 292 322 # need GSDLHOME for default macro directory … … 295 325 unless $ENV{'GSDLHOME'}; 296 326 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 297 332 298 333 ### action! … … 327 362 328 363 # now recurse those macros 329 foreach my $m (@refs) { 330 get_macro($m); 331 } 364 get_macro($_) 365 foreach @refs; 332 366 } 333 367 } … … 343 377 344 378 # now print those macros 345 foreach my $m (@macros) { 346 get_macro($m); 347 } 379 get_macro($_) 380 foreach @macros; 348 381 } 349 382 … … 360 393 $SIG{'PIPE'} = 'IGNORE'; 361 394 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) 364 396 get_pager() 365 397 if $ARG{'paged'}; … … 459 491 next unless length $line; # skip empty lines 460 492 next if $line =~ /^\s*$/; # skip "empty" lines 461 next if $line =~ /^\s*#/; # skip comments ( i hope this doesn't462 # affectcases we actually wanted to keep)493 next if $line =~ /^\s*#/; # skip comments (hope this doesn't affect 494 # cases we actually wanted to keep) 463 495 464 496 if ($line =~ /^$PACKAGE_DECLARATION\s*($PACKAGE_NAME)/) { … … 476 508 # don't include unnecessary version, unless we're interactive (where version may change during session) 477 509 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;481 510 482 511 if (exists $macro_db{$name}->{$curpkg}->{$version}) { … … 496 525 $macro_db{$name}->{$curpkg}->{$version}->{'line'} = $.; 497 526 $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; 498 530 } 499 531 elsif ($contd) { 500 532 # continuation of macro definition 501 533 534 # store additional content 535 push(@{$macro_db{$name}->{$curpkg}->{$version}->{'content'}} => $line); 536 502 537 # is the macro definition already finished? 503 538 $contd = ($line =~ s/\s*$DEFINITION_END.*//) ? 0 : 1; 504 505 # store additional content506 push(@{$macro_db{$name}->{$curpkg}->{$version}->{'content'}} => $line);507 539 } 508 540 else { … … 675 707 # we want to skip some macros that have no content anyway (defined 676 708 # from within the server) - unless we're doing a "reverse" search 677 next if $seen{$m}++ || ($m =~ /^(cgiarg.*| if|httpimg|gwcgi|(decoded)?compressedoptions)$/i709 next if $seen{$m}++ || ($m =~ /^(cgiarg.*|histvalue\d+|if|httpimg|gwcgi|(decoded)?compressedoptions)$/i 678 710 && ! $ARG{'reverse'}); 679 711 … … 702 734 # usage: 703 735 # @macros = get_r_macros($macro) 704 # @macros = get_r_macros($query )736 # @macros = get_r_macros($query, $is_query) 705 737 # 706 738 # macro: macro name 707 739 # query: query string (regular expression) 740 # is_query: boolean value to indicate whether arg is a query or a macro 708 741 # 709 742 # => macros: list of macros … … 947 980 # 948 981 # usage: 949 # $ line= prompt([$prompt])982 # $reply = prompt([$prompt]) 950 983 # 951 984 # prompt: optional prompt (default: '> ') 952 985 # 953 # => line: user input986 # => reply: user input 954 987 # 955 988 sub prompt { … … 958 991 959 992 # 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) { 963 996 # 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*\././) { 968 1001 # execute command 969 my $return = parse_command($ line);1002 my $return = parse_command($reply); 970 1003 971 1004 return $return if defined $return; 972 1005 } 973 1006 else { 974 return $ line;1007 return $reply; 975 1008 } 976 1009 } … … 998 1031 if ($ARG{'paged'}) { 999 1032 # pass output to pager 1000 open( LESS, "| $ARG{'pager'}")1033 open(PAGER, "| $ARG{'pager'}") 1001 1034 or die "can't open pipe to '$ARG{'pager'}': $!"; 1002 1035 1003 print LESS"$output";1004 1005 close LESS;1036 print PAGER "$output"; 1037 1038 close PAGER; 1006 1039 } 1007 1040 else { … … 1024 1057 # 1025 1058 sub wait_for_user { 1026 print "[press <enter>to continue]";1059 print "[press key to continue]"; 1027 1060 print "\n" if <STDIN>; 1028 1061 } … … 1053 1086 1054 1087 if ($command =~ /^(h|\?|help)$/) { 1055 print "$ INTERACTIVE_HELP\n";1088 print "$HELP_INTERACTIVE\n"; 1056 1089 1057 1090 # wait for user reaction to continue
Note:
See TracChangeset
for help on using the changeset viewer.