2

我想为标题标签生成目录。有免费的程序吗?

4

3 回答 3

2

我刚刚写了一篇关于使用 hypertoc、sed 和 make 为 HTML 页面生成目录的博文。shell 脚本还使用广告名称插入谷歌广告。我还创建了一个导航部分,可用于导航到网站上的其他页面。

我使用的网页示例模板如下所示:

<!-- INCLUDE Navigation -->
<div id="centerDoc">
<h1>Title</h1>
<!-- Insert an ad -->
<!-- INCLUDE GoogleAd1 -->
<!-- Insert the table of contents here -->
<!--toc-->
<h2>More HTML code here</h2>

我编写了一个名为include的脚本,它读取一个名为 *.html.in 的文件并创建了一个 *.html 文件。脚本看起来像这样。我还使用 makefile 将 *.html.in 文件转换为 *.html 文件

#!/bin/sh
#This script modifies HTML pages staticly, using something similar 
# to the "#INCLUDE" C preprocessor mechanism
INCLUDE=${1?'Missing include file'}
shift
IFILE=${1?'Missing input file'}
OFILE=`echo $IFILE  | sed 's/\.in$//'`
# get the name without the path
OFILENAME=`echo $OFILE  | sed 's:.*/::'`
if [ "$IFILE" = "$OFILE" ]
then
    echo input file $IFILE same as output file $OFILE - exit
    exit
fi

ARGS="--toc_entry 'H1=1' --toc_end 'H1=/H1' --toc_entry 'H2=2' --toc_end 'H2=/H2'  --toc_entry 'H3=3' --toc_end 'H3=/H3'  --toc_entry 'H4=4' --toc_end 'H4=/H4' --toc_entry 'H5=5' --toc_end 'H5=/H5'"
# The string !--toc-- is used as a marker to insert the new Table of Contents
TOC="--toc_tag '!--toc--' --toc_tag_replace"
eval hypertoc $ARGS $TOC --make_anchors --make_toc --inline --outfile - $IFILE| \
sed "/<!-- INCLUDE [Nn]avigation/ r $INCLUDE
# Quick and dirty way to add a way to get back to the Toc from an Entry
# 1) put a marker in the beginning of the ToC
    s/<h1>Table of Contents/<h1><a name=\"TOC\">Table Of Contents/
# 2) Add a link back to the ToC from each entry
    s:\(<h[1234]>\)<a name=:\1<a href=\"$OFILENAME#TOC\" name=:g
# Include ad named 'GoogleAd1'
    /INCLUDE GoogleAd1/ {
    r Ads/GoogleAd1
    }
" >$OFILE
于 2014-04-16T21:01:14.657 回答
2

作为htmltoc@user5858 提到的脚本的替代方案,有hypertoc一个更强大的相同脚本重写,同样使用 Perl 完成。像这样安装它(例如在 Ubuntu Linux 上):

sudo cpan -f -i HTML::GenToc

像这样使用它:

hypertoc --inline --make_anchors --make_toc --overwrite file.html
于 2014-01-06T00:20:09.210 回答
1

samaxesjs TOC jQuery 插件运行良好。

于 2012-04-10T13:35:59.147 回答