From b8d520725d87c13f00a7ab7ce6eb1e96b9a25a68 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 3 Apr 2023 11:10:37 +0200 Subject: [PATCH 1/8] Improve error messaging in sidebar YAML parser In this commit: - Added a error message when the path of a page is wrong - Added an error message when an inddex or a page path in a subsection is missing - Added an error message when a title in a subsection is missing --- .../tools/scaladoc/site/SidebarParser.scala | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala index 1aefeaa21032..064e59c41024 100644 --- a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala +++ b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala @@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.`type`.TypeReference; import scala.jdk.CollectionConverters._ import java.util.Optional import scala.beans._ +import java.nio.file.{Files, Paths} enum Sidebar: case Category( @@ -30,13 +31,26 @@ object Sidebar: private object RawInputTypeRef extends TypeReference[RawInput] - private def toSidebar(r: RawInput)(using CompilerContext): Sidebar = r match + 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") + val errors = (title.isEmpty(), page.isEmpty(), index.isEmpty(), subsection.isEmpty(), dir.isEmpty()) + errors match + case (true, _, _, _, _) => + report.error(s"Title is not provided.\n$schemaMessage") + case (false, true, true, _, _) => + report.error(s"Index or page path to at least one page is missing.\n$schemaMessage") + case _ => report.error(s"Error parsing YAML configuration file.\n$schemaMessage") Sidebar.Page(None, page, hidden) private def schemaMessage: String = @@ -75,7 +89,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") From dd5ebccdcd0cb47ab3e345330f28d6f8a646c889 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 8 May 2023 09:49:33 +0200 Subject: [PATCH 2/8] Corrections: - Change the case match (Overkill) to a if else - Two types of messages : - Error: NoTitle and No index or No page. - Warning: When the parsing is not done correctly. Example: When a title got two pages. --- .../tools/scaladoc/site/SidebarParser.scala | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala index 064e59c41024..c5fb9a9be9b3 100644 --- a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala +++ b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala @@ -44,13 +44,15 @@ object Sidebar: 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(_, content)).toList, Option.when(dir.nonEmpty)(dir)) case RawInput(title, page, index, subsection, dir, hidden) => - val errors = (title.isEmpty(), page.isEmpty(), index.isEmpty(), subsection.isEmpty(), dir.isEmpty()) - errors match - case (true, _, _, _, _) => - report.error(s"Title is not provided.\n$schemaMessage") - case (false, true, true, _, _) => - report.error(s"Index or page path to at least one page is missing.\n$schemaMessage") - case _ => report.error(s"Error parsing YAML configuration file.\n$schemaMessage") + if title.isEmpty() then + val msg = "Error parsing YAML configuration file: Title is not provided." + report.error(s"$msg\n$schemaMessage") + else if title.nonEmpty && (page.isEmpty() || index.isEmpty()) then + val msg = "Error parsing YAML configuration file: Index or page path to at least one page is missing." + report.error(s"$msg\n$schemaMessage") + else + val msg = "The parsing seems not to have been done correctly." + report.warning(s"$msg\n$schemaMessage") Sidebar.Page(None, page, hidden) private def schemaMessage: String = From 3ce49f8cb47c3b3ebafe1c86ae281548c3b61e93 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 8 May 2023 15:06:11 +0200 Subject: [PATCH 3/8] Add tests --- .../scaladoc/site/SidebarParserTest.scala | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala index 72fc6515fdee..6f8ec1fdef37 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala @@ -34,6 +34,55 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin + private val sidebarErrorNoTitle = + """index: index.md + |subsection: + | page: my-page1.md + | - page: my-page2.md + | - page: my-page3/subsection + | - title: Reference + | subsection: + | - page: my-page3.md + | hidden: true + | - index: my-page4/index.md + | subsection: + | - page: my-page4/my-page4.md + | - title: My subsection + | index: my-page5/index.md + | subsection: + | - page: my-page5/my-page5.md + | - subsection: + | - page: my-page7/my-page7.md + | - index: my-page6/index.md + | subsection: + | - index: my-page6/my-page6/index.md + | subsection: + | - page: my-page6/my-page6/my-page6.md + """.stripMargin + + private val msg = "Error parsing YAML configuration file: Title is not provided." + + private def schemaMessage: String = + s"""Static site YAML configuration file should comply with the following description: + |The root element of static site needs to be + |`title` and `directory` properties are ignored in root subsection. + | + |: + | title: # optional - Default value is file name. Title can be also set using front-matter. + | index: # optional - If not provided, default empty index template is generated. + | directory: # optional - By default, directory name is title name in kebab case. + | subsection: # optional - If not provided, pages are loaded from the index directory + | - | + | # either index or subsection needs to be present + |: + | title: # optional - Default value is file name. Title can be also set using front-matter. + | page: + | hidden: # optional - Default value is false. + | + |For more information visit: + |https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html + |""".stripMargin + @Test def loadSidebar(): Unit = assertEquals( Sidebar.Category( @@ -53,3 +102,16 @@ class SidebarParserTest: ), Sidebar.load(sidebar)(using testContext) ) + + @Test(expected = classOf[IllegalArgumentException]) + def loadSidebarError(): Unit = + assertEquals( + Sidebar.Category( + None, + None, + List(), + None + ), + Sidebar.load(sidebarErrorNoTitle)(using testContext) + ) + throw new IllegalArgumentException(s"$msg\n$schemaMessage") \ No newline at end of file From cfa44615ff15f4dc46fbdea1efc6a61c1c072449 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 9 May 2023 16:27:31 +0200 Subject: [PATCH 4/8] Add a first test: - Error when a title but no pages --- .../tools/scaladoc/site/SidebarParser.scala | 5 +- .../scaladoc/site/SidebarParserTest.scala | 54 +++++++------------ 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala index c5fb9a9be9b3..189112ad2b80 100644 --- a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala +++ b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala @@ -55,7 +55,7 @@ object Sidebar: report.warning(s"$msg\n$schemaMessage") Sidebar.Page(None, page, hidden) - 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 |`title` and `directory` properties are ignored in root subsection. @@ -73,8 +73,7 @@ object Sidebar: | hidden: # 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 diff --git a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala index 6f8ec1fdef37..71bc3660bb0d 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala @@ -3,6 +3,10 @@ package site import org.junit.Test import org.junit.Assert._ +import dotty.tools.scaladoc.site.Sidebar +import dotty.tools.scaladoc.site.Sidebar.RawInput +import java.io.ByteArrayOutputStream +import java.io.PrintStream // TODO add negaitve and more details tests class SidebarParserTest: @@ -34,10 +38,10 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin - private val sidebarErrorNoTitle = - """index: index.md + private val sidebarErrorNoPage = + """index: index.md |subsection: - | page: my-page1.md + | - title: My title | - page: my-page2.md | - page: my-page3/subsection | - title: Reference @@ -60,28 +64,12 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin - private val msg = "Error parsing YAML configuration file: Title is not provided." + private val msgNoTitle = "Error parsing YAML configuration file: Title is not provided." + private val msgNoPage = "Error parsing YAML configuration file: Index or page path to at least one page is missing." - private def schemaMessage: String = - s"""Static site YAML configuration file should comply with the following description: - |The root element of static site needs to be - |`title` and `directory` properties are ignored in root subsection. - | - |: - | title: # optional - Default value is file name. Title can be also set using front-matter. - | index: # optional - If not provided, default empty index template is generated. - | directory: # optional - By default, directory name is title name in kebab case. - | subsection: # optional - If not provided, pages are loaded from the index directory - | - | - | # either index or subsection needs to be present - |: - | title: # optional - Default value is file name. Title can be also set using front-matter. - | page: - | hidden: # optional - Default value is false. - | - |For more information visit: - |https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html - |""".stripMargin + private def schemaMessage: String = Sidebar.schemaMessage + + private val noPageExpectedError = s"$msgNoPage\n$schemaMessage\nPage my-page2.md does not exist.\nPage my-page3/subsection does not exist.\nPage my-page3.md does not exist.\nPage my-page4/my-page4.md does not exist.\nPage my-page5/my-page5.md does not exist.\nPage my-page7/my-page7.md does not exist.\nPage my-page6/my-page6/my-page6.md does not exist." @Test def loadSidebar(): Unit = assertEquals( @@ -103,15 +91,11 @@ class SidebarParserTest: Sidebar.load(sidebar)(using testContext) ) - @Test(expected = classOf[IllegalArgumentException]) + @Test def loadSidebarError(): Unit = - assertEquals( - Sidebar.Category( - None, - None, - List(), - None - ), - Sidebar.load(sidebarErrorNoTitle)(using testContext) - ) - throw new IllegalArgumentException(s"$msg\n$schemaMessage") \ No newline at end of file + val out = new ByteArrayOutputStream() + Console.withErr(new PrintStream(out)) { + Sidebar.load(sidebarErrorNoPage)(using testContext) + } + val error = out.toString().trim() + assertEquals(noPageExpectedError, error) From e9719f6865037c78ed1ab59579bb49f400ffba2e Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 10 May 2023 11:54:50 +0200 Subject: [PATCH 5/8] Corrections: - Do a new method of test (Use a 'contains' instead) - Improve the accuracy of the error message by putting variables. --- .../tools/scaladoc/site/SidebarParser.scala | 6 +-- .../scaladoc/site/SidebarParserTest.scala | 49 ++++++++++++++++--- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala index 189112ad2b80..824ee279ff99 100644 --- a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala +++ b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala @@ -45,13 +45,13 @@ object Sidebar: 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) => if title.isEmpty() then - val msg = "Error parsing YAML configuration file: Title is not provided." + val msg = s"Error parsing YAML configuration file: 'title' is not provided." report.error(s"$msg\n$schemaMessage") else if title.nonEmpty && (page.isEmpty() || index.isEmpty()) then - val msg = "Error parsing YAML configuration file: Index or page path to at least one page is missing." + 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 = "The parsing seems not to have been done correctly." + val msg = s"The parsing seems not to have been done correctly." report.warning(s"$msg\n$schemaMessage") Sidebar.Page(None, page, hidden) diff --git a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala index 71bc3660bb0d..3116c7619a24 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala @@ -38,6 +38,32 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin + private val sidebarNoTitle = + """index: index.md + |subsection: + | page: my-page1.md + | - page: my-page2.md + | - page: my-page3/subsection + | - title: Reference + | subsection: + | - page: my-page3.md + | hidden: true + | - index: my-page4/index.md + | subsection: + | - page: my-page4/my-page4.md + | - title: My subsection + | index: my-page5/index.md + | subsection: + | - page: my-page5/my-page5.md + | - subsection: + | - page: my-page7/my-page7.md + | - index: my-page6/index.md + | subsection: + | - index: my-page6/my-page6/index.md + | subsection: + | - page: my-page6/my-page6/my-page6.md + """.stripMargin + private val sidebarErrorNoPage = """index: index.md |subsection: @@ -64,13 +90,11 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin - private val msgNoTitle = "Error parsing YAML configuration file: Title is not provided." - private val msgNoPage = "Error parsing YAML configuration file: Index or page path to at least one page is missing." + private val msgNoTitle = "Error parsing YAML configuration file: 'title' is not provided." + private val msgNoPage = "Error parsing YAML configuration file: 'index' or 'page' path is missing for title 'My title'." private def schemaMessage: String = Sidebar.schemaMessage - private val noPageExpectedError = s"$msgNoPage\n$schemaMessage\nPage my-page2.md does not exist.\nPage my-page3/subsection does not exist.\nPage my-page3.md does not exist.\nPage my-page4/my-page4.md does not exist.\nPage my-page5/my-page5.md does not exist.\nPage my-page7/my-page7.md does not exist.\nPage my-page6/my-page6/my-page6.md does not exist." - @Test def loadSidebar(): Unit = assertEquals( Sidebar.Category( @@ -92,10 +116,23 @@ class SidebarParserTest: ) @Test - def loadSidebarError(): Unit = + def loadSidebarNoPageError: Unit = val out = new ByteArrayOutputStream() Console.withErr(new PrintStream(out)) { Sidebar.load(sidebarErrorNoPage)(using testContext) } + println(out.toString()) val error = out.toString().trim() - assertEquals(noPageExpectedError, error) + + assert(error.contains(msgNoPage) && error.contains(schemaMessage)) + + + @Test + def loadSidebarNoTitleError(): Unit = + val out = new ByteArrayOutputStream() + Console.withErr(new PrintStream(out)) { + Sidebar.load(sidebarNoTitle)(using testContext) + } + val error = out.toString().trim() + + assert(error.contains(msgNoTitle) && error.contains(schemaMessage)) \ No newline at end of file From 49d3c6276466896d19594dd2c02009c38086d684 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 12 May 2023 14:57:44 +0200 Subject: [PATCH 6/8] Add the page to title missing error --- .../tools/scaladoc/site/SidebarParser.scala | 19 ++++++++++++++++--- .../scaladoc/site/SidebarParserTest.scala | 9 +++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala index 824ee279ff99..3e13ae3345b2 100644 --- a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala +++ b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala @@ -8,6 +8,7 @@ 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( @@ -31,6 +32,17 @@ object Sidebar: private object RawInputTypeRef extends TypeReference[RawInput] + 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("") + 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 @@ -44,14 +56,15 @@ object Sidebar: 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(_, content)).toList, Option.when(dir.nonEmpty)(dir)) case RawInput(title, page, index, subsection, dir, hidden) => - if title.isEmpty() then - val msg = s"Error parsing YAML configuration file: 'title' is not provided." + if title.isEmpty() && index.isEmpty() then + val page = pageWithNoTitle(content).trim() + 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"The parsing seems not to have been done correctly." + val msg = s"Error parsing YAML configuration file." report.warning(s"$msg\n$schemaMessage") Sidebar.Page(None, page, hidden) diff --git a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala index 3116c7619a24..d5bbc07e313c 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala @@ -8,7 +8,7 @@ import dotty.tools.scaladoc.site.Sidebar.RawInput import java.io.ByteArrayOutputStream import java.io.PrintStream -// TODO add negaitve and more details tests +// TODO add negative and more details tests class SidebarParserTest: private val sidebar = @@ -41,6 +41,7 @@ class SidebarParserTest: private val sidebarNoTitle = """index: index.md |subsection: + | - title: My title | page: my-page1.md | - page: my-page2.md | - page: my-page3/subsection @@ -56,7 +57,7 @@ class SidebarParserTest: | subsection: | - page: my-page5/my-page5.md | - subsection: - | - page: my-page7/my-page7.md + | page: my-page7/my-page7.md | - index: my-page6/index.md | subsection: | - index: my-page6/my-page6/index.md @@ -90,7 +91,7 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin - private val msgNoTitle = "Error parsing YAML configuration file: 'title' is not provided." + private val msgNoTitle = "Error parsing YAML configuration file: 'title' is not provided for page 'my-page7/my-page7.md'." private val msgNoPage = "Error parsing YAML configuration file: 'index' or 'page' path is missing for title 'My title'." private def schemaMessage: String = Sidebar.schemaMessage @@ -135,4 +136,4 @@ class SidebarParserTest: } val error = out.toString().trim() - assert(error.contains(msgNoTitle) && error.contains(schemaMessage)) \ No newline at end of file + assert(error.contains(msgNoTitle) && error.contains(schemaMessage)) From 5e7e205ad70fee9088d9fb646614eb38b87d3127 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 26 May 2023 10:30:16 +0200 Subject: [PATCH 7/8] Avoid re-parse and change errors messages --- .../tools/scaladoc/site/SidebarParser.scala | 16 ++-------------- .../tools/scaladoc/site/SidebarParserTest.scala | 2 +- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala index 3e13ae3345b2..1916c32e53dc 100644 --- a/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala +++ b/scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala @@ -32,17 +32,6 @@ object Sidebar: private object RawInputTypeRef extends TypeReference[RawInput] - 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("") - 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 @@ -57,14 +46,13 @@ object Sidebar: 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) => if title.isEmpty() && index.isEmpty() then - val page = pageWithNoTitle(content).trim() - val msg = s"Error parsing YAML configuration file: 'title' is not provided for page '$page'." + val msg = "`title` property is missing for some 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." + val msg = "Problem when parsing YAML configuration file." report.warning(s"$msg\n$schemaMessage") Sidebar.Page(None, page, hidden) diff --git a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala index d5bbc07e313c..f30990d07cfa 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala @@ -91,7 +91,7 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin - private val msgNoTitle = "Error parsing YAML configuration file: 'title' is not provided for page 'my-page7/my-page7.md'." + private val msgNoTitle = "`title` property is missing for some page." private val msgNoPage = "Error parsing YAML configuration file: 'index' or 'page' path is missing for title 'My title'." private def schemaMessage: String = Sidebar.schemaMessage From 73fd8a13ef371021c5ba8699acd31cf984dcce46 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 12 Jun 2023 13:55:48 +0200 Subject: [PATCH 8/8] Fix: Error sidebar PR --- .../test/dotty/tools/scaladoc/site/SidebarParserTest.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala index f30990d07cfa..408a8a5a9476 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/SidebarParserTest.scala @@ -38,10 +38,9 @@ class SidebarParserTest: | - page: my-page6/my-page6/my-page6.md """.stripMargin - private val sidebarNoTitle = + private val sidebarErrorNoTitle = """index: index.md |subsection: - | - title: My title | page: my-page1.md | - page: my-page2.md | - page: my-page3/subsection @@ -122,7 +121,6 @@ class SidebarParserTest: Console.withErr(new PrintStream(out)) { Sidebar.load(sidebarErrorNoPage)(using testContext) } - println(out.toString()) val error = out.toString().trim() assert(error.contains(msgNoPage) && error.contains(schemaMessage)) @@ -132,7 +130,7 @@ class SidebarParserTest: def loadSidebarNoTitleError(): Unit = val out = new ByteArrayOutputStream() Console.withErr(new PrintStream(out)) { - Sidebar.load(sidebarNoTitle)(using testContext) + Sidebar.load(sidebarErrorNoTitle)(using testContext) } val error = out.toString().trim()