Skip to content

Commit 05f7310

Browse files
committed
API pages are not top level
1 parent ec15557 commit 05f7310

File tree

17 files changed

+134
-68
lines changed

17 files changed

+134
-68
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package docs.tests
2+
3+
class Adoc:
4+
def foo = 123
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package resources.tests
2+
3+
class Adoc:
4+
def foo = 123

scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,18 @@ class HtmlRenderer(rootPackage: Member, val members: Map[DRI, Member])(using ctx
5858

5959
val hiddenPages: Seq[Page] =
6060
staticSite match
61-
case None =>
62-
Seq(navigablePage.copy( // Add index page that is a copy of api/index.html
63-
link = navigablePage.link.copy(dri = docsRootDRI),
64-
children = Nil
65-
))
61+
case None => Nil
6662
case Some(siteContext) =>
67-
(siteContext.orphanedTemplates :+ siteContext.indexTemplate()).map(templateToPage(_, siteContext))
63+
val actualIndexTemplate = siteContext.indexTemplates() match
64+
case Nil if effectiveMembers.isEmpty => Seq(siteContext.emptyIndexTemplate)
65+
case templates => templates
66+
67+
(siteContext.orphanedTemplates ++ actualIndexTemplate).map(templateToPage(_, siteContext))
6868

6969
/**
7070
* Here we have to retrive index pages from hidden pages and replace fake index pages in navigable page tree.
7171
*/
72-
private def getAllPages: Seq[Page] =
73-
72+
val allPages: Seq[Page] =
7473
def traversePages(page: Page): (Page, Seq[Page]) =
7574
val (newChildren, newPagesToRemove): (Seq[Page], Seq[Page]) = page.children.map(traversePages(_)).foldLeft((Seq[Page](), Seq[Page]())) {
7675
case ((pAcc, ptrAcc), (p, ptr)) => (pAcc :+ p, ptrAcc ++ ptr)
@@ -83,9 +82,22 @@ class HtmlRenderer(rootPackage: Member, val members: Map[DRI, Member])(using ctx
8382

8483
val (newNavigablePage, pagesToRemove) = traversePages(navigablePage)
8584

86-
newNavigablePage +: hiddenPages.filterNot(pagesToRemove.contains)
85+
val all = newNavigablePage +: hiddenPages.filterNot(pagesToRemove.contains)
86+
// We need to check for conflicts only if we have top-level member called blog or docs
87+
val hasPotentialConflict =
88+
rootPackage.members.exists(m => m.name.startsWith("docs") || m.name.startsWith("blog"))
89+
90+
if hasPotentialConflict then
91+
def walk(page: Page): Unit =
92+
if page.link.dri.isStaticFile then
93+
val dest = absolutePath(page.link.dri)
94+
if apiPaths.contains(dest) then
95+
report.error(s"Conflict between static page and API member for $dest")
96+
page.children.foreach(walk)
97+
98+
all.foreach (walk)
8799

88-
val allPages = getAllPages
100+
all
89101

90102
def renderContent(page: Page) = page.content match
91103
case m: Member =>

scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ val UnresolvedLocationLink = "#"
1919
trait Locations(using ctx: DocContext):
2020
def effectiveMembers: Map[DRI, Member]
2121

22+
// We generate this collection only if there may be a conflict with resources.
23+
// Potentially can be quite big.
24+
lazy val apiPaths = effectiveMembers.keySet.filterNot(_.isStaticFile).map(absolutePath)
25+
2226
var cache = new JHashMap[DRI, Seq[String]]()
2327

2428
// TODO verify if location exisits
@@ -27,17 +31,16 @@ trait Locations(using ctx: DocContext):
2731
case null =>
2832
val path = dri match
2933
case `docsRootDRI` => List("docs", "index")
30-
case `apiPageDRI` => List("api", "index")
34+
case `apiPageDRI` =>
35+
if ctx.staticSiteContext.fold(false)(_.hasIndexFile) then List("api", "index") else List("index")
3136
case dri if dri.isStaticFile =>
3237
Paths.get(dri.location).iterator.asScala.map(_.toString).toList
3338
case dri =>
3439
val loc = dri.location
35-
val fqn = loc.split(Array('.')).toList match
40+
loc.split(Array('.')).toList match
3641
case "<empty>" :: Nil => "_empty_" :: Nil
3742
case "<empty>" :: tail => "_empty_" :: tail
3843
case other => other
39-
40-
Seq("api") ++ fqn
4144
cache.put(dri, path)
4245
path
4346
case cached => cached

scaladoc/src/dotty/tools/scaladoc/renderers/Resources.scala

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,23 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
171171
)
172172

173173
def renderResource(resource: Resource): Seq[String] =
174-
resource match
175-
case Resource.Text(path, content) =>
176-
Seq(write(path, content))
177-
case Resource.Classpath(path, name) =>
178-
getClass.getClassLoader.getResourceAsStream(name) match
179-
case null =>
180-
report.error(s"Unable to find $name on classpath")
181-
Nil
182-
case is =>
183-
try Seq(copy(is, path)) finally is.close()
184-
case Resource.File(path, file) =>
185-
Seq(copy(file, path))
186-
case Resource.URL(url) =>
187-
Nil
188-
case Resource.URLToCopy(url, dest) =>
189-
Seq(copy(new URL(url).openStream(), dest))
174+
if resource.path.endsWith(".html") && apiPaths.contains(resource.path) then
175+
report.error(s"Conflict between resource and API member for ${resource.path}")
176+
Nil
177+
else
178+
resource match
179+
case Resource.Text(path, content) =>
180+
Seq(write(path, content))
181+
case Resource.Classpath(path, name) =>
182+
getClass.getClassLoader.getResourceAsStream(name) match
183+
case null =>
184+
report.error(s"Unable to find $name on classpath")
185+
Nil
186+
case is =>
187+
try Seq(copy(is, path)) finally is.close()
188+
case Resource.File(path, file) =>
189+
Seq(copy(file, path))
190+
case Resource.URL(url) =>
191+
Nil
192+
case Resource.URLToCopy(url, dest) =>
193+
Seq(copy(new URL(url).openStream(), dest))

scaladoc/src/dotty/tools/scaladoc/site/StaticSiteContext.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ class StaticSiteContext(
1919

2020
var memberLinkResolver: String => Option[DRI] = _ => None
2121

22-
def indexTemplate(): LoadedTemplate =
22+
private def indexFiles =
2323
val files = List(new File(root, "index.html"), new File(root, "index.md")).filter { _.exists() }
2424

2525
if files.size > 1 then
2626
val msg = s"ERROR: Multiple root index pages found: ${files.map(_.getAbsolutePath)}"
2727
report.error(msg)
28+
files
2829

29-
files.flatMap(loadTemplate(_, isBlog = false)).headOption.getOrElse {
30-
val fakeFile = new File(root, "index.html")
31-
LoadedTemplate(emptyTemplate(fakeFile, "index"), List.empty, fakeFile)
32-
}
30+
def hasIndexFile = indexFiles.nonEmpty
31+
32+
def emptyIndexTemplate =
33+
val fakeFile = new File(root, "index.html")
34+
LoadedTemplate(emptyTemplate(fakeFile, "index"), List.empty, fakeFile)
35+
36+
def indexTemplates(): Seq[LoadedTemplate] = indexFiles.flatMap(loadTemplate(_, isBlog = false))
3337

3438
lazy val layouts: Map[String, TemplateFile] =
3539
val layoutRoot = new File(root, "_layouts")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Trying to override a api page!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html><body>I am causing conflicts!</body></html>

scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class BaseHtmlTest:
4141

4242
finally IO.delete(dest.toFile)
4343

44-
val testDocPath = Paths.get(BuildInfo.testDocumentationRoot)
45-
4644
class DocumentContext(d: Document, path: Path):
4745
import collection.JavaConverters._
4846

scaladoc/test/dotty/tools/scaladoc/ExternalLocationProviderIntegrationTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ abstract class ExternalLocationProviderIntegrationTest(
6060
)
6161

6262
override def runTest = afterRendering {
63-
val output = summon[DocContext].args.output.toPath.resolve("api")
63+
val output = summon[DocContext].args.output.toPath
6464
val linksBuilder = List.newBuilder[String]
6565

6666
def processFile(path: Path): Unit =
@@ -72,7 +72,6 @@ abstract class ExternalLocationProviderIntegrationTest(
7272
linksBuilder ++= hrefValues
7373
}
7474

75-
println(output)
7675
IO.foreachFileIn(output, processFile)
7776
val links = linksBuilder.result
7877
val errors = expectedLinks.flatMap(expect => Option.when(!links.contains(expect))(expect))

0 commit comments

Comments
 (0)