@@ -3,7 +3,9 @@ package site
3
3
4
4
import java .io .File
5
5
import java .nio .file .Files
6
-
6
+ import java .nio .file .FileVisitOption
7
+ import java .nio .file .Path
8
+ import java .nio .file .Paths
7
9
8
10
import org .jetbrains .dokka .base .parsers .MarkdownParser
9
11
import org .jetbrains .dokka .base .transformers .pages .comments .DocTagToContentConverter
@@ -15,14 +17,16 @@ import org.jetbrains.dokka.pages.{ContentKind, ContentNode, DCI, PageNode}
15
17
import org .jetbrains .dokka .plugability .DokkaContext
16
18
import org .jetbrains .dokka .pages .Style
17
19
import org .jetbrains .dokka .model .DisplaySourceSet
20
+ import util .Try
18
21
19
22
import scala .collection .JavaConverters ._
20
23
21
24
class StaticSiteContext (val root : File , sourceSets : Set [SourceSetWrapper ]):
22
25
23
26
def indexPage (): Option [StaticPageNode ] =
24
27
val files = List (new File (root, " index.html" ), new File (root, " index.md" )).filter { _.exists() }
25
- if (files.size > 1 ) println(s " ERROR: Multiple root index pages found: ${files.map(_.getAbsolutePath)}" ) // TODO (#14): provide proper error handling
28
+ // TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
29
+ if (files.size > 1 ) println(s " ERROR: Multiple root index pages found: ${files.map(_.getAbsolutePath)}" )
26
30
files.flatMap(loadTemplate(_, isBlog = false )).headOption.map(templateToPage)
27
31
28
32
lazy val layouts : Map [String , TemplateFile ] =
@@ -37,7 +41,27 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
37
41
38
42
lazy val templates : Seq [LoadedTemplate ] = sideBarConfig.fold(loadAllFiles())(_.map(loadSidebarContent))
39
43
40
- lazy val pages = templates.map(templateToPage)
44
+ lazy val mainPages : Seq [StaticPageNode ] = templates.map(templateToPage)
45
+
46
+ lazy val allPages : Seq [StaticPageNode ] = sideBarConfig.fold(mainPages){ sidebar =>
47
+ def flattenPages (p : StaticPageNode ): Set [Path ] =
48
+ Set (p.template.file.toPath) ++ p.getChildren.asScala.collect { case p : StaticPageNode => flattenPages(p) }.flatten
49
+
50
+ val mainFiles = mainPages.toSet.flatMap(flattenPages)
51
+ val docsPath = root.toPath.resolve(" docs" )
52
+ val allPaths =
53
+ if ! Files .exists(docsPath) then Nil
54
+ else Files .walk(docsPath, FileVisitOption .FOLLOW_LINKS ).iterator().asScala.toList
55
+
56
+ val orphanedFiles = allPaths.filterNot(mainFiles.contains).filter { p =>
57
+ val name = p.getFileName.toString
58
+ name.endsWith(" .md" ) || name.endsWith(" .html" )
59
+ }
60
+
61
+ val orphanedTemplates = orphanedFiles.flatMap(p => loadTemplate(p.toFile, isBlog = false ))
62
+
63
+ mainPages ++ orphanedTemplates.map(templateToPage)
64
+ }
41
65
42
66
private def isValidTemplate (file : File ): Boolean =
43
67
(file.isDirectory && ! file.getName.startsWith(" _" )) ||
@@ -52,7 +76,8 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
52
76
val allFiles = topLevelFiles.filter(_.isDirectory).flatMap(_.listFiles())
53
77
val (indexes, children) = allFiles.flatMap(loadTemplate(_)).partition(_.templateFile.isIndexPage())
54
78
if (indexes.size > 1 )
55
- println(s " ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}" ) // TODO (#14): provide proper error handling
79
+ // TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
80
+ println(s " ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}" )
56
81
57
82
def loadIndexPage (): TemplateFile =
58
83
val indexFiles = from.listFiles { file => file.getName == " index.md" || file.getName == " index.html" }
@@ -68,7 +93,8 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
68
93
Some (LoadedTemplate (templateFile, children.toList, from))
69
94
catch
70
95
case e : RuntimeException =>
71
- e.printStackTrace() // TODO (#14): provide proper error handling
96
+ // TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
97
+ e.printStackTrace()
72
98
None
73
99
74
100
def asContent (doctag : DocTag , dri : DRI ) = new DocTagToContentConverter ().buildContent(
@@ -83,10 +109,11 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
83
109
case Sidebar .Page (title, url) =>
84
110
val isBlog = title == " Blog"
85
111
val path = if isBlog then " blog" else url.stripSuffix(" .html" ) + " .md"
86
- val file = root.toPath.resolve(path) // Add support for.html files!
112
+ val file = root.toPath.resolve(path) // Add support for .html files!
87
113
val LoadedTemplate (template, children, tFile) = loadTemplate(file.toFile, isBlog).get // Add proper logging if file does not exisits
88
114
LoadedTemplate (template.copy(settings = template.settings + (" title" -> List (title))), children, tFile)
89
115
case Sidebar .Category (title, nested) =>
116
+ // Add support for index.html/index.md files!
90
117
val fakeFile = new File (root, title)
91
118
LoadedTemplate (emptyTemplate(fakeFile), nested.map(loadSidebarContent), fakeFile)
92
119
@@ -95,9 +122,15 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
95
122
dir(" docs" ).flatMap(_.listFiles()).flatMap(loadTemplate(_, isBlog = false ))
96
123
++ dir(" blog" ).flatMap(loadTemplate(_, isBlog = true ))
97
124
125
+ def driForLink (template : TemplateFile , link : String ): Try [DRI ] = Try (driFor(
126
+ if link.startsWith(" /" ) then root.toPath.resolve(link.drop(1 ))
127
+ else template.file.toPath.getParent().resolve(link)
128
+ ))
129
+
130
+ private def driFor (dest : Path ): DRI = mkDRI(s " _. ${root.toPath.relativize(dest)}" )
131
+
98
132
def templateToPage (myTemplate : LoadedTemplate ): StaticPageNode =
99
- def pathToDRI (path : String ) = mkDRI(s " _. $path" )
100
- val dri = pathToDRI(myTemplate.relativePath(root))
133
+ val dri = driFor(myTemplate.file.toPath)
101
134
val content = new PartiallyRenderedContent (
102
135
myTemplate.templateFile,
103
136
this ,
0 commit comments