【问题标题】:Creating Javadoc HTML pages that use a favicon创建使用网站图标的 Javadoc HTML 页面
【发布时间】:2012-10-18 04:57:15
【问题描述】:

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

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

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

我尝试使用 Ant 任务的 &lt;header&gt; 元素,但放置在那里的任何标记最终都在 &lt;h1&gt; 标记内,这是无效的,因此被浏览器忽略。

【问题讨论】:

    标签: java ant javadoc favicon


    【解决方案1】:

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

    但是,它有一些问题:

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

    这是一个更正这些问题的版本。 expanded version 可以在 html-tools 存储库中找到。如果您发现问题或想提出改进建议,请在此处发表评论或使用html-tools issue tracker

    #!/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
    

    【讨论】:

      【解决方案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
      

      【讨论】:

        【解决方案3】:

        我肯定会将输出文件修改为一种简单的蛮力解决方案。但一种复杂的方法是使用自定义 doclet。此 doclet 将是标准 doclet (Where can you download the source for the standard JavaDoc doclet for current releases (1.5 or 1.6)) 的副本。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-14
          • 1970-01-01
          • 1970-01-01
          • 2019-01-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多