Skip to content

Commit 98fd9cb

Browse files
authored
Support link to methods with bounded types (#130)
* Adds test exercising the issue * Test and document bounded types. * Bonus: document higher-order functions * Adds a suggestion to fix the error on the error message
1 parent 3a5b2ea commit 98fd9cb

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ produce an unambigious result, you will have to use the FQCN.
9191
* Scala: method - `akka/stream/scaladsl/Flow.html#method():Unit`
9292
* Java: method - `akka/stream/javadsl/Flow.html#method()`
9393

94+
* `@apidoc[method](Flow) { scala="#method[T]():Unit" java="#method()" }` (Link to method anchors.)
95+
* classes: `akka.stream.scaladsl.Flow` - `akka.stream.javadsl.Flow`
96+
* Scala: method - `akka/stream/scaladsl/Flow.html#method[T]():Unit`
97+
* Java: method - `akka/stream/javadsl/Flow.html#method()`
98+
99+
* `@apidoc[method](Flow) { scala="#method%5BT%3C:S]():Unit" java="#method()" }` (Link to method anchors. Where the method has type bounds.)
100+
* classes: `akka.stream.scaladsl.Flow` - `akka.stream.javadsl.Flow`
101+
* Scala: method - `akka/stream/scaladsl/Flow.html#method[T<:S]():Unit`
102+
* Java: method - `akka/stream/javadsl/Flow.html#method()`
103+
104+
* `@apidoc[method](Flow) { scala="#method(String=%3EInt):Unit" java="#method()" }` (Link to method anchors. Using higher-order function arguments)
105+
* classes: `akka.stream.scaladsl.Flow` - `akka.stream.javadsl.Flow`
106+
* Scala: method - `akka/stream/scaladsl/Flow.html#method(String=>Int):Unit`
107+
* Java: method - `akka/stream/javadsl/Flow.html#method()`
108+
94109

95110
### When only Scaladoc is generated
96111

src/main/scala/com/lightbend/paradox/apidoc/ApidocDirective.scala

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.lightbend.paradox.apidoc
1818

19+
import com.lightbend.paradox.ParadoxError
20+
import com.lightbend.paradox.ParadoxException
21+
import com.lightbend.paradox.markdown.{Url => ParadoxUrl}
1922
import com.lightbend.paradox.markdown.{InlineDirective, Writer}
2023
import org.pegdown.Printer
2124
import org.pegdown.ast.DirectiveNode.Source
@@ -149,6 +152,23 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], ctx: Writer.Cont
149152
node: DirectiveNode
150153
): DirectiveNode = {
151154
val attributes = new org.pegdown.ast.DirectiveAttributes.AttributeMap()
155+
val theUrl = fqcn + anchor
156+
try {
157+
ParadoxUrl(theUrl)
158+
} catch {
159+
case ParadoxUrl.Error(reason) =>
160+
val suggestedUrl = theUrl
161+
.replace("<", "%3C")
162+
.replace(">", "%3E")
163+
.replace("[", "%5B")
164+
throw new ParadoxException(
165+
ParadoxError(
166+
s"$reason. Try percent-encoding manually some of the reserved characters, for example: [$suggestedUrl]. See https://github.com/lightbend/sbt-paradox-apidoc/pull/130 for more details.",
167+
None,
168+
None
169+
)
170+
)
171+
}
152172
new DirectiveNode(
153173
DirectiveNode.Format.Inline,
154174
group,
@@ -160,7 +180,7 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], ctx: Writer.Cont
160180
DirectiveNode.Format.Inline,
161181
doctype + "doc",
162182
label,
163-
new DirectiveNode.Source.Direct(fqcn + anchor),
183+
new DirectiveNode.Source.Direct(theUrl),
164184
node.attributes,
165185
label, // contents
166186
null

src/test/scala/com/lightbend/paradox/apidoc/ApidocDirectiveSpec.scala

+29
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.lightbend.paradox.apidoc
1818

19+
import java.io.IOException
20+
1921
import com.lightbend.paradox.ParadoxException
2022
import com.lightbend.paradox.markdown.Writer
2123

@@ -284,4 +286,31 @@ class ApidocDirectiveSpec extends MarkdownBaseSpec {
284286
|thingie</p>""".stripMargin
285287
)
286288
}
289+
290+
it should "use anchors for methods with scala bounded types" in {
291+
markdown(
292+
"""The @apidoc[label](Flow) { scala="#method%5BT%3C:Q[T]](Flow=%3EUnit):Unit" java="#method()" } thingie"""
293+
) shouldEqual
294+
html(
295+
"""<p>The <span class="group-java">
296+
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/Flow.html#method()" title="akka.stream.javadsl.Flow"><code>label</code></a></span><span class="group-scala">
297+
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/Flow.html#method[T%3C:Q[T]](Flow=%3EUnit):Unit" title="akka.stream.scaladsl.Flow"><code>label</code></a></span>
298+
|thingie</p>""".stripMargin
299+
)
300+
}
301+
302+
it should "catch exception on malformed URIs and make suggestions" in {
303+
try {
304+
305+
markdown(
306+
"""The @apidoc[label](Flow) { scala="#method[ T <: Q[T] ](Flow => Unit):Unit" java="#method()" } thingie"""
307+
)
308+
} catch {
309+
case t @ ParadoxException(error) => {
310+
error.msg should include("template resulted in an invalid URL")
311+
error.msg should include("method%5B T %3C: Q%5BT] ](Flow =%3E Unit):Unit")
312+
}
313+
}
314+
}
315+
287316
}

0 commit comments

Comments
 (0)