Skip to content

Feat: Add a blog configuration with yaml #17214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions scaladoc/src/dotty/tools/scaladoc/site/BlogParser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dotty.tools.scaladoc.site

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.databind.DeserializationFeature
import java.io.File
import scala.beans.{BooleanBeanProperty, BeanProperty}
import scala.util.Try

case class BlogConfig(
@BeanProperty input: String,
@BeanProperty output: String,
@BooleanBeanProperty hidden: Boolean
):
def this() = this(null, null, false)

object BlogParser:
def readYml(content: File | String): BlogConfig =
val mapper = ObjectMapper(YAMLFactory())
.findAndRegisterModules()

content match
case f: File =>
val ymlFile = f.toPath.resolve("blog.yml").toFile
if ymlFile.exists then mapper.readValue(ymlFile, classOf[BlogConfig]) else new BlogConfig
case s: String => Try(mapper.readValue(s, classOf[BlogConfig])).getOrElse(new BlogConfig)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class StaticSiteContext(
val docsPath = root.toPath.resolve("_docs")
val blogPath = root.toPath.resolve("_blog")

def resolveNewBlogPath(stringPath: String): Path =
if stringPath.nonEmpty then root.toPath.resolve(stringPath)
else blogPath

def relativize(path: Path): Path =
if args.apiSubdirectory then
docsPath.relativize(path)
Expand Down
9 changes: 6 additions & 3 deletions scaladoc/src/dotty/tools/scaladoc/site/StaticSiteLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io.File
import java.nio.file.Files
import java.nio.file.{ Paths, Path }
import scala.io._
import dotty.tools.scaladoc.site.BlogParser

class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSiteContext, CompilerContext):
val ctx: StaticSiteContext = summon[StaticSiteContext]
Expand Down Expand Up @@ -114,10 +115,12 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
}

def loadBlog(): Option[LoadedTemplate] = {
val blogConfig = BlogParser.readYml(root)
val rootPath = Option(blogConfig.input).map(input => ctx.resolveNewBlogPath(input)).getOrElse(ctx.blogPath)
val defaultDirectory = Option(blogConfig.output).getOrElse("blog")

type Date = (String, String, String)
val rootPath = ctx.blogPath
val defaultDirectory = "blog"
if (!Files.exists(rootPath)) None
if (!Files.exists(rootPath) || blogConfig.hidden) None
else {
val indexPageOpt = Seq(
rootPath.resolve("index.md"),
Expand Down
19 changes: 19 additions & 0 deletions scaladoc/test/dotty/tools/scaladoc/site/BlogParserTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dotty.tools.scaladoc
package site

import org.junit.Test
import org.junit.Assert._

class BlogParserTest:

private val blogConfig =
"""input: blog
|output: blog
|hidden: false
|""".stripMargin

@Test
def loadBlog(): Unit = assertEquals(
BlogConfig("blog", "blog", false),
BlogParser.readYml(blogConfig)
)