Skip to content

Commit

Permalink
cp
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoc committed Apr 16, 2020
1 parent d1b1cb8 commit 784f076
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/scala/io/bazeltools/proto3docgen/HugoContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ case class HugoContext(

def currentPackageName = currentPackageNameOpt.getOrElse(sys.error(s"Invalid state, pkg name is unset :/"))


def renderType(fullName: String): String = typeMap.get(fullName) match {
case Some((protoPkg, longName)) =>
val targetPackage = pkgToPath(protoPkg)
Expand All @@ -62,6 +63,32 @@ case class HugoContext(
case None => fullName
}

private[this] def initializeParents(outputP: Path): Unit = {
val outputFolder = outputP.getParent

val segments = outputP.toString.split('/').toList
val root = outputRoot
@annotation.tailrec
def go(p: Path, remaining: List[String]): Unit = {
remaining match {
case h :: t =>
val nxtFolder = p.resolve(h)
Files.createDirectories(nxtFolder)
val indexFilePath = nxtFolder.resolve("_index.md")
if(!indexFilePath.toFile.exists) {
Files.write(indexFilePath, s"""
|---
|title: "$h"
|---
|""".stripMargin.getBytes)
}
go(nxtFolder, t)
case Nil => ()
}
}
}


def writePackage(pkg: ProtoPackage): Unit = {
val pkgTitle = layoutMode match {
case LayoutMode.Nested => pkg.name.split('.').last
Expand All @@ -76,19 +103,7 @@ ${pkg.toSection(this).toMarkdown(1).render(0)}
"""

val outputP = pkgToPath(pkg.name)
val outputFolder = outputRoot.resolve(outputP).getParent
if(!outputFolder.toFile.exists) {
Files.createDirectories(outputFolder)
layoutMode match {
case LayoutMode.Nested =>
Files.write(outputFolder.resolve("_index.md"), s"""
|---
|title: "${outputFolder.basenameWithoutExtension}"
|---
|""".stripMargin.getBytes)
case _ => ()
}
}
initializeParents(outputP)
Files.write(
outputRoot.resolve(outputP),
contents.getBytes
Expand Down

0 comments on commit 784f076

Please sign in to comment.