|
| 1 | +package dev.atedeg |
| 2 | + |
| 3 | +import better.files.Dsl.{ ls, SymbolicOperations } |
| 4 | +import better.files.FileExtensions |
| 5 | +import better.files.File |
| 6 | +import net.ruippeixotog.scalascraper.browser.JsoupBrowser |
| 7 | +import net.ruippeixotog.scalascraper.dsl.DSL.deepFunctorOps |
| 8 | +import net.ruippeixotog.scalascraper.dsl.DSL.* |
| 9 | +import net.ruippeixotog.scalascraper.dsl.DSL.Extract.text |
| 10 | +import net.steppschuh.markdowngenerator.table.Table |
| 11 | + |
| 12 | +import java.io.{ File => JFile } |
| 13 | + |
| 14 | +object UbiquitousScaladoc { |
| 15 | + private val tableFirstRow: (String, String) = ("Term", "Definition") |
| 16 | + private val fileSuffix: String = "UbiquitousLanguage.md" |
| 17 | + |
| 18 | + def apply(sourceDir: JFile, targetDir: JFile): Unit = ubiquitousScaladocTask(sourceDir, targetDir) |
| 19 | + |
| 20 | + private def ubiquitousScaladocTask(sourceDir: JFile, targetDir: JFile): Unit = { |
| 21 | + extractFilesWithDirName(sourceDir.toScala).foreach { s => |
| 22 | + { |
| 23 | + val tableBuilder = generateMarkdownTable() |
| 24 | + val dirName = s._1 |
| 25 | + val files = s._2 |
| 26 | + val lines = extractTextFromHtml(files) |
| 27 | + lines.foreach { l => |
| 28 | + tableBuilder addRow (l._1, l._2) |
| 29 | + } |
| 30 | + generateMarkdownFile(dirName, tableBuilder, targetDir.toScala) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private def extractFilesWithDirName(file: File): Seq[(String, Seq[File])] = { |
| 36 | + file match { |
| 37 | + case f if f.isDirectory => |
| 38 | + ls(file).collect { |
| 39 | + case f if f.isDirectory => (f.name, ls(f).toSeq) |
| 40 | + }.toSeq |
| 41 | + case f => Seq((f.name, Seq(f))) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private def extractTextFromHtml(files: Seq[File]): Seq[(String, String)] = { |
| 46 | + files.map { f => |
| 47 | + val doc = JsoupBrowser().parseFile(f.toJava) |
| 48 | + (doc >> text("title"), doc >> text("div.doc > p")) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private def generateMarkdownTable(): Table.Builder = { |
| 53 | + new Table.Builder() |
| 54 | + .withAlignment(Table.ALIGN_LEFT) |
| 55 | + .addRow(tableFirstRow._1, tableFirstRow._2) |
| 56 | + } |
| 57 | + |
| 58 | + private def generateMarkdownFile(filePrefix: String, tableBuilder: Table.Builder, targetDir: File) = { |
| 59 | + val table = tableBuilder.build() |
| 60 | + val file = File(s"${targetDir}${filePrefix}${fileSuffix}") |
| 61 | + file < table.serialize() |
| 62 | + } |
| 63 | +} |
0 commit comments