Skip to content

Commit c5575b8

Browse files
committed
Change requests: Uniformize property names in YAML config
1 parent 2ef7753 commit c5575b8

File tree

5 files changed

+61
-67
lines changed

5 files changed

+61
-67
lines changed

docs/sidebar.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
rootIndex: index.md
2-
pages:
3-
# - title: Blog
1+
index: index.md
2+
subsection:
43
- title: Usage
54
subsection:
65
- page: usage/sbt-projects.md

scaladoc-testcases/docs/sidebar.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
rootIndex: index.md
2-
pages:
1+
index: index.md
2+
subsection:
33
- page: docs/f1.md
44
- page: docs/f2.md
55
- page: docs/f3.md

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,15 @@ import java.util.Optional
99
import scala.beans._
1010

1111
enum Sidebar:
12-
case Root(index: Option[String], pages: List[Sidebar.Child])
1312
case Category(
1413
title: Option[String],
1514
indexPath: Option[String],
16-
nested: List[Sidebar.Child],
15+
nested: List[Sidebar],
1716
directory: Option[String]
1817
)
1918
case Page(title: Option[String], pagePath: String, hidden: Boolean)
2019

2120
object Sidebar:
22-
23-
type Child = Category | Page
24-
case class RawRoot(var rootIndex: String, var pages: JList[RawInput]):
25-
def this() = this("", JList())
26-
27-
def setRootIndex(s: String) = rootIndex = s
28-
def setPages(l: JList[RawInput]) = pages = l
29-
3021
case class RawInput(
3122
@BeanProperty var title: String,
3223
@BeanProperty var page: String,
@@ -37,9 +28,9 @@ object Sidebar:
3728
):
3829
def this() = this("", "", "", JList(), "", false)
3930

40-
private object RootTypeRef extends TypeReference[RawRoot]
31+
private object RawInputTypeRef extends TypeReference[RawInput]
4132

42-
private def toSidebar(r: RawInput)(using CompilerContext): Sidebar.Child = r match
33+
private def toSidebar(r: RawInput)(using CompilerContext): Sidebar = r match
4334
case RawInput(title, page, index, subsection, dir, hidden) if page.nonEmpty && index.isEmpty && subsection.isEmpty() =>
4435
Sidebar.Page(Option.when(title.nonEmpty)(title), page, hidden)
4536
case RawInput(title, page, index, subsection, dir, hidden) if page.isEmpty && (!subsection.isEmpty() || !index.isEmpty()) =>
@@ -49,43 +40,43 @@ object Sidebar:
4940
Sidebar.Page(None, page, hidden)
5041

5142
private def schemaMessage: String =
52-
s"""Static site YAML configuration file should comply to the following description:
53-
|rootIndex: <string> # optional
54-
|pages:
55-
| - <subsection> | <page>
43+
s"""Static site YAML configuration file should comply with the following description:
44+
|The root element of static site needs to be <subsection>
45+
|`title` and `directory` properties are ignored in root subsection.
5646
|
5747
|<subsection>:
58-
| title: <string> # optional
59-
| index: <string> # optional
60-
| directory: <string> # optional
61-
| subsection: # optional
48+
| title: <string> # optional - Default value is file name. Title can be also set using front-matter.
49+
| index: <string> # optional - If not provided, default empty index template is generated.
50+
| directory: <string> # optional - By default, directory name is title name in kebab case.
51+
| subsection: # optional - If not provided, pages are loaded from the index directory
6252
| - <subsection> | <page>
6353
| # either index or subsection needs to be present
6454
|<page>:
65-
| title: <string> # optional
55+
| title: <string> # optional - Default value is file name. Title can be also set using front-matter.
6656
| page: <string>
67-
| hidden: <boolean> # optional
57+
| hidden: <boolean> # optional - Default value is false.
6858
|
6959
|For more information visit:
7060
|https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html
7161
|""".stripMargin
7262

73-
def load(content: String | java.io.File)(using CompilerContext): Sidebar.Root =
63+
def load(content: String | java.io.File)(using CompilerContext): Sidebar.Category =
7464
import scala.util.Try
7565
val mapper = ObjectMapper(YAMLFactory())
7666
def readValue = content match
77-
case s: String => mapper.readValue(s, RootTypeRef)
78-
case f: java.io.File => mapper.readValue(f, RootTypeRef)
67+
case s: String => mapper.readValue(s, RawInputTypeRef)
68+
case f: java.io.File => mapper.readValue(f, RawInputTypeRef)
7969

80-
val root: RawRoot = Try(readValue)
70+
val root: RawInput = Try(readValue)
8171
.fold(
8272
{ e =>
8373
report.warn(schemaMessage, e)
84-
RawRoot("", java.util.Collections.emptyList())
74+
new RawInput()
8575
},
8676
identity
8777
)
88-
89-
val rootIndex: String = root.rootIndex
90-
val pages: List[Sidebar.Child] = root.pages.asScala.toList.map(toSidebar)
91-
Sidebar.Root(Option.when(rootIndex.nonEmpty)(rootIndex), pages)
78+
toSidebar(root) match
79+
case c: Sidebar.Category => c
80+
case _ =>
81+
report.error(s"Root element is not a subsection.\n$schemaMessage")
82+
Sidebar.Category(None, None, List.empty, None)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
3636
* However, you can override default name by setting "directory" property of the subsection object.
3737
*
3838
*/
39-
def loadBasedOnYaml(yamlRoot: Sidebar.Root): StaticSiteRoot = {
39+
def loadBasedOnYaml(yamlRoot: Sidebar.Category): StaticSiteRoot = {
4040
val rootDest = ctx.docsPath.resolve("index.html").toFile
41-
val rootIndex = yamlRoot.index
41+
val rootIndex = yamlRoot.indexPath
4242
.map(ctx.docsPath.resolve(_).toFile)
4343
.filter(_.exists)
4444
.fold(emptyTemplate(rootDest, "index")) { f =>
@@ -48,7 +48,7 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
4848
loaded
4949
}.copy(title = TemplateName.FilenameDefined(args.name))
5050

51-
def loadChild(pathFromRoot: Path): Sidebar.Child => LoadedTemplate = {
51+
def loadChild(pathFromRoot: Path): Sidebar => LoadedTemplate = {
5252
case Sidebar.Category(optionTitle, optionIndexPath, nested, dir) =>
5353
val indexPageOpt = optionIndexPath
5454
.map(relativizeIfNeeded)
@@ -89,7 +89,7 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
8989
val templateFile = loadTemplateFile(file, title)
9090
LoadedTemplate(templateFile, List.empty, pathFromRoot.resolve(file.getName).toFile, hidden)
9191
}
92-
val rootTemplate = LoadedTemplate(rootIndex, yamlRoot.pages.map(c => loadChild(ctx.docsPath)(c)) ++ loadBlog(), rootDest)
92+
val rootTemplate = LoadedTemplate(rootIndex, yamlRoot.nested.map(c => loadChild(ctx.docsPath)(c)) ++ loadBlog(), rootDest)
9393
val mappings = createMapping(rootTemplate)
9494
StaticSiteRoot(rootTemplate, mappings)
9595
}

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,39 @@ import org.junit.Assert._
77
// TODO add negaitve and more details tests
88
class SidebarParserTest:
99

10-
private val sidebar = """pages:
11-
- title: My title
12-
page: my-page1.md
13-
- page: my-page2.md
14-
- page: my-page3/subsection
15-
- title: Reference
16-
subsection:
17-
- page: my-page3.md
18-
hidden: true
19-
- index: my-page4/index.md
20-
subsection:
21-
- page: my-page4/my-page4.md
22-
- title: My subsection
23-
index: my-page5/index.md
24-
subsection:
25-
- page: my-page5/my-page5.md
26-
- subsection:
27-
- page: my-page7/my-page7.md
28-
- index: my-page6/index.md
29-
subsection:
30-
- index: my-page6/my-page6/index.md
31-
subsection:
32-
- page: my-page6/my-page6/my-page6.md
33-
"""
10+
private val sidebar =
11+
"""index: index.md
12+
|subsection:
13+
| - title: My title
14+
| page: my-page1.md
15+
| - page: my-page2.md
16+
| - page: my-page3/subsection
17+
| - title: Reference
18+
| subsection:
19+
| - page: my-page3.md
20+
| hidden: true
21+
| - index: my-page4/index.md
22+
| subsection:
23+
| - page: my-page4/my-page4.md
24+
| - title: My subsection
25+
| index: my-page5/index.md
26+
| subsection:
27+
| - page: my-page5/my-page5.md
28+
| - subsection:
29+
| - page: my-page7/my-page7.md
30+
| - index: my-page6/index.md
31+
| subsection:
32+
| - index: my-page6/my-page6/index.md
33+
| subsection:
34+
| - page: my-page6/my-page6/my-page6.md
35+
""".stripMargin
3436

3537
@Test
3638
def loadSidebar(): Unit = assertEquals(
37-
Sidebar.Root(
39+
Sidebar.Category(
3840
None,
39-
List(
41+
Some("index.md"),
42+
List(
4043
Sidebar.Page(Some("My title"), "my-page1.md", false),
4144
Sidebar.Page(None, "my-page2.md", false),
4245
Sidebar.Page(None, "my-page3/subsection", false),
@@ -45,7 +48,8 @@ class SidebarParserTest:
4548
Sidebar.Category(Some("My subsection"), Some("my-page5/index.md"), List(Sidebar.Page(None, "my-page5/my-page5.md", false)), None),
4649
Sidebar.Category(None, None, List(Sidebar.Page(None, "my-page7/my-page7.md", false)), None),
4750
Sidebar.Category(None, Some("my-page6/index.md"), List(Sidebar.Category(None, Some("my-page6/my-page6/index.md"), List(Sidebar.Page(None, "my-page6/my-page6/my-page6.md", false)), None)), None),
48-
)
51+
),
52+
None
4953
),
5054
Sidebar.load(sidebar)(using testContext)
5155
)

0 commit comments

Comments
 (0)