Sunday, May 30, 2010

Tooltips with Vim Regular Expressions

Off the top of your head, can you define "anodyne" or "faradic"?

Probably not, but these and many more obscure words, occur throughout Traditional Hydrotherapy. To overcome the lack of knowledge of these terms I had made up a glossary. I now wanted to make this available easily to the reader of Traditional Hydrotherapy. The answer was in Tooltips.

To illustrate a tooltip just hover your mouse over anodyne or faradic. That box that springs up is a tooltip and exactly what I needed... a simple way to get the definition to the reader.

The code for the "anodyne" tooltip looks like this:

<span title="a treatment to relieve pain">anodyne</span>

Problems presented themselves immediately, and, as always, Vim was up to the task.

This is the text from the glossary file (made under DOS with PCWrite):

#card @faradic
electric current used mainly for stimulating innervated muscle, consists of pulsed DC current

Problem: I had to turn the definition into a single line.
Answer: Use "J", which appends the following line with a space in between.

Second Problem: These words are often part of larger words and I didn't want to chop words up.
Answer: Use the Vim regular expressions:
Word Beginning, "\<", and Word End, "\>".

This way I would be able to make tooltips around whole words only.

Thirdly: how do I substitute the text for both upper and lowercase occurrences, "Faradic" and "faradic"?

The answer is two macros that begin on the "#card" in the glossary file, glos.txt and split to a single file which contained all four html files (appropriately called "big.html" as it ended up at over 3Mb:

@G - which deals with lowercase version

then @M which does the tooltips for the upper-case and also deals with words in the titles of pages.

and @N a final macro that corrects some of the changes @M made and goes to the next card in the glossary.

Let's have a look at the macros:
/@^Ml"tyEj^"dy$:sp big.html^M:%s+\<^Rt\>+<span title="^Rd">^Rt</span>+gc^M
/@^M -search for "@"
l"tyE - move right one (onto the first letter of the title) and yank to End of the word into register "t"
j^"dy$ - move down one line and to the beginning of the word then yank to end of line into register "d"
:sp big.html^M - split screen and open into the file containing all the html
:%s+\<^Rt\>+<span title="^Rd">^Rt</span>+gc^M
:%s+\<^Rt\>+^Rt+gc^M - the business substitution - using Word Beginning, "\<", and Word End, "\>", this is to prevent words as part of URLs being substituted. The registers are called using the "-R (register letter)" form. The substitution is for all occurrences on a line and "c"hecks. so I can be sure it is going OK.

^Wpk/@^Ml~h"lyE^Wp:%s+^Rl.html+LL^Rl.html+ge^M:%s+\<^Rl\>+<span title="^Rd">^Rl</span>+gc^M
^Wp - return to the previous window (glos.txt) with -W p
k/@^M - move up a line and search for "@"
l~h - move one letter to the right and change the case of the letter (to capitalise)
"lyE^Wp - yank to end of the word into register "l" and return to the previous window (big.html)
:%s+^Rl.html+LL^Rl.html+ge^M - a substitution to add "LL" to the beginning of the word if it is a filename and thus preventing it being changed.
:%s+\<^Rl\>+<span title="^Rd">^Rl</span>+gc^M - much the same as the substitution in @G except using register "l" instead of "t"

:%s+LL^Rl+^Rl+ge^M - a substitution to remove the, now unneeded "LL" notice the "e" tag which means it doesn't stop on an error (usually where the LL doesn't exist because there were no pages to be made with that filename)
:wq^M - write and quit the big.html file
j/#card^M - now in glos.txt, move down a line and search for the next occurrence of "#card" (for the next word)

Using these files I managed to add tooltips to about 200 words in my spare time last week.

Now to make all those files (and validate them).