Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/scaladoc/scala/tools/nsc/doc/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ class Settings(error: String => Unit, val printMsg: String => Unit = println(_))
"Prevents parsing and inclusion of comments from java sources."
)

val docCanonicalBaseUrl = StringSetting (
"-doc-canonical-base-url",
"url",
s"A base URL to use as prefix and add `canonical` URLs to all pages. The canonical URL may be used by search engines to choose the URL that you want people to see in search results. If unset no canonical URLs are generated.",
""
)

// For improved help output.
def scaladocSpecific = Set[Settings#Setting](
docformat, doctitle, docfooter, docversion, docUncompilable, docsourceurl, docgenerator, docRootContent,
Expand Down
11 changes: 9 additions & 2 deletions src/scaladoc/scala/tools/nsc/doc/html/page/Entity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ trait EntityPage extends HtmlPage {
def headers: Elems = {
def extScript(str: String) = Script(`type` = "text/javascript", src = str)
def libScript(value: String) = extScript(relativeLinkTo(List(value, "lib")))

List(HtmlTags.Link(href = relativeLinkTo(List("index.css", "lib")), media = "screen", `type` = "text/css", rel = "stylesheet"),
val canonicalSetting = universe.settings.docCanonicalBaseUrl
val canonicalLink = if (canonicalSetting.isSetByUser) {
val canonicalUrl =
if (canonicalSetting.value.endsWith("/")) canonicalSetting.value
else canonicalSetting.value + "/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this normalization to end with a "/" always desired?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List(HtmlTags.Link(href = canonicalUrl + Page.relativeLinkTo(List("."), path), rel = "canonical"))
} else Nil
canonicalLink ++ List(
HtmlTags.Link(href = relativeLinkTo(List("index.css", "lib")), media = "screen", `type` = "text/css", rel = "stylesheet"),
HtmlTags.Link(href = relativeLinkTo(List("template.css", "lib")), media = "screen", `type` = "text/css", rel = "stylesheet"),
HtmlTags.Link(href = relativeLinkTo(List("diagrams.css", "lib")), media = "screen", `type` = "text/css", rel = "stylesheet", id = "diagrams-css"),
libScript("jquery.js"),
Expand Down
15 changes: 15 additions & 0 deletions test/scaladoc/resources/canonical.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package p

class Canonical
2 changes: 2 additions & 0 deletions test/scaladoc/run/canonical-unset.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
As expected, no canonical URL found.
Done.
43 changes: 43 additions & 0 deletions test/scaladoc/run/canonical-unset.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

import scala.tools.nsc.{ScalaDocReporter, doc, io}
import scala.tools.nsc.doc.DocFactory
import scala.tools.nsc.doc.model._
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.partest.ScaladocModelTest

object Test extends ScaladocModelTest {

override def resourceFile: String = "test/scaladoc/resources/canonical.scala"

def destinationDir = "target/canonical-unset"

override def scaladocSettings =
s"-d ${destinationDir}"

override def code = ""

def testModel(rootPackage: Package): Unit = {
val dir = new java.io.File(destinationDir)
dir.mkdirs()
newDocFactory.document(List(resourceFile))
val Pattern = """<link href="([^"]*)" rel="canonical"/>""".r
val s = io.File(s"${dir.getAbsolutePath}/p/Canonical.html").slurp()
Pattern.findFirstIn(s) match {
case Some(s) =>
println(s)
case _ =>
println("As expected, no canonical URL found.")
}
}
}
2 changes: 2 additions & 0 deletions test/scaladoc/run/canonical.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<link href="https://www.scala-lang.org/files/archive/nightly/2.13.x/api/2.13.x/p/Canonical.html" rel="canonical"/>
Done.
44 changes: 44 additions & 0 deletions test/scaladoc/run/canonical.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

import scala.tools.nsc.{ScalaDocReporter, doc, io}
import scala.tools.nsc.doc.DocFactory
import scala.tools.nsc.doc.model._
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.partest.ScaladocModelTest

object Test extends ScaladocModelTest {

override def resourceFile: String = "test/scaladoc/resources/canonical.scala"

def destinationDir = "target/canonical-unset"

override def scaladocSettings =
s"-doc-canonical-base-url https://www.scala-lang.org/files/archive/nightly/2.13.x/api/2.13.x/ -d $destinationDir"

override def code = ""

def testModel(rootPackage: Package): Unit = {
val dir = new java.io.File(destinationDir)
dir.mkdirs()
newDocFactory.document(List(resourceFile))
val Pattern = """<link href="([^"]*)" rel="canonical"/>""".r
val s = io.File(s"${dir.getAbsolutePath}/p/Canonical.html").slurp()
Pattern.findFirstIn(s) match {
case Some(s) =>
println(s)
case _ =>
println("No canonical URL found.")
println(s.substring(0, Math.min(1000, s.length)))
}
}
}