Sunday, February 14, 2010

Vim Substitution to make List Items

As you can see from the Hyperhelper code, most of my lists used "-" as the bullet.

I used "substitution" to mark these all up as List Items (ie <li> </li>).
Substitution is a command-mode command with this form:
Pretty simple and we have come across it before.
So to convert "- lists to HTML, I used:
%s+^- +</li><li>+
That is:
%s - over the whole file substitute
+ - I used "+" as a delimiter instead of the usual "/" because the slash appeared in the body of the substitution (</li>), but I could have escaped it with \/
^- + - (the "from" part of the substitution) the caret is a regular expression meaning that the search is for the pattern (in this case a dash followed by a space (- ) are the first characters on the line.
</li><li>+ - the "to". This configuration (the closing tag preceding the opening tag) is effective in closing previous list-item line and I can then simply substitute the initial closing tag with the <ul> tag to start a list. Otherwise it is all good.

If you look carefully at the Hyperhelper code, you will notice that I nested list with spaces. Subpoints were indented a space or two). To use this to effectively nest my HTML lists, I needed to simply add a space to the "from" and "to" fields of the substitution and repeat it.

The interesting thing about substitution is that it shows the number of substitutions it makes. I recorded these so I can tell you that there are a grand total of 8,034 bulleted list items in the whole of Traditional Hydrotherapy. Even though some lists are not bulleted, I would have expected more. In any case it has taken longer to write about this than it actually took to do it. Substitution is much faster than macros! The whole process was done in under an hour

So here are my substitutions followed by the number of substitutions in each of the files

%s+^- +</li><li>+
Problems: 1726 Diseases:2098 Effects:314 Techniques:186 Total: 4324
%s+^ - + </li><li>+
Problems: 1259 Diseases:1096 Effects:158 Techniques:348 Total: 2861
%s+^  - +  </li><li>+
Problems: 213 Diseases:219 Effects:32 Techniques:49 Total: 513
%s+^   - +   </li><li>+
Problems: 91 Diseases:154 Effects:8 Techniques:30 Total: 283
%s+^    - +    </li><li>+
Problems: 12 Diseases:30 Effects:0 Techniques:11 Total: 53,
Grand Total: 8,034

No comments:

Post a Comment