Skip to content

Commit 150448c

Browse files
committed
Add support for multiple redirects
1 parent 927287d commit 150448c

File tree

7 files changed

+47
-38
lines changed

7 files changed

+47
-38
lines changed

.github/workflows/scaladoc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
java-version: 11
3737

3838
- name: Compile and test scala3doc-js
39-
run: ./project/scripts/sbt scaladoc-js/test
39+
run: ./project/scripts/sbt scaladoc-js-main/test
4040

4141
- name: Compile and test
4242
run: |

project/Build.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,20 +1258,23 @@ object Build {
12581258
lazy val `scaladoc-js-common` = project.in(file("scaladoc-js/common")).
12591259
enablePlugins(DottyJSPlugin).
12601260
dependsOn(`scala3-library-bootstrappedJS`).
1261-
settings(
1262-
Test / fork := false,
1263-
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "1.1.0").cross(CrossVersion.for3Use2_13)
1264-
)
1261+
settings(libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "1.1.0").cross(CrossVersion.for3Use2_13))
12651262

12661263
lazy val `scaladoc-js-main` = project.in(file("scaladoc-js/main")).
12671264
enablePlugins(DottyJSPlugin).
12681265
dependsOn(`scaladoc-js-common`).
1269-
settings(scalaJSUseMainModuleInitializer := true)
1266+
settings(
1267+
scalaJSUseMainModuleInitializer := true,
1268+
Test / fork := false
1269+
)
12701270

12711271
lazy val `scaladoc-js-markdown` = project.in(file("scaladoc-js/markdown")).
12721272
enablePlugins(DottyJSPlugin).
12731273
dependsOn(`scaladoc-js-common`).
1274-
settings(scalaJSUseMainModuleInitializer := true)
1274+
settings(
1275+
scalaJSUseMainModuleInitializer := true,
1276+
Test / fork := false
1277+
)
12751278

12761279
lazy val `scaladoc-js-contributors` = project.in(file("scaladoc-js/contributors")).
12771280
enablePlugins(DottyJSPlugin).

project/CopyDocs.scala

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,14 @@ object CopyDocs {
1919
*/
2020
val outputDir = FileSystems.getDefault.getPath("docs-for-dotty-page")
2121

22-
implicit def stringToFun(s: String): MyParams => String = _ => s
23-
24-
// Patterns, for convenience
25-
val titlePattern = """(?s)^(---\n.*?title: ([^\n]*).*?---)"""
26-
val redirectFromPattern = """(?s)^---.*?((redirectFrom: ([^\n]*)).*?---|---)"""
2722
val jekyllLinkPattern = """\{\% link _overviews/scala3-reference(.*) %\}"""
2823
val jekyllLinkSubstitution = "..$1"
2924
val jekyllLinkPattern2 = """\{\% link _overviews/scala3-scaladoc(.*) %\}"""
3025
val jekyllLinkSubstitution2 = ".$1"
3126
val localLinkPattern = """\((?!http|www)(.*).html\)"""
3227
val localLinkSubstitution = "($1.md)"
3328

34-
case class MyParams(newPath: String)
35-
36-
val commonTransformations: List[(String, MyParams => String)] = List(
29+
val commonTransformations: Map[String, String] = Map(
3730
jekyllLinkPattern -> jekyllLinkSubstitution,
3831
jekyllLinkPattern2 -> jekyllLinkSubstitution2,
3932
localLinkPattern -> localLinkSubstitution,
@@ -44,32 +37,26 @@ object CopyDocs {
4437
* The outer map is holding morphism `directory prefix` -> `List of transformations`.
4538
* The inner list is a collection of pairs `regex pattern` -> `substitution value`.
4639
*/
47-
val transformationMap: Map[String, List[(String, MyParams => String)]] = Map(
48-
"docs/docs/usage/scaladoc/index.md" -> List(
40+
val transformationMap: Map[String, Map[String, String]] = Map(
41+
"docs/docs/usage/scaladoc/index.md" -> Map(
4942
("""\{\{ site\.baseurl \}\}/resources/images/scala3/scaladoc/logo\.svg""" -> "images/scaladoc_logo.svg"),
5043
),
5144

52-
"docs/docs/usage/scaladoc/site-versioning.md" -> List(
45+
"docs/docs/usage/scaladoc/site-versioning.md" -> Map(
5346
("""/resources/images/scala3/scaladoc/nightly\.gif""" -> "images/scaladoc/nightly.gif"),
5447
),
5548

56-
"docs/docs/usage/scaladoc/search-engine.md" -> List(
49+
"docs/docs/usage/scaladoc/search-engine.md" -> Map(
5750
("""/resources/images/scala3/scaladoc/inkuire-1\.0\.0-M2_js_flatMap\.gif""" -> "images/scaladoc/inkuire-1.0.0-M2_js_flatMap.gif"),
5851
),
5952

60-
"docs/docs/reference/other-new-features/explicit-nulls.md" -> List(
53+
"docs/docs/reference/other-new-features/explicit-nulls.md" -> Map(
6154
("""/resources/images/scala3/explicit-nulls/explicit-nulls-type-hierarchy\.png""" -> "images/explicit-nulls/explicit-nulls-type-hierarchy.png"),
6255
),
6356

64-
"docs/docs/reference/" -> (commonTransformations ++ List[(String, MyParams => String)](
65-
(titlePattern -> ((p) => s"$$1\nlayout: doc-page\ntitle: $$2\nmovedTo: https://docs.scala-lang.org/scala3/reference/${p.newPath}.html\n---")),
66-
(redirectFromPattern -> "---\n$2")
67-
)),
57+
"docs/docs/reference/" -> commonTransformations,
6858

69-
"docs/docs/usage/scaladoc/" -> (commonTransformations ++ List[(String, MyParams => String)](
70-
(titlePattern -> s"$$1\nlayout: doc-page\ntitle: $$2\n---"),
71-
(redirectFromPattern -> "---\n$2")
72-
)),
59+
"docs/docs/usage/scaladoc/" -> commonTransformations
7360
)
7461

7562
def copyDocs() = {
@@ -78,14 +65,24 @@ object CopyDocs {
7865
Files.createDirectories(newPath.getParent())
7966

8067
path.toString match {
81-
case s if s.startsWith("docs/docs/") =>
68+
case s if s.startsWith("docs/docs/") && s.endsWith(".md") =>
8269
val inputStream = Source.fromFile(path.toFile)(Codec.UTF8)
8370
val fileContent = inputStream.getLines().mkString("\n")
8471

8572
new PrintStream(newPath.toFile) {
8673
val patterns = transformationMap.filter { case (k, v) => path.toString.startsWith(k) }.flatMap(_._2)
87-
val params = MyParams(newPath = s.stripPrefix("docs/docs/reference/").stripSuffix(".md"))
88-
val transformed = patterns.foldLeft(fileContent) { case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution(params)) }
74+
val _ :: frontMatter :: actualContent :: Nil = fileContent.split("---", 3).toList
75+
write("---".getBytes("UTF8"))
76+
val frontMatterSplitted = frontMatter.split("\n(?=[^\\s])")
77+
val frontMatterUpdated = List(
78+
Some("layout: doc-page"),
79+
frontMatterSplitted.find(_.startsWith("title")),
80+
frontMatterSplitted.find(_.startsWith("redirectFrom")),
81+
if (s.startsWith("docs/docs/reference/")) Some(s"movedTo: https://docs.scala-lang.org/scala3/reference/${s.stripPrefix("docs/docs/reference/").stripSuffix(".md")}.html") else None
82+
).flatten.mkString("\n", "\n", "\n")
83+
write(frontMatterUpdated.getBytes("UTF8"))
84+
write("---\n".getBytes("UTF8"))
85+
val transformed = patterns.foldLeft(actualContent) { case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution) }
8986
write(transformed.getBytes("UTF8"))
9087
}
9188
case s =>

scaladoc-js/main/test/dotty/dokka/QueryParserTest.scala renamed to scaladoc-js/main/test/dotty/tools/scaladoc/QueryParserTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ class QueryParserTest:
3232
testCase("trait", EngineMatchersQuery(List(ByName("trait"))))
3333
testCase("trait A", EngineMatchersQuery(List(ByKind("trait"), ByName("A"))))
3434
testCase("`trait A`", EngineMatchersQuery(List(ByName("trait A"))))
35-
}
35+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
111111

112112
(siteContext.orphanedTemplates ++ actualIndexTemplate).map(templateToPage(_, siteContext))
113113

114-
val redirectPages: Seq[Page] = staticSite.fold(Seq.empty)(siteContext => siteContext.redirectTemplates.map(templateToPage(_, siteContext)))
114+
val redirectPages: Seq[Page] = staticSite.fold(Seq.empty)(siteContext => siteContext.redirectTemplates.map {
115+
case (template, driFrom, driTo) =>
116+
val redirectTo = pathToPage(driFrom, driTo)
117+
templateToPage(template.copy(templateFile = template.templateFile.copy(settings = template.templateFile.settings ++ Map("redirectTo" -> redirectTo))), siteContext)
118+
})
115119

116120
/**
117121
* Here we have to retrive index pages from hidden pages and replace fake index pages in navigable page tree.

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,21 @@ class StaticSiteContext(
7171
orphanedFiles.flatMap(p => loadTemplate(p.toFile, isBlog = false))
7272
}
7373

74-
lazy val redirectTemplates: Seq[LoadedTemplate] = {
74+
lazy val redirectTemplates: Seq[(LoadedTemplate, DRI, DRI)] = {
7575
def doFlatten(t: LoadedTemplate): Seq[LoadedTemplate] =
7676
t +: t.children.flatMap(doFlatten)
7777
val mainFiles = templates.flatMap(doFlatten)
7878
mainFiles.flatMap { loadedTemplate =>
79-
loadedTemplate.templateFile.settings.getOrElse("page", Map.empty).asInstanceOf[Map[String, Object]].get("redirectFrom").map { case redirectFrom: String =>
79+
val redirectFrom = loadedTemplate.templateFile.settings.getOrElse("page", Map.empty).asInstanceOf[Map[String, Object]].get("redirectFrom")
80+
def redirectToTemplate(redirectFrom: String) =
8081
val fakeFile = new File(docsPath.toFile, redirectFrom)
81-
val redirectTo = fakeFile.toPath.getParent.relativize(loadedTemplate.file.toPath).toString.stripSuffix(".md") + ".html"
82-
LoadedTemplate(layouts("redirect").copy(settings = layouts("redirect").settings ++ Map("redirectTo" -> redirectTo)), List.empty, fakeFile)
83-
}
82+
val driFrom = driFor(fakeFile.toPath)
83+
val driTo = driFor(loadedTemplate.file.toPath)
84+
(LoadedTemplate(layouts("redirect"), List.empty, fakeFile), driFrom, driTo)
85+
redirectFrom.map {
86+
case redirectFrom: String => Seq(redirectToTemplate(redirectFrom))
87+
case redirects: List[?] => redirects.asInstanceOf[List[String]].map(redirectToTemplate)
88+
}.getOrElse(Nil)
8489
}
8590
}
8691

0 commit comments

Comments
 (0)