|
1 |
| -import java.nio.file.{ FileSystems, Files, Path } |
| 1 | +import java.nio.file.{ FileSystems, Files, Path, StandardCopyOption } |
2 | 2 | import java.io.PrintStream
|
3 | 3 | import scala.io.{ Codec, Source }
|
4 | 4 | import scala.collection.JavaConverters._
|
5 | 5 |
|
6 |
| -val inputDir = FileSystems.getDefault.getPath("docs-for-scalalang") |
7 |
| -val outputDir = FileSystems.getDefault.getPath("docs") |
| 6 | +val inputDir = FileSystems.getDefault.getPath("docs") |
| 7 | +val outputDir = FileSystems.getDefault.getPath("docs-for-dotty-page") |
8 | 8 |
|
9 | 9 | def copyFile(path: Path): Unit =
|
10 |
| - val inputStream = Source.fromFile(path.toFile)(Codec.UTF8) |
11 |
| - val fileContent = inputStream.getLines().mkString("\n") |
12 |
| - |
13 | 10 | val newPath = outputDir.resolve(inputDir.relativize(path))
|
14 | 11 | Files.createDirectories(newPath.getParent())
|
15 | 12 |
|
16 |
| - new PrintStream(newPath.toFile): |
17 |
| - |
18 |
| - val titlePattern = "(?s)^---\n.*title: (\".*\").*---" |
19 |
| - val reference = raw".*/reference/.*".r |
20 |
| - val usageScaladoc = raw".*/usage/scaladoc/.*".r |
21 |
| - val transformed = newPath.toString match |
22 |
| - case reference(_*) => fileContent.replaceAll( |
23 |
| - titlePattern, |
24 |
| - s"---\nlayout: doc-page\ntitle: $$1\nmovedTo: https://docs.scala-lang.org/scala3/reference/contextual/${newPath.getFileName.toString.stripSuffix(".md")}.html\n---" |
25 |
| - ) |
26 |
| - case usageScaladoc(_*) => fileContent.replaceAll( |
27 |
| - titlePattern, |
28 |
| - s"---\ntitle: $$1\n---" |
29 |
| - ) |
30 |
| - case _ => |
31 |
| - fileContent |
32 |
| - |
33 |
| - |
34 |
| - val jekyllLinkPattern = """\(\{\% link _scala3-reference(.*) %\}\)""" |
35 |
| - val jekyllLinkSubstitution = "(..$1)" |
36 |
| - val localLinkPattern = """\((?!http|www)(.*).html\)""" |
37 |
| - val localLinkSubstitution = "($1.md)" |
38 |
| - val transformedWithLinks = transformed.replaceAll( |
39 |
| - jekyllLinkPattern, |
40 |
| - jekyllLinkSubstitution |
41 |
| - ).replaceAll( |
42 |
| - localLinkPattern, |
43 |
| - localLinkSubstitution |
44 |
| - ) |
45 |
| - |
46 |
| - write(transformedWithLinks.getBytes("UTF8")) |
| 13 | + path.toString match |
| 14 | + case s if s.startsWith("docs/docs/") => |
| 15 | + val inputStream = Source.fromFile(path.toFile)(Codec.UTF8) |
| 16 | + val fileContent = inputStream.getLines().mkString("\n") |
| 17 | + |
| 18 | + new PrintStream(newPath.toFile): |
| 19 | + |
| 20 | + // Patterns |
| 21 | + val titlePattern = "(?s)^---\n.*title: (\".*\").*---" |
| 22 | + |
| 23 | + val jekyllLinkPattern = """\{\% link _overviews/scala3-scaladoc(.*) %\}""" |
| 24 | + val jekyllLinkSubstitution = "..$1" |
| 25 | + val jekyllLinkPattern2 = """\{\% link |_overviews/scala3-scaladoc/(.*) %\}""" |
| 26 | + val jekyllLinkSubstitution2 = "$1" |
| 27 | + val localLinkPattern = """\((?!http|www)(.*).html\)""" |
| 28 | + val localLinkSubstitution = "($1.md)" |
| 29 | + |
| 30 | + // Prefixes |
| 31 | + val reference = raw"docs/docs/reference/.*".r |
| 32 | + val usageScaladoc = raw"docs/docs/usage/scaladoc/.*".r |
| 33 | + |
| 34 | + val patterns = path.toString match |
| 35 | + case reference(_*) => Map( |
| 36 | + titlePattern -> s"---\nlayout: doc-page\ntitle: $$1\nmovedTo: https://docs.scala-lang.org/scala3/reference/contextual/${newPath.getFileName.toString.stripSuffix(".md")}.html\n---", |
| 37 | + jekyllLinkPattern -> jekyllLinkSubstitution, |
| 38 | + jekyllLinkPattern2 -> jekyllLinkSubstitution2, |
| 39 | + localLinkPattern -> localLinkSubstitution, |
| 40 | + ) |
| 41 | + case usageScaladoc(_*) => Map( |
| 42 | + titlePattern -> s"---\ntitle: $$1\n---", |
| 43 | + jekyllLinkPattern -> jekyllLinkSubstitution, |
| 44 | + jekyllLinkPattern2 -> jekyllLinkSubstitution2, |
| 45 | + localLinkPattern -> localLinkSubstitution, |
| 46 | + ) |
| 47 | + case _ => |
| 48 | + Map.empty |
| 49 | + |
| 50 | + val transformed = patterns.foldLeft(fileContent){ case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution) } |
| 51 | + write(transformed.getBytes("UTF8")) |
| 52 | + |
| 53 | + case s => |
| 54 | + Files.copy(path, newPath, StandardCopyOption.REPLACE_EXISTING); |
47 | 55 |
|
48 | 56 | def main(args: Array[String]): Unit =
|
49 | 57 | Files.walk(inputDir).iterator().asScala.filter(Files.isRegularFile(_)).foreach(copyFile)
|
0 commit comments