in Sources/SourceKitLSP/Swift/CommentXML.swift [74:173]
mutating func toMarkdown(_ node: XMLElement) {
switch node.name {
case "Declaration":
newlineIfNeeded(count: 2)
out += "```swift\n"
toMarkdown(node.children)
out += "\n```\n\n---\n"
case "Name", "USR", "Direction":
break
case "Abstract", "Para":
if !inParam {
newlineIfNeeded(count: 2)
}
toMarkdown(node.children)
case "Discussion", "ResultDiscussion", "ThrowsDiscussion":
if !inParam {
newlineIfNeeded(count: 2)
}
out += "### "
switch node.name {
case "Discussion": out += "Discussion"
case "ResultDiscussion": out += "Returns"
case "ThrowsDiscussion": out += "Throws"
default: fatalError("handled in outer switch")
}
newline(count: 2)
toMarkdown(node.children)
case "Parameters":
newlineIfNeeded(count: 2)
out += "- Parameters:"
indentCount += 1
toMarkdown(node.children)
indentCount -= 1
case "Parameter":
guard let name = node.elements(forName: "Name").first else { break }
newlineIfNeeded()
out += "- "
toMarkdown(name.children)
if let discussion = node.elements(forName: "Discussion").first {
out += ": "
inParam = true
toMarkdown(discussion.children)
inParam = false
}
// FIXME: closure parameters would go here.
case "CodeListing":
lineNumber = 0
newlineIfNeeded(count: 2)
out += "```\(node.attributes?.first(where: { $0.name == "language" })?.stringValue ?? "")\n"
toMarkdown(node.children, separator: "\n")
out += "```"
case "zCodeLineNumbered":
lineNumber += 1
out += "\(lineNumber).\t"
toMarkdown(node.children)
case "codeVoice":
out += "`"
toMarkdown(node.children)
out += "`"
case "emphasis":
out += "*"
toMarkdown(node.children)
out += "*"
case "bold":
out += "**"
toMarkdown(node.children)
out += "**"
case "h1", "h2", "h3", "h4", "h5", "h6":
newlineIfNeeded(count: 2)
let n = Int(node.name!.dropFirst())
out += String(repeating: "#", count: n!)
out += " "
toMarkdown(node.children)
out += "\n\n"
case "Link":
if let href = node.attributes?.first(where: { $0.name == "href" })?.stringValue {
out += "["
toMarkdown(node.children)
out += "](\(href))"
} else {
// Not a valid link.
toMarkdown(node.children)
}
default:
toMarkdown(node.children)
}
}