@@ -19,21 +19,14 @@ object CopyDocs {
19
19
*/
20
20
val outputDir = FileSystems .getDefault.getPath(" docs-for-dotty-page" )
21
21
22
- implicit def stringToFun (s : String ): MyParams => String = _ => s
23
-
24
- // Patterns, for convenience
25
- val titlePattern = """ (?s)^(---\n.*?title: ([^\n]*).*?---)"""
26
- val redirectFromPattern = """ (?s)^---.*?((redirectFrom: ([^\n]*)).*?---|---)"""
27
22
val jekyllLinkPattern = """ \{\% link _overviews/scala3-reference(.*) %\}"""
28
23
val jekyllLinkSubstitution = " ..$1"
29
24
val jekyllLinkPattern2 = """ \{\% link _overviews/scala3-scaladoc(.*) %\}"""
30
25
val jekyllLinkSubstitution2 = " .$1"
31
26
val localLinkPattern = """ \((?!http|www)(.*).html\)"""
32
27
val localLinkSubstitution = " ($1.md)"
33
28
34
- case class MyParams (newPath : String )
35
-
36
- val commonTransformations : List [(String , MyParams => String )] = List (
29
+ val commonTransformations : Map [String , String ] = Map (
37
30
jekyllLinkPattern -> jekyllLinkSubstitution,
38
31
jekyllLinkPattern2 -> jekyllLinkSubstitution2,
39
32
localLinkPattern -> localLinkSubstitution,
@@ -44,32 +37,26 @@ object CopyDocs {
44
37
* The outer map is holding morphism `directory prefix` -> `List of transformations`.
45
38
* The inner list is a collection of pairs `regex pattern` -> `substitution value`.
46
39
*/
47
- val transformationMap : Map [String , List [( String , MyParams => String ) ]] = Map (
48
- " docs/docs/usage/scaladoc/index.md" -> List (
40
+ val transformationMap : Map [String , Map [ String , String ]] = Map (
41
+ " docs/docs/usage/scaladoc/index.md" -> Map (
49
42
(""" \{\{ site\.baseurl \}\}/resources/images/scala3/scaladoc/logo\.svg""" -> " images/scaladoc_logo.svg" ),
50
43
),
51
44
52
- " docs/docs/usage/scaladoc/site-versioning.md" -> List (
45
+ " docs/docs/usage/scaladoc/site-versioning.md" -> Map (
53
46
(""" /resources/images/scala3/scaladoc/nightly\.gif""" -> " images/scaladoc/nightly.gif" ),
54
47
),
55
48
56
- " docs/docs/usage/scaladoc/search-engine.md" -> List (
49
+ " docs/docs/usage/scaladoc/search-engine.md" -> Map (
57
50
(""" /resources/images/scala3/scaladoc/inkuire-1\.0\.0-M2_js_flatMap\.gif""" -> " images/scaladoc/inkuire-1.0.0-M2_js_flatMap.gif" ),
58
51
),
59
52
60
- " docs/docs/reference/other-new-features/explicit-nulls.md" -> List (
53
+ " docs/docs/reference/other-new-features/explicit-nulls.md" -> Map (
61
54
(""" /resources/images/scala3/explicit-nulls/explicit-nulls-type-hierarchy\.png""" -> " images/explicit-nulls/explicit-nulls-type-hierarchy.png" ),
62
55
),
63
56
64
- " docs/docs/reference/" -> (commonTransformations ++ List [(String , MyParams => String )](
65
- (titlePattern -> ((p) => s " $$ 1 \n layout: doc-page \n title: $$ 2 \n movedTo: https://docs.scala-lang.org/scala3/reference/ ${p.newPath}.html \n --- " )),
66
- (redirectFromPattern -> " ---\n $2" )
67
- )),
57
+ " docs/docs/reference/" -> commonTransformations,
68
58
69
- " docs/docs/usage/scaladoc/" -> (commonTransformations ++ List [(String , MyParams => String )](
70
- (titlePattern -> s " $$ 1 \n layout: doc-page \n title: $$ 2 \n --- " ),
71
- (redirectFromPattern -> " ---\n $2" )
72
- )),
59
+ " docs/docs/usage/scaladoc/" -> commonTransformations
73
60
)
74
61
75
62
def copyDocs () = {
@@ -78,14 +65,24 @@ object CopyDocs {
78
65
Files .createDirectories(newPath.getParent())
79
66
80
67
path.toString match {
81
- case s if s.startsWith(" docs/docs/" ) =>
68
+ case s if s.startsWith(" docs/docs/" ) && s.endsWith( " .md " ) =>
82
69
val inputStream = Source .fromFile(path.toFile)(Codec .UTF8 )
83
70
val fileContent = inputStream.getLines().mkString(" \n " )
84
71
85
72
new PrintStream (newPath.toFile) {
86
73
val patterns = transformationMap.filter { case (k, v) => path.toString.startsWith(k) }.flatMap(_._2)
87
- val params = MyParams (newPath = s.stripPrefix(" docs/docs/reference/" ).stripSuffix(" .md" ))
88
- val transformed = patterns.foldLeft(fileContent) { case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution(params)) }
74
+ val _ :: frontMatter :: actualContent :: Nil = fileContent.split(" ---" , 3 ).toList
75
+ write(" ---" .getBytes(" UTF8" ))
76
+ val frontMatterSplitted = frontMatter.split(" \n (?=[^\\ s])" )
77
+ val frontMatterUpdated = List (
78
+ Some (" layout: doc-page" ),
79
+ frontMatterSplitted.find(_.startsWith(" title" )),
80
+ frontMatterSplitted.find(_.startsWith(" redirectFrom" )),
81
+ if (s.startsWith(" docs/docs/reference/" )) Some (s " movedTo: https://docs.scala-lang.org/scala3/reference/ ${s.stripPrefix(" docs/docs/reference/" ).stripSuffix(" .md" )}.html " ) else None
82
+ ).flatten.mkString(" \n " , " \n " , " \n " )
83
+ write(frontMatterUpdated.getBytes(" UTF8" ))
84
+ write(" ---\n " .getBytes(" UTF8" ))
85
+ val transformed = patterns.foldLeft(actualContent) { case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution) }
89
86
write(transformed.getBytes(" UTF8" ))
90
87
}
91
88
case s =>
0 commit comments