NOTE:If you're not a help developer, then this entry isn't going to mean too much to you. Read some other entry below this one, or come back in a day or two when I have something interesting to say. :)I ran into a bit of a problem the other day, where I wanted to print out the Table of Contents from an HTML Help file (a CHM) I was working on.... and found I couldn't! There's no obvious (or even non-obvious) method I could find to print out a simple table of contents.
Update: Because I'm a big geek, I managed to find a way to make this an executable file (through the use of a utility called PriadoBlender, which converts PHP scripts to Windows executables). Unzip the contents of this ZIP file to c:\hhc2html, and run the following command:hhc2html input.hhc output.html
Simple as that.
Now, there is a flat file for the CHM format, that takes a .hhc extension. The problem is, it uses non-valid HTML markup, and places TOC entries as object parameters. Not exactly easy to load up in the old web browser and print off.
Instead of labouring through trying to reproduce the TOC by hand (it was an option), I decided to see if I could write a PHP script to do it for me. So I did.
And here it is:
<?php
/* converthhc.php - converts hhc file to HTML flat file.
darren james harkness
webmaster@staticred.net
March 23, 2004This script is governed by
a Creative Commons license. For more information,
refer to the following URL:
http://creativecommons.org/licenses/by/1.0/
*/
// get the input and output files
$infile = $_REQUEST['infile'] or die('no input file specified');
$outfile = $_REQUEST['outfile'] or die('no output file specified');// open the HHC file and place the contents in a string variable
$hhcfname = file_get_contents($infile);// convert the string variable to an array, using a newline character as the delimiter.
$hhcfile = explode ("\n",$hhcfname);
$i=0;// for each part of the array, perform these operations,
// and put the value of the array part into the $line string
// variable.foreach ($hhcfile as $line) {
// if we find a <ul> tag, we know that this is a book marker.
// likewise, if we find a </ul> we know that we can close the book.
if (strstr($line,'<ul>') || strstr($line,'</ul>')) {
// add the value to the output array, and increment the array
// since the <ul> and </ul> tags will render what we want,
// let's pipe them into the output.
$putarray[$i] = $line;
$i++;
}
// now, let's look for the actual topic title
if (strstr($line,'param name="Name"')) {
// if we find a topic, make it human readable
$line = str_replace('<param name="Name" value="','',$line);
$line = str_replace('">','<br>',$line);
// then add the value to the output array, and increment the array
$putarray[$i] = $line;
$i++;
}
}// prepare the array for writing to disk
$outcontents = implode("\n",$putarray);
// write the file to disk and close the file handle
if (!$handle = fopen($outfile, 'w')) {
die("Cannot open $output");
}
if (!fwrite($handle, $outcontents)) {
die("Cannot write to $output");
}
echo"\n$outfile written.\n";
fclose($handle);?>
Usage is as follows:
c:\php\php.exe converthhc infile=foo.hhc outfile=foo.html

>> Anders » Thursday, March 25, 2004 09:24 AM
Post a comment
* under no circumstances will your email address be traded for a sack of quarters. No-sirree.