Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 1 | package org.jetbrains.dokka.Formats |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 2 | |
Simon Ogorodnik | 7acc52b | 2017-12-02 00:50:08 +0300 | [diff] [blame] | 3 | import com.google.inject.Binder |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 4 | import com.google.inject.Inject |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 5 | import com.google.inject.name.Named |
| 6 | import kotlinx.html.* |
| 7 | import kotlinx.html.Entities.nbsp |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 8 | import kotlinx.html.stream.appendHTML |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 9 | import org.jetbrains.dokka.* |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 10 | import org.jetbrains.dokka.LanguageService.RenderMode.FULL |
| 11 | import org.jetbrains.dokka.LanguageService.RenderMode.SUMMARY |
| 12 | import org.jetbrains.dokka.NodeKind.Companion.classLike |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 13 | import org.jetbrains.dokka.NodeKind.Companion.memberLike |
Simon Ogorodnik | 7acc52b | 2017-12-02 00:50:08 +0300 | [diff] [blame] | 14 | import org.jetbrains.dokka.Utilities.bind |
Simon Ogorodnik | 21a5b3e | 2017-12-11 16:08:58 +0300 | [diff] [blame] | 15 | import org.jetbrains.dokka.Utilities.lazyBind |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 16 | import org.jetbrains.dokka.Utilities.toOptional |
Simon Ogorodnik | 7acc52b | 2017-12-02 00:50:08 +0300 | [diff] [blame] | 17 | import org.jetbrains.dokka.Utilities.toType |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 18 | import org.jetbrains.kotlin.preprocessor.mkdirsOrFail |
Simon Ogorodnik | da153a8 | 2017-12-11 21:42:23 +0300 | [diff] [blame] | 19 | import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 20 | import java.io.BufferedWriter |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 21 | import java.io.File |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 22 | import java.net.URI |
| 23 | import java.net.URLEncoder |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 24 | import kotlin.reflect.KClass |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 25 | |
| 26 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 27 | abstract class JavaLayoutHtmlFormatDescriptorBase : FormatDescriptor, DefaultAnalysisComponent { |
Simon Ogorodnik | 7acc52b | 2017-12-02 00:50:08 +0300 | [diff] [blame] | 28 | |
| 29 | override fun configureOutput(binder: Binder): Unit = with(binder) { |
| 30 | bind<Generator>() toType generatorServiceClass |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 31 | bind<LanguageService>() toType languageServiceClass |
| 32 | bind<JavaLayoutHtmlTemplateService>() toType templateServiceClass |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 33 | bind<JavaLayoutHtmlUriProvider>() toType generatorServiceClass |
Simon Ogorodnik | 21a5b3e | 2017-12-11 16:08:58 +0300 | [diff] [blame] | 34 | lazyBind<JavaLayoutHtmlFormatOutlineFactoryService>() toOptional outlineFactoryClass |
Simon Ogorodnik | 7acc52b | 2017-12-02 00:50:08 +0300 | [diff] [blame] | 35 | } |
| 36 | |
Simon Ogorodnik | 7acc52b | 2017-12-02 00:50:08 +0300 | [diff] [blame] | 37 | val generatorServiceClass = JavaLayoutHtmlFormatGenerator::class |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 38 | abstract val languageServiceClass: KClass<out LanguageService> |
| 39 | abstract val templateServiceClass: KClass<out JavaLayoutHtmlTemplateService> |
| 40 | abstract val outlineFactoryClass: KClass<out JavaLayoutHtmlFormatOutlineFactoryService>? |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 41 | } |
| 42 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 43 | class JavaLayoutHtmlFormatDescriptor : JavaLayoutHtmlFormatDescriptorBase(), DefaultAnalysisComponentServices by KotlinAsKotlin { |
| 44 | override val languageServiceClass = KotlinLanguageService::class |
| 45 | override val templateServiceClass = JavaLayoutHtmlTemplateService.Default::class |
| 46 | override val outlineFactoryClass = null |
| 47 | } |
| 48 | |
| 49 | |
| 50 | interface JavaLayoutHtmlTemplateService { |
| 51 | fun composePage( |
| 52 | nodes: List<DocumentationNode>, |
| 53 | tagConsumer: TagConsumer<Appendable>, |
| 54 | headContent: HEAD.() -> Unit, |
| 55 | bodyContent: BODY.() -> Unit |
| 56 | ) |
| 57 | |
| 58 | class Default : JavaLayoutHtmlTemplateService { |
| 59 | override fun composePage( |
| 60 | nodes: List<DocumentationNode>, |
| 61 | tagConsumer: TagConsumer<Appendable>, |
| 62 | headContent: HEAD.() -> Unit, |
| 63 | bodyContent: BODY.() -> Unit |
| 64 | ) { |
| 65 | tagConsumer.html { |
| 66 | head(headContent) |
| 67 | body(block = bodyContent) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | interface JavaLayoutHtmlFormatOutlineFactoryService { |
| 74 | fun generateOutlines(outputProvider: (URI) -> Appendable, nodes: Iterable<DocumentationNode>) |
| 75 | } |
| 76 | |
| 77 | class JavaLayoutHtmlFormatOutputBuilder( |
| 78 | val output: Appendable, |
| 79 | val languageService: LanguageService, |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 80 | val uriProvider: JavaLayoutHtmlUriProvider, |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 81 | val templateService: JavaLayoutHtmlTemplateService, |
| 82 | val uri: URI |
| 83 | ) { |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 84 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 85 | val htmlConsumer = output.appendHTML() |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 86 | |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 87 | val contentToHtmlBuilder = ContentToHtmlBuilder(uriProvider, uri) |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 88 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 89 | private fun FlowContent.summaryNodeGroup(nodes: Iterable<DocumentationNode>, header: String, headerAsRow: Boolean = false, row: TBODY.(DocumentationNode) -> Unit) { |
| 90 | if (nodes.none()) return |
Simon Ogorodnik | 6362f27 | 2017-12-06 20:18:47 +0300 | [diff] [blame] | 91 | if (!headerAsRow) { |
| 92 | h2 { +header } |
Simon Ogorodnik | 6362f27 | 2017-12-06 20:18:47 +0300 | [diff] [blame] | 93 | } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 94 | table { |
| 95 | if (headerAsRow) thead { tr { td { h3 { +header } } } } |
| 96 | tbody { |
| 97 | nodes.forEach { node -> |
| 98 | row(node) |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 104 | fun FlowContent.metaMarkup(content: ContentNode) = with(contentToHtmlBuilder) { |
| 105 | appendContent(content) |
| 106 | } |
| 107 | |
| 108 | fun FlowContent.metaMarkup(content: List<ContentNode>) = with(contentToHtmlBuilder) { |
| 109 | appendContent(content) |
| 110 | } |
| 111 | |
| 112 | private fun TBODY.formatClassLikeRow(node: DocumentationNode) = tr { |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 113 | td { a(href = uriProvider.linkTo(node, uri)) { +node.simpleName() } } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 114 | td { metaMarkup(node.summary) } |
| 115 | } |
| 116 | |
| 117 | private fun TBODY.formatFunctionSummaryRow(node: DocumentationNode) = tr { |
| 118 | td { |
| 119 | for (modifier in node.details(NodeKind.Modifier)) { |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 120 | renderedSignature(modifier, SUMMARY) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 121 | } |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 122 | renderedSignature(node.detail(NodeKind.Type), SUMMARY) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 123 | } |
| 124 | td { |
| 125 | div { |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 126 | a(href = uriProvider.linkTo(node, uri)) { +node.name } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | metaMarkup(node.summary) |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 133 | private fun FlowContent.renderedSignature(node: DocumentationNode, mode: LanguageService.RenderMode = SUMMARY) { |
| 134 | metaMarkup(languageService.render(node, mode)) |
| 135 | } |
| 136 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 137 | private fun FlowContent.fullFunctionDocs(node: DocumentationNode) { |
| 138 | div { |
Simon Ogorodnik | 9b1ccf7 | 2017-12-11 21:43:03 +0300 | [diff] [blame^] | 139 | id = node.signature() |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 140 | h3 { +node.name } |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 141 | pre { renderedSignature(node, FULL) } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 142 | metaMarkup(node.content) |
| 143 | for ((name, sections) in node.content.sections.groupBy { it.tag }) { |
| 144 | table { |
| 145 | thead { tr { td { h3 { +name } } } } |
| 146 | tbody { |
| 147 | sections.forEach { |
| 148 | tr { |
| 149 | td { it.subjectName?.let { +it } } |
| 150 | td { |
| 151 | metaMarkup(it.children) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 159 | } |
| 160 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 161 | fun appendPackage(node: DocumentationNode) = templateService.composePage( |
| 162 | listOf(node), |
| 163 | htmlConsumer, |
| 164 | headContent = { |
| 165 | |
| 166 | }, |
| 167 | bodyContent = { |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 168 | h1 { +node.name } |
| 169 | metaMarkup(node.content) |
| 170 | summaryNodeGroup(node.members(NodeKind.Class), "Classes") { formatClassLikeRow(it) } |
| 171 | summaryNodeGroup(node.members(NodeKind.Exception), "Exceptions") { formatClassLikeRow(it) } |
| 172 | summaryNodeGroup(node.members(NodeKind.TypeAlias), "Type-aliases") { formatClassLikeRow(it) } |
| 173 | summaryNodeGroup(node.members(NodeKind.AnnotationClass), "Annotations") { formatClassLikeRow(it) } |
| 174 | summaryNodeGroup(node.members(NodeKind.Enum), "Enums") { formatClassLikeRow(it) } |
| 175 | |
| 176 | summaryNodeGroup(node.members(NodeKind.Function), "Top-level functions summary") { formatFunctionSummaryRow(it) } |
| 177 | |
| 178 | |
| 179 | h2 { +"Top-level functions" } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 180 | for (function in node.members(NodeKind.Function)) { |
| 181 | fullFunctionDocs(function) |
| 182 | } |
| 183 | } |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 184 | ) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 185 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 186 | |
| 187 | fun appendClassLike(node: DocumentationNode) = templateService.composePage( |
| 188 | listOf(node), |
| 189 | htmlConsumer, |
| 190 | headContent = { |
| 191 | |
| 192 | }, |
| 193 | bodyContent = { |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 194 | h1 { +node.name } |
| 195 | pre { renderedSignature(node, FULL) } |
| 196 | metaMarkup(node.content) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 197 | |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 198 | h2 { +"Summary" } |
Simon Ogorodnik | 6362f27 | 2017-12-06 20:18:47 +0300 | [diff] [blame] | 199 | val functionsToDisplay = node.members(NodeKind.Function) + node.members(NodeKind.CompanionObjectFunction) |
Simon Ogorodnik | 6362f27 | 2017-12-06 20:18:47 +0300 | [diff] [blame] | 200 | summaryNodeGroup(functionsToDisplay, "Functions", headerAsRow = true) { formatFunctionSummaryRow(it) } |
| 201 | |
| 202 | h2 { +"Functions" } |
Simon Ogorodnik | 6362f27 | 2017-12-06 20:18:47 +0300 | [diff] [blame] | 203 | for (function in functionsToDisplay) { |
| 204 | fullFunctionDocs(function) |
| 205 | } |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 206 | } |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 207 | ) |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 208 | } |
| 209 | |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 210 | class ContentToHtmlBuilder(val uriProvider: JavaLayoutHtmlUriProvider, val uri: URI) { |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 211 | fun FlowContent.appendContent(content: List<ContentNode>): Unit = content.forEach { appendContent(it) } |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 212 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 213 | private fun FlowContent.hN(level: Int, classes: String? = null, block: CommonAttributeGroupFacadeFlowHeadingPhrasingContent.() -> Unit) { |
| 214 | when (level) { |
| 215 | 1 -> h1(classes, block) |
| 216 | 2 -> h2(classes, block) |
| 217 | 3 -> h3(classes, block) |
| 218 | 4 -> h4(classes, block) |
| 219 | 5 -> h5(classes, block) |
| 220 | 6 -> h6(classes, block) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | fun FlowContent.appendContent(content: ContentNode) { |
| 225 | when (content) { |
| 226 | is ContentText -> +content.text |
| 227 | is ContentSymbol -> span("symbol") { +content.text } |
| 228 | is ContentKeyword -> span("keyword") { +content.text } |
| 229 | is ContentIdentifier -> span("identifier") { |
| 230 | content.signature?.let { id = it } |
| 231 | +content.text |
| 232 | } |
| 233 | |
| 234 | is ContentHeading -> hN(level = content.level) { appendContent(content.children) } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 235 | |
| 236 | is ContentEntity -> +content.text |
| 237 | |
| 238 | is ContentStrong -> strong { appendContent(content.children) } |
| 239 | is ContentStrikethrough -> del { appendContent(content.children) } |
| 240 | is ContentEmphasis -> em { appendContent(content.children) } |
| 241 | |
| 242 | is ContentOrderedList -> ol { appendContent(content.children) } |
| 243 | is ContentUnorderedList -> ul { appendContent(content.children) } |
| 244 | is ContentListItem -> consumer.li { |
| 245 | (content.children.singleOrNull() as? ContentParagraph) |
| 246 | ?.let { paragraph -> appendContent(paragraph.children) } |
| 247 | ?: appendContent(content.children) |
| 248 | } |
| 249 | |
| 250 | |
| 251 | is ContentCode -> pre { code { appendContent(content.children) } } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 252 | is ContentBlockSampleCode -> pre { code {} } |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 253 | is ContentBlockCode -> pre { code {} } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 254 | |
| 255 | |
| 256 | is ContentNonBreakingSpace -> +nbsp |
| 257 | is ContentSoftLineBreak, is ContentIndentedSoftLineBreak -> { |
| 258 | } |
| 259 | |
| 260 | is ContentParagraph -> p { appendContent(content.children) } |
| 261 | |
| 262 | is ContentNodeLink -> { |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 263 | a(href = uriProvider.linkTo(content.node!!, uri)) { appendContent(content.children) } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 264 | } |
| 265 | is ContentExternalLink -> { |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 266 | a(href = content.href) { appendContent(content.children) } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 267 | } |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 268 | |
| 269 | is ContentBlock -> appendContent(content.children) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 274 | |
| 275 | interface JavaLayoutHtmlUriProvider { |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 276 | fun tryGetContainerUri(node: DocumentationNode): URI? |
| 277 | fun tryGetMainUri(node: DocumentationNode): URI? |
| 278 | fun containerUri(node: DocumentationNode): URI = tryGetContainerUri(node) ?: error("Unsupported ${node.kind}") |
| 279 | fun mainUri(node: DocumentationNode): URI = tryGetMainUri(node) ?: error("Unsupported ${node.kind}") |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 280 | |
| 281 | fun linkTo(to: DocumentationNode, from: URI): String { |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 282 | return mainUri(to).relativeTo(from).toString() |
| 283 | } |
| 284 | |
| 285 | fun mainUriOrWarn(node: DocumentationNode): URI? = tryGetMainUri(node) ?: (null).also { |
| 286 | AssertionError("Not implemented mainUri for ${node.kind}").printStackTrace() |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 290 | class JavaLayoutHtmlFormatGenerator @Inject constructor( |
| 291 | @Named("outputDir") val root: File, |
| 292 | val languageService: LanguageService, |
Simon Ogorodnik | da153a8 | 2017-12-11 21:42:23 +0300 | [diff] [blame] | 293 | val templateService: JavaLayoutHtmlTemplateService, |
| 294 | val logger: DokkaLogger |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 295 | ) : Generator, JavaLayoutHtmlUriProvider { |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 296 | |
Simon Ogorodnik | 21a5b3e | 2017-12-11 16:08:58 +0300 | [diff] [blame] | 297 | @set:Inject(optional = true) var outlineFactoryService: JavaLayoutHtmlFormatOutlineFactoryService? = null |
| 298 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 299 | fun createOutputBuilderForNode(node: DocumentationNode, output: Appendable) |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 300 | = JavaLayoutHtmlFormatOutputBuilder(output, languageService, this, templateService, mainUri(node)) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 301 | |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 302 | override fun tryGetContainerUri(node: DocumentationNode): URI? { |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 303 | return when (node.kind) { |
Simon Ogorodnik | 92db044 | 2017-12-07 19:26:29 +0300 | [diff] [blame] | 304 | NodeKind.Module -> URI("/").resolve(node.name + "/") |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 305 | NodeKind.Package -> tryGetContainerUri(node.owner!!)?.resolve(node.name.replace('.', '/') + '/') |
| 306 | in classLike -> tryGetContainerUri(node.owner!!)?.resolve("${node.name}.html") |
| 307 | else -> null |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 311 | override fun tryGetMainUri(node: DocumentationNode): URI? { |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 312 | return when (node.kind) { |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 313 | NodeKind.Package -> tryGetContainerUri(node)?.resolve("package-summary.html") |
Simon Ogorodnik | c08417e | 2017-12-07 22:12:02 +0300 | [diff] [blame] | 314 | in classLike -> tryGetContainerUri(node)?.resolve("#") |
Simon Ogorodnik | da153a8 | 2017-12-11 21:42:23 +0300 | [diff] [blame] | 315 | in memberLike -> tryGetMainUri(node.owner!!)?.resolveInPage(node) |
| 316 | NodeKind.TypeParameter -> node.path.firstNotNullResult(this::tryGetContainerUri)?.also { logger.warn("Not implemented mainUri for $node") } |
Simon Ogorodnik | fe71c9e | 2017-12-07 20:00:19 +0300 | [diff] [blame] | 317 | NodeKind.AllTypes -> tryGetContainerUri(node.owner!!)?.resolve("allclasses.html") |
| 318 | else -> null |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 322 | fun buildClass(node: DocumentationNode, parentDir: File) { |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 323 | val fileForClass = parentDir.resolve(node.simpleName() + ".html") |
| 324 | fileForClass.bufferedWriter().use { |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 325 | createOutputBuilderForNode(node, it).appendClassLike(node) |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 326 | } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | fun buildPackage(node: DocumentationNode, parentDir: File) { |
| 330 | assert(node.kind == NodeKind.Package) |
| 331 | val members = node.members |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 332 | val directoryForPackage = parentDir.resolve(node.name.replace('.', File.separatorChar)) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 333 | directoryForPackage.mkdirsOrFail() |
| 334 | |
| 335 | directoryForPackage.resolve("package-summary.html").bufferedWriter().use { |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 336 | createOutputBuilderForNode(node, it).appendPackage(node) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 337 | } |
| 338 | |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 339 | members.filter { it.kind in classLike }.forEach { |
| 340 | buildClass(it, directoryForPackage) |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | |
| 345 | override fun buildPages(nodes: Iterable<DocumentationNode>) { |
| 346 | val module = nodes.single() |
| 347 | |
Simon Ogorodnik | 6dfe582 | 2017-12-06 20:05:31 +0300 | [diff] [blame] | 348 | val moduleRoot = root.resolve(module.name) |
| 349 | module.members.filter { it.kind == NodeKind.Package }.forEach { buildPackage(it, moduleRoot) } |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | override fun buildOutlines(nodes: Iterable<DocumentationNode>) { |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 353 | val uriToWriter = mutableMapOf<URI, BufferedWriter>() |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 354 | |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 355 | fun provideOutput(uri: URI): BufferedWriter { |
| 356 | val normalized = uri.normalize() |
| 357 | uriToWriter[normalized]?.let { return it } |
| 358 | val file = root.resolve(normalized.path.removePrefix("/")) |
| 359 | val writer = file.bufferedWriter() |
| 360 | uriToWriter[normalized] = writer |
| 361 | return writer |
| 362 | } |
| 363 | |
Simon Ogorodnik | 21a5b3e | 2017-12-11 16:08:58 +0300 | [diff] [blame] | 364 | outlineFactoryService?.generateOutlines(::provideOutput, nodes) |
Simon Ogorodnik | 1aa31e8 | 2017-12-07 14:58:45 +0300 | [diff] [blame] | 365 | |
| 366 | uriToWriter.values.forEach { it.close() } |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 367 | } |
| 368 | |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 369 | override fun buildSupportFiles() {} |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 370 | |
| 371 | override fun buildPackageList(nodes: Iterable<DocumentationNode>) { |
Simon Ogorodnik | 1b722f7 | 2017-11-30 20:34:25 +0300 | [diff] [blame] | 372 | |
Simon Ogorodnik | f3db3f3 | 2017-11-30 19:04:11 +0300 | [diff] [blame] | 373 | } |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 374 | } |
| 375 | |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 376 | fun DocumentationNode.signature() = detail(NodeKind.Signature).name |
| 377 | fun DocumentationNode.signatureUrlEncoded() = URLEncoder.encode(detail(NodeKind.Signature).name, "UTF-8") |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 378 | |
Simon Ogorodnik | 21465d8 | 2017-12-06 18:54:54 +0300 | [diff] [blame] | 379 | |
Simon Ogorodnik | bf22e82 | 2017-12-11 21:41:27 +0300 | [diff] [blame] | 380 | fun URI.relativeTo(uri: URI): URI { |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 381 | // Normalize paths to remove . and .. segments |
Simon Ogorodnik | bf22e82 | 2017-12-11 21:41:27 +0300 | [diff] [blame] | 382 | val base = uri.normalize() |
| 383 | val child = this.normalize() |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 384 | |
| 385 | // Split paths into segments |
| 386 | var bParts = base.path.split('/').dropLastWhile { it.isEmpty() } |
| 387 | val cParts = child.path.split('/').dropLastWhile { it.isEmpty() } |
| 388 | |
| 389 | // Discard trailing segment of base path |
| 390 | if (bParts.isNotEmpty() && !base.path.endsWith("/")) { |
| 391 | bParts = bParts.dropLast(1) |
| 392 | } |
| 393 | |
Simon Ogorodnik | bf22e82 | 2017-12-11 21:41:27 +0300 | [diff] [blame] | 394 | // Compute common prefix |
| 395 | val commonPartsSize = bParts.zip(cParts).count { (basePart, childPart) -> basePart == childPart } |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 396 | |
Simon Ogorodnik | bf22e82 | 2017-12-11 21:41:27 +0300 | [diff] [blame] | 397 | return URI.create(buildString { |
| 398 | bParts.drop(commonPartsSize).joinTo(this, separator = "") { "../" } |
| 399 | cParts.drop(commonPartsSize).joinTo(this, separator = "/") |
| 400 | child.rawQuery?.let { |
| 401 | append("?") |
| 402 | append(it) |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 403 | } |
Simon Ogorodnik | bf22e82 | 2017-12-11 21:41:27 +0300 | [diff] [blame] | 404 | child.rawFragment?.let { |
| 405 | append("#") |
| 406 | append(it) |
| 407 | } |
| 408 | }) |
| 409 | } |
Simon Ogorodnik | 8ebfdda | 2017-12-07 00:13:40 +0300 | [diff] [blame] | 410 | |
Simon Ogorodnik | da153a8 | 2017-12-11 21:42:23 +0300 | [diff] [blame] | 411 | fun URI.resolveInPage(node: DocumentationNode): URI = resolve("#${node.signatureUrlEncoded()}") |