|
| 1 | +/** |
| 2 | + * Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com> |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | +package com.lightbend.paradox.unidoc |
| 7 | + |
| 8 | +import com.lightbend.paradox.tree.Tree.{Forest, Location} |
| 9 | +import java.io.{File, PrintWriter} |
| 10 | + |
| 11 | +import org.scalatest.{FlatSpec, Matchers} |
| 12 | +import com.lightbend.paradox.template.PageTemplate |
| 13 | +import java.nio.file._ |
| 14 | + |
| 15 | +import com.lightbend.paradox.markdown._ |
| 16 | + |
| 17 | +abstract class MarkdownBaseSpec extends FlatSpec with Matchers { |
| 18 | + |
| 19 | + val markdownReader = new Reader |
| 20 | + val markdownWriter = new Writer |
| 21 | + |
| 22 | + def markdown(text: String)(implicit context: Location[Page] => Writer.Context = writerContext): String = { |
| 23 | + markdownPages("test.md" -> text).getOrElse("test.html", "") |
| 24 | + } |
| 25 | + |
| 26 | + def markdownPages(mappings: (String, String)*)(implicit context: Location[Page] => Writer.Context = writerContext): Map[String, String] = { |
| 27 | + def render(location: Option[Location[Page]], rendered: Seq[(String, String)] = Seq.empty): Seq[(String, String)] = location match { |
| 28 | + case Some(loc) => |
| 29 | + val page = loc.tree.label |
| 30 | + val html = normalize(markdownWriter.write(page.markdown, context(loc))) |
| 31 | + render(loc.next, rendered :+ (page.path, html)) |
| 32 | + case None => rendered |
| 33 | + } |
| 34 | + render(Location.forest(pages(mappings: _*))).toMap |
| 35 | + } |
| 36 | + |
| 37 | + def layoutPages(mappings: (String, String)*)(templates: (String, String)*)(implicit context: Location[Page] => Writer.Context = writerContext): Map[String, String] = { |
| 38 | + val templateDirectory = Files.createTempDirectory("templates") |
| 39 | + createFileTemplates(templateDirectory, templates) |
| 40 | + def render(location: Option[Location[Page]], rendered: Seq[(String, String)] = Seq.empty): Seq[(String, String)] = location match { |
| 41 | + case Some(loc) => |
| 42 | + val page = loc.tree.label |
| 43 | + val html = normalize(markdownWriter.write(page.markdown, context(loc))) |
| 44 | + val outputFile = new File(page.path) |
| 45 | + val emptyPageContext = PartialPageContent(page.properties.get, html) |
| 46 | + val template = new PageTemplate(new File(templateDirectory.toString)) |
| 47 | + template.write(page.properties(Page.Properties.DefaultLayoutMdIndicator, template.defaultName), emptyPageContext, outputFile, new PageTemplate.ErrorLogger(s => println("[error] " + s))) |
| 48 | + val fileContent = fileToContent(outputFile) |
| 49 | + outputFile.delete |
| 50 | + render(loc.next, rendered :+ (page.path, normalize(fileContent))) |
| 51 | + case None => rendered |
| 52 | + } |
| 53 | + render(Location.forest(pages(mappings: _*))).toMap |
| 54 | + } |
| 55 | + |
| 56 | + def fileToContent(file: File): String = { |
| 57 | + import scala.io.Source |
| 58 | + Source.fromFile(file).getLines.mkString("\n") |
| 59 | + } |
| 60 | + |
| 61 | + def createFileTemplates(dir: Path, templates: Seq[(String, String)]) = { |
| 62 | + val suffix = ".st" |
| 63 | + (templates map { |
| 64 | + case (path, content) if (path.endsWith(suffix)) => |
| 65 | + val writer = new PrintWriter(new File(dir.toString + "/" + path)) |
| 66 | + writer.write(prepare(content)) |
| 67 | + writer.close() |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + def writerContextWithProperties(properties: (String, String)*): Location[Page] => Writer.Context = { location => |
| 72 | + writerContext(location).copy(properties = properties.toMap) |
| 73 | + } |
| 74 | + |
| 75 | + def writerContext(location: Location[Page]): Writer.Context = { |
| 76 | + Writer.Context( |
| 77 | + location, |
| 78 | + Page.allPaths(List(location.root.tree)).toSet, |
| 79 | + groups = Map("Language" -> Seq("Scala", "Java")) |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + def pages(mappings: (String, String)*): Forest[Page] = { |
| 84 | + import com.lightbend.paradox.markdown.Path |
| 85 | + val parsed = mappings map { |
| 86 | + case (path, text) => |
| 87 | + val frontin = Frontin(prepare(text)) |
| 88 | + (new File(path), path, markdownReader.read(frontin.body), frontin.header) |
| 89 | + } |
| 90 | + Page.forest(parsed, Path.replaceSuffix(Writer.DefaultSourceSuffix, Writer.DefaultTargetSuffix)) |
| 91 | + } |
| 92 | + |
| 93 | + def html(text: String): String = { |
| 94 | + normalize(prepare(text)) |
| 95 | + } |
| 96 | + |
| 97 | + def htmlPages(mappings: (String, String)*): Map[String, String] = { |
| 98 | + (mappings map { case (path, text) => (path, html(text)) }).toMap |
| 99 | + } |
| 100 | + |
| 101 | + def prepare(text: String): String = { |
| 102 | + text.stripMargin.trim |
| 103 | + } |
| 104 | + |
| 105 | + def normalize(html: String) = { |
| 106 | + val reader = new java.io.StringReader(html) |
| 107 | + val writer = new java.io.StringWriter |
| 108 | + val tidy = new org.w3c.tidy.Tidy |
| 109 | + tidy.setTabsize(2) |
| 110 | + tidy.setPrintBodyOnly(true) |
| 111 | + tidy.setTrimEmptyElements(false) |
| 112 | + tidy.setShowWarnings(false) |
| 113 | + tidy.setQuiet(true) |
| 114 | + tidy.parse(reader, writer) |
| 115 | + writer.toString.replace("\r\n", "\n").replace("\r", "\n") |
| 116 | + } |
| 117 | + |
| 118 | + case class PartialPageContent(properties: Map[String, String], content: String) extends PageTemplate.Contents { |
| 119 | + import scala.collection.JavaConverters._ |
| 120 | + |
| 121 | + val getTitle = "" |
| 122 | + val getContent = content |
| 123 | + |
| 124 | + lazy val getBase = "" |
| 125 | + lazy val getHome = new EmptyLink() |
| 126 | + lazy val getPrev = new EmptyLink() |
| 127 | + lazy val getNext = new EmptyLink() |
| 128 | + lazy val getBreadcrumbs = "" |
| 129 | + lazy val getNavigation = "" |
| 130 | + lazy val hasSubheaders = false |
| 131 | + lazy val getToc = "" |
| 132 | + lazy val getSource_url = "" |
| 133 | + |
| 134 | + lazy val getProperties = properties.asJava |
| 135 | + } |
| 136 | + |
| 137 | + case class EmptyLink() extends PageTemplate.Link { |
| 138 | + lazy val getHref: String = "" |
| 139 | + lazy val getHtml: String = "" |
| 140 | + lazy val getTitle: String = "" |
| 141 | + lazy val isActive: Boolean = false |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments