1########################################################################
2#
3# Copyright (c) 2010, Secure Endpoints Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11#   notice, this list of conditions and the following disclaimer.
12#
13# - Redistributions in binary form must reproduce the above copyright
14#   notice, this list of conditions and the following disclaimer in
15#   the documentation and/or other materials provided with the
16#   distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32use HTML::TreeBuilder;
33
34
35my $input_file = "index.html";
36my $toc_file = "toc.hhc";
37
38for (@ARGV) {
39  ARG: {
40      /-o(.*)/ && do {
41          $toc_file = $1;
42          last ARG;
43      };
44
45      $input_file = $_;
46    }
47}
48
49print "Processing TOC in $input_file\n";
50print "Writing to $toc_file\n";
51
52open(TOC, '>', $toc_file) or die "Can't open $toc_file\n";
53
54my $tree = HTML::TreeBuilder->new();
55
56$tree->parse_file($input_file);
57
58my $contents = $tree->look_down('class', 'contents');
59my $clist = $contents->find_by_tag_name('ul');
60
61print TOC '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
62<html>
63<head>
64<meta name="GENERATOR" content="Heimdal">
65</head>
66<body>
67';
68
69process_ul_element($clist, 0);
70
71print TOC '
72</body>
73</html>
74';
75
76
77sub process_ul_element
78{
79    my $e = shift;
80    my $level = shift;
81
82    if ($e->tag() eq "ul") {
83
84        print TOC '  'x$level;
85        print TOC "<ul>\n";
86
87        my @items = $e->content_list();
88
89        for (@items) {
90            process_li_element($_, $level + 1);
91        }
92
93        print TOC '  'x$level;
94        print TOC "</ul>\n";
95    }
96}
97
98sub process_li_element
99{
100    my $e = shift;
101    my $level = shift;
102
103    if ($e->tag() eq "li") {
104        my $a = $e->find_by_tag_name('a');
105
106        my $href = $a->attr('href');
107        my @ac = $a->content_list();
108        my $title = $ac[0];
109
110        print TOC "  "x$level;
111        print TOC "<li><object type=\"text/sitemap\"><param name=\"Name\" value=\"$title\"><param name=\"Local\" value=\"$href\"></object>\n";
112
113        my @items = $e->content_list();
114
115        for (@items) {
116            process_ul_element($_, $level + 1);
117        }
118    }
119}
120
121