@@ -18,6 +18,7 @@ package com.lightbend.paradox.apidoc
18
18
19
19
import com .lightbend .paradox .markdown .InlineDirective
20
20
import org .pegdown .Printer
21
+ import org .pegdown .ast .DirectiveNode .Source
21
22
import org .pegdown .ast .{DirectiveNode , Visitor }
22
23
23
24
class ApidocDirective (allClassesAndObjects : IndexedSeq [String ], properties : Map [String , String ])
@@ -29,9 +30,13 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
29
30
30
31
val allClasses = allClassesAndObjects.filterNot(_.endsWith(" $" ))
31
32
32
- private case class Query (pattern : String , generics : String , linkToObject : Boolean ) {
33
+ private case class Query (label : Option [ String ], pattern : String , generics : String , linkToObject : Boolean ) {
33
34
def scalaLabel (matched : String ): String =
34
- matched.split('.' ).last + generics
35
+ label match {
36
+ case None => matched.split('.' ).last + generics
37
+ case Some (la) => la + generics
38
+ }
39
+
35
40
def javaLabel (matched : String ): String =
36
41
scalaLabel(matched)
37
42
.replaceAll(" \\ [" , " <" )
@@ -44,19 +49,33 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
44
49
}
45
50
private object Query {
46
51
def apply (label : String ): Query = {
47
- val (pattern, generics) = label.indexOf('[' ) match {
48
- case - 1 => (label, " " )
49
- case n => label.replaceAll(" \\\\ _" , " _" ).splitAt(n)
50
- }
52
+ val (pattern, generics) = splitGenerics(label)
53
+ if (pattern.endsWith(" $" ))
54
+ Query (None , pattern.init, generics, linkToObject = true )
55
+ else
56
+ Query (None , pattern, generics, linkToObject = false )
57
+ }
58
+
59
+ def apply (label : String , pattern : String ): Query = {
60
+ val (labelPattern, generics) = splitGenerics(label)
51
61
if (pattern.endsWith(" $" ))
52
- Query (pattern.init, generics, linkToObject = true )
62
+ Query (Some (labelPattern), pattern.init, generics, linkToObject = true )
53
63
else
54
- Query (pattern, generics, linkToObject = false )
64
+ Query (Some (labelPattern), pattern, generics, linkToObject = false )
55
65
}
66
+
67
+ private def splitGenerics (label : String ): (String , String ) =
68
+ label.indexOf('[' ) match {
69
+ case - 1 => (label, " " )
70
+ case n => label.replaceAll(" \\\\ _" , " _" ).splitAt(n)
71
+ }
56
72
}
57
73
58
74
def render (node : DirectiveNode , visitor : Visitor , printer : Printer ): Unit = {
59
- val query = Query (node.label)
75
+ val query = node.source match {
76
+ case Source .Empty | _ : Source .Ref => Query (node.label)
77
+ case s : Source .Direct => Query (node.label, s.value)
78
+ }
60
79
if (query.pattern.contains('.' )) {
61
80
if (allClasses.contains(query.pattern)) {
62
81
renderMatches(query, Seq (query.pattern), node, visitor, printer)
@@ -84,6 +103,7 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
84
103
doctype : String ,
85
104
label : String ,
86
105
fqcn : String ,
106
+ anchor : String ,
87
107
node : DirectiveNode
88
108
): DirectiveNode = {
89
109
val attributes = new org.pegdown.ast.DirectiveAttributes .AttributeMap ()
@@ -98,7 +118,7 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
98
118
DirectiveNode .Format .Inline ,
99
119
doctype + " doc" ,
100
120
label,
101
- new DirectiveNode .Source .Direct (fqcn),
121
+ new DirectiveNode .Source .Direct (fqcn + anchor ),
102
122
node.attributes,
103
123
label, // contents
104
124
null
@@ -114,6 +134,8 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
114
134
printer : Printer
115
135
): Unit = {
116
136
val scalaClassSuffix = if (query.linkToObject) " $" else " "
137
+ val sAnchor = node.attributes.value(" scala" , " " )
138
+ val jAnchor = node.attributes.value(" java" , " " )
117
139
118
140
matches.size match {
119
141
case 0 =>
@@ -122,20 +144,22 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
122
144
throw new java.lang.IllegalStateException (s " Match for $query only found in one language: ${matches(0 )}" )
123
145
case 1 =>
124
146
val pkg = matches(0 )
125
- syntheticNode(" scala" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, node).accept(visitor)
126
- if (hasJavadocUrl(pkg))
127
- syntheticNode(" java" , " java" , query.javaLabel(pkg), pkg, node).accept(visitor)
128
- else
129
- syntheticNode(" java" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, node).accept(visitor)
147
+ syntheticNode(" scala" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, sAnchor, node).accept(visitor)
148
+ if (hasJavadocUrl(pkg)) {
149
+ syntheticNode(" java" , " java" , query.javaLabel(pkg), pkg, jAnchor, node).accept(visitor)
150
+ } else
151
+ syntheticNode(" java" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, jAnchor, node).accept(visitor)
130
152
case 2 if matches.forall(_.contains(" adsl" )) =>
131
153
matches.foreach(pkg => {
132
154
if (! pkg.contains(" javadsl" ))
133
- syntheticNode(" scala" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, node).accept(visitor)
155
+ syntheticNode(" scala" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, sAnchor, node)
156
+ .accept(visitor)
134
157
if (! pkg.contains(" scaladsl" )) {
135
158
if (hasJavadocUrl(pkg))
136
- syntheticNode(" java" , " java" , query.javaLabel(pkg), pkg, node).accept(visitor)
159
+ syntheticNode(" java" , " java" , query.javaLabel(pkg), pkg, jAnchor, node).accept(visitor)
137
160
else
138
- syntheticNode(" java" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, node).accept(visitor)
161
+ syntheticNode(" java" , " scala" , query.scalaLabel(pkg), pkg + scalaClassSuffix, jAnchor, node)
162
+ .accept(visitor)
139
163
}
140
164
})
141
165
case n =>
0 commit comments