@@ -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,10 +103,78 @@ 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
+ println(Paths .get(" src/main/ressources" ).resolve(str.stripPrefix(" /" )).toAbsolutePath)
117
+ println(Files .exists(Paths .get(" src/main/ressources" ).resolve(str.stripPrefix(" /" ))))
118
+ Option .when(
119
+ Files .exists(Paths .get(" src/main/ressources" ).resolve(str.stripPrefix(" /" )))
120
+ )(
121
+ s " src/main/ressources/ $str"
122
+ )
123
+
124
+ def asStaticSite : Option [String ] =
125
+ Option .when(
126
+ Files .exists(Paths .get(" docs/_docs" ).resolve(str.stripPrefix(" /" )))
127
+ )(
128
+ s " docs/_docs/ $str"
129
+ )
130
+
131
+ def asApiLink : Option [String ] =
132
+ val strWithoutHtml = if str.endsWith(" $.html" ) then
133
+ str.stripSuffix(" $.html" )
134
+ else
135
+ str.stripSuffix(" .html" )
136
+ val sourceDir = Paths .get(" src" , " main" , " scala" )
137
+ val scalaPath = sourceDir.resolve(s " $strWithoutHtml.scala " )
138
+ val scalaDirPath = sourceDir.resolve(strWithoutHtml)
139
+ Option .when(
140
+ Files .exists(scalaPath) || Files .exists(scalaDirPath))
141
+ (
142
+ s " api/ $strWithoutHtml.html "
143
+ )
144
+
145
+
146
+ asValidURL
147
+ .orElse(asStaticSite)
148
+ .orElse(asAsset)
149
+ .orElse(asApiLink)
150
+ .getOrElse{
151
+ report.warning(s " Unable to resolve link ' $str' " )
152
+ str
153
+ }
154
+
155
+ // println(asValidURL)
156
+
157
+ // println(Paths.get("src/main/ressources").resolve(str.stripPrefix("/")).toAbsolutePath)
158
+ // println(Files.exists(Paths.get("src/main/ressources").resolve(str.stripPrefix("/"))))
159
+ // def asAsset = Option.when(
160
+ // Files.exists(Paths.get("docs/_assets").resolve(str.stripPrefix("/")))
161
+ // )(
162
+ // s"docs/_assets/$str"
163
+ // )
164
+
103
165
def memberInfo (m : Member , withBrief : Boolean = false , full : Boolean = false ): Seq [AppliedTag ] =
104
166
val comment = m.docs
105
167
val bodyContents = m.docs.fold(Nil )(e => renderDocPart(e.body) :: Nil )
106
168
169
+ val document = Jsoup .parse(bodyContents.mkString)
170
+ document.select(" a" ).forEach(element =>
171
+ element.attr(" href" , processLocalLinkWithGuard(element.attr(" href" )))
172
+ )
173
+
174
+ document.select(" img" ).forEach { element =>
175
+ element.attr(" src" , validationLink(element.attr(" src" )))
176
+ }
177
+
107
178
val classLikeInfo : TagArg = classLikeParts(m, full)
108
179
109
180
val memberTypeParams = typeParams(m)
0 commit comments