Skip to content

Commit 0daeabe

Browse files
committed
Fix: Refactor the function using jackson
1 parent 3347eb4 commit 0daeabe

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dotty.tools.scaladoc
2+
package site
3+
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
6+
import com.fasterxml.jackson.databind.DeserializationFeature
7+
import java.io.File
8+
import scala.beans._
9+
10+
case class BlogConfig(
11+
@BeanProperty var input: String,
12+
@BeanProperty var output: String,
13+
@BooleanBeanProperty var hidden: Boolean
14+
):
15+
def this() = this(null, null, false)
16+
17+
object BlogParser:
18+
def readYml(root: File): BlogConfig =
19+
val ymlFile = root.toPath
20+
.resolve("blog.yml")
21+
.toFile
22+
23+
if ymlFile.exists then
24+
val mapper = new ObjectMapper(new YAMLFactory())
25+
mapper.findAndRegisterModules();
26+
27+
val blogConfig: BlogConfig = mapper.readValue(ymlFile, classOf[BlogConfig])
28+
blogConfig
29+
else new BlogConfig

scaladoc/src/dotty/tools/scaladoc/site/StaticSiteLoader.scala

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.io.File
55
import java.nio.file.Files
66
import java.nio.file.{ Paths, Path }
77
import scala.io._
8+
import dotty.tools.scaladoc.site.BlogParser
89

910
class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSiteContext, CompilerContext):
1011
val ctx: StaticSiteContext = summon[StaticSiteContext]
@@ -113,37 +114,13 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
113114
StaticSiteRoot(withBlog, mappings)
114115
}
115116

116-
var hiddenBlog = false
117-
118-
def readYml: (Option[Boolean], Option[Boolean], Option[Boolean], String) =
119-
val ymlPath = root.toPath.resolve("blog.yml")
120-
if (Files.exists(ymlPath)) then
121-
val yamlContent = Source.fromFile(ymlPath.toString).getLines().mkString("\n")
122-
val hidden = if (yamlContent.contains("hidden: true")) Some(true) else None
123-
val input = if (yamlContent.contains("input:")) Some(true) else None
124-
val output = if (yamlContent.contains("output:")) Some(true) else None
125-
(hidden, input, output, yamlContent)
126-
else
127-
(None, None, None, "")
128-
129117
def loadBlog(): Option[LoadedTemplate] = {
130-
val (hidden, input, output, yamlContent) = readYml
131-
val lines = yamlContent.split("\n")
132-
val rootPath = input.collect {
133-
case true =>
134-
lines.collectFirst { case line if line.contains("input:") => line.replaceFirst("input:", "").trim }
135-
.map(ctx.resolveNewBlogPath)
136-
.getOrElse(ctx.blogPath)
137-
}.getOrElse(ctx.blogPath)
138-
val defaultDirectory = output.collect {
139-
case true =>
140-
lines
141-
.collectFirst { case line if line.contains("output:") => line.replaceFirst("output:", "").trim }
142-
.getOrElse("blog")
143-
}.getOrElse("blog")
144-
hidden.collect { case true => hiddenBlog = true }
118+
val blogConfig = BlogParser.readYml(root)
119+
val rootPath = Option(blogConfig.input).map(input => ctx.resolveNewBlogPath(input)).getOrElse(ctx.blogPath)
120+
val defaultDirectory = Option(blogConfig.output).getOrElse("blog")
121+
145122
type Date = (String, String, String)
146-
if (!Files.exists(rootPath) || hiddenBlog) None
123+
if (!Files.exists(rootPath) || blogConfig.hidden) None
147124
else {
148125
val indexPageOpt = Seq(
149126
rootPath.resolve("index.md"),

0 commit comments

Comments
 (0)