package dotty.tools
package dottydoc
package staticsite
import com.vladsch.flexmark.ast._
import com.vladsch.flexmark.util.sequence.CharSubSequence
import model.{ Package, NonEntity, Val, Def, TypeAlias }
import dottydoc.util.MemberLookup
object MarkdownLinkVisitor {
private val EntityLink = """([^\.]+)(\.[^\.]+)*""".r
def apply(node: Node, docs: Map[String, Package], params: Map[String, AnyRef]): Unit =
(new NodeVisitor(
new VisitHandler(classOf[Link], new Visitor[Link] with MemberLookup {
override def visit(node: Link): Unit = {
val url = node.getUrl
if (EntityLink.unapplySeq(url.toString).isDefined) {
lookup(NonEntity, docs, url.toString).foreach { ent =>
val (path, suffix) = ent match {
case ent: Val => (ent.path.dropRight(1), ".html#" + ent.signature)
case ent: Def => (ent.path.dropRight(1), ".html#" + ent.signature)
case ent: TypeAlias => (ent.path.dropRight(1), ".html#" + ent.signature)
case ent: Package => (ent.path, "/index.html")
case ent => (ent.path, ".html")
}
params("site") match {
case map: java.util.Map[String, String] @unchecked => node.setUrl {
CharSubSequence.of(path.mkString(map.get("baseurl") + "/api/", "/", suffix))
}
case _ => ()
}
}
}
else if (url.endsWith(".md")) node.setUrl {
url.subSequence(0, url.lastIndexOf('.')).append(".html")
}
}
})
))
.visit(node)
}