2012-10-28 19 views
3

創建的Javadoc HTML網頁我想在我的生成的Javadoc HTML的<head>元素:使用一個圖標

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

請注意,我使用的是Ant任務生成的Javadoc。

我嘗試使用Ant任務的<header>元素,但放置在那裏的任何標記最終都在<h1>標記內,因此該標記無效,因此被瀏覽器忽略。

回答

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 
0

Markus的解決方案是一個良好的開端。感謝您提供它!

但是,它也存在一些問題:

  • 如果目錄中不包含任何.html文件,它會創建一個名爲*.html文件 。
  • 它不會添加由javadoc的JDK 8版本生成的HTML文件的favicon,該文件使用<head>而不是<HEAD>
  • 如果多次運行,它會多次插入favicon。
  • mktemp命令不可移植(例如,它在Mac OS上不起作用)。
  • 它不保留文件權限。

以下是糾正這些問題的版本。它也可以在plume-libbe found。如果您發現問題或想要提出改進建議,請在此處發表評論或使用plume-lib 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 
相關問題