Re: Automatically updated Table of Contents with Nroff

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi -

> From: "Julian Reschke" <julian.reschke@xxxxxx>
> To: "Randy Presuhn" <randy_presuhn@xxxxxxxxxxxxxx>
> Cc: "IETF Discussion Mailing List" <ietf@xxxxxxxx>
> Sent: Tuesday, December 21, 2010 8:43 AM
> Subject: Re: Automatically updated Table of Contents with Nroff
...
> So, I do understand how generate the ToC at the end, and I'll probably
> grok .so, but what is needed to extract the ToC into a separate file? Is
> there anything in nroff supporting that, or were you just referring to a
> set of homegrown tools? Also, as far as I can tell, the generated ToC
> will already be paginated, so do you post-process it again so it can be
> inserted properly?

Here's one incarnation of what I used:

Along with the other macro definitions (for six-month expiration date
generation and so on) I define a custom ".Nh" macro for section headings,
which just turns around an invokes the ms macros for section headers and
for table of contents entry generation.

.\" ****************************************************
.\" * section numbering - built on the  ms macro       *
.\" ****************************************************
.de Nh
.in 0
.NH \\$1
\\$2
.XS
\\*(SN \\$2
.XE
.in 3

Then, at the point in the document where we actually want the table
of contents to appear:

.\"
.\" Table of contents - we cheat, using the table of contents
.\" file generated on the last pass through nroff.
.\"
.sp
.ne 5
.ti 0
Table of Contents
.sp
.so toc.ms
.bp

Then, at the very end, we spit out the current table of contents:

.\"
.\" Table of contents here
.\"
.TC yes

To process all this, use the following script to invoke nroff, and peel off the most recently
generated table of contents from the end.  This means that the table of contents that shows
up in the formatted document is actually that from the previous run.  Running the script
twice resolves pagination inconsistencies.  "Bootstrapping" the very first run (empty "txt"
file) will start out with an empty table of contents, but it will be fine the next time around (for
a single page ToC, second time around for a multi-page ToC.)  This also handles the
FormFeed insertion.

#
# Files used:
#  document.ms = input
#  txt = intermediate table of contents extracted from previous nroff run
#  toc.ms = table of contents data (updated)
#  stdout = "real" output
#

#
# extract table of contents from previous run
#
awk < txt 'BEGIN{skipping=1;}
          /    Table of Contents/{skipping=0;}
          /\.\.\.\./{if (skipping) next; print; printf(".br\n");}'  |
sed '1,$s/\.\.\.//' >toc.ms

#
# now run nroff using that old table of contents
#
nroff -ms document.ms       |
awk '/FORMFEED/{DoFF=1; print; next;}
     /^$/{if (DoFF) next; else print; next;}
               {if (DoFF) {DoFF = 0; printf("\n");} print;}' |
sed '1,$s/.//g'      |
sed '1,$s/FORMFEED/        /' >txt

#
# Strip off the trailing table of contents and we're through
#
awk <txt '/   Table of Contents/{exit;} \
          {print;}'



Obviously, this is a little hacky, and there are a zillion other ways to do
it, but this works well and requires no "manual" intervention.  If I were
going to turn this into "production" code, I think I'd parameterize it
and the intermediate file names.

Randy


_______________________________________________
Ietf mailing list
Ietf@xxxxxxxx
https://www.ietf.org/mailman/listinfo/ietf


[Index of Archives]     [IETF Annoucements]     [IETF]     [IP Storage]     [Yosemite News]     [Linux SCTP]     [Linux Newbies]     [Fedora Users]