Chapter 5
Html Files

   5.1 Open Files
   5.2 Close Files
   5.3 Cross References
   5.4 Root File
   5.5 Document Head
   5.6 Document Tail

\HP:file was before \:tempa, and it got screwed when \PauseHFont and \EndPauseHFont have been defined and introdued to \Configure{HtmlPar}. The name is probably misused at some of its occurences.

<..files..>
 <.elements for file.>
 \ifHtml <.html file commands.>  \fi
 -_-_-

5.1 Open Files

The first optional parameter of \FileStream goes to the title of the html file. For a second parabeter, one should be careful of not providing an undefined \csname...\endcsname file name. An empty second parameter refers to the root file.

<..elements for file..>
 \ifx \FileStream\:UnDef \else
    \errmessage{--- TeX4ht error ---
        \noexpand\FileStream is defined outside of TeX4ht}\endinput
 \fi
 \def\FileStream{\IgnorePar
    \let<.needs preamble.>=-\futurelet\:temp\F:le}
 \def\F:le{\if +\:temp \let<.needs preamble.>=+\expandafter\Fl:e
    \else \expand:after{\Fl:e-}\fi}
 \def\Fl:e#1{\futurelet\:temp\::File}
 \def\::File{\ifx [\:temp \expandafter\:File
             \else  \expand:after{\:File[]}\fi }
 \def\:File[#1]#2{}
 -_-_-

<..html file commands..>
 \def\:File[#1]#2{%
    \no:pic\edef\file:name{#2}\yes:pic
    \expandafter\ifx\csname
           <.csname: file name -> file id.>\file:name\endcsname\relax
                 <.open html file name.>%
    \else        <.reopen html file.>%
    \fi}
 \HAssign\bs:fileid = 1
 -_-_-

The definition \edef\file:name{#2} is needed just in case the parameter #2 depends on \file:id, because the latter is being changed within the course of the code.

<..reopen html file..>
 \xdef\file:id{\csname
             <.csname: file name -> file id.>\file:name\endcsname}%
 \ht:special{t4ht>\ifnum \file:id>\bs:fileid\space \file:name\fi }%
 -_-_-

<..open html file name..>
 \ht:special{t4ht>\file:name}%
 \immediate\write-1{--- file \file:name\space ---}%
 <.round ids before file.>%
 \gHAdvance\maxfile:id by 1
 \global\let\file:id=\maxfile:id
 \Tag{<.auto file tag.>\file:id
    <.Tag/Ref: file-id -> file-name.>}{\file:name}%
 \expandafter\xdef\csname
    <.csname: file name -> file id.>\file:name\endcsname{\file:id}%
 <.HPage TITLE.>%
 <.remove par skip space.>%
 \a:PROLOG \a:HTML \a:HEAD
 \ifx +<.needs preamble.>%
    <.initiate frames.>%
    \xdef:cs{<.live.>\file:id}{\noexpand\a:TITLE
       <.noexpanded configured title.>%
       \noexpand\b:TITLE\noexpand\a:@HEAD\noexpand\b:HEAD}%
 \else
    \xdef:cs{<.live.>\file:id}{}%
       \a:TITLE<.configured title.>\b:TITLE \a:@HEAD\b:HEAD\B:DY
 \fi
 -_-_-

<..configured title..>
 \bgroup \:TITLE: \c:TITLE \egroup
 -_-_-

<..noexpanded configured title..>
 \bgroup \noexpand\:TITLE: \noexpand\c:TITLE \egroup
 -_-_-

<..remove par skip space..>
 \ht:special{t4ht@[}{\ht:everypar{}\leavevmode}\ht:special{t4ht@]}%
 -_-_-

Titles need to be evaluated immediately to capture meanings that come from sectioning commands, where the latter may include macro names taht can change in time.

The following should appear before \Preamble.

<..first pass..>+
 \NewConfigure{HTML}[2]{\c:def\a:HTML{#1}\c:def\b:HTML{#2}}
 \NewConfigure{HEAD}[2]{\c:def\a:HEAD{#1}\c:def\b:HEAD{#2}}
 \NewConfigure{BODY}[2]{\c:def\a:BODY{#1}%
    \c:def\b:BODY{\csname a:@/BODY\endcsname#2}}
 \NewConfigure{TITLE}[2]{\c:def\a:TITLE{#1}\c:def\b:TITLE{#2}}
 \NewConfigure{TITLE+}[1]{\c:def\c:TITLE{#1}}
 \NewConfigure{@TITLE}[1]{\concat:config\:TITLE:{#1}}
 \NewConfigure{@HEAD}[1]{\concat:config\a:@HEAD{#1}}
 \let\a:@HEAD=\relax
 -_-_-

<..concat config..>
 \long\def\concat:config#1#2{\def\:temp{#2}%
    \ifx \:temp\empty  \let#1=\empty
    \else \ifx #1\relax  \let#1=\empty\fi
        \append:def#1{#2}\fi
 }
 -_-_-

\Configure{@HEAD}{} empties the content. \Configure{@HEAD}{...} appends the content.

Same for the following

<..first pass..>+
 \NewConfigure{@BODY}[1]{\concat:config\a:@BODY{#1}}
 \let\a:@BODY\relax
 \NewConfigure{@/BODY}[1]{\expandafter
    \concat:config\csname a:@/BODY\endcsname{#1}}
 \expandafter\let\csname a:@/BODY\endcsname\relax
 -_-_-

Why the \noexpand above. Put a \special instead.

<..utilities..>+
 \:CheckOption{hshow} \if:Option
    \def\hshow#1{\immediate\write16
      {..#1.......\expandafter\meaning\csname #1\endcsname}}
    <.Show Configure.>
 \fi
 -_-_-

<..Show Configure..>
 \def\ShowConfigure#1{%
    \edef\:tempb{\expandafter \meaning\csname c:#1:\endcsname}%
    \expandafter\par:N  \:tempb////%
    \immediate\write16{\string\Configure{#1}}%
    {\show:Arg{1}{a:#1}%
    \show:Arg{2}{b:#1}%
    \show:Arg{3}{c:#1}%
    \show:Arg{4}{d:#1}%
    \show:Arg{5}{e:#1}%
    \show:Arg{6}{f:#1}%
    \show:Arg{7}{g:#1}%
    \show:Arg{8}{h:#1}%
    \show:Arg{9}{i:#1}}%
 }
 \edef\:temp{\def\noexpand
   \par:N##1\string :##2->##3////{\noexpand\par:NN?##2//}}
 \:temp
 \def\par:NN#1#2//{\def\:tempb{#2}\ifx\:tempb\empty
   \def\:tempb{#1}\else \def\:tempb{\par:NN#2//}\expandafter\:tempb\fi}
 \def\show:Arg#1#2{%
    \ifnum \:tempb=#1 \let\show:Arg=\:gobbleII \fi
    \expandafter\let\expandafter\:temp \csname #2\endcsname
    \immediate\write16{ {\ifx \:temp\relax ???\else
       \expandafter\:gobbleM \meaning \:temp\fi}}}
 \def\:gobbleM#1->{}
 -_-_-

The following tag is for recording files that are still alive, and for keeping a status location for HEAD/FRAMESET/BODY entity types.

<..live..>
 Lv -_-_-

5.2 Close Files

<..html file commands..>+
 \def\EndFileStream#1{%
    \expandafter\ifx
             \csname <.csname: file name -> file id.>#1\endcsname\relax
       \:warning{\string\EndFileStream{#1}?}%
    \else
       \ifnum\csname <.csname: file name -> file id.>#1\endcsname=\bs:fileid
          \:warning{\string\EndFileStream{\jobname .\:html}?}%
       \else \expandafter\ifnum  <.file not overided by /ChildOf.>%
          <.close html file name.>%
    \fi\fi\fi }
 -_-_-

Note thta \EndFileStream might get fake calls due to end of HPages. Hence, \RecallEverypar should be invoked only when real HPageas are closed. Well, we can’t put \SaveEverypar and \RecallEverypar in \FileStream and \EndFileStream because they are not structured. We can reopen and close files randomly.

An extra empty line at end of file (for some reason, some settings of emacs prompt the user for such a line).

<..close html file name..>
 {\let\inputlineno=\tmp:cnt \tmp:cnt=1   \root:next }%
 <.end body and frames.>%
 \ht:special{t4ht<#1}%
 \g:delete{\empty <.csname: file name -> file id.>#1}%
 <.round ids after file.>%
 -_-_-

Why the \edef above

Upon closing a file with special{t4ht<#1}’, the program ‘’tex4ht.c’ moves to the youngest file that is still openned.

<..close html file name..>+
 \expandafter\expandafter\expandafter\global \expandafter
                      \let\csname  <.live.>\file:id\endcsname=\:UnDef
 \global\let\file:id=\maxfile:id
 \find:fileid
 -_-_-

<..html file commands..>+
 \def\find:fileid{%
   \expandafter\ifx \csname  <.live.>\file:id\endcsname \relax
       \gHAdvance\file:id by -1  \expandafter\find:fileid
   \fi
 }
 -_-_-

5.3 Cross References

The commands referencing files are defined for use also within restricted environments (\edef, \write).

<..html file commands..>+
 \def\TagFile#1{%
    \expandafter\ifx \csname
          <.tag of Tag.><.file name for TagFile.>\endcsname \relax \else
       \Tag{#1<.File tag.>}{\LikeRef{<.file name for TagFile.>}}%
    \fi
 }
 \def\RefFile#1{\LikeRef{#1<.File tag.>}}
 \def\FileName{\LikeRef
    {<.auto file tag.>\file:id <.Tag/Ref: file-id -> file-name.>}}
 -_-_-

<..file name for TagFile..>
 <.auto file tag.>\file:id <.Tag/Ref: file-id -> file-name.>%
 -_-_-

\def\RefFile#1{\Ref{#1|<File tag|>}} \def\LikeRefFile#1{\LikeRef{#1|<File tag|>}}

<..html file commands..>+
 \def\RefFileNumber#1{%
    \expandafter\ifx  \csname
          <.tag of Tag.><.auto file tag.>#1%
                   <.Tag/Ref: file-id -> file-name.>\endcsname\relax
    \else
       \:LikeRef{<.auto file tag.>#1<.Tag/Ref: file-id -> file-name.>}%
    \fi  }
 -_-_-

 
 > Why is it that during the first run, \file:name returns the correct 
 > filename, but \FileName returns a blank? 
 
\file:name doesn’t always return the correct 
 
    \documentclass{article} 
    \begin{document} 
 
    \immediate\write16 
       {\csname file:name\endcsname...\csname FileName\endcsname} 
 
    \end{document} 
 
 > Is this a bug or a feature? 
 
\FileName gets its information through the xref file from the previous 
compilation, in a similar manner that \ref gets its information from 
\name in latex. The indirection is needed to allow communication 
between different files that are to be compiled into a joint html 
output. It was introduced at a time that I and others had no access to 
large latex compilers, forcing the break up of large source (eg, 
books) into smaller files. 

5.4 Root File

<..html file commands..>+
 \xdef:cs{\empty <.csname: file name -> file id.>\jobname .\:html}{\file:id}
 \xdef:cs{\empty <.csname: file name -> file id.>}{\file:id}
 \Tag{<.auto file tag.>\file:id
                 <.Tag/Ref: file-id -> file-name.>}{\jobname .\:html}
 \bgroup
   <.protect from /special redef.>%
   \xdef:cs{<.live.>\file:id}{\noexpand\a:TITLE
      <.noexpanded configured title.>%
      \noexpand\b:TITLE \noexpand\a:@HEAD\noexpand\b:HEAD}%
 \egroup
 -_-_-

5.5 Document Head

<..start html doc..>
 \:startdoc
 -_-_-

<..at doc start..>
 \def\at:startdoc{%
     \let\:startdoc=\:UnDef    \let\at:startdoc=\:UnDef
     <.activate html env.>%
     \a:Preamble
     <.start html file.>}
 \def\:startdoc{%
     \at:startdoc
     \edef\:temp{\pageno=\the\pageno}%
     {\let\HCode=\:gobble \par\newpage}\:temp}
 -_-_-

The \let\HCode=\:gobble is to prevent <BR> from \line activated by ty’ and called upon from a uer-designed \output routines taht have not been adjusted yet for haeders and footers.

The following is to ensure that no shipout of content is done before the header is out.

<..start html doc..>+
 \let\:shipout=\shipout
 \def\shipout{\vfil\break\let\shipout=\:shipout \shipout}
 -_-_-

<..start html file..>
 \a:PROLOG \a:HTML \a:HEAD
 <.initiate frames.>%
 -_-_-

The following empty line is to avoid lines that are too long in the style file.

The following is for the root file, and it can be overriden by definitions before \EndPreamble.

<..concat config..>+
 \def\:prolog#1#2,#3<.par del.>{%
    \if ,#2,\else
       \edef\:tempc{\if*#2\fi}%
       \ifx \:tempc\empty
          \expandafter\append:def
             \csname a:#1\endcsname{\csname a:#2\endcsname}%
          \expandafter\ifx \csname c:#2:\endcsname\relax
             \NewConfigure{#2}{1}%
          \fi
       \else
          \expand:after{\pro:log{#1}}\expandafter{\:tempc}%
    \fi\fi
    \if ,#3,\else \expand:after{\:prolog{#1}#3<.par del.>}\fi
 }
 \def\pro:log#1#2{%
    \expandafter\append:def
        \csname a:#1\endcsname{\csname a:#2\endcsname}%
    \expandafter\ifx \csname c:#2:\endcsname\relax
        \NewConfigure{#2}[1]{\expandafter
             \concat:config\csname a:#2\endcsname{##1}}%
        \expandafter\let\csname a:#2\endcsname\relax
    \fi
 }
 \def\seq:config#1#2{%
    \expandafter\let\csname a:#1\endcsname=\empty
    \:prolog {#1}#2,<.par del.>}
 -_-_-

The \seq:config is for introducing a sequence of hooks, where a star ‘*’ before the hook name indicates it is to be of accumulative kind.

<..define /Preamble..>+
 \NewConfigure{PROLOG}[1]{\seq:config{PROLOG}{#1}}
 -_-_-

<..html Configure’s..>+
 \def\Goto:Configure{\long\def
    \Goto:Configure##1\:Configure{\let\Goto:Configure
               =\:UnDef}\expandafter\Goto:Configure}
 <.trace configurations.>
 <.html config util.>
 <.shared config.>
 <.font config util.>
 <.accents.>
 <.math config util.>
 -_-_-

5.6 Document Tail

<..TeX4ht hook on end..>
 \expand:after{\let\o:end=}\csname end\endcsname
 \expandafter\def\csname end\endcsname{%
    \csname export:hook\endcsname  \csname o:end\endcsname}
 -_-_-

<..latex hook on end..>
 \let\o:enddocumenthook=\@enddocumenthook
 \def\@enddocumenthook{%
    \o:enddocumenthook   \csname export:hook\endcsname  }
 -_-_-

<..hook on end..>
 \def\at:docend{<.close html doc.>\global\let\at:docend=\empty}
 \expand:after{\let\o:end=}\csname end\endcsname
 \expandafter\def\csname end\endcsname{\HtmlEnv
    \at:docend
    \csname export:hook\endcsname   \csname o:end\endcsname}
 -_-_-

<..close html doc..>+
 <.close HPages in @end.>%
 \HAssign\op:fl=\maxfile:id\space  \close:files
 <.buttons at end of child.>%
 <.end body and frames.>%
 -_-_-

<..end body and frames..>
 {\b:BODY \b:HTML}%
 -_-_-

<..html file commands..>+
 \def\close:files{%
    \ifnum \op:fl<\bs:fileid \else
       \def\:warning##1{}%
       \EndFileStream{\RefFileNumber\op:fl}%
       \Advance:\op:fl by -1
       \expandafter\close:files
    \fi}
 -_-_-