Skip to content

Commit 57c4425

Browse files
authored
Merge pull request #10426 from romanowski/scala3doc/static-page-tests
Create some basic integration tests for static sites in scala3doc
2 parents 30e10b5 + e5ad9ee commit 57c4425

File tree

15 files changed

+177
-36
lines changed

15 files changed

+177
-36
lines changed

docs/docs/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
layout: doc-page
3-
title: "Dotty Documentation"
43
---
54

65
Dotty is the project name for technologies that are considered for inclusion in Scala 3. Scala has

project/Build.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ object Build {
11711171

11721172
val testcasesOutputDir = taskKey[String]("Root directory where tests classses are generated")
11731173
val testcasesSourceRoot = taskKey[String]("Root directory where tests sources are generated")
1174+
val testDocumentationRoot = taskKey[String]("Root directory where tests documentation are stored")
11741175
val generateSelfDocumentation = taskKey[Unit]("Generate example documentation")
11751176
// Note: the two tasks below should be one, but a bug in Tasty prevents that
11761177
val generateScala3Documentation = taskKey[Unit]("Generate documentation for dotty lib")
@@ -1544,7 +1545,9 @@ object Build {
15441545
buildInfoKeys in Test := Seq[BuildInfoKey](
15451546
Build.testcasesOutputDir.in(Test),
15461547
Build.testcasesSourceRoot.in(Test),
1548+
Build.testDocumentationRoot,
15471549
),
1550+
testDocumentationRoot := (baseDirectory.value / "test-documentations").getAbsolutePath,
15481551
buildInfoPackage in Test := "dotty.dokka",
15491552
BuildInfoPlugin.buildInfoScopedSettings(Test),
15501553
BuildInfoPlugin.buildInfoDefaultSettings,

scala3doc/src/dotty/dokka/Main.scala

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ case class Args(
7878
tastyRoots: Seq[File],
7979
classpath: String,
8080
output: File,
81-
docsRoot: Option[String],
82-
projectVersion: Option[String],
83-
projectTitle: Option[String],
84-
projectLogo: Option[String],
85-
defaultSyntax: Option[Args.CommentSyntax],
86-
sourceLinks: List[String],
87-
revision: Option[String]
81+
docsRoot: Option[String] = None,
82+
projectVersion: Option[String] = None,
83+
projectTitle: Option[String] = None,
84+
projectLogo: Option[String] = None,
85+
defaultSyntax: Option[Args.CommentSyntax] = None,
86+
sourceLinks: List[String] = Nil,
87+
revision: Option[String] = None
8888
)
8989

9090
object Args:
@@ -121,12 +121,8 @@ enum DocConfiguration extends BaseDocConfiguration:
121121
* - [](package.DottyDokkaConfig) is our config for Dokka.
122122
*/
123123
object Main:
124-
def main(args: Array[String]): Unit =
124+
def main(parsedArgs: Args): Unit =
125125
try
126-
val rawArgs = new RawArgs
127-
new CmdLineParser(rawArgs).parseArgument(args:_*)
128-
val parsedArgs = rawArgs.toArgs
129-
130126
val (files, dirs) = parsedArgs.tastyRoots.partition(_.isFile)
131127
val (providedTastyFiles, jars) = files.toList.map(_.getAbsolutePath).partition(_.endsWith(".tasty"))
132128
jars.foreach(j => if(!j.endsWith(".jar")) sys.error(s"Provided file $j is not jar not tasty file") )
@@ -147,11 +143,17 @@ object Main:
147143
new DokkaGenerator(new DottyDokkaConfig(config), DokkaConsoleLogger.INSTANCE).generate()
148144

149145
println("Done")
150-
151-
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
152-
sys.exit(0)
153146
catch
154147
case a: Exception =>
155148
a.printStackTrace()
156149
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
157150
sys.exit(1)
151+
152+
def main(args: Array[String]): Unit =
153+
val rawArgs = new RawArgs
154+
new CmdLineParser(rawArgs).parseArgument(args:_*)
155+
main(rawArgs.toArgs)
156+
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
157+
sys.exit(0)
158+
159+

scala3doc/src/dotty/dokka/site/LoadedTemplate.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ case class LoadedTemplate(templateFile: TemplateFile, children: List[LoadedTempl
4141

4242
def resolveToHtml(ctx: StaticSiteContext): ResolvedPage =
4343
val posts = children.map(_.lazyTemplateProperties(ctx))
44-
val site = templateFile.settings.getOrElse("site", Map.empty).asInstanceOf[Map[String, Object]]
44+
def getMap(key: String) = templateFile.settings.getOrElse(key, Map.empty).asInstanceOf[Map[String, Object]]
4545
val sourceLinks = if !file.exists() then Nil else
4646
// TODO (https://github.com/lampepfl/scala3doc/issues/240): configure source root
4747
// toRealPath is used to turn symlinks into proper paths
@@ -50,6 +50,7 @@ case class LoadedTemplate(templateFile: TemplateFile, children: List[LoadedTempl
5050
ctx.sourceLinks.pathTo(actualPath, operation = "edit").map("editSource" -> _ )
5151

5252
val updatedSettings = templateFile.settings ++ ctx.projectWideProperties +
53-
("site" -> (site + ("posts" -> posts))) + ("urls" -> sourceLinks.toMap)
53+
("site" -> (getMap("site") + ("posts" -> posts))) + ("urls" -> sourceLinks.toMap) +
54+
("page" -> (getMap("page") + ("title" -> templateFile.title)))
5455

5556
templateFile.resolveInner(RenderingContext(updatedSettings, ctx.layouts))

scala3doc/src/dotty/dokka/site/StaticSiteContext.scala

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import util.Try
2020

2121
import scala.collection.JavaConverters._
2222

23-
class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper], args: Args, val sourceLinks: SourceLinks):
23+
class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper], val args: Args, val sourceLinks: SourceLinks):
2424

2525
var memberLinkResolver: String => Option[DRI] = _ => None
2626

@@ -81,16 +81,15 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper], args:
8181
val topLevelFiles = if isBlog then Seq(from, new File(from, "_posts")) else Seq(from)
8282
val allFiles = topLevelFiles.filter(_.isDirectory).flatMap(_.listFiles())
8383
val (indexes, children) = allFiles.flatMap(loadTemplate(_)).partition(_.templateFile.isIndexPage())
84-
if (indexes.size > 1)
85-
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
86-
println(s"ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}")
84+
8785
def loadIndexPage(): TemplateFile =
88-
val indexFiles = from.listFiles { file =>file.getName == "index.md" || file.getName == "index.html" }
89-
indexFiles.size match
90-
case 0 => emptyTemplate(from, from.getName)
91-
case 1 => loadTemplateFile(indexFiles.head).copy(file = from)
86+
val indexFiles = from.listFiles { file => file.getName == "index.md" || file.getName == "index.html" }
87+
indexes match
88+
case Nil => emptyTemplate(from, from.getName)
89+
case Seq(loadedTemplate) => loadedTemplate.templateFile.copy(file = from)
9290
case _ =>
93-
val msg = s"ERROR: Multiple index pages found under ${from.toPath}"
91+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
92+
val msg = s"ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}"
9493
throw new java.lang.RuntimeException(msg)
9594

9695
val templateFile = if (from.isDirectory) loadIndexPage() else loadTemplateFile(from)
@@ -101,7 +100,15 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper], args:
101100
pageSettings.flatMap(_.get("date").collect{ case s: String => s}).getOrElse("1900-01-01") // blogs without date are last
102101
children.sortBy(dateFrom).reverse
103102

104-
Some(LoadedTemplate(templateFile, processedChildren.toList, from))
103+
val processedTemplate = // Set provided name as arg in page for `docs`
104+
if from.getParentFile.toPath == docsPath && templateFile.isIndexPage() then
105+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
106+
if templateFile.title != "index" then println(s"[WARN] title in $from will be overriden")
107+
val projectTitle = args.projectTitle.getOrElse(args.name)
108+
templateFile.copy(title = projectTitle)
109+
else templateFile
110+
111+
Some(LoadedTemplate(processedTemplate, processedChildren.toList, from))
105112
catch
106113
case e: RuntimeException =>
107114
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling

scala3doc/src/dotty/dokka/site/common.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def loadTemplateFile(file: File): TemplateFile = {
7474
def getSettingValue(k: String, v: JList[String]): String | List[String] =
7575
if v.size == 1 then v.get(0) else v.asScala.toList
7676

77-
val globalKeys = Set("extraJS", "extraCSS", "layout", "hasFrame", "name")
77+
val globalKeys = Set("extraJS", "extraCSS", "layout", "hasFrame", "name", "title")
7878
val allSettings = yamlCollector.getData.asScala.toMap.transform(getSettingValue)
7979
val (global, inner) = allSettings.partition((k,_) => globalKeys.contains(k))
8080
val settings = Map("page" -> inner)

scala3doc/src/dotty/dokka/site/processors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SitePagesCreator(ctx: Option[StaticSiteContext]) extends BaseStaticSitePro
103103
val rootContent = indexes.headOption.fold(ctx.asContent(Text(), mkDRI(extra = "root_content")).get(0))(_.getContent)
104104

105105
val root = AContentPage(
106-
input.getName,
106+
ctx.args.projectTitle.getOrElse(ctx.args.name),
107107
(List(modifiedModuleRoot.modified("API", modifiedModuleRoot.getChildren)) ++ children).asJava,
108108
rootContent,
109109
JSet(docsDRI),

scala3doc/src/dotty/dokka/transformers/PackageHierarchyTransformer.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,9 @@ class PackageHierarchyTransformer(context: DokkaContext) extends PageTransformer
6363

6464
val packagePagesWithTokens = packagePages.map(page => (("""\.""".r.split(page.getName)).toSeq, page))
6565

66-
val maxDepthElem = packagePagesWithTokens.maxBy( (tokens, page) => tokens.size )
67-
68-
page.modified(
69-
page.getName,
66+
val newPages = if packagePagesWithTokens.isEmpty then page.getChildren else
67+
val maxDepthElem = packagePagesWithTokens.maxBy( (tokens, page) => tokens.size )
7068
(otherPages ++ buildPackageTree(maxDepthElem(0).size, packagePagesWithTokens, Seq.empty)).asJava
71-
)
72-
7369

70+
page.modified(page.getName, newPages)
7471
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Adoc
3+
---
4+
# Header in Adoc
5+
6+
And a text!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: A directory
3+
---
4+
# {{ page.title }}
5+
6+
And a text!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Nested in a directory
3+
---
4+
# {{ page.title }}
5+
6+
And a text!
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# {{ page.title }} in header
2+
3+
And a text!

0 commit comments

Comments
 (0)