Code snippet to add TOC to HTML doc? 
Author Message
 Code snippet to add TOC to HTML doc?

Hi,

I googled the web and Usenet, but didn't find what I was looking for :
I'm tired of adding a table of contents at the beginning of my HTML
documents to include links to the different sections they contain.

I'd like to include some PHP snippet that would generate this part
automatically when the user views the documents.

I guess regexing the document to find all the anchors (<a
name="FOO"><h2>bar</h2></a>) and the section title it refers to (<a
name="foo"><h2>BAR</h2></a>) would be good enough.

Before I spend time writing my own parser, does anyone know where I
could find this?

Thx much
JD.



Sun, 31 Jul 2005 19:21:37 GMT  
 Code snippet to add TOC to HTML doc?
 http://www.hotscripts.com/PHP  maybe


Quote:
> Hi,

> I googled the web and Usenet, but didn't find what I was looking for :
> I'm tired of adding a table of contents at the beginning of my HTML
> documents to include links to the different sections they contain.

> I'd like to include some PHP snippet that would generate this part
> automatically when the user views the documents.

> I guess regexing the document to find all the anchors (<a
> name="FOO"><h2>bar</h2></a>) and the section title it refers to (<a
> name="foo"><h2>BAR</h2></a>) would be good enough.

> Before I spend time writing my own parser, does anyone know where I
> could find this?

> Thx much
> JD.



Sun, 31 Jul 2005 21:56:55 GMT  
 Code snippet to add TOC to HTML doc?

Quote:

> Hi,

> I googled the web and Usenet, but didn't find what I was looking for :
> I'm tired of adding a table of contents at the beginning of my HTML
> documents to include links to the different sections they contain.

> I'd like to include some PHP snippet that would generate this part
> automatically when the user views the documents.

> I guess regexing the document to find all the anchors (<a
> name="FOO"><h2>bar</h2></a>) and the section title it refers to (<a
> name="foo"><h2>BAR</h2></a>) would be good enough.

> Before I spend time writing my own parser, does anyone know where I
> could find this?

> Thx much
> JD.

Why not put the TOC in a separate file and include() that file on any
page you need the TOC? That way you only need to change one file
("toc.html") and your entire website is changed.

Example:
<?php include("toc.html") ?>

later...
Jeff S.



Mon, 01 Aug 2005 01:07:12 GMT  
 Code snippet to add TOC to HTML doc?


Quote:
>Why not put the TOC in a separate file and include() that file on any
>page you need the TOC? That way you only need to change one file
>("toc.html") and your entire website is changed.

Thx but that's not what I'm looking for : I need the code to parse the
current HTML document, and generate a TOC automatically with all the
anchors it found.

Thx
JD.



Mon, 01 Aug 2005 02:49:55 GMT  
 Code snippet to add TOC to HTML doc?

Quote:

> Hi,

> I googled the web and Usenet, but didn't find what I was looking for :
> I'm tired of adding a table of contents at the beginning of my HTML
> documents to include links to the different sections they contain.

> I'd like to include some PHP snippet that would generate this part
> automatically when the user views the documents.

Not PHP, but from what I gather you're publishing fairly static documents,
so a TOC generator could help. The first hit that Google came up with was
this: http://www.zzee.com/aht/

I'm sure there are plenty more out there.

Andr N?ss



Mon, 01 Aug 2005 02:52:00 GMT  
 Code snippet to add TOC to HTML doc?
On Wed, 12 Feb 2003 19:52:00 +0100, Andr N?ss

Quote:

>Not PHP, but from what I gather you're publishing fairly static documents,
>so a TOC generator could help. The first hit that Google came up with was
>this: http://www.zzee.com/aht/

Thx, but it's shareware, and static. I'd rather write a function that
I can just include in all my web pages so that the TOC is rebuilt
dynamically every time users read a page, so I can just add new
contents without bothering to update a static TOC.

Thx
JD.



Tue, 02 Aug 2005 02:18:53 GMT  
 Code snippet to add TOC to HTML doc?

Quote:

> On Wed, 12 Feb 2003 19:52:00 +0100, Andr N?ss

>>Not PHP, but from what I gather you're publishing fairly static documents,
>>so a TOC generator could help. The first hit that Google came up with was
>>this: http://www.*-*-*.com/

> Thx, but it's shareware, and static. I'd rather write a function that
> I can just include in all my web pages so that the TOC is rebuilt
> dynamically every time users read a page, so I can just add new
> contents without bothering to update a static TOC.

It's not very hard. What you need to do is scan the file for HTML header
elements (using a regular expression for example). Each time you encounter
a header you insert the anchor tag. You should also note the anchor name in
some data structure. An associative array using the anchor name as key is
ok. The anchor names can be created from the header text by t{*filter*} away
all whitespace. You also need to keep track of what level the current
header is at, and maintain a tree of elements (this can be achieved by
using the associate array).

Creating the TOC should be very simple when you have done this. Output the
TOC as a HTML list.

Andr N?ss



Tue, 02 Aug 2005 02:38:51 GMT  
 Code snippet to add TOC to HTML doc?


Quote:


>>Why not put the TOC in a separate file and include() that file on any
>>page you need the TOC? That way you only need to change one file
>>("toc.html") and your entire website is changed.

>Thx but that's not what I'm looking for : I need the code to parse the
>current HTML document, and generate a TOC automatically with all the
>anchors it found.

If your HTML documents are well-formed XML, you could use
an XML parser to scan for the right elements and generate
the table of contents that way.

You may also have to manipulate the H1, H2's etc. as well to
insert <A NAME> elements, otherwise you can't link to them
in the TOC.

With DOM it's something like

$doctree = xmldocfile($argv[1]);
$root = $doctree->root();
$children = $root->children();
foreach ($children as $child) {
  if ($child->tagname=="H1") {
    $tocitemtext = $child->get_contents();
    $tocitemlabel = $child->get_attribute("id");
    $tocitem = array("title" => $tocitemtext, "label" => $tocitemlabel);
    $toc[] = $tocitem;
  }

Quote:
}

Now you can print the elements from $toc[] to display your
table of contents.

I suppose you could also do it with SAX, but I don't have
experience with that.

Arnoud
--
Arnoud Engelfriet, Dutch patent attorney - Speaking only for myself
Patents, copyright and IPR explained for techies: http://www.iusmentis.com/



Tue, 02 Aug 2005 03:05:01 GMT  
 Code snippet to add TOC to HTML doc?
If your PHP code generated nothing but XML which was then transformed into
HTML using an XSL stylesheet, you could use an XSL template to automatically
generate a TOC.

Tony Marston



Quote:




> >>Why not put the TOC in a separate file and include() that file on any
> >>page you need the TOC? That way you only need to change one file
> >>("toc.html") and your entire website is changed.

> >Thx but that's not what I'm looking for : I need the code to parse the
> >current HTML document, and generate a TOC automatically with all the
> >anchors it found.

> If your HTML documents are well-formed XML, you could use
> an XML parser to scan for the right elements and generate
> the table of contents that way.

> You may also have to manipulate the H1, H2's etc. as well to
> insert <A NAME> elements, otherwise you can't link to them
> in the TOC.

> With DOM it's something like

> $doctree = xmldocfile($argv[1]);
> $root = $doctree->root();
> $children = $root->children();
> foreach ($children as $child) {
>   if ($child->tagname=="H1") {
>     $tocitemtext = $child->get_contents();
>     $tocitemlabel = $child->get_attribute("id");
>     $tocitem = array("title" => $tocitemtext, "label" => $tocitemlabel);
>     $toc[] = $tocitem;
>   }
> }

> Now you can print the elements from $toc[] to display your
> table of contents.

> I suppose you could also do it with SAX, but I don't have
> experience with that.

> Arnoud
> --
> Arnoud Engelfriet, Dutch patent attorney - Speaking only for myself
> Patents, copyright and IPR explained for techies:

http://www.iusmentis.com/


Tue, 02 Aug 2005 05:16:49 GMT  
 Code snippet to add TOC to HTML doc?


Quote:
> I'd like to include some PHP snippet that would generate this part
> automatically when the user views the documents.
> Before I spend time writing my own parser, does anyone know where I
> could find this?

Interesting problem.

Here's some code (save it in TOC-generator.php).

<?php

define('TOC_PLACEHOLDER','[--- TOC goes here ---]');

$TOC_ident = 0;

function build_TOC($max_level)
{
    global $TOC;
    ob_start();
    echo "<div class=\"TOC\">\n";
    echo "<h1>Table of Contents</h1>\n";
    foreach($TOC as $TOC_Entry) {
        list($level, $text, $ident) = $TOC_Entry;
        if($level <= $max_level) {
            echo "<A class='toc-entry-h$level'"
            . " HREF='#$ident'>$text</A><br>\n";
        }
    }
    echo "</div>\n";
    $TOC_text = ob_get_contents();
    ob_end_clean();
    return $TOC_text;

Quote:
}

function patch_header_for_TOC($level, $text)
{
    global $TOC;
    global $TOC_ident;

    $ident = "toc" . $TOC_ident++;
    $TOC[] = array($level, $text, $ident);
    return
    "<A NAME=\"$ident\"><H$level>$text</H$level></A>";

Quote:
}

function render_with_TOC($max_level=3)
{
    global $TOC;
    $TOC = array();

    $content = ob_get_contents();
    ob_end_clean();

    // patch headers
    $patched_content =
    preg_replace('/<[Hh]([1-6])>(.*)<\/[Hh][1-6]>/e',
            "patch_header_for_TOC('$1','$2')",
            $content);

    // build & add TOC
    $TOC_content = build_TOC($max_level);
    $content_with_TOC =
        str_replace(TOC_PLACEHOLDER,
            $TOC_content,
            $patched_content);

    echo $content_with_TOC;

Quote:
}

ob_start();
?>

And here's how it is used:

<?php require "TOC-generator.php"; ?>

<html>
    <head>
        <title>TOC Generator Test</title>
    </head>
<body>
    <?php echo TOC_PLACEHOLDER; ?>
    <hr>
    <h1>Header 1</h1>
    <h2>Header 1.2</h2>
    <h3>Header 1.3</h3>
    <p>Content ...</p>

    <h1>Header 2</h1>
    <h2>Header 2.1</h2>
    <p>Content ...</p>
</body>
</html>
<?php render_with_TOC(); ?>

--
Roberto Lupi



Tue, 02 Aug 2005 08:13:09 GMT  
 Code snippet to add TOC to HTML doc?


Quote:
>Here's some code (save it in TOC-generator.php).

Thx much for the code but... it's filed with ob_X() calls which are
not located in your code. Does it refer to some code library that was
missing?

Thx again
JD.



Sun, 07 Aug 2005 00:36:50 GMT  
 Code snippet to add TOC to HTML doc?


Quote:


> >Here's some code (save it in TOC-generator.php).

> Thx much for the code but... it's filed with ob_X() calls which are
> not located in your code. Does it refer to some code library that was
> missing?

They are Output Control functions, they've been
introduced in PHP 4.

ob_start
(PHP 4 )

ob_start -- Turn on output buffering
Description
void ob_start ( [string output_callback])

This function will turn output buffering on. While
output buffering is active no output is sent from the
script (other than headers), instead the output is
stored in an internal buffer.

ob_get_contents
(PHP 4 )

ob_get_contents --  Return the contents of the output
buffer
Description
string ob_get_contents ( void)

This will return the contents of the output buffer or
FALSE, if output buffering isn't active.

ob_end_clean
(PHP 4 )

ob_end_clean --  Clean (erase) the output buffer and
turn off output buffering
Description
void ob_end_clean ( void)

This function discards the contents of the output buffer
and turns off output buffering.

--
Roberto Lupi



Sun, 07 Aug 2005 02:08:21 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Python Snippets added to python.faqts.com

2. Doc to html saving

3. Open HTML Doc in a Default Browser

4. Output from reports to .html for .doc file format

5. How to Create Pdf/html/doc from report

6. Printing a File(.html, .doc ,etc) from LabVIEW

7. Command line HTML doc reader

8. New page - http://www.python.org/doc/NonEnglish.html

9. 2.2.1 doc to HTML help

10. Doc make html broken?

11. Command line HTML doc reader

12. extract info from pdf doc,PDF to XML, HTML

 

 
Powered by phpBB® Forum Software