|
| 1 | +import scala.io.{ Codec, Source } |
| 2 | +import java.nio.file.{ FileSystems, Files, Path, StandardCopyOption } |
| 3 | +import java.io.PrintStream |
| 4 | +import collection.JavaConverters._ |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * Object used for copying docs from docs to docs-for-dotty-page. More explenation can be found and [readme](../docs/README.md) |
| 9 | + */ |
| 10 | +object CopyDocs { |
| 11 | + |
| 12 | + /** |
| 13 | + * Input directory from which we will take all the docs. |
| 14 | + */ |
| 15 | + val inputDir = FileSystems.getDefault.getPath("docs") |
| 16 | + |
| 17 | + /** |
| 18 | + * Output directory. Most of the sources will be copied as they are, but some of the files will have headers and links processed. |
| 19 | + */ |
| 20 | + val outputDir = FileSystems.getDefault.getPath("docs-for-dotty-page") |
| 21 | + |
| 22 | + implicit def stringToFun(s: String): MyParams => String = _ => s |
| 23 | + |
| 24 | + // Patterns, for convenience |
| 25 | + val titlePattern = "(?s)^---\n.*title: ([^\n]*).*---" |
| 26 | + val jekyllLinkPattern = """\{\% link _overviews/scala3-scaladoc(.*) %\}""" |
| 27 | + val jekyllLinkSubstitution = "..$1" |
| 28 | + val jekyllLinkPattern2 = """\{\% link |_overviews/scala3-scaladoc/(.*) %\}""" |
| 29 | + val jekyllLinkSubstitution2 = "$1" |
| 30 | + val localLinkPattern = """\((?!http|www)(.*).html\)""" |
| 31 | + val localLinkSubstitution = "($1.md)" |
| 32 | + |
| 33 | + case class MyParams(newPath: String) |
| 34 | + |
| 35 | + /** |
| 36 | + * Structure for holding which transformations should be applied to which directories. |
| 37 | + * The outer map is holding morphism `directory prefix` -> `set of transformations`. |
| 38 | + * The inner set is a collection of pairs `regex pattern` -> `substitution value`. |
| 39 | + */ |
| 40 | + val transformationMap: Map[String, Set[(String, MyParams => String)]] = Map( |
| 41 | + "docs/docs/reference/" -> Set( |
| 42 | + titlePattern -> ((p) => s"---\nlayout: doc-page\ntitle: $$1\nmovedTo: https://docs.scala-lang.org/scala3/reference/contextual/${p.newPath}.html\n---"), |
| 43 | + jekyllLinkPattern -> jekyllLinkSubstitution, |
| 44 | + jekyllLinkPattern2 -> jekyllLinkSubstitution2, |
| 45 | + localLinkPattern -> localLinkSubstitution, |
| 46 | + ), |
| 47 | + |
| 48 | + "docs/docs/usage/scaladoc/" -> Set( |
| 49 | + titlePattern -> s"---\ntitle: $$1\n---", |
| 50 | + jekyllLinkPattern -> jekyllLinkSubstitution, |
| 51 | + jekyllLinkPattern2 -> jekyllLinkSubstitution2, |
| 52 | + localLinkPattern -> localLinkSubstitution, |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + def copyDocs() = { |
| 57 | + def copyFile(path: Path): Unit = { |
| 58 | + val newPath = outputDir.resolve(inputDir.relativize(path)) |
| 59 | + Files.createDirectories(newPath.getParent()) |
| 60 | + |
| 61 | + path.toString match { |
| 62 | + case s if s.startsWith("docs/docs/") => |
| 63 | + val inputStream = Source.fromFile(path.toFile)(Codec.UTF8) |
| 64 | + val fileContent = inputStream.getLines().mkString("\n") |
| 65 | + |
| 66 | + new PrintStream(newPath.toFile) { |
| 67 | + val patterns = transformationMap.find { case (k, v) => path.toString.startsWith(k) }.map(_._2).getOrElse(Set.empty) |
| 68 | + val fileName = newPath.getFileName.toString.stripSuffix(".md") |
| 69 | + val ghSubpath = if (Seq("overview", "syntax", "soft-modifier").contains(fileName)) |
| 70 | + fileName |
| 71 | + else |
| 72 | + s"contextual/$fileName" |
| 73 | + |
| 74 | + val params = MyParams(newPath = ghSubpath) |
| 75 | + val transformed = patterns.foldLeft(fileContent) { case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution(params)) } |
| 76 | + write(transformed.getBytes("UTF8")) |
| 77 | + } |
| 78 | + case s => |
| 79 | + Files.copy(path, newPath, StandardCopyOption.REPLACE_EXISTING); |
| 80 | + } |
| 81 | + } |
| 82 | + Files.walk(inputDir).iterator().asScala.filter(Files.isRegularFile(_)).foreach(copyFile) |
| 83 | + } |
| 84 | +} |
0 commit comments