If I had a blog, I’d put this there, but maybe someone will find it useful.
While working on the index for my book, I discovered some cool stuff about Word.
I created the index by going through the book and making entries in a text file that looks like this:
back end|system events,699-713
system events|back end,699-713
modX object,699
variables|$modx,699
$modx variable,699
variables|$_POST,699
$_POST variable,699
variables|$_COOKIE,699
$_COOKIE variable,699
variables|$_SESSION,699
$_SESSION variable,699
manager access system events,700-713
system events|manager action,700-713
Entries without a | character are main heads, and in the ones with |, the part after the | is a subhead. I wrote a PHP program to collate them all into a text file that looks like this:
Package Manager, 22, 52-54, 122-123
add package, 537
cURL and, 536
installing snippets, 268
Search Locally for Packages, 53, 123, 537, 610
troubleshooting, 536
packages (see: transport packages)
PackMan add-on component, 600
page count snippet tutorial, 413-414
I was importing that into Word and formatting it, but I realized that I could make it into an HTML file instead. I turns out that when you "insert" a file with an HTML extension into word, all the classes in the tags are converted into styles and you can have both paragraph and character styles. I re-wrote the code to produce HTML with all the appropriate styles and now the output looks like this:
class="IndexLetterHeading">A</p>
<hr class="IndexLetterHeadingHr" size="2" width="40%" align="left" />
<p class="indexmainhead"><span class = "indexmainheadtext">aborting transport packages</span><span class="indexmainheadnumbers">, 620</span></p>
<p class="indexmainhead"><span class = "indexmainheadtext">about MODX</span><span class="indexmainheadnumbers">, vi-xiii, 122</span></p>
<p class="indexmainhead"><span class = "indexmainheadtext">absolute URL in <span class="indexinlinecodebold">makeUrl()</span> method</span><span class="indexmainheadnumbers">, 646</span></p>
<p class="indexmainhead"><span class = "indexmainheadtext">Access Control Lists</span><span class="indexmainheadnumbers">, 441, 449-454, 449-454</span></p>
<p class=indexsubhead><span class="indexsubheadtext">context access entries, </span><span class="indexsubheadnumbers">449-451</span></p>
<p class=indexsubhead><span class="indexsubheadtext">creating entries, </span><span class="indexsubheadnumbers">449-454</span></p>
<p class=indexsubhead><span class="indexsubheadtext">element category access entries, </span><span class="indexsubheadnumbers">453-554</span></p>
<p class=indexsubhead><span class="indexsubheadtext">entries, </span><span class="indexsubheadnumbers">438</span></p>
<p class=indexsubhead><span class="indexsubheadtext">resource group access entries, </span><span class="indexsubheadnumbers">451-453, 458-459</span></p>
<p class="indexmainhead"><span class = "indexmainheadtext">Access Controls menu</span><span class="indexmainheadnumbers">, 115</span></p>
<
Which automagically appears in Word as:
[size=20pt]A[/size]
[hr]
[b]aborting transport packages,[/b] 620
[b]about MODX[/b], vi-xiii, 122
[b]absolute URL in [font=Courier]makeUrl()[/font] method[/b], 646
[b]Access Control Lists[/b], 441, 449-454, 449-454
context access entries, 449-451
creating entries, 449-454
element category access entries, 453-554
entries, 438
resource group access entries, 451-453, 458-459
[b]Access Controls menu[/b], 115
The [] tags aren’t there (the Forum editor doesn’t like me), but rather the appropriate fonts and font weights as defined by the styles I created in Word.
If you want to see how the sausage is made (and fair warning, it’s not pretty, there are some pretty gnarly preg_replace operations to mark the code font items), it’s up at GitHub:
https://github.com/BobRay/IndexIt
You can see a preview of the book’s index in final.txt (with # and ~ tokens for the stuff that should be in the code font) or you can insert final.html into Word (you won’t see the styling since you don’t have the styles defined in Word).