Skip to content

Commit 2a0a566

Browse files
committed
Add redirects mechanism for scaladoc
1 parent a9876a8 commit 2a0a566

File tree

8 files changed

+60
-56
lines changed

8 files changed

+60
-56
lines changed

docs/_layouts/main.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
</div>
3131
</div>
3232
{{ content }}
33-
<nav class="nav-wide-wrapper" aria-label="Page navigation">
33+
<nav class="arrows-wrapper" aria-label="Page navigation">
3434
{% if page.previous %}
35-
<a rel="prev" href="{{ page.previous }}" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
35+
<a rel="prev" href="{{ page.previous }}" class="arrows previous" aria-keyshortcuts="Left">
3636
<i class="fa fa-angle-left"></i>
3737
</a>
3838
{% endif %}
3939
{% if page.next %}
40-
<a rel="next" href="{{ page.next }}" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
40+
<a rel="next" href="{{ page.next }}" class="arrows next" aria-keyshortcuts="Right">
4141
<i class="fa fa-angle-right"></i>
4242
</a>
4343
{% endif %}

docs/_layouts/redirect.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
hasFrame: false
3+
---
4+
<!DOCTYPE html>
5+
<html lang="en-US">
6+
<meta charset="utf-8">
7+
<title>Redirecting&hellip;</title>
8+
<link rel="canonical" href="{{ redirectTo }}">
9+
<script>location="{{ redirectTo }}"</script>
10+
<meta http-equiv="refresh" content="0; url={{ redirectTo }}">
11+
<meta name="robots" content="noindex">
12+
<h1>Redirecting&hellip;</h1>
13+
<a href="{{ redirectTo }}">Click here if you were not redirected.</a>
14+
</html>

docs/docs/reference/experimental/named-typeargs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
layout: singlepage-overview
33
scala3: true
44
title: "Named Type Arguments"
5+
redirectFrom: reference/other-new-features/named-typeargs.html
56
---
67

78
**Note:** This feature is implemented in Scala 3, but is not expected to be part of Scala 3.0.

docs/docs/reference/other-new-features/named-typeargs.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

project/CopyDocs.scala

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ object CopyDocs {
2222
implicit def stringToFun(s: String): MyParams => String = _ => s
2323

2424
// Patterns, for convenience
25-
val titlePattern = "(?s)^---\n.*?title: ([^\n]*).*?---"
25+
val titlePattern = """(?s)^(---\n.*?title: ([^\n]*).*?---)"""
26+
val redirectFromPattern = """(?s)^---.*?((redirectFrom: ([^\n]*)).*?---|---)"""
2627
val jekyllLinkPattern = """\{\% link _overviews/scala3-reference(.*) %\}"""
2728
val jekyllLinkSubstitution = "..$1"
2829
val jekyllLinkPattern2 = """\{\% link _overviews/scala3-scaladoc(.*) %\}"""
@@ -32,41 +33,43 @@ object CopyDocs {
3233

3334
case class MyParams(newPath: String)
3435

35-
val commonTransformations: Set[(String, MyParams => String)] = Set(
36+
val commonTransformations: List[(String, MyParams => String)] = List(
3637
jekyllLinkPattern -> jekyllLinkSubstitution,
3738
jekyllLinkPattern2 -> jekyllLinkSubstitution2,
3839
localLinkPattern -> localLinkSubstitution,
3940
)
4041

4142
/**
4243
* Structure for holding which transformations should be applied to which directories.
43-
* The outer map is holding morphism `directory prefix` -> `set of transformations`.
44-
* The inner set is a collection of pairs `regex pattern` -> `substitution value`.
44+
* The outer map is holding morphism `directory prefix` -> `List of transformations`.
45+
* The inner list is a collection of pairs `regex pattern` -> `substitution value`.
4546
*/
46-
val transformationMap: Map[String, Set[(String, MyParams => String)]] = Map(
47-
"docs/docs/usage/scaladoc/index.md" -> Set(
47+
val transformationMap: Map[String, List[(String, MyParams => String)]] = Map(
48+
"docs/docs/usage/scaladoc/index.md" -> List(
4849
("""\{\{ site\.baseurl \}\}/resources/images/scala3/scaladoc/logo\.svg""" -> "images/scaladoc_logo.svg"),
4950
),
5051

51-
"docs/docs/usage/scaladoc/site-versioning.md" -> Set(
52+
"docs/docs/usage/scaladoc/site-versioning.md" -> List(
5253
("""/resources/images/scala3/scaladoc/nightly\.gif""" -> "images/scaladoc/nightly.gif"),
5354
),
5455

55-
"docs/docs/usage/scaladoc/search-engine.md" -> Set(
56+
"docs/docs/usage/scaladoc/search-engine.md" -> List(
5657
("""/resources/images/scala3/scaladoc/inkuire-1\.0\.0-M2_js_flatMap\.gif""" -> "images/scaladoc/inkuire-1.0.0-M2_js_flatMap.gif"),
5758
),
5859

59-
"docs/docs/reference/other-new-features/explicit-nulls.md" -> Set(
60+
"docs/docs/reference/other-new-features/explicit-nulls.md" -> List(
6061
("""/resources/images/scala3/explicit-nulls/explicit-nulls-type-hierarchy\.png""" -> "images/explicit-nulls/explicit-nulls-type-hierarchy.png"),
6162
),
6263

63-
"docs/docs/reference/" -> (commonTransformations +
64-
(titlePattern -> ((p) => s"---\nlayout: doc-page\ntitle: $$1\nmovedTo: https://docs.scala-lang.org/scala3/reference/${p.newPath}.html\n---")),
65-
),
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+
)),
6668

67-
"docs/docs/usage/scaladoc/" -> (commonTransformations +
68-
(titlePattern -> s"---\nlayout: doc-page\ntitle: $$1\n---"),
69-
),
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+
)),
7073
)
7174

7275
def copyDocs() = {

scaladoc/resources/dotty_res/styles/scalastyle.css

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,10 +1066,9 @@ footer .socials {
10661066

10671067
/* Nav Icons */
10681068

1069-
.nav-chapters {
1070-
font-size: 2.5em;
1069+
.arrows {
1070+
font-size: 3em;
10711071
text-align: center;
1072-
text-decoration: none;
10731072

10741073
position: fixed;
10751074
top: 0;
@@ -1080,53 +1079,32 @@ footer .socials {
10801079

10811080
display: flex;
10821081
justify-content: center;
1083-
align-content: center;
10841082
flex-direction: column;
10851083

10861084
transition: color 0.5s, background-color 0.5s;
10871085
}
10881086

1089-
.nav-chapters.previous, .nav-chapters.next {
1087+
.arrows.previous, .arrows.next {
10901088
color: var(--grey400);
10911089
}
10921090

1093-
.nav-chapters:hover {
1091+
.arrows:hover {
10941092
text-decoration: none;
10951093
color: var(--grey300);
10961094
background-color: var(--grey900);
10971095
transition: background-color 0.15s, color 0.15s;
10981096
}
10991097

1100-
.nav-wrapper {
1101-
margin-top: 50px;
1102-
display: none;
1103-
}
1104-
1105-
.mobile-nav-chapters {
1106-
font-size: 2.5em;
1107-
text-align: center;
1108-
text-decoration: none;
1109-
width: 90px;
1110-
border-radius: 5px;
1111-
background-color: var(--sidebar-bg);
1112-
}
1113-
11141098
.previous {
1115-
float: left;
11161099
left: var(--side-width);
1100+
float: left;
11171101
}
11181102

11191103
.next {
1120-
float: right;
11211104
right: 0;
1105+
float: right;
11221106
}
11231107

1124-
@media only screen and (max-width: 1080px) {
1125-
.nav-wide-wrapper { display: none; }
1126-
.nav-wrapper { display: block; }
1127-
}
1128-
1129-
@media only screen and (max-width: 1380px) {
1130-
.sidebar-visible .nav-wide-wrapper { display: none; }
1131-
.sidebar-visible .nav-wrapper { display: block; }
1108+
@media screen and (max-width: 1000px) {
1109+
.arrows-wrapper { display: none; }
11321110
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
116116

117117
(siteContext.orphanedTemplates ++ actualIndexTemplate).map(templateToPage(_, siteContext))
118118

119+
val redirectPages: Seq[Page] = staticSite.map(siteContext => siteContext.redirectTemplates.map(templateToPage(_, siteContext))).get
120+
119121
/**
120122
* Here we have to retrive index pages from hidden pages and replace fake index pages in navigable page tree.
121123
*/
@@ -132,7 +134,7 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
132134

133135
val (newNavigablePage, pagesToRemove) = traversePages(navigablePage)
134136

135-
val all = newNavigablePage +: hiddenPages.filterNot(pagesToRemove.contains)
137+
val all = newNavigablePage +: (hiddenPages.filterNot(pagesToRemove.contains) ++ redirectPages)
136138
// We need to check for conflicts only if we have top-level member called blog or docs
137139
val hasPotentialConflict =
138140
rootPackage.members.exists(m => m.name.startsWith("docs") || m.name.startsWith("blog"))

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

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

74+
lazy val redirectTemplates: Seq[LoadedTemplate] = {
75+
def doFlatten(t: LoadedTemplate): Seq[LoadedTemplate] =
76+
t +: t.children.flatMap(doFlatten)
77+
val mainFiles = templates.flatMap(doFlatten)
78+
mainFiles.flatMap { loadedTemplate =>
79+
loadedTemplate.templateFile.settings.apply("page").asInstanceOf[Map[String, Object]].get("redirectFrom").map { case redirectFrom: String =>
80+
val fakeFile = new File(docsPath.toFile, redirectFrom)
81+
val redirectTo = fakeFile.toPath.getParent.relativize(loadedTemplate.file.toPath).toString.stripSuffix(".md") + ".html"
82+
LoadedTemplate(layouts("redirectFrom").copy(settings = layouts("redirectFrom").settings ++ Map("redirectTo" -> redirectTo)), List.empty, fakeFile)
83+
}
84+
}
85+
}
86+
7487
val docsPath = root.toPath.resolve("docs")
7588

7689
private def isValidTemplate(file: File): Boolean =

0 commit comments

Comments
 (0)