> I've commited a Master Table of Contents to the lyxdoc repository
> (TOC.lyx) along with the shell script that got me 98% of the way there.
> (As an exercize for Amir, rewrite it with 5 lines of Perl code ...). Let
> me know if you like it.
I don't really understand why you used that instead of the working but buggy
doc_toc.pl. Oh, I see. All that stuff to get the page & section numbers. You
certainly could do that in Perl (using the 'system' function to call latex).
However, do we really think that many people will be printing out all of the
LyX documentation? If not, then all you need is the section number, and we
can get that the same way (I assume) LyX GUI does, by counting sections.
So, for your reading pleasure, I'm attaching doc_toc.pl and toc.lyx.
doc_toc.pl is 107 lines. If I got rid of the comments and whitespace, I
could shrink it, though to get it to five lines they would have to be really
long lines :) I'm attaching it so we can have a csh vs. Perl write-only
competition.
If you like it, it ought to be trivial to add subsubsections. I'm not
entirely sure that's a good idea. You'd think that if we had good section
titles, it would be clear which section you need to go to. But maybe not. Of
course, once we've got foldable insets, we can have the best of both worlds.
I was shocked and horrified to see that you used WYSIWYG tools to get the
indenting happening. (My friend's resume was a LaTeX document with like 8000
~'s all over the page to get spacing exactly right, which similarly shocked
and horrified me. OTOH, he has since gotten a post doc and a job; I'm still
working on my PhD.) I'm using Descriptions instead. Admittedly, that does
seem to lead to a larger space between lines. (If negative spaces worked "as
expected" in the LyX GUI, we'd be OK, but I guess there are dangers with
supporting that, like overlapping text.)
If we ever get cross-doc references going, we can have links to all the
sections (though that requires giving them all labels...) instead of the
section numbers.
If you're *not* going to be using doc_toc.pl (hint: silly idea) I can still
probably turn toc_make into slightly more readable perl (no, really!)
-Amir
toc.lyx.gz
#!/usr/bin/perl -w
use strict;
@ARGV = map {"$_.lyx"} qw
(Intro Tutorial UserGuide Extended Customization FAQ Reference)
unless @ARGV;
my $in_sec = 0; # in top-level section (e.g. chapter for book class) or Title
my $in_sub_sec = 0; # in second-level section (e.g. section for book class)
# First print out initial info
print <<"ENDPREAMBLE";
#This file was created by The LyX Team Robot
#LyX 1.0 (C) 1995-1999 Matthias Ettrich and the LyX Team
\\lyxformat 2.15
\\textclass article
\\layout Title
LyX Doc Table of Contents
ENDPREAMBLE
my $match = "";
my $ignore = 0; # ignore lines (e.g. footnote in a heading)
my $sub_match;
my @sec_counter;
# Now loop through files
while (<>) {
# first few lines of the file
unless ($match) {
if (/^\\textclass (\w+)/) {
# Hopefully it's book, report, or article
my $class = $1;
if ($class eq "article") {
$match = "Section";
$sub_match = "Subsection";
} else {
$match = "Chapter";
$sub_match = "Section";
}
}
next;
}
# Footnotes in a section heading could confuse things!
# And don't bother printing out section labels.
if (/^\\begin_float footnote/ || /^\\begin_inset LatexCommand \\label/) {
$ignore = 1;
}
# Unset ignoring. But note that end_inset could be the end of another
# kind of inset!
if ($ignore && /^\\end_(float|inset)/) {
$ignore = 0;
next; # don't print out \end_float line
}
next if $ignore;
# Now actually handle title & section headings
if (/^\\layout Title/) {
$in_sec = 1;
$sec_counter[0] = 0; # (re)start section counter
print "\\layout Section*\n";
} elsif (/^\\layout $match/) {
if ($in_sub_sec) {
print "\\end_deeper\n";
$in_sub_sec = 0;
}
$sec_counter[0]++;
$in_sec = 1;
print "\\layout Description\n$sec_counter[0] ";
} elsif (/^\\layout $sub_match/) {
$in_sec = 1;
unless ($in_sub_sec) {
$in_sub_sec = 1;
$sec_counter[1] = 0;
print "\\begin_deeper\n";
}
$sec_counter[1]++;
print "\\layout Description\n$sec_counter[0].$sec_counter[1] ";
# Finished with a heading when we get to a new \layout command
} elsif ($in_sec) {
if (/^\\layout /) {
$in_sec = 0;
} else {
print;
}
}
# First few lines of each file, $match isn't set yet
if (eof) {
if ($in_sub_sec) { # from end of last file
print "\\end_deeper\n";
$in_sub_sec = 0;
}
$match = "";
}
}
END{
print "\n\\the_end\n";
}