Skip to content

Commit 2ebba6f

Browse files
committed
Fix gathering index html for sections without explicit index
1 parent 773ce31 commit 2ebba6f

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ class StaticSiteContext(
100100
val (indexes, children) = allFiles.flatMap(loadTemplate(_, isBlog)).partition(_.templateFile.isIndexPage())
101101

102102
def loadIndexPage(): TemplateFile =
103-
val indexFiles = from.listFiles { file => file.getName == "index.md" || file.getName == "index.html" }
104103
indexes match
105-
case Nil => emptyTemplate(from, from.getName)
106-
case Seq(loadedTemplate) => loadedTemplate.templateFile.copy(file = from)
104+
case Nil =>
105+
args.defaultTemplate.fold(emptyTemplate(from, from.getName))(layouts(_).copy(title = TemplateName.FilenameDefined(from.getName))).copy(file = File(from, "index.html"))
106+
case Seq(loadedTemplate) => loadedTemplate.templateFile
107107
case _ =>
108108
// TODO (https://github.com/lampepfl/scaladoc/issues/238): provide proper error handling
109109
val msg = s"ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}"
@@ -129,14 +129,14 @@ class StaticSiteContext(
129129
}
130130

131131
val processedTemplate = // Set provided name as arg in page for `docs`
132-
if from.getParentFile.toPath == docsPath && templateFile.isIndexPage() then
132+
if templateFile.file.getParentFile.toPath == docsPath && templateFile.isIndexPage() then
133133
if templateFile.title.name != "index" then
134134
report.warn("Property `title` will be overridden by project name", from)
135135

136136
templateFile.copy(title = TemplateName.FilenameDefined(args.name))
137137
else templateFile
138138

139-
Some(LoadedTemplate(processedTemplate, processedChildren.toList, from))
139+
Some(LoadedTemplate(processedTemplate, processedChildren.toList, processedTemplate.file))
140140
catch
141141
case e: RuntimeException =>
142142
// TODO (https://github.com/lampepfl/scaladoc/issues/238): provide proper error handling
@@ -150,8 +150,8 @@ class StaticSiteContext(
150150
if Files.exists(root.toPath.resolve(pagePath)) then pagePath
151151
else pagePath.stripSuffix(".html") + ".md"
152152

153-
val file = root.toPath.resolve(path).toFile
154-
val LoadedTemplate(template, children, _) = loadTemplate(file, isBlog).get // Add proper logging if file does not exisits
153+
// val file = root.toPath.resolve(path).toFile
154+
val LoadedTemplate(template, children, file) = loadTemplate(root.toPath.resolve(path).toFile, isBlog).get // Add proper logging if file does not exisits
155155
optionTitle match
156156
case Some(title) =>
157157
val newTitle = template.title match
@@ -170,10 +170,22 @@ class StaticSiteContext(
170170
val title = optionTitle match
171171
case Some(t) => t
172172
case None => "index"
173-
val fakeFile = Paths.get(root.toString, "docs", title, "index.html").toFile
173+
val sidebarContent = nested.map(loadSidebarContent)
174+
// Heuristic to add section index.html around its child pages. Otherwise we put it in directory named after title of section in top-level
175+
def longestPrefix(s1: String, s2: String): String = s1.zip(s2).takeWhile(Function.tupled(_ == _)).map(_._1).mkString
176+
val sortedNames = sidebarContent.collect {
177+
case t if t.file.getName.reverse.dropWhile(_ != '.').reverse != "index" =>
178+
t.file.toPath.toString
179+
}.sorted
180+
def indexPathTemplate(s: String) = Paths.get(root.toString, s, "index.html").toFile
181+
val fakeFile = Option.when(sortedNames.nonEmpty)(sortedNames).map { s =>
182+
indexPathTemplate(longestPrefix(s.head, s.last))
183+
}.filter(_.exists).getOrElse {
184+
indexPathTemplate(s"docs/${title.toLowerCase}")
185+
}
174186
LoadedTemplate(
175187
args.defaultTemplate.fold(emptyTemplate(fakeFile, title))(layouts(_).copy(title = TemplateName.FilenameDefined(title))),
176-
nested.map(loadSidebarContent),
188+
sidebarContent,
177189
fakeFile
178190
)
179191

0 commit comments

Comments
 (0)