Skip to content

Commit 49d3c62

Browse files
committed
Add the page to title missing error
1 parent e9719f6 commit 49d3c62

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import scala.jdk.CollectionConverters._
88
import java.util.Optional
99
import scala.beans._
1010
import java.nio.file.{Files, Paths}
11+
import scala.io.Source
1112

1213
enum Sidebar:
1314
case Category(
@@ -31,6 +32,17 @@ object Sidebar:
3132

3233
private object RawInputTypeRef extends TypeReference[RawInput]
3334

35+
private def pageWithNoTitle(content: String | java.io.File): String =
36+
val fileContent = content match {
37+
case file: java.io.File => Source.fromFile(file).getLines().mkString("\n")
38+
case str: String => str
39+
}
40+
val lines = fileContent.split("\n")
41+
lines.zipWithIndex
42+
.find { case (line, i) => line.trim.startsWith("page:") && !lines(i - 1).contains("- title:") }
43+
.map(_._1.trim.stripPrefix("page:"))
44+
.getOrElse("")
45+
3446
private def toSidebar(r: RawInput, content: String | java.io.File)(using CompilerContext): Sidebar = r match
3547
case RawInput(title, page, index, subsection, dir, hidden) if page.nonEmpty && index.isEmpty && subsection.isEmpty() =>
3648
val sidebarPath = content match
@@ -44,14 +56,15 @@ object Sidebar:
4456
case RawInput(title, page, index, subsection, dir, hidden) if page.isEmpty && (!subsection.isEmpty() || !index.isEmpty()) =>
4557
Sidebar.Category(Option.when(title.nonEmpty)(title), Option.when(index.nonEmpty)(index), subsection.asScala.map(toSidebar(_, content)).toList, Option.when(dir.nonEmpty)(dir))
4658
case RawInput(title, page, index, subsection, dir, hidden) =>
47-
if title.isEmpty() then
48-
val msg = s"Error parsing YAML configuration file: 'title' is not provided."
59+
if title.isEmpty() && index.isEmpty() then
60+
val page = pageWithNoTitle(content).trim()
61+
val msg = s"Error parsing YAML configuration file: 'title' is not provided for page '$page'."
4962
report.error(s"$msg\n$schemaMessage")
5063
else if title.nonEmpty && (page.isEmpty() || index.isEmpty()) then
5164
val msg = s"Error parsing YAML configuration file: 'index' or 'page' path is missing for title '$title'."
5265
report.error(s"$msg\n$schemaMessage")
5366
else
54-
val msg = s"The parsing seems not to have been done correctly."
67+
val msg = s"Error parsing YAML configuration file."
5568
report.warning(s"$msg\n$schemaMessage")
5669
Sidebar.Page(None, page, hidden)
5770

scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import dotty.tools.scaladoc.site.Sidebar.RawInput
88
import java.io.ByteArrayOutputStream
99
import java.io.PrintStream
1010

11-
// TODO add negaitve and more details tests
11+
// TODO add negative and more details tests
1212
class SidebarParserTest:
1313

1414
private val sidebar =
@@ -41,6 +41,7 @@ class SidebarParserTest:
4141
private val sidebarNoTitle =
4242
"""index: index.md
4343
|subsection:
44+
| - title: My title
4445
| page: my-page1.md
4546
| - page: my-page2.md
4647
| - page: my-page3/subsection
@@ -56,7 +57,7 @@ class SidebarParserTest:
5657
| subsection:
5758
| - page: my-page5/my-page5.md
5859
| - subsection:
59-
| - page: my-page7/my-page7.md
60+
| page: my-page7/my-page7.md
6061
| - index: my-page6/index.md
6162
| subsection:
6263
| - index: my-page6/my-page6/index.md
@@ -90,7 +91,7 @@ class SidebarParserTest:
9091
| - page: my-page6/my-page6/my-page6.md
9192
""".stripMargin
9293

93-
private val msgNoTitle = "Error parsing YAML configuration file: 'title' is not provided."
94+
private val msgNoTitle = "Error parsing YAML configuration file: 'title' is not provided for page 'my-page7/my-page7.md'."
9495
private val msgNoPage = "Error parsing YAML configuration file: 'index' or 'page' path is missing for title 'My title'."
9596

9697
private def schemaMessage: String = Sidebar.schemaMessage
@@ -135,4 +136,4 @@ class SidebarParserTest:
135136
}
136137
val error = out.toString().trim()
137138

138-
assert(error.contains(msgNoTitle) && error.contains(schemaMessage))
139+
assert(error.contains(msgNoTitle) && error.contains(schemaMessage))

0 commit comments

Comments
 (0)