aboutsummaryrefslogtreecommitdiff
path: root/src/Generation/FileGenerator.kt
blob: 40645b516a61762e32fcae386e111a44773565a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.jetbrains.dokka

import java.io.FileOutputStream
import java.io.OutputStreamWriter

public class FileGenerator(val signatureGenerator: LanguageService,
                           val locationService: FileLocationService,
                           val formatService: FormatService,
                           val outlineService: OutlineFormatService?) {

    public fun buildPage(node: DocumentationNode): Unit = buildPages(listOf(node))
    public fun buildOutline(node: DocumentationNode): Unit = buildOutlines(listOf(node))

    public fun buildPages(nodes: Iterable<DocumentationNode>) {
        for ((location, items) in nodes.groupBy { locationService.location(it) }) {
            val file = location.file
            file.parentFile?.mkdirs()
            FileOutputStream(file).use {
                OutputStreamWriter(it, Charsets.UTF_8).use {
                    it.write(formatService.format(location, items))
                }
            }
            buildPages(items.flatMap { it.members })
        }
    }

    public fun buildOutlines(nodes: Iterable<DocumentationNode>) {
        if (outlineService == null) {
            return
        }
        for ((location, items) in nodes.groupBy { locationService.location(it) }) {
            val file = outlineService.getOutlineFileName(location)
            file.parentFile?.mkdirs()
            FileOutputStream(file).use {
                OutputStreamWriter(it, Charsets.UTF_8).use {
                    it.write(outlineService.formatOutline(location, items))
                }
            }
        }
    }
}