3

我想在<head>生成的 Javadoc HTML 中包含一个元素:

<link rel="shortcut icon" href="my-project-icon.ico" />

请注意,我正在使用 Ant 任务来生成 Javadoc。

我尝试使用<header>Ant 任务的元素,但放置在那里的任何标记最终都包含在一个<h1>标记中,这是无效的,因此被浏览器忽略。

4

3 回答 3

2

我肯定会将输出文件修改为一种简单的蛮力解决方案。但是一种复杂的方法是使用自定义 doclet。此 doclet 将是标准 doclet 的副本(在哪里可以下载当前版本(1.5 或 1.6)的标准 JavaDoc doclet 的源代码)。

里面有HtmlDocletWriter.java很多行head.addContent。您可以再添加一条这样的行,可能基于HtmlTree.LINK.

于 2012-10-28T21:08:46.217 回答
2

我们使用以下 bash/sed 脚本采用“蛮力”方法。

(请注意,javadoc 在创建的目录中创建了一些名为“*.html”的丑陋文件,这会在 sed 尝试处理它们时导致错误消息。我们还没有想出如何避免这种情况,但它似乎是对我们的目的无害!-)

当然,xslt 脚本会更专业,...

#!/bin/sh
# patch favicon into header of javadoc generated api doc html 
#
# assume started with P=`pwd` , then the args must be
# 1 directory to process / full path relative to  $P
# 2 favicon filename to insert / full path relative to $P
function patchIt () {
   for f in $1/*.html ; do  
     tmpfile=`mktemp -p . `
     sed -e " s%<HEAD>%<HEAD><link rel=\"icon\" href=\"$2\" type=\"image/png\"/>%" \
         $f > $tmpfile  ; mv $tmpfile  $f ; 
   done ; 
   for d in $1/* ; do 
     if [ -d $d ]; then echo "descending to "$d ; patchIt $d ../$2 ; fi ; 
   done
}
patchIt $1 $2 
#eof
于 2012-11-07T12:24:21.290 回答
2

Markus 的解决方案是一个好的开始。感谢您提供!

但是,它有一些问题:

  • 如果目录不包含任何 .html 文件,则会创建一个名为 .html 的文件*.html
  • 它不会将网站图标添加到由 JDK 8 版本的 javadoc 生成的 HTML 文件中,该版本<head>使用<HEAD>.
  • 如果多次运行,它会多次插入网站图标。
  • mktemp 命令不可移植(例如,它不适用于 Mac OS)。
  • 它不保留文件权限。

这是纠正这些问题的版本。扩展版本可以在html-tools存储库中找到。如果您发现问题或想提出改进建议,请在此处发表评论或使用html-tools 问题跟踪器

#!/bin/sh
# Add favicon to header of HTML files.
# One use case is for javadoc-generated API documentation.
#
# Run like this:
# add-favicon <directory> <favicon.png>
# The arguments should be paths relative to the current working directory.

# Once this has been run, running it another time has no effect.

patchIt () {
  for f in $1/*.html ; do
    if [ -f "$f" ]; then     # if no .html files exist, f is literal "*.html"
      tmpfile=`mktemp patch_favicon_XXXXX`
      # This creates tmpfile, with the same permissions as $f.
      # The next command will overwrite it but preserve the permissions.
      # Hat tip to http://superuser.com/questions/170226/standard-way-to-duplicate-a-files-permissions for this trick.
      \cp -p $f $tmpfile
      sed -e " s%<head>\$%<head><link rel=\"icon\" href=\"$2\" type=\"image/png\"/>%" $f > $tmpfile
      mv -f $tmpfile $f
    fi;
  done ;
  for d in $1/* ; do
    if [ -d $d ]; then echo "descending to "$d ; patchIt $d ../$2 ; fi ;
  done
}

patchIt $1 $2

#eof
于 2015-03-08T00:18:39.603 回答