Sunday, July 29, 2018

Enabling IE9 rendering of HTMLHelp (chm) files

Chm files are rendered in IE7 mode by default. This is a problem if you want to use CSS3 features such as box-shadow and rounded corners. To enable compatibility with CSS3, add

<meta http-equiv="X-UA-Compatible" content="IE=9" />

right after the tag <head>, e.g.
e.g. <html><head><meta http-equiv="X-UA-Compatible" content="IE=9" />

As a bonus, here is a simple batch file to quickly create a very basic .chm file. This batch file can be used to convert a webpage that is saved using Save Complete in your browser into a standalone chm file. If the resulting chm file does not display properly, try adding
<meta http-equiv="X-UA-Compatible" content="IE=9" />
to the source html file.

::@echo off
if "%HH%"=="" set HH=C:\Program Files\HTML Help Workshop\HHC.EXE
if not exist "%HH%" set HH=C:\Program Files (x86)\HTML Help Workshop\HHC.EXE
if "%~1"=="" goto default
if %1==/? goto help
if exist "%~1\*" goto dir
if exist %1 goto %~x1
exit

:help
echo HHC
echo     Attempt to make a chm out of current directory
echo HHC x.htm
echo    Attempt to make a chm out of x.htm
echo HHC x.hhp
echo    Compiles x.hhp
echo.
echo Uses environment variable HH to compile chm
echo Default for HH is: C:\Program Files\HTML Help Workshop\HHC.EXE

:dir
set d=%~1
set f=%d:~-6%
set d=%d:~0,-6%
if "%f%"=="_files" if exist "%d%.htm" %0 "%d%.htm"
if not exist "%~1\*.hhp" call :hhp %1
for %%v in ("%~1\*.hhp") do "%HH%" "%%v"
exit

:default
if not exist *.hhp call :hhp "%CD%"
for %%v in (*.hhp) do "%HH%" "%%v"
exit

:hhp
for %%v in (index.html index.htm main.html main.htm %~nx1.htm *.htm) do if exist "%%v" (set SRC=%%v) & goto :make
echo Error: No Html files found
exit

:make
set DEST="%tmp%\%~nx1.hhp"
>%DEST% echo [OPTIONS]
::if not exist index.hhk >>%DEST% echo Auto Index=Yes
::if not exist toc.hhc >>%DEST% echo Auto TOC=9
::>>%DEST% echo Binary TOC=Yes
>>%DEST% echo Compatibility=1.1 or later
>>%DEST% echo Compiled file="%~nx1.chm"
::>>%DEST% echo Contents file=toc.hhc
>>%DEST% echo Default topic=%SRC%
::>>%DEST% echo Default Window=Main
>>%DEST% echo Display compile progress=No
if exist index.hhk >>%DEST% echo Index file=Index.hhk
>>%DEST% echo Language=0x409 English (United States)
>>%DEST% echo Title=%~n1
::>>%DEST% echo.
::>>%DEST% echo [WINDOWS]
::if exist index.hhk >>%DEST% echo echo Main="%~n1",toc.hhc,index.hhk,"%SRC%","%SRC%",,,,,0x3520,,0x60384e,,,,,,,,0
::>>%DEST% echo Main="%~n1",toc.hhc,,"%SRC%","%SRC%",,,,,0x3520,,0x60384e,,,,,,,,0
>>%DEST% echo.
>>%DEST% echo [FILES]
>>%DEST% dir /b /s /a-d %1
::>>%DEST% echo.
::>>%DEST% echo [INFOTYPES]
move "%tmp%\%~nx1.hhp" %1
goto :eof

:.htm
:.html
set SRC=%1
set DEST="%tmp%\%~n1.hhp"
>%DEST% echo [OPTIONS]
>>%DEST% echo Compatibility=1.1 or later
>>%DEST% echo Compiled file=%~nx1.chm
>>%DEST% echo Default topic=%~1
>>%DEST% echo Display compile progress=No
>>%DEST% echo Language=0x409 English (United States)
>>%DEST% echo Title=%~n1
>>%DEST% echo.
>>%DEST% echo [FILES]
>>%DEST% echo %~1
>>%DEST% dir /b /s /a-d "%~n1%_files"
move "%tmp%\%~n1.hhp" .
"%HH%" "%~n1.hhp"
goto :eof

:.hhp
"%HH%" %1
goto :eof