Maintaining my quotes file

I wanted to build some automated tools to maintain my quotes file. The thing is, the same raw quotes are used both for that web-page and as a source of signatures for my email program, and they kept getting out of sync. So I decided to automate the process. This is the (quick) account of how I did that. I hope to come back and explain how I figured out the various scripts, so if I haven’t done that and you’d like to see it, shoot me an email.

First, I needed to define a format for the raw quotes. I came up with the following:

% Premarital_Sex
"It's only premarital sex if you're going to get married."
                                                     - Anonymous
% tag_for_next quote
....
%%

That’s simple enough to parse, I figure. I needed output that looks like the following for my email signature file:

-- 
Dave Polaschek - http://betternerds.com/  http://davespicks.com/
"It's only premarital sex if you're going to get married."
                                                     - Anonymous
--
....

That seems easy enough to do. I just need to do some really simple substitutions. sed seems like a pretty good solution for the problem, and I came up with the following script (which I called brq.sed) which does the job just fine.

s|\% [-0-9a-zA-Z_\.]*|-- \
Dave Polaschek - http://betternerds.com/  http://davespicks.com/|
s|\%\%||

Next, and more tricky, was generating the web-page. After an hour or two of futzing around with awk, I came up with a script I called bqhtml.awk:

#!/usr/bin/awk -f
BEGIN { RS="% "; FS="\n"; OFS="\n"}
{
        DESCRIPTOR = substr($1,2);
        if (length(DESCRIPTOR) > 0) {
                QUOTE = "";
                for (i = 2; i < NF; i++) {
                        QUOTE = QUOTE "\n" $i;
                }
        printf ("<p><a name=\"%s\"><span class=\"pre\">%s
        		 <a href=\"#%s\">link</a></span></a></p><hr />\n",
                        DESCRIPTOR, QUOTE, DESCRIPTOR);
        }
}

(Note that the line beginning with the printf should all be one long line.)

That generates the guts of the quotes page for my website, but there are some special characters I have to deal with. Another sed script for handling that (below, called htmlspecials.sed), and add in a header and footer file that put in the rest of the HTML and I’m good to go.

s/&/\&amp;/g
s/</\&lt;/g
s/>/\&gt;/g
s/é/\&eacute;/g

Next up, I decided I wanted to build the files that fortune can use. Fortune files have the following format:

"It's only premarital sex if you're going to get married."
                                                     - Anonymous
%

sed isn’t quite up to the task again, so I’ll tackle the problem with awk. I start with the same parsing that the previous awk script used, and tweak the output so I have:

#!/usr/bin/awk -f
BEGIN { RS="% "; FS="\n"; OFS="\n"}
{
	DESCRIPTOR = substr($1,2);
	if (length(DESCRIPTOR) > 0) {
		QUOTE = "";
		for (i = 2; i < NF; i++) {
			QUOTE = QUOTE $i "\n";
		}
	printf ("%s%%\n", QUOTE);
	}
}

That gets everything in the right format, and then I need to use strfile (it’s included in the fortune sources) to build the index that fortune needs.

The final step is automating the whole thing so I have to think even less about it. I came up with the following makefile:

all: quotes.html randomquotes.txt fortunequotes.dat

randomquotes.txt: rawquotes.txt brq.sed
	sed -f brq.sed < rawquotes.txt > randomquotes.txt
	mail -s quotes me <randomquotes.txt

quotes.html: header.html body.html footer.html
	cat header.html body.html footer.html >quotes.html
	cp quotes.html /web/misc/quotes.html

body.html: rawquotes.txt htmlspecials.sed bqhtml.awk
	sed -f htmlspecials.sed <rawquotes.txt | bqhtml.awk >body.html

fortunequotes: rawquotes.txt raw2fortunes.awk
	raw2fortunes.awk <rawquotes.txt >fortunequotes

fortunequotes.dat: fortunequotes
	/usr/obj/games/fortune/strfile/strfile fortunequotes

I edit up the rawquotes.txt file, and from that, randomquotes.txt is generated. I email that to myself, and copy it into the signature file for my mail program, and that’s done. It also creates a quotes.html file and puts it in the right directory for my webserver to find it, and leaves the fortunes file right there in the same directory. Handy! The makefile also makes sure that if I edit one of the scripts used in the process, the file depending on it gets rebuilt as soon as I type "make", too.

Copyright 2009, Dave Polaschek. Last updated on Mon, 15 Feb 2010 14:09:34.