Sunday, February 14, 2010

Making an Index page with Vim Macros

As I worked my way through Kellogg's "Rational Hydrotherapy", whenever I came across particularly interesting bits of prose, I added these to a stack, "Quote". When making Traditional Hydrotherapy I appended these quotes to the appropriate cards using similar macros to those described in Vim Keyboard Macros and Split Windows. These quotes will appear in the third (right) column on the final page.
This is how a quote appeared in the file

#card @<a href="OtherApplications.html">Other Applications</a>
Other Baths
#quote Dr Kellogg says...
#qhead Hydrotherapy Departments

The most elegantly equipped establishment for the administration
of hydriatic procedures may be only the means for bungling and
unscientific dabbling with human ailments, unless conducted under
skilled medical direction and by the aid of attendants well
trained in the versatile procedures of hydrotherapy. p 402

The "#quote" marks the quote , #qhead indicates the title if it differs from the page title.

I wanted a page where people could look up quotes on particular topics, so that meant creating QuoteIndex.html... an index page.

To achieve this I used the following macros on each of the four section files:

"@q" searches for #quote and copies it, the heading and the #card (including link to the page) to quoteindex.txt (which was open in a split screen with txt file being edited, in this case Techniques.txt)

This means:
/#quote^M - search for "/#quote" <Enter>
ma - set "mark a" on the line
j$y'a - move down a line and go to the end of the line then yank (copy) everything back to "mark a"
^Wp - move back to the previous window (ie. into quoteindex.txt)
p - put (place) the two yanked lines after the previous line, in effect it appended the lines (that means we have the #quote line above the #qhead line just as it is in the Techniques file
^Wp - return to the previous window (back to Techniques.txt)
?#card^Myy - search backwards for "#card" and yank the line
^WpP - move the cursor back to the previous window (quoteindex.txt) and place insert the yanked #card line Previous to line the cursor is on (in other words it is put above the #quote line
G - move the cursor to the last line of the file
^Wp'ajj - move the cursor back to previous window (Techniques.txt) and move to "mark a" then down two lines (to get ready for the next /#quote search which was repeated until there were no more #quote found

"@w" added the directory to the link (as QuoteIndex.html will be in home directory) - change directory between
That is while editing quoteindex.txt,
/#card^M - search for next #card <Enter>
f" - find " (which puts the cursor on the double quote before the filename in the link)
a../Techniques/^[ - append into Insert mode (after the ") and insert ../Techniques/ <Esc> back to normal mode. The inserted text was changed between Sections.

For the quote we are looking at, we now have in quoteindex.txt:

#card @<a href="../Techniques/OtherApplications.html">Other Applications</a>
#quote Dr Kellogg says...
#qhead Hydrotherapy Departments

I did this for each quote in each of the Sections (Disease, Effects, Problems, Techniques separately then "read" (appended) the contents of of quoteindex.txt into QuoteIndex.html with the command:

:read quoteindex.txt

No comments:

Post a Comment