Sunday, February 14, 2010

HTML Links with Vim Keyboard Macros and Registers

Inserting links turned out to be the most laborious part of the transition, it took about two months of my spare time.

You will notice the first three lines of the card above:
#card @<a href="VisceralCongestion.html">Visceral Congestion</a>
Visceral Congestion
VisceralCongestion.html

The first line has the link to what will become the "Visceral Congestion" page, the second line will be the page's title and the third line is the page's file name.

To achieve this and to search through the rest of the text files and replace the Hyperhelper link with new HTML links I used the "@c" macro after:
  • combining the other three, non-working txt files into a single file entitled htech.txt
  • searching for the next card with "/#card" so the cursor was on the line
  • visually checking what sort of card title it was, there were six different sorts of titles.
Titles with:
  1. just spaces
  2. underscores
  3. simple one word title, probably no doubles
  4. simple one-word titles but probably with doubles (eg there is a card called "Congestion" which would affect the card "Visceral Congestion" on a simple "find and replace")
  5. underscores and spaces
  6. dashes
Visceral_Congestion was the second type, each type had its own macro. "@a" - to change all occurrences of "Visceral_Congestion" to a link to the "Visceral Congestion" page.
f@l"oyW:put^M:s/_/ /g^M0"ty$:put^M:s/ //g^MA.html^[0"fy$I<a href="~@@7">^Rt</a>^[0"ly$kma:%s/^Ro/\=@l/^M'ao^Rf^[j0f"a../Problems/^[0"ld$:sp htech.txt^M:%s/^Ro/\=@l/^M:wq^M:w^M/#card ^M

This means:
f@ - "find @", the cursor moves to the @
l - the cursor moves right one character (so it ends up on the start of the title in this case the "V")
"oyW - yank (copy) to register "o" (o for "old") the Word (that is, to the next space. In effect, all Visceral_Congestion)
:put - open a line below and insert the contents of the "yank"
^M - <Enter>
:s/_/ /g - substitute a space for every underscore on this line (becomes Visceral Congestion)
0 - move cursor to first character on the line
"ty$ - yank to register "t" (t for "title") from the cursor to the last character on the line
:put - open a line below and insert the contents of the "yank"
:s/ //g - substitute "no character" for every space on this line (in effect: delete the spaces to become VisceralCongestion)
A.html - start insert mode after the last character of the line and insert .html
^[ - escape back to normal mode
0"fy$ - move to first character of the line and yank to end of the line to register "f" (f for filename)
I<a href=" - start Insert mode before the first character of the line and insert "<a href="" (to start the link)
~@@7"> - <End> and insert ">
^Rt</a>^[ - <Ctrl>-R to insert "register t" (Visceral Congestion) followed by </a> and <Escape> to normal mode
0"ly$ - move cursor to beginning of the line and yank to "register l" (l for link) everything to end of the line
kma - move cursor up one line and set mark "a" (this is the filename line VisceralCongestion.html
:%s/^Ro/\=@l/^M - over the whole file substitute <Ctrl>-R o (that is the contents of "register o", in this case Visceral_Congestion) with the contents of @l ie "register l", (in this case <a href="VisceralCongestion.html">Visceral Congestion</a>) followed by <Enter>
'a - move the cursor to "mark a" (ie the filename line) now that the substitution is complete
o^Rf - open a line below in Insert mode and <Ctrl>-R f to insert the contents of "register f" (VisceralCongestion.html)
^[j0 - <Esc> to normal mode, move cursor down a line and to the beginning of the line (this is the line containing the link)
f" - "find" (move the cursor to) the first occurance of " on the line (the cursor is at the beginning of the filename in the link)
a../Problems/ - "append" (start insert mode after the cursor) and insert "../Problems/ (thus turning the link into <a href="../Problems/VisceralCongestion.html">Visceral Congestion</a> "../" points to a fellow subdirectory)
^[0"ld$ - escape to normal mode, move to beginning of the line, delete to end of the line to "l (register l) This deletes the contents of the line from beginning to end but the empty line remains there as you can see in file.
:sp htech.txt^M - split the screen and open htech.txt in the new window, with the cursor in the window - (this is the file containing, in this case, Diseases, Effects and Techniques - all of which will be put in other subdirectories and need links that point to the ../Problems directory) then <Enter>
:%s/^Ro/\=@l/^M - over the whole file substitute <Ctrl>-R o (the contents of "register o" or in this case Visceral_Congestion) with @l (the contents of register l, ie <a href="../Problems/VisceralCongestion.html">Visceral Congestion</a>) and <Enter>
:wq^M - write (save) and quit htech.txt and <Enter> (this leaves the cursor back in Problems.txt)
:w^M - write Problems.txt
/#card ^M - find the next occurrence of of "#card " (ready for the next card macro to be run)

This is a powerful and quick macro (5 seconds per card). It not only creates the title and filename (URL) of the new webpage and puts them in a place where I can call them later, it changes all the links through the whole of "Traditional Hydrotherapy" so they will point to the new URL.

No comments:

Post a Comment