Article 13276 of comp.lang.perl: Xref: feenix.metronet.com comp.infosystems.www:14537 comp.lang.perl:13276 Newsgroups: comp.infosystems.www,comp.lang.perl Path: feenix.metronet.com!news.utdallas.edu!chpc.utexas.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!netline-fddi.jpl.nasa.gov!nntp-server.caltech.edu!news.claremont.edu!paris.ics.uci.edu!csulb.edu!csus.edu!netcom.com!dls From: dls@netcom.com (Daniel L Smith) Subject: abex - abbrev. expander for HTML docs and more Message-ID: Organization: Vivid Software Date: Fri, 29 Apr 1994 18:48:41 GMT Lines: 173 Ever type long paths in HTML over and over and... Ever want to write an HTML template, and have it do different things on different servers, or be able to update the resulting HTML easily? Perhaps from crontab? I like the current crop of HTML editors (I tend to use the emacs mode for it), but I don't like to commit to every last filename and so on when trying to sketch out a page - I'd rather get the template down and then expand it into HTML. I wrote a simple perl script to expand abbreviations. The abbreviations appear in your template like this one: ~WWW~, and are expanded according to how you set up abbreviations in a separate file. This script is intended to address 90% of the problem with 10% of the work. I'm open to lots of improvements, but I don't intend it to be some heavy duty macro facility or anything like that. Below is "abex". Say "nroff -man abex | $PAGER" for an example. Have fun, Daniel #! /usr/local/bin/perl 'di'; 'ig00'; # # $Header$ # # $Log$ # # abex - abbreviation expander 1.0 # # Daniel Smith (dls@autodesk.com), April 29, 1994 # default delimiter $delimiter = '~'; $DEBUG = 0; # set to 1 to see associative array if ($#ARGV != 1) { print "usage: abex abbrev_file file_to_expand\n"; exit 0; } open (MACRO_FILE, "< $ARGV[0]") || die "can't open macro file\n"; # gather abbrevs and what they expand to while () { chop; # our comments are '#' if (/^#/) { next; } ($abbrev, $expand) = split('\t'); $all_abbrevs{$abbrev} = $expand; } $ev_str = "while () {\n"; foreach (keys %all_abbrevs) { $ev_str .= "\ts!$delimiter$_$delimiter!$all_abbrevs{$_}!g;\n"; } $ev_str .= "\tprint;\n"; $ev_str .= "}\n"; $DEBUG && print " our string is $ev_str "; open (REPLACE_FILE, "< $ARGV[1]") || die "can't open source file\n"; eval $ev_str; ############################################################### # These next few lines are legal in both Perl and nroff. .00; # finish .ig 'di \" finish diversion--previous line must be blank .nr nl 0-1 \" fake up transition to first page again .nr % 0 \" start at page 1 '; __END__ ##### From here on it's a standard manual page ##### .TH ABEX 1 "April 29, 1994" .AT 3 .SH NAME abex \- abbreviation expander .SH SYNOPSIS .B abex abbreviation_file template .SH DESCRIPTION .I Abex expands template files after reading an abbrevation file. It can be used for writing HTML templates and much more. Abbreviations in the template are delimited by the tilde '~' character. The expansion goes to standard output. Abex is well suited for updating documents from crontab (can change abbreviation files, etc.). .SH ABBREVIATION FILE FORMAT .nf A sample abbreviation file: # abbreviation file # # format is # WAIS Wide Area Information Server WWW World Wide Web WWWW World Wide Web Worm SGML Standard Generalized Markup Language RTF Rich Text Format # as # # href start, href end # hs HREF="http://some_host/ he "> # bs be # mle This is\\na multiple\\nline\\nexample .SH SAMPLE TEMPLATE Here is a sample. Note that while the abbreviations here are intended to be constant, the output you get is entirely up to which abbreviation file you feed into Abex. .nf ~bs~ this is a test for ~WWW~ Here is a ~as~ ~hs~some/file/path~he~ reference~ae~ This should be a multiple line example (but not in ~RTF~). ~mle~ ~be~ The result is: this is a test for World Wide Web Here is a reference This should be a multiple line example (but not in Rich Text Format). This is a multiple line example .SH ENVIRONMENT No environment variables are used. .SH FILES None. .SH AUTHOR Daniel L. Smith (dls@autodesk.com, dls@netcom.com), POB 613, Sausalito, CA, 94966 .SH DIAGNOSTICS Set $DEBUG to 1 to see the string that gets built up from the abbreviation file. .SH BUGS Don't use the character '!' in an abbreviation or an expansion. Perhaps '^' would be better? -- dls@netcom.com dls@autodesk.com Daniel L Smith, P.O.B. 613, Sausalito, 94966, California