Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor syntax improvements #514

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions core/src/main/scala/com/lightbend/paradox/ErrorContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package com.lightbend.paradox

import java.io.File

import com.lightbend.paradox.markdown.Page
import org.pegdown.ast.Node

import scala.collection.mutable.ListBuffer
import scala.io.BufferedSource

trait ErrorContext {
def apply(msg: String, index: Int): Unit
Expand All @@ -46,7 +46,7 @@ trait ErrorContext {
class ErrorCollector extends ErrorContext {
private val errors = new ListBuffer[ParadoxError]()

private def addError(msg: String, page: Option[File], index: Option[Int]) = {
private def addError(msg: String, page: Option[File], index: Option[Int]): Unit = {
errors.append(ParadoxError(msg, page, index))
}

Expand All @@ -62,7 +62,7 @@ class ErrorCollector extends ErrorContext {

def errorCount: Int = errors.toList.distinct.size

def logErrors(log: ParadoxLogger) = {
def logErrors(log: ParadoxLogger): Unit = {
val totalErrors = errors.toList.distinct
// First log general errors
totalErrors.foreach {
Expand All @@ -78,7 +78,13 @@ class ErrorCollector extends ErrorContext {
.foreach {
case (page, errors) =>
// Load contents of the page
val lines = scala.io.Source.fromFile(page)("UTF-8").getLines().toList
Copy link
Contributor Author

@mdedetrich mdedetrich Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intellij handily picked up the fact that the handle from source is never actually closed which actually causes a leak hence the change here. There are nicer ways of doing this i.e. using Using but this is only available in Scala 2.13

var source: BufferedSource = null
val lines = try {
source = scala.io.Source.fromFile(page)("UTF-8")
source.getLines().toList
} finally {
source.close()
}
errors.sortBy(_.index.getOrElse(0)).foreach {
case ParadoxError(error, _, Some(idx)) =>
val (_, lineNo, colNo, line) = lines.foldLeft((0, 0, 0, None: Option[String])) { (state, line) =>
Expand Down
65 changes: 35 additions & 30 deletions core/src/main/scala/com/lightbend/paradox/ParadoxProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.jsoup.nodes.Document
import org.pegdown.ast._

import java.net.SocketTimeoutException
import java.util
import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.util.control.NonFatal
Expand Down Expand Up @@ -162,7 +163,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,
errorCollector.errorCount
}

private def validateExternalLink(capturedLink: CapturedLink, retryCount: Int, errorContext: ErrorContext, logger: ParadoxLogger) = {
private def validateExternalLink(capturedLink: CapturedLink, retryCount: Int, errorContext: ErrorContext, logger: ParadoxLogger): Unit = {
logger.info(s"Validating external link: ${capturedLink.link}")

def reportError = reportErrorOnSources(errorContext, capturedLink.allSources)(_)
Expand All @@ -181,7 +182,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,
// but if you've already read the response body, that will throw an exception, and there's no way to check if
// you've already tried to read the response body, so we can't do that in a finally block, we have to do it
// explicitly every time we don't want to consume the stream.
def close() = response.bodyStream().close()
def close(): Unit = response.bodyStream().close()

if (response.statusCode() / 100 == 3) {
close()
Expand Down Expand Up @@ -305,13 +306,17 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,
private def createMetadata(outputDirectory: File, properties: Map[String, String]): (File, String) = {
val metadataFilename = "paradox.json"
val target = new File(outputDirectory, metadataFilename)
val osWriter = new OutputStreamWriter(new FileOutputStream(target), StandardCharsets.UTF_8)
osWriter.write(
s"""{
| "name" : "${properties("project.name")}",
| "version" : "${properties("project.version")}"
|}""".stripMargin)
osWriter.close()
var osWriter: OutputStreamWriter = null
try {
osWriter = new OutputStreamWriter(new FileOutputStream(target), StandardCharsets.UTF_8)
osWriter.write(
s"""{
| "name" : "${properties("project.name")}",
| "version" : "${properties("project.version")}"
|}""".stripMargin)
} finally {
osWriter.close()
}
(target, metadataFilename)
}

Expand All @@ -323,8 +328,8 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,

private val page = loc.tree.label

val getTitle = page.title
val getContent =
val getTitle: String = page.title
val getContent: String =
try writer.writeContent(page.markdown, context)
catch {
case e: Throwable =>
Expand All @@ -333,23 +338,23 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,
""
}

lazy val getBase = page.base
lazy val getHome = link(Some(loc.root))
lazy val getPrev = link(loc.prev)
lazy val getSelf = link(Some(loc))
lazy val getNext = link(loc.next)
lazy val getBreadcrumbs = writer.writeBreadcrumbs(Breadcrumbs.markdown(leadingBreadcrumbs, loc.path), context)
lazy val getNavigation = writer.writeNavigation(navToc.root(loc), context)
lazy val getGroups = Groups.html(groups)
lazy val hasSubheaders = page.headers.nonEmpty
lazy val getToc = writer.writeToc(pageToc.headers(loc), context)
lazy val getSource_url = githubLink(Some(loc)).getHtml
def getPath = page.path
lazy val getBase: String = page.base
lazy val getHome: PageTemplate.Link = link(Some(loc.root))
lazy val getPrev: PageTemplate.Link = link(loc.prev)
lazy val getSelf: PageTemplate.Link = link(Some(loc))
lazy val getNext: PageTemplate.Link = link(loc.next)
lazy val getBreadcrumbs: String = writer.writeBreadcrumbs(Breadcrumbs.markdown(leadingBreadcrumbs, loc.path), context)
lazy val getNavigation: String = writer.writeNavigation(navToc.root(loc), context)
lazy val getGroups: String = Groups.html(groups)
lazy val hasSubheaders: Boolean = page.headers.nonEmpty
lazy val getToc: String = writer.writeToc(pageToc.headers(loc), context)
lazy val getSource_url: String = githubLink(Some(loc)).getHtml
def getPath: String = page.path

// So you can $page.properties.("project.name")$
lazy val getProperties = context.properties.asJava
lazy val getProperties: util.Map[String, String] = context.properties.asJava
// So you can $if(page.property_is.("project.license").("Apache-2.0"))$
lazy val getProperty_is = context.properties.map {
lazy val getProperty_is: util.Map[String, util.Map[String, Boolean]] = context.properties.map {
case (key, value) => (key -> Map(value -> true).asJava)
}.asJava

Expand Down Expand Up @@ -393,15 +398,15 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,
lazy val getTitle: String = location.map(title).orNull
lazy val isActive: Boolean = false

override def variables = context.properties
override def variables: Map[String, String] = context.properties

private def href(location: Location[Page]): String = {
try {
val sourceFilePath = location.tree.label.file.toString
val rootPath = new File(".").getCanonicalFile.toString
(treeUrl / Path.relativeLocalPath(rootPath, sourceFilePath)).toString
} catch {
case e: Url.Error => null
case _: Url.Error => null
}
}

Expand Down Expand Up @@ -442,14 +447,14 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer,
val labels = include.attributes.values("identifier").asScala
val source = include.source match {
case direct: DirectiveNode.Source.Direct => direct.value
case other =>
case _ =>
error(s"Only explicit links are supported by the include directive, reference links are not", file, include)
""
}
val includeFile = SourceDirective.resolveFile("include", source, file, properties)
val frontin = Frontin(includeFile)
val filterLabels = Directive.filterLabels("include", include.attributes, labels.toSeq, properties)
val (text, snippetLang) = Snippet(includeFile, labels.toSeq, filterLabels)
val filterLabels = Directive.filterLabels("include", include.attributes, labels, properties)
val (text, snippetLang) = Snippet(includeFile, labels, filterLabels)
// I guess we could support multiple markup languages in future...
if (snippetLang != "md" && snippetLang != "markdown") {
error(s"Don't know how to include '*.$snippetLang' content.", file, include)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object Breadcrumbs {
*/
def markdown(leadingBreadcrumbs: List[(String, String)], locations: List[Location[Page]]): BulletListNode =
locations match {
case current :: parents => crumbs(current.tree.label.base, current.tree.label.path, leadingBreadcrumbs, locations.reverse)
case _ => list(Nil)
case current :: _ => crumbs(current.tree.label.base, current.tree.label.path, leadingBreadcrumbs, locations.reverse)
case _ => list(Nil)
}

private def crumbs(base: String, active: String, leadingBreadcrumbs: List[(String, String)], locations: List[Location[Page]]): BulletListNode = {
Expand Down
Loading