-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refactor: Improve error messaging in sidebar YAML parser #17229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
b8d5207
dd5ebcc
3ce49f8
cfa4461
e9719f6
49d3c62
5e7e205
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import com.fasterxml.jackson.core.`type`.TypeReference; | |
import scala.jdk.CollectionConverters._ | ||
import java.util.Optional | ||
import scala.beans._ | ||
import java.nio.file.{Files, Paths} | ||
import scala.io.Source | ||
|
||
enum Sidebar: | ||
case Category( | ||
|
@@ -30,16 +32,43 @@ object Sidebar: | |
|
||
private object RawInputTypeRef extends TypeReference[RawInput] | ||
|
||
private def toSidebar(r: RawInput)(using CompilerContext): Sidebar = r match | ||
private def pageWithNoTitle(content: String | java.io.File): String = | ||
val fileContent = content match { | ||
case file: java.io.File => Source.fromFile(file).getLines().mkString("\n") | ||
case str: String => str | ||
} | ||
val lines = fileContent.split("\n") | ||
lines.zipWithIndex | ||
.find { case (line, i) => line.trim.startsWith("page:") && !lines(i - 1).contains("- title:") } | ||
.map(_._1.trim.stripPrefix("page:")) | ||
.getOrElse("") | ||
Dedelweiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private def toSidebar(r: RawInput, content: String | java.io.File)(using CompilerContext): Sidebar = r match | ||
case RawInput(title, page, index, subsection, dir, hidden) if page.nonEmpty && index.isEmpty && subsection.isEmpty() => | ||
val sidebarPath = content match | ||
case s: String => Paths.get(s) | ||
case f: java.io.File => f.toPath() | ||
val basePath = sidebarPath.getParent().resolve("_docs") | ||
val pagePath = basePath.resolve(page) | ||
if !Files.exists(pagePath) then | ||
report.error(s"Page $page does not exist.") | ||
Sidebar.Page(Option.when(title.nonEmpty)(title), page, hidden) | ||
case RawInput(title, page, index, subsection, dir, hidden) if page.isEmpty && (!subsection.isEmpty() || !index.isEmpty()) => | ||
Sidebar.Category(Option.when(title.nonEmpty)(title), Option.when(index.nonEmpty)(index), subsection.asScala.map(toSidebar).toList, Option.when(dir.nonEmpty)(dir)) | ||
Sidebar.Category(Option.when(title.nonEmpty)(title), Option.when(index.nonEmpty)(index), subsection.asScala.map(toSidebar(_, content)).toList, Option.when(dir.nonEmpty)(dir)) | ||
case RawInput(title, page, index, subsection, dir, hidden) => | ||
report.error(s"Error parsing YAML configuration file.\n$schemaMessage") | ||
if title.isEmpty() && index.isEmpty() then | ||
val page = pageWithNoTitle(content).trim() | ||
julienrf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val msg = s"Error parsing YAML configuration file: 'title' is not provided for page '$page'." | ||
report.error(s"$msg\n$schemaMessage") | ||
else if title.nonEmpty && (page.isEmpty() || index.isEmpty()) then | ||
val msg = s"Error parsing YAML configuration file: 'index' or 'page' path is missing for title '$title'." | ||
report.error(s"$msg\n$schemaMessage") | ||
else | ||
val msg = s"Error parsing YAML configuration file." | ||
report.warning(s"$msg\n$schemaMessage") | ||
Sidebar.Page(None, page, hidden) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we still return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I think there's a problem with this return. Because we need to return the
This sends an error but also blocks the generation of all the documentation (which is rather extreme). |
||
|
||
private def schemaMessage: String = | ||
def schemaMessage: String = | ||
s"""Static site YAML configuration file should comply with the following description: | ||
|The root element of static site needs to be <subsection> | ||
|`title` and `directory` properties are ignored in root subsection. | ||
|
@@ -57,8 +86,7 @@ object Sidebar: | |
| hidden: <boolean> # optional - Default value is false. | ||
| | ||
|For more information visit: | ||
|https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html | ||
|""".stripMargin | ||
|https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html""".stripMargin | ||
|
||
def load(content: String | java.io.File)(using CompilerContext): Sidebar.Category = | ||
import scala.util.Try | ||
|
@@ -75,7 +103,7 @@ object Sidebar: | |
}, | ||
identity | ||
) | ||
toSidebar(root) match | ||
toSidebar(root, content) match | ||
case c: Sidebar.Category => c | ||
case _ => | ||
report.error(s"Root element is not a subsection.\n$schemaMessage") | ||
|
Uh oh!
There was an error while loading. Please reload this page.