@@ -9,6 +9,9 @@ import scala.jdk.CollectionConverters.*
9
9
import dotty .tools .scaladoc .translators .FilterAttributes
10
10
import org .jsoup .Jsoup
11
11
import translators .*
12
+ import java .net .URL
13
+ import scala .util .Try
14
+ import java .nio .file .{Files , Paths }
12
15
13
16
class MemberRenderer (signatureRenderer : SignatureRenderer )(using DocContext ) extends DocRender (signatureRenderer):
14
17
import signatureRenderer ._
@@ -100,6 +103,63 @@ class MemberRenderer(signatureRenderer: SignatureRenderer)(using DocContext) ext
100
103
def typeParams (m : Member ): Seq [AppliedTag ] = m.docs.fold(Nil )(d => flattenedDocPart(d.typeParams))
101
104
def valueParams (m : Member ): Seq [AppliedTag ] = m.docs.fold(Nil )(d => flattenedDocPart(d.valueParams))
102
105
106
+ def processLocalLinkWithGuard (str : String ): String =
107
+ if str.startsWith(" #" ) || str.isEmpty then
108
+ str
109
+ else
110
+ validationLink(str)
111
+
112
+ def validationLink (str : String ): String =
113
+ def asValidURL = Try (URL (str)).toOption.map(_ => str)
114
+
115
+ def asAsset =
116
+ Option .when(
117
+ Files .exists(Paths .get(" src/main/ressources" ).resolve(str.stripPrefix(" /" )))
118
+ )(
119
+ s " src/main/ressources/ $str"
120
+ )
121
+
122
+ def asStaticSite : Option [String ] =
123
+ Option .when(
124
+ Files .exists(Paths .get(" docs/_docs" ).resolve(str.stripPrefix(" /" )))
125
+ )(
126
+ s " docs/_docs/ $str"
127
+ )
128
+
129
+ def asApiLink : Option [String ] =
130
+ val strWithoutHtml = if str.endsWith(" $.html" ) then
131
+ str.stripSuffix(" $.html" )
132
+ else
133
+ str.stripSuffix(" .html" )
134
+ val sourceDir = Paths .get(" src" , " main" , " scala" )
135
+ val scalaPath = sourceDir.resolve(s " $strWithoutHtml.scala " )
136
+ val scalaDirPath = sourceDir.resolve(strWithoutHtml)
137
+ Option .when(
138
+ Files .exists(scalaPath) || Files .exists(scalaDirPath))
139
+ (
140
+ s " api/ $strWithoutHtml.html "
141
+ )
142
+
143
+
144
+ asValidURL
145
+ .orElse(asStaticSite)
146
+ .orElse(asAsset)
147
+ .orElse(asApiLink)
148
+ .getOrElse{
149
+ report.warning(s " Unable to resolve link ' $str' " )
150
+ str
151
+ }
152
+
153
+ // println(asValidURL)
154
+
155
+ // println(Paths.get("src/main/ressources").resolve(str.stripPrefix("/")).toAbsolutePath)
156
+ // println(Files.exists(Paths.get("src/main/ressources").resolve(str.stripPrefix("/"))))
157
+ // def asAsset = Option.when(
158
+ // Files.exists(Paths.get("docs/_assets").resolve(str.stripPrefix("/")))
159
+ // )(
160
+ // s"docs/_assets/$str"
161
+ // )
162
+
103
163
def memberInfo (m : Member , withBrief : Boolean = false , full : Boolean = false ): Seq [AppliedTag ] =
104
164
val comment = m.docs
105
165
val bodyContents = m.docs.fold(Nil )(e => renderDocPart(e.body) :: Nil )
@@ -122,6 +182,21 @@ class MemberRenderer(signatureRenderer: SignatureRenderer)(using DocContext) ext
122
182
case _ => true
123
183
}
124
184
185
+ val document = Jsoup .parse(bodyContents.mkString)
186
+ val document2 = Jsoup .parse(attributes.mkString)
187
+
188
+ document.select(" img" ).forEach(element =>
189
+ element.attr(" src" , validationLink(element.attr(" src" )))
190
+ )
191
+
192
+ document.select(" a" ).forEach(element =>
193
+ element.attr(" href" , processLocalLinkWithGuard(element.attr(" href" )))
194
+ )
195
+
196
+ // document2.select("a").forEach(element =>
197
+ // println("BONJOUR"+element.attr("href"))
198
+ // ) <--- Take some href that I don't want
199
+
125
200
Seq (
126
201
Option .when(withBrief && comment.flatMap(_.short).nonEmpty)(div(cls := " documentableBrief doc" )(comment.flatMap(_.short).fold(" " )(renderDocPart))),
127
202
Option .when(bodyContents.nonEmpty || attributes.nonEmpty)(
0 commit comments