Skip to content

Commit 1ad42c5

Browse files
committed
Process links from static documentaiton corrently
1 parent 7578a63 commit 1ad42c5

File tree

7 files changed

+37
-13
lines changed

7 files changed

+37
-13
lines changed

project/Build.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,7 @@ object Build {
14801480
"org.jetbrains.kotlinx" % "kotlinx-html-jvm" % "0.7.2", // Needs update when dokka version changes
14811481
"com.vladsch.flexmark" % "flexmark-all" % "0.42.12",
14821482
"nl.big-o" % "liqp" % "0.6.7",
1483+
"org.jsoup" % "jsoup" % "1.13.1", // Needed to process .html files for static site
14831484
"args4j" % "args4j" % "2.33",
14841485
Dependencies.`jackson-dataformat-yaml`,
14851486

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package site
44
import org.jetbrains.dokka.model.DisplaySourceSet
55
import org.jetbrains.dokka.model.properties.PropertyContainer
66
import org.jetbrains.dokka.pages.{ContentNode, DCI, Style}
7+
import org.jetbrains.dokka.base.resolvers.local.LocationProvider
8+
import com.vladsch.flexmark.convert.html.FlexmarkHtmlParser
9+
import org.jsoup.Jsoup
710

811
case class PartiallyRenderedContent(
912
template: TemplateFile,
@@ -23,3 +26,8 @@ case class PartiallyRenderedContent(
2326
copy(getSourceSets = sourceSets)
2427

2528
lazy val resolved = template.resolveToHtml(context)
29+
30+
def procsesHtml(linkTo: String => String): String =
31+
val document = Jsoup.parse(resolved.code)
32+
document.select("a").forEach(element => element.attr("href", linkTo(element.attr("href"))))// forrach does not work here
33+
document.outerHtml()

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import org.jetbrains.dokka.model.Documentable
1010
import org.jetbrains.dokka.pages._
1111
import org.jetbrains.dokka.transformers.pages.PageTransformer
1212

13-
case class LoadedTemplate(templateFile: TemplateFile, children: List[LoadedTemplate], file: File) {
14-
def relativePath(root: File): String =
15-
root.toPath().relativize(file.toPath()).toString().replace(File.separatorChar, '.')
16-
}
13+
case class LoadedTemplate(templateFile: TemplateFile, children: List[LoadedTemplate], file: File)
1714

1815
case class StaticPageNode(
1916
template: TemplateFile,

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package site
33

44
import java.io.File
55
import java.nio.file.Files
6-
6+
import java.nio.file.Path
7+
import java.nio.file.Paths
78

89
import org.jetbrains.dokka.base.parsers.MarkdownParser
910
import org.jetbrains.dokka.base.transformers.pages.comments.DocTagToContentConverter
@@ -15,14 +16,16 @@ import org.jetbrains.dokka.pages.{ContentKind, ContentNode, DCI, PageNode}
1516
import org.jetbrains.dokka.plugability.DokkaContext
1617
import org.jetbrains.dokka.pages.Style
1718
import org.jetbrains.dokka.model.DisplaySourceSet
19+
import util.Try
1820

1921
import scala.collection.JavaConverters._
2022

2123
class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
2224

2325
def indexPage():Option[StaticPageNode] =
2426
val files = List(new File(root, "index.html"), new File(root, "index.md")).filter { _.exists() }
25-
if (files.size > 1) println(s"ERROR: Multiple root index pages found: ${files.map(_.getAbsolutePath)}") // TODO (#14): provide proper error handling
27+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
28+
if (files.size > 1) println(s"ERROR: Multiple root index pages found: ${files.map(_.getAbsolutePath)}")
2629
files.flatMap(loadTemplate(_, isBlog = false)).headOption.map(templateToPage)
2730

2831
lazy val layouts: Map[String, TemplateFile] =
@@ -52,7 +55,8 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
5255
val allFiles = topLevelFiles.filter(_.isDirectory).flatMap(_.listFiles())
5356
val (indexes, children) = allFiles.flatMap(loadTemplate(_)).partition(_.templateFile.isIndexPage())
5457
if (indexes.size > 1)
55-
println(s"ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}") // TODO (#14): provide proper error handling
58+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
59+
println(s"ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}")
5660

5761
def loadIndexPage(): TemplateFile =
5862
val indexFiles = from.listFiles { file =>file.getName == "index.md" || file.getName == "index.html" }
@@ -68,7 +72,8 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
6872
Some(LoadedTemplate(templateFile, children.toList, from))
6973
catch
7074
case e: RuntimeException =>
71-
e.printStackTrace() // TODO (#14): provide proper error handling
75+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
76+
e.printStackTrace()
7277
None
7378

7479
def asContent(doctag: DocTag, dri: DRI) = new DocTagToContentConverter().buildContent(
@@ -95,9 +100,15 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
95100
dir("docs").flatMap(_.listFiles()).flatMap(loadTemplate(_, isBlog = false))
96101
++ dir("blog").flatMap(loadTemplate(_, isBlog = true))
97102

103+
def driForLink(template: TemplateFile, link: String): Try[DRI] = Try(driFor(
104+
if link.startsWith("/") then root.toPath.resolve(link.drop(1))
105+
else template.file.toPath.getParent().resolve(link)
106+
))
107+
108+
private def driFor(dest: Path): DRI = mkDRI(s"_.${root.toPath.relativize(dest)}")
109+
98110
def templateToPage(myTemplate: LoadedTemplate): StaticPageNode =
99-
def pathToDRI(path: String) = mkDRI(s"_.$path")
100-
val dri = pathToDRI(myTemplate.relativePath(root))
111+
val dri = driFor(myTemplate.file.toPath)
101112
val content = new PartiallyRenderedContent(
102113
myTemplate.templateFile,
103114
this,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class SitePagesCreator(ctx: Option[StaticSiteContext]) extends BaseStaticSitePro
9191
val (contentPage, others) = input.getChildren.asScala.toList.partition { _.isInstanceOf[ContentPage] }
9292
val modifiedModuleRoot = processRootPage(input, contentPage)
9393
val (indexes, children) = ctx.pages.partition(_.template.isIndexPage())
94-
// TODO (#14): provide proper error handling
94+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
9595
if (indexes.size > 1) println("ERROR: Multiple index pages found $indexes}")
9696

9797
val rootContent = indexes.headOption.fold(ctx.asContent(Text(), mkDRI(extra = "root_content")).get(0))(_.getContent)

scala3doc/src/dotty/renderers/DotDiagramBuilder.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dotty.dokka
33
import dotty.dokka.model._
44
import org.jetbrains.dokka.model._
55
import org.jetbrains.dokka.links.DRI
6-
import org.jetbrains.dokka.base.resolvers.local.LocationProvider
76
import org.jetbrains.dokka.pages._
87
import dotty.dokka.model.api.Kind
98
import HTML._

scala3doc/src/dotty/renderers/ScalaHtmlRenderer.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jetbrains.dokka._
88
import HTML._
99
import collection.JavaConverters._
1010
import java.net.URI
11+
import java.net.URL
1112
import java.util.{List => JList, Set => JSet}
1213
import kotlinx.html.FlowContent
1314
import kotlinx.html.stream.StreamKt
@@ -21,6 +22,7 @@ import dotty.dokka.model.api.HierarchyGraph
2122
import org.jetbrains.dokka.base.resolvers.local.LocationProvider
2223
import dotty.dokka.site.StaticPageNode
2324
import dotty.dokka.site.PartiallyRenderedContent
25+
import scala.util.Try
2426

2527
class SignatureRenderer(pageContext: ContentPage, sourceSetRestriciton: JSet[DisplaySourceSet], locationProvider: LocationProvider):
2628
def link(dri: DRI): Option[String] = Option(locationProvider.resolve(dri, sourceSetRestriciton, pageContext))
@@ -240,7 +242,13 @@ class ScalaHtmlRenderer(ctx: DokkaContext) extends HtmlRenderer(ctx) {
240242

241243
page.getContent match
242244
case prc: PartiallyRenderedContent =>
243-
withHtml(context, prc.resolved.code)
245+
def processLocalLink(str: String): String =
246+
// TODO (https://github.com/lampepfl/scala3doc/issues/238) error handling
247+
prc.context.driForLink(prc.template, str).toOption
248+
.flatMap(dri => Option(getLocationProvider.resolve(dri, sourceSets, page)))
249+
.getOrElse(str)
250+
251+
withHtml(context, prc.procsesHtml(url => Try(URL(url)).fold(_ => processLocalLink(url), _ => url)))
244252
case content =>
245253
build(content, context, page, /*sourceSetRestriction=*/null)
246254

0 commit comments

Comments
 (0)