Skip to content

Commit e8c6ebe

Browse files
authored
Feat: Add a blog configuration with yaml (#17214)
- Add input config, who allow to define the path import - Add output config, who allow to define the path destination - Add hidden, who allow to not generate the blog Should I add warnings? For example when the path is not found. If so, how do I test? Because the path is searched for _blog but it is not found so the assert that asks for no warnings does not work as in [this one](https://github.com/lampepfl/dotty/blob/d4a86000404cd3bb774025d48151c6311d6bc96d/scaladoc/test/dotty/tools/scaladoc/ReportingTest.scala#L73). Here is my proposal to add a blog configuration using a .yaml in the same place as the sidebar.yaml. This [feature](#14487) request is a bit old but I thought it would be interesting to create it to give a bit more flexibility to Scaladoc users. This first PR allows me to validate with you if this feature is good to implement and also to have some feedbacks I answer well to what is asked and that my code is relevant. So if it is the case, I will do the tests and the associated documentation. ### TO-DO list: - [x] First version of the code - [x] Add documentation Will be implemented in docs.scala-lang, [PR #2796](scala/docs.scala-lang#2796): *** ### Blog Configuration Documentation Blog configuration is an important aspect of any blog platform. In order to customize the configuration of a blog, it is often necessary to modify the default settings. In this document, we will explain how to change the default config of a blog documentation with a file blog.yaml. In order to modify the default settings of the blog documentation, users need to create a file named "blog.yaml" in the root directory of the blog. The file should contain the parameters that the user wants to change. For example, if a user wants to change the input directory to "my_posts", the output directory to "my_docs", and temporarily hide the blog, they can create a file with the following content: `blog.yaml:` ``` input: my_posts output: my_docs hidden: true ``` Parameters: The blog.yaml file is a configuration file that allows users to modify the default settings of their blog documentation. The following parameters can be configured in the blog.yaml file: input: This parameter specifies the directory where the markdown pages and other files will be taken for the blogs page. By default, this is set to the "_posts" folder in the "docs" directory of the blog. However, users can change this to any other directory in "docs". output: This parameter specifies the folder where the HTML pages will be generated. By default, this is set to the "blog" folder in the "target/docs" directory. However, users can change this to any other directory on "target/docs". hidden: This parameter allows users to hide or unhide the blog temporarily. By default, this is set to "false", which means that the blog is visible to all users. However, if a user wants to temporarily hide the blog, they can set this parameter to "true". This can be useful if the user wants to make some changes to the blog and doesn't want anyone to see the changes until they are ready. Once the file is created, the user needs to save it in the root directory of the blog. The next time the blog is built, the new settings will be applied. *** - [x] Add tests ## Example (Success) ### Code & Input <img width="328" alt="Screenshot 2023-04-06 at 14 41 47" src="https://user-images.githubusercontent.com/44496264/230382241-9433d641-03ec-4495-8fd2-81fa6ce3bc17.png"> <img width="329" alt="Screenshot 2023-04-06 at 14 41 58" src="https://user-images.githubusercontent.com/44496264/230382414-7876c601-b1f0-498f-85e1-71ecc500cf09.png"> ### Result <img width="326" alt="Screenshot 2023-04-06 at 14 42 09" src="https://user-images.githubusercontent.com/44496264/230382463-9fe234ac-3fd0-4d56-a04c-6f6deb01a308.png"> ## First example (Hidden) ### Code <img width="254" alt="Screenshot 2023-04-06 at 14 46 27" src="https://user-images.githubusercontent.com/44496264/230382770-7aabf9a2-e46f-429d-b1c9-97892072349a.png"> ### Result <img width="335" alt="Screenshot 2023-04-06 at 14 42 36" src="https://user-images.githubusercontent.com/44496264/230383192-05e0187f-5ecf-4bd9-8697-122093577d43.png"> <img width="316" alt="Screenshot 2023-04-06 at 14 42 47" src="https://user-images.githubusercontent.com/44496264/230382928-8130cddd-f8dd-4b43-bee1-15129af308c8.png"> ## Example (Error) <img width="316" alt="Screenshot 2023-04-06 at 14 43 03" src="https://user-images.githubusercontent.com/44496264/230383219-ff6844e2-47c5-40d6-9056-a8ab1dc6f8e6.png"> <img width="945" alt="Screenshot 2023-04-06 at 14 43 14" src="https://user-images.githubusercontent.com/44496264/230383236-2ee07f63-640c-4bc1-a3b1-e0785ff467e9.png"> Fixes: #14487
2 parents d103f8c + 968ebab commit e8c6ebe

File tree

4 files changed

+55
-3
lines changed

4 files changed

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class StaticSiteContext(
2323
val docsPath = root.toPath.resolve("_docs")
2424
val blogPath = root.toPath.resolve("_blog")
2525

26+
def resolveNewBlogPath(stringPath: String): Path =
27+
if stringPath.nonEmpty then root.toPath.resolve(stringPath)
28+
else blogPath
29+
2630
def relativize(path: Path): Path =
2731
if args.apiSubdirectory then
2832
docsPath.relativize(path)

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

Lines changed: 6 additions & 3 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]
@@ -114,10 +115,12 @@ class StaticSiteLoader(val root: File, val args: Scaladoc.Args)(using StaticSite
114115
}
115116

116117
def loadBlog(): Option[LoadedTemplate] = {
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+
117122
type Date = (String, String, String)
118-
val rootPath = ctx.blogPath
119-
val defaultDirectory = "blog"
120-
if (!Files.exists(rootPath)) None
123+
if (!Files.exists(rootPath) || blogConfig.hidden) None
121124
else {
122125
val indexPageOpt = Seq(
123126
rootPath.resolve("index.md"),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dotty.tools.scaladoc
2+
package site
3+
4+
import org.junit.Test
5+
import org.junit.Assert._
6+
7+
class BlogParserTest:
8+
9+
private val blogConfig =
10+
"""input: blog
11+
|output: blog
12+
|hidden: false
13+
|""".stripMargin
14+
15+
@Test
16+
def loadBlog(): Unit = assertEquals(
17+
BlogConfig("blog", "blog", false),
18+
BlogParser.readYml(blogConfig)
19+
)

0 commit comments

Comments
 (0)