如何将类从JDK链接到scaladoc生成的文档中?

2022-09-02 04:42:15

我正在尝试将类从JDK链接到scaladoc生成的文档中。我使用了 scaladoc 2.10.1 的选项,但没有成功。-doc-external-doc

我正在使用 ,但我得到的链接,例如 而不是 .似乎此选项仅适用于 scaladoc 生成的文档。-doc-external-doc:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar#http://docs.oracle.com/javase/7/docs/api/index.html#java.io.Fileindex.html?java/io/File.html

我是否错过了 scaladoc 中的某个选项,或者我应该填写功能请求?

我已按如下方式配置了 sbt:

 scalacOptions in (Compile,doc) += "-doc-external-doc:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar#http://docs.oracle.com/javase/7/docs/api"

注意:我在即将推出的 sbt 0.13 中看到了 util。我认为一个很好的补充(不确定是否可能)是传递一个而不是一个.该实用程序将确定哪个文件对应于 .Opts.doc.externalAPIModuleIDFileModuleID


答案 1

我使用sbt 0.13.5。

没有开箱即用的方法可以在scaladoc中具有Javadoc链接的功能。正如我的理解所说,这不是sbt的错,而是scaladoc的工作方式。正如Josh在他的评论中指出的那样,你应该向scaladoc报告。

然而,我想出了一个解决方法 - 对生成的scaladoc进行后处理,以便替换Java URL以形成正确的Javadoc链接。doc

文件 scaladoc.sbt 应该放在 sbt 项目中,每当任务执行时,通过任务进行的后处理就会启动。docfixJavaLinksTask

注意有很多硬编码的路径,所以要谨慎使用它(又名做任何你认为合适的抛光)。

import scala.util.matching.Regex.Match

autoAPIMappings := true

// builds -doc-external-doc
apiMappings += (
    file("/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar") -> 
    url("http://docs.oracle.com/javase/8/docs/api")
)

lazy val fixJavaLinksTask = taskKey[Unit](
    "Fix Java links - replace #java.io.File with ?java/io/File.html"
)

fixJavaLinksTask := {
  println("Fixing Java links")
  val t = (target in (Compile, doc)).value
  (t ** "*.html").get.filter(hasJavadocApiLink).foreach { f => 
    println("fixing " + f)
    val newContent = javadocApiLink.replaceAllIn(IO.read(f), fixJavaLinks)
    IO.write(f, newContent)
  }
}

val fixJavaLinks: Match => String = m =>
    m.group(1) + "?" + m.group(2).replace(".", "/") + ".html"

val javadocApiLink = """\"(http://docs\.oracle\.com/javase/8/docs/api/index\.html)#([^"]*)\"""".r

def hasJavadocApiLink(f: File): Boolean = (javadocApiLink findFirstIn IO.read(f)).nonEmpty

fixJavaLinksTask <<= fixJavaLinksTask triggeredBy (doc in Compile)

答案 2

我通过@jacek-laskowski来获取答案并对其进行了修改,以便它避免硬编码字符串,并且可以用于任意数量的Java库,而不仅仅是标准库。

编辑:现在使用从运行时确定的位置,不必硬编码。rt.jarsun.boot.class.path

您唯一需要修改的是地图,我在下面调用了它:externalJavadocMap

import scala.util.matching.Regex
import scala.util.matching.Regex.Match

val externalJavadocMap = Map(
  "owlapi" -> "http://owlcs.github.io/owlapi/apidocs_4_0_2/index.html"
)

/*
 * The rt.jar file is located in the path stored in the sun.boot.class.path system property.
 * See the Oracle documentation at http://docs.oracle.com/javase/6/docs/technotes/tools/findingclasses.html.
 */
val rtJar: String = System.getProperty("sun.boot.class.path").split(java.io.File.pathSeparator).collectFirst {
  case str: String if str.endsWith(java.io.File.separator + "rt.jar") => str
}.get // fail hard if not found

val javaApiUrl: String = "http://docs.oracle.com/javase/8/docs/api/index.html"

val allExternalJavadocLinks: Seq[String] = javaApiUrl +: externalJavadocMap.values.toSeq

def javadocLinkRegex(javadocURL: String): Regex = ("""\"(\Q""" + javadocURL + """\E)#([^"]*)\"""").r

def hasJavadocLink(f: File): Boolean = allExternalJavadocLinks exists {
  javadocURL: String => 
    (javadocLinkRegex(javadocURL) findFirstIn IO.read(f)).nonEmpty
}

val fixJavaLinks: Match => String = m =>
  m.group(1) + "?" + m.group(2).replace(".", "/") + ".html"

/* You can print the classpath with `show compile:fullClasspath` in the SBT REPL.
 * From that list you can find the name of the jar for the managed dependency.
 */
lazy val documentationSettings = Seq(
  apiMappings ++= {
    // Lookup the path to jar from the classpath
    val classpath = (fullClasspath in Compile).value
    def findJar(nameBeginsWith: String): File = {
      classpath.find { attributed: Attributed[File] => (attributed.data ** s"$nameBeginsWith*.jar").get.nonEmpty }.get.data // fail hard if not found
    }
    // Define external documentation paths
    (externalJavadocMap map {
      case (name, javadocURL) => findJar(name) -> url(javadocURL)
    }) + (file(rtJar) -> url(javaApiUrl))
  },
  // Override the task to fix the links to JavaDoc
  doc in Compile <<= (doc in Compile) map {
    target: File =>
      (target ** "*.html").get.filter(hasJavadocLink).foreach { f => 
        //println(s"Fixing $f.")
        val newContent: String = allExternalJavadocLinks.foldLeft(IO.read(f)) {
          case (oldContent: String, javadocURL: String) =>
            javadocLinkRegex(javadocURL).replaceAllIn(oldContent, fixJavaLinks)
        }
        IO.write(f, newContent)
      }
      target
  }
)

我使用的是 SBT 0.13.8。


推荐