Skip to content

Commit bea5619

Browse files
authored
Merge pull request #11273 from lampepfl/scala3doc/social-links2
Add configurable social links to scala3doc
2 parents 3f1e20a + 1598da1 commit bea5619

18 files changed

+129
-8
lines changed

project/Build.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,9 @@ object Build {
16241624
"-skip-by-regex:.+\\.impl($|\\..+) " +
16251625
"-comment-syntax wiki -siteroot scaladoc/scala3-docs -project-logo scaladoc/scala3-docs/logo.svg " +
16261626
"-external-mappings:.*java.*::javadoc::https://docs.oracle.com/javase/8/docs/api/ " +
1627+
"-social-links:github::https://github.com/lampepfl/dotty," +
1628+
"gitter::https://gitter.im/scala/scala," +
1629+
"twitter::https://twitter.com/scala_lang " +
16271630
s"-source-links:$stdLibRoot=github://scala/scala/v${stdlibVersion(Bootstrapped)}#src/library " +
16281631
s"-doc-root-content $docRootFile"
16291632
))

scaladoc-js/src/Main.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package dotty.tools.scaladoc
22

33
object Main extends App {
44
Searchbar()
5+
SocialLinks()
56
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dotty.tools.scaladoc
2+
3+
import org.scalajs.dom._
4+
import org.scalajs.dom.ext._
5+
6+
class SocialLinks:
7+
def addIcon(elem: html.Element) =
8+
val img = document.createElement("img").asInstanceOf[html.Image]
9+
img.src = s"${Globals.pathToRoot}images/${elem.getAttribute("data-icon-path")}"
10+
elem.appendChild(img)
11+
12+
document.querySelectorAll(".social-icon").collect { case e: html.Element => e }.foreach(addIcon)

scaladoc/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,9 @@ Make sure all the tests pass (simply run `sbt test` to verify that).
167167
A documentation tool needs to access compiler information about the project - it
168168
needs to list all definitions, resolve them by name, and query their members.
169169
Tasty Reflect is the dedicated way in Scala 3 of accessing this information.
170+
171+
## Credits
172+
173+
- [Flatart](https://www.iconfinder.com/Flatart) - Gitter icon
174+
175+
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

scaladoc/resources/dotty_res/styles/scalastyle.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,34 @@ footer .pull-right {
661661
content: url("../images/given.svg")
662662
}
663663

664+
#leftColumn .socials {
665+
margin-left: 5%;
666+
margin-right: 5%;
667+
}
668+
669+
footer .socials {
670+
margin-left: 10px;
671+
margin-right: 10px;
672+
display: flex;
673+
align-items: center;
674+
}
675+
676+
.social-icon {
677+
padding-right: 5px;
678+
padding-left: 5px;
679+
}
664680

681+
.social-icon img {
682+
height: 20px;
683+
width: 20px;
684+
}
685+
686+
#generated-by {
687+
position: absolute;
688+
right: 10px;
689+
display: flex;
690+
align-items: center;
691+
}
665692

666693
/* Large Screens */
667694
@media(min-width: 1100px) {

scaladoc/src/dotty/tools/scaladoc/Scaladoc.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ object Scaladoc:
3838
sourceLinks: List[String] = Nil,
3939
revision: Option[String] = None,
4040
externalMappings: List[ExternalDocLink] = Nil,
41+
socialLinks: List[SocialLinks] = Nil,
4142
identifiersToSkip: List[String] = Nil,
4243
regexesToSkip: List[String] = Nil,
4344
rootDocPath: Option[String] = None

scaladoc/src/dotty/tools/scaladoc/ScaladocArgs.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ScaladocArgs extends SettingGroup with CommonScalaSettings:
3636
"Mapping between regexes matching classpath entries and external documentation. " +
3737
"'regex::[scaladoc|scaladoc|javadoc]::path' syntax is used")
3838

39+
val socialLinks: Setting[List[String]] =
40+
MultiStringSetting("-social-links", "social-links",
41+
"Links to social sites. '[github|twitter|gitter|discord]::link' syntax is used. " +
42+
"'custom::link::white_icon_name::black_icon_name' is also allowed, in this case icons must be present in 'images/'' directory.")
43+
3944
val deprecatedSkipPackages: Setting[List[String]] =
4045
MultiStringSetting("-skip-packages", "packages", "Deprecated, please use `-skip-by-id` or `-skip-by-regex`")
4146

@@ -49,7 +54,7 @@ class ScaladocArgs extends SettingGroup with CommonScalaSettings:
4954
StringSetting("-doc-root-content", "path", "The file from which the root package documentation should be imported.", "")
5055

5156
def scaladocSpecificSettings: Set[Setting[_]] =
52-
Set(sourceLinks, syntax, revision, externalDocumentationMappings, skipById, skipByRegex, deprecatedSkipPackages, docRootContent)
57+
Set(sourceLinks, syntax, revision, externalDocumentationMappings, socialLinks, skipById, skipByRegex, deprecatedSkipPackages, docRootContent)
5358

5459
object ScaladocArgs:
5560
def extract(args: List[String], rootCtx: CompilerContext):(Scaladoc.Args, CompilerContext) =
@@ -119,6 +124,14 @@ object ScaladocArgs:
119124
)
120125
)
121126

127+
val socialLinksParsed =
128+
socialLinks.get.flatMap { s =>
129+
SocialLinks.parse(s).fold(left => {
130+
report.warning(left)
131+
None
132+
},right => Some(right))
133+
}
134+
122135
unsupportedSettings.filter(s => s.get != s.default).foreach { s =>
123136
report.warning(s"Setting ${s.name} is currently not supported.")
124137
}
@@ -142,6 +155,7 @@ object ScaladocArgs:
142155
sourceLinks.get,
143156
revision.nonDefault,
144157
externalMappings,
158+
socialLinksParsed,
145159
skipById.get ++ deprecatedSkipPackages.get,
146160
skipByRegex.get,
147161
docRootContent.nonDefault
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dotty.tools.scaladoc
2+
3+
import java.nio.file.Path
4+
import java.nio.file.Paths
5+
import dotty.tools.dotc.core.Contexts.Context
6+
7+
enum SocialLinks(val url: String, val whiteIconName: String, val blackIconName: String):
8+
case Github(ghUrl: String) extends SocialLinks(ghUrl, "github-icon-white.png", "github-icon-black.png")
9+
case Twitter(tUrl: String) extends SocialLinks(tUrl, "twitter-icon-white.png", "twitter-icon-black.png")
10+
case Gitter(gUrl: String) extends SocialLinks(gUrl, "gitter-icon-white.png", "gitter-icon-black.png")
11+
case Discord(dUrl: String) extends SocialLinks(dUrl, "discord-icon-white.png", "discord-icon-black.png")
12+
case Custom(cUrl: String, cWhiteIconName: String, cBlackIconName: String) extends SocialLinks(cUrl, cWhiteIconName, cBlackIconName)
13+
14+
object SocialLinks:
15+
def parse(s: String): Either[String, SocialLinks] =
16+
val errorPrefix = s"Social links arg $s is invalid: "
17+
val splitted = s.split("::")
18+
splitted.head match {
19+
case "custom" if splitted.size == 4 => Right(Custom(splitted(1), splitted(2), splitted(3)))
20+
case "custom" => Left(errorPrefix + "For 'custom' arg expected three arguments: url, white icon name and black icon name")
21+
case "github" if splitted.size == 2 => Right(Github(splitted(1)))
22+
case "github" => Left(errorPrefix + "For 'github' arg expected one argument: url")
23+
case "twitter" if splitted.size == 2 => Right(Twitter(splitted(1)))
24+
case "twitter" => Left(errorPrefix + "For 'twitter' arg expected one argument: url")
25+
case "gitter" if splitted.size == 2 => Right(Gitter(splitted(1)))
26+
case "gitter" => Left(errorPrefix + "For 'gitter' arg expected one argument: url")
27+
case "discord" if splitted.size == 2 => Right(Discord(splitted(1)))
28+
case "discord" => Left(errorPrefix + "For 'discord' arg expected one argument: url")
29+
case _ => Left(errorPrefix)
30+
}

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ class HtmlRenderer(rootPackage: Member, val members: Map[DRI, Member])(using ctx
172172
)
173173
renderNested(navigablePage)._2
174174

175+
private def hasSocialLinks = !args.socialLinks.isEmpty
176+
177+
private def socialLinks(whiteIcon: Boolean = true) =
178+
val icon = (link: SocialLinks) => if whiteIcon then link.whiteIconName else link.blackIconName
179+
args.socialLinks.map { link =>
180+
a(href := link.url)(
181+
span(cls := s"social-icon", Attr("data-icon-path") := icon(link))
182+
)
183+
}
184+
175185
private def mkFrame(link: Link, parents: Vector[Link], content: => AppliedTag): AppliedTag =
176186
val projectLogo =
177187
args.projectLogo.map { path =>
@@ -195,6 +205,9 @@ class HtmlRenderer(rootPackage: Member, val members: Map[DRI, Member])(using ctx
195205
),
196206
span(
197207
args.projectVersion.map(v => div(cls:="projectVersion")(v)).toList
208+
),
209+
div(cls := "socials")(
210+
socialLinks()
198211
)
199212
),
200213
div(id := "paneSearch"),
@@ -220,14 +233,20 @@ class HtmlRenderer(rootPackage: Member, val members: Map[DRI, Member])(using ctx
220233
raw(" Back to top")
221234
)
222235
),
223-
raw("Generated by "),
224-
a(href := "https://github.com/lampepfl/dotty/tree/master/scaladoc")(
225-
img(
226-
src := resolveRoot(link.dri, "images/scaladoc_logo.svg"),
227-
alt := "scaladoc",
228-
cls := "scaladoc_logo"
236+
div(cls := "socials")(
237+
if hasSocialLinks then Seq(raw("Social links ")) else Nil,
238+
socialLinks(whiteIcon = false)
239+
),
240+
div(id := "generated-by")(
241+
raw("Generated by "),
242+
a(href := "https://github.com/lampepfl/dotty/tree/master/scaladoc")(
243+
img(
244+
src := resolveRoot(link.dri, "images/scaladoc_logo.svg"),
245+
alt := "scaladoc",
246+
cls := "scaladoc_logo"
247+
)
229248
)
230-
)
231249
)
232250
)
233251
)
252+
)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
138138
dottyRes("images/enum.svg"),
139139
dottyRes("images/enum_comp.svg"),
140140
dottyRes("images/given.svg"),
141+
dottyRes("images/github-icon-black.png"),
142+
dottyRes("images/github-icon-white.png"),
143+
dottyRes("images/discord-icon-black.png"),
144+
dottyRes("images/discord-icon-white.png"),
145+
dottyRes("images/twitter-icon-black.png"),
146+
dottyRes("images/twitter-icon-white.png"),
147+
dottyRes("images/gitter-icon-black.png"),
148+
dottyRes("images/gitter-icon-white.png"),
141149
searchData(pages)
142150
)
143151

0 commit comments

Comments
 (0)