Skip to content

Commit 1e0ea84

Browse files
committed
CRs from review
1 parent e196b51 commit 1e0ea84

File tree

7 files changed

+61
-48
lines changed

7 files changed

+61
-48
lines changed

scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
4444
Using(Files.walk(file)) { stream =>
4545
stream.iterator().asScala.toSeq
4646
.map(from => Resource.File(resourceFile.toPath.relativize(from).toString, from))
47-
}.get
47+
}.fold (
48+
{ t =>
49+
report.warn(s"Error occured while processing _assets file.", t)
50+
Seq.empty
51+
},
52+
identity
53+
)
4854
}
4955
}
5056
val resources = staticSiteResources ++ allResources(allPages) ++ onlyRenderedResources

scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ trait Locations(using ctx: DocContext):
3434
cache.get(dri) match
3535
case null =>
3636
val path = dri match
37-
// case `docsRootDRI` => List("docs", "index")
3837
case `apiPageDRI` =>
3938
if ctx.args.apiSubdirectory && ctx.staticSiteContext.nonEmpty
4039
then List("api", "index")

scaladoc/src/dotty/tools/scaladoc/renderers/Renderer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
7474
)
7575
updatedTemplates.result()
7676

77-
val newTemplates = updateSettings(Seq(siteContext.staticSiteRoot.rootTemplate), newSettings.to(ListBuffer))
77+
val newTemplates = updateSettings(Seq(rootTemplate), newSettings.to(ListBuffer))
7878
val templatePages = newTemplates.map(templateToPage(_, siteContext))
7979

8080
val newRoot = newTemplates.head
8181

82-
if newRoot.children.size == 0 && newRoot.templateFile.rawCode == ""
82+
if newRoot.children.isEmpty && newRoot.templateFile.rawCode.isEmpty
8383
then rootPckPage.withTitle(args.name)
8484
else {
8585
val newRootPage = templateToPage(newRoot, siteContext)

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import collection.JavaConverters._
88
import java.util.Optional
99

1010
enum Sidebar:
11-
case Root(index: Option[String], pages: List[Sidebar])
12-
case Category(title: Option[String], indexPath: Option[String], nested: List[Sidebar], directory: Option[String])
11+
case Root(index: Option[String], pages: List[Sidebar.Child])
12+
case Category(title: Option[String], indexPath: Option[String], nested: List[Sidebar.Child], directory: Option[String])
1313
case Page(title: Option[String], pagePath: String)
1414

1515
object Sidebar:
16+
17+
type Child = Category | Page
1618
case class RawRoot(var rootIndex: String, var pages: JList[RawInput]):
1719
def this() = this("", JList())
1820

@@ -30,24 +32,40 @@ object Sidebar:
3032

3133
private object RootTypeRef extends TypeReference[RawRoot]
3234

33-
private def toSidebar(r: RawInput): Sidebar = r match
35+
private def toSidebar(r: RawInput): Sidebar.Child = r match
3436
case RawInput(title, page, index, subsection, dir) if page.nonEmpty && index.isEmpty && subsection.isEmpty() || title == "Blog" =>
3537
Sidebar.Page(Option.when(title.nonEmpty)(title), page)
3638
case RawInput(title, page, index, subsection, dir) if page.isEmpty && (!subsection.isEmpty() || !index.isEmpty()) =>
3739
Sidebar.Category(Option.when(title.nonEmpty)(title), Option.when(index.nonEmpty)(index), subsection.asScala.map(toSidebar).toList, Option.when(dir.nonEmpty)(dir))
3840

39-
def load(content: String): Sidebar.Root =
40-
val mapper = ObjectMapper(YAMLFactory())
41-
val root: RawRoot = mapper.readValue(content, RootTypeRef)
42-
43-
val rootIndex: String = root.rootIndex
44-
val pages: List[Sidebar] = root.pages.asScala.toList.map(toSidebar)
45-
Sidebar.Root(Option.when(rootIndex.nonEmpty)(rootIndex), pages)
41+
private def schemaMessage: String =
42+
s"""Static site YAML configuration file should comply to following model:
43+
|case class Root(rootIndex: String, pages: List[Child])
44+
|case class Page(title: Option[String], page: String) extends Child
45+
|case class Subsection(
46+
| index: Option[String],
47+
| title: Option[String],
48+
| directory: Option[String],
49+
| subsection: List[Child]
50+
|)
51+
|""".stripMargin
4652

47-
def load(file: java.io.File): Sidebar.Root =
53+
def load(content: String | java.io.File)(using CompilerContext): Sidebar.Root =
54+
import scala.util.Try
4855
val mapper = ObjectMapper(YAMLFactory())
49-
val root: RawRoot = mapper.readValue(file, RootTypeRef)
56+
def readValue = content match
57+
case s: String => mapper.readValue(s, RootTypeRef)
58+
case f: java.io.File => mapper.readValue(f, RootTypeRef)
59+
60+
val root: RawRoot = Try(readValue)
61+
.fold(
62+
{ e =>
63+
report.warn(schemaMessage, e)
64+
RawRoot("", java.util.Collections.emptyList())
65+
},
66+
identity
67+
)
5068

5169
val rootIndex: String = root.rootIndex
52-
val pages: List[Sidebar] = root.pages.asScala.toList.map(toSidebar)
70+
val pages: List[Sidebar.Child] = root.pages.asScala.toList.map(toSidebar)
5371
Sidebar.Root(Option.when(rootIndex.nonEmpty)(rootIndex), pages)

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,22 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
2424
}
2525
}
2626

27+
/** Method loading static site structure based on YAML configuration file.
28+
*
29+
* The rendered static site will only contain pages that are present in YAML.
30+
* The following rules are applied:
31+
* - Each subsection will be a separate directory.
32+
* - Nested subsections will result in nested directories.
33+
* - If the subsection object contains location of index and doesn't contain any item,
34+
* items are loaded using file system from the directory of the index file.
35+
* - By default, directory name is a subsection title converted to kebab case.
36+
* However, you can override default name by setting "directory" property of the subsection object.
37+
*
38+
*/
2739
def loadBasedOnYaml(yamlRoot: Sidebar.Root): StaticSiteRoot = {
2840
val rootDest = ctx.docsPath.resolve("index.html").toFile
2941
val rootIndex = yamlRoot.index
30-
.map(Paths.get(root.getPath, _).toFile)
42+
.map(ctx.docsPath.resolve(_).toFile)
3143
.filter(_.exists)
3244
.fold(emptyTemplate(rootDest, "index")) { f =>
3345
val loaded = loadTemplateFile(f)
@@ -36,13 +48,13 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
3648
loaded
3749
}.copy(title = TemplateName.FilenameDefined(args.name))
3850

39-
def loadChild(pathFromRoot: Path): Sidebar => LoadedTemplate = {
51+
def loadChild(pathFromRoot: Path): Sidebar.Child => LoadedTemplate = {
4052
case Sidebar.Category(optionTitle, optionIndexPath, nested, dir) =>
4153
val indexPageOpt = optionIndexPath
4254
.map(relativizeIfNeeded)
4355
.map(_.toFile)
4456
.filter(_.exists)
45-
.map(loadTemplateFile)
57+
.map(loadTemplateFile(_))
4658
val title = (
4759
optionTitle.map(TemplateName.SidebarDefined(_)) ++
4860
indexPageOpt.map(_.title)
@@ -73,15 +85,9 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
7385
case Sidebar.Page(optionTitle, pagePath) =>
7486
val path = relativizeIfNeeded(pagePath)
7587
val file = path.toFile
76-
val templateFile = loadTemplateFile(file)
77-
val withUpdatedTitle = optionTitle.fold(templateFile) { t => templateFile.title match
78-
case _: TemplateName.FilenameDefined => templateFile.copy(title = TemplateName.SidebarDefined(t))
79-
case _ => templateFile
80-
}
81-
LoadedTemplate(withUpdatedTitle, List.empty, pathFromRoot.resolve(file.getName).toFile)
82-
case Sidebar.Root(_, _) =>
83-
// Cannot happen
84-
???
88+
val title = optionTitle.map(TemplateName.SidebarDefined(_))
89+
val templateFile = loadTemplateFile(file, title)
90+
LoadedTemplate(templateFile, List.empty, pathFromRoot.resolve(file.getName).toFile)
8591
}
8692
val rootTemplate = LoadedTemplate(rootIndex, yamlRoot.pages.map(c => loadChild(ctx.docsPath)(c)) ++ loadBlog(), rootDest)
8793
val mappings = createMapping(rootTemplate)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final val LineSeparator = "\n"
5858

5959
def yamlParser(using ctx: StaticSiteContext): Parser = Parser.builder(defaultMarkdownOptions).build()
6060

61-
def loadTemplateFile(file: File)(using ctx: StaticSiteContext): TemplateFile = {
61+
def loadTemplateFile(file: File, titleOverride: Option[TemplateName] = None)(using ctx: StaticSiteContext): TemplateFile = {
6262
val lines = Files.readAllLines(file.toPath).asScala.toList
6363

6464
val (config, content) = if (lines.head == ConfigSeparator) {
@@ -105,7 +105,7 @@ def loadTemplateFile(file: File)(using ctx: StaticSiteContext): TemplateFile = {
105105
rawCode = content.mkString(LineSeparator),
106106
settings = settings,
107107
name = name,
108-
title = stringSetting(allSettings, "title").map(TemplateName.YamlDefined(_)).getOrElse(TemplateName.FilenameDefined(name)),
108+
title = stringSetting(allSettings, "title").map(TemplateName.YamlDefined(_)).orElse(titleOverride).getOrElse(TemplateName.FilenameDefined(name)),
109109
hasFrame = !stringSetting(allSettings, "hasFrame").contains("false"),
110110
resources = (listSetting(allSettings, "extraCSS") ++ listSetting(allSettings, "extraJS")).flatten.toList,
111111
layout = stringSetting(allSettings, "layout"),

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,9 @@ case class TemplateFile(
115115
// Library requires mutable maps..
116116
val mutableProperties = new JHashMap(ctx.properties.transform((_, v) => asJavaElement(v)).asJava)
117117

118-
val tag = new Tag("highlight"):
119-
override def render(context: TemplateContext, nodes: Array[? <: LNode]): Object =
120-
super.asString(nodes(0).render(context), context) match
121-
case "diff" =>
122-
s"<pre><code class=\"language-diff hljs\" data-lang=\"diff\">${super.asString(nodes(1).render(context), context)}</code></pre>\n\n"
123-
case _ =>
124-
report.warn("Unsupported highlight value. Currenlty supported values are: `diff`", file)(using ssctx.outerCtx)
125-
s"```${super.asString(nodes(1).render(context), context)}```\n\n"
126-
127-
val tag2 = new Tag("link"):
128-
override def render(context: TemplateContext, nodes: Array[? <: LNode]): Object =
129-
super.asString(nodes(0).render(context), context) match
130-
case sth =>
131-
report.warn(s"Unsupported link tag. Link to $sth can't be resolved", file)(using ssctx.outerCtx)
132-
"/"
133-
134118
val parseSettings = ParseSettings.Builder().withFlavor(Flavor.JEKYLL).build()
135119

136-
val rendered = Template.parse(this.rawCode, parseSettings).`with`(tag).`with`(tag2).render(mutableProperties)
120+
val rendered = Template.parse(this.rawCode, parseSettings).render(mutableProperties)
137121

138122
// We want to render markdown only if next template is html
139123
val code = if (isHtml || layoutTemplate.exists(!_.isHtml)) rendered else

0 commit comments

Comments
 (0)