Skip to content

Commit

Permalink
Merge pull request #71 from marcospereira/build/prepare-to-release
Browse files Browse the repository at this point in the history
Prepare for next release
  • Loading branch information
marcospereira authored Nov 19, 2019
2 parents 7a56bd1 + fb9429c commit 2bb9b52
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 310 deletions.
11 changes: 11 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
align = true
assumeStandardLibraryStripMargin = true
danglingParentheses = true
docstrings = JavaDoc
maxColumn = 120
project.git = true
rewrite.rules = [ AvoidInfix, ExpandImportSelectors, RedundantParens, SortModifiers, PreferCurlyFors ]
rewrite.sortModifiers.order = [ "private", "protected", "final", "sealed", "abstract", "implicit", "override", "lazy" ]
spaces.inImportCurlyBraces = true # more idiomatic to include whitepsace in import x.{ yyy }
trailingCommas = preserve
version = 2.2.2
25 changes: 6 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
language: scala

before_install:
- curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh

before_install: curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh
install: jabba install "adopt@~1.$TRAVIS_JDK.0-0" && jabba use "$_" && java -Xmx32m -version

env:
global:
- JABBA_HOME=$HOME/.jabba
matrix:
- TRAVIS_JDK=8
- TRAVIS_JDK=11

matrix:
fast_finish: true
allow_failures:
# Java 11 is still not fully supported. It is good that we are already
# testing play-ws using it to better discover possible problems but we
# can allow failures here too.
- env: TRAVIS_JDK=11
- TRAVIS_JDK=8
- TRAVIS_JDK=11

scala:
- 2.10.7
- 2.12.8
- 2.13.0
- 2.12.10
- 2.13.1

script: sbt ++$TRAVIS_SCALA_VERSION test publishLocal
script: sbt ++$TRAVIS_SCALA_VERSION test publishLocal scalafmtCheckAll scalafmtSbtCheck

cache:
directories:
Expand Down
15 changes: 9 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ lazy val `play-doc` = (project in file("."))
crossScalaVersions := Seq(scala210, scala212, scala213)

libraryDependencies ++= Seq(
"org.pegdown" % "pegdown" % "1.6.0",
"commons-io" % "commons-io" % "2.6"
"org.pegdown" % "pegdown" % "1.6.0",
"commons-io" % "commons-io" % "2.6"
) ++ specs2Deps(scalaVersion.value)

def specs2Deps(scalaVer: String): Seq[ModuleID] = scalaVer match {
case ScalaVersions.scala210 => Seq("org.specs2" %% "specs2-core" % "3.9.5" % Test)
case _ => Seq("org.specs2" %% "specs2-core" % "4.5.1" % Test)
case _ => Seq("org.specs2" %% "specs2-core" % "4.8.1" % Test)
}

javacOptions ++= Seq(
"-source", "1.8",
"-target", "1.8",
"-source",
"1.8",
"-target",
"1.8",
"-Xlint:deprecation",
"-Xlint:unchecked",
)
Expand All @@ -30,7 +32,8 @@ scalacOptions ++= {
} else {
Seq(
"-target:jvm-1.8",
"-encoding", "utf8",
"-encoding",
"utf8",
"-deprecation",
"-feature",
"-unchecked",
Expand Down
5 changes: 3 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
addSbtPlugin("com.typesafe.play" % "interplay" % "2.1.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.5.0-M5")
addSbtPlugin("com.typesafe.play" % "interplay" % "2.1.2")
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.5.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.2.1")
43 changes: 24 additions & 19 deletions src/main/scala/play/doc/FileRepository.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package play.doc

import java.io.{FileInputStream, File, InputStream}
import java.io.FileInputStream
import java.io.File
import java.io.InputStream
import java.util.jar.JarFile
import java.util.zip.ZipEntry
import scala.collection.JavaConverters._
Expand Down Expand Up @@ -59,7 +61,6 @@ trait FileRepository {
* @param base The base dir of the file
*/
class FilesystemRepository(base: File) extends FileRepository {

private def cleanUp[A](loader: InputStream => A) = { is: InputStream =>
try {
loader(is)
Expand All @@ -82,7 +83,7 @@ class FilesystemRepository(base: File) extends FileRepository {

def handleFile[A](path: String)(handler: FileHandle => A) = {
getFile(path).map { file =>
val is = new FileInputStream(file)
val is = new FileInputStream(file)
val handle = FileHandle(file.getName, file.length, is, () => is.close())
handler(handle)
}
Expand All @@ -104,13 +105,12 @@ class FilesystemRepository(base: File) extends FileRepository {
* Jar file implementation of the repository
*/
class JarRepository(jarFile: JarFile, base: Option[String] = None) extends FileRepository {

private val PathSeparator = "/"
private val basePrefix = base.map(_ + PathSeparator).getOrElse("")
private val basePrefix = base.map(_ + PathSeparator).getOrElse("")

def getEntry(path: String): Option[(ZipEntry, InputStream)] = {
Option(jarFile.getEntry(basePrefix + path)).flatMap {
entry => Option(jarFile.getInputStream(entry)).map(is => (entry, is))
Option(jarFile.getEntry(basePrefix + path)).flatMap { entry =>
Option(jarFile.getInputStream(entry)).map(is => (entry, is))
}
}

Expand All @@ -119,24 +119,29 @@ class JarRepository(jarFile: JarFile, base: Option[String] = None) extends FileR
}

def handleFile[A](path: String)(handler: FileHandle => A) = {
getEntry(path).map { case (entry, is) =>
val handle = FileHandle(entry.getName.split(PathSeparator).last, entry.getSize, is, () => is.close())
handler(handle)
getEntry(path).map {
case (entry, is) =>
val handle = FileHandle(entry.getName.split(PathSeparator).last, entry.getSize, is, () => is.close())
handler(handle)
}
}

def findFileWithName(name: String) = {
def startsWith(full: String, part: String) = if (part.isEmpty) true else {
val comparePart = if (full.length == part.length) full else full.take(part.length)
comparePart.equalsIgnoreCase(part)
}
def endsWith(full: String, part: String) = if (part.isEmpty) true else {
val comparePart = if (full.length == part.length) full else full.takeRight(part.length)
comparePart.equalsIgnoreCase(part)
}
def startsWith(full: String, part: String) =
if (part.isEmpty) true
else {
val comparePart = if (full.length == part.length) full else full.take(part.length)
comparePart.equalsIgnoreCase(part)
}
def endsWith(full: String, part: String) =
if (part.isEmpty) true
else {
val comparePart = if (full.length == part.length) full else full.takeRight(part.length)
comparePart.equalsIgnoreCase(part)
}

val slashName = PathSeparator + name
val found = jarFile.entries().asScala.map(_.getName).find(n => startsWith(n, basePrefix) && endsWith(n, slashName))
val found = jarFile.entries().asScala.map(_.getName).find(n => startsWith(n, basePrefix) && endsWith(n, slashName))
found.map(_.substring(basePrefix.length))
}

Expand Down
114 changes: 59 additions & 55 deletions src/main/scala/play/doc/PageIndex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ sealed trait TocTree {

/**
* A table of contents
*
*
* @param title The title of this table of contents
* @param nodes The nodes in the table of contents
* @param descend Whether a table of contents should descend into this table of contents
*/
case class Toc(name: String, title: String, nodes: List[(String, TocTree)], descend: Boolean = true) extends TocTree {
require(nodes.nonEmpty)
def page = nodes.head._2.page

def page = nodes.head._2.page
}

/**
* A page (leaf node) pointed to by the table of contents
*
*
* @param page The page
* @param title The title of the page
* @param next Explicitly provided next links. If None, then the index structure are used to generate the next links,
Expand All @@ -44,12 +44,10 @@ case class TocPage(page: String, title: String, next: Option[List[String]]) exte

/**
* The page index
*
*
* @param toc The table of contents
*/
class PageIndex(val toc: Toc, path: Option[String] = None) {


private val byPage: Map[String, Page] = {
// First, create a by name index
def indexByName(node: TocTree): List[(String, TocTree)] = node match {
Expand All @@ -60,27 +58,29 @@ class PageIndex(val toc: Toc, path: Option[String] = None) {
}
val byNameMap = indexByName(toc).toMap


def indexPages(path: Option[String], nav: List[Toc], toc: Toc): List[Page] = {
toc.nodes.flatMap {
case (_, TocPage(page, title, explicitNext)) =>
val nextLinks = explicitNext.map { links =>
links.collect(Function.unlift(byNameMap.get))
}.getOrElse {
findNext(page, nav).toList
}
val nextLinks = explicitNext
.map { links =>
links.collect(Function.unlift(byNameMap.get))
}
.getOrElse {
findNext(page, nav).toList
}
List(Page(page, path, title, nav, nextLinks))
case (pathPart, tocPart: Toc) => indexPages(
path.map(_ + "/" + pathPart).orElse(Some(pathPart)), tocPart :: nav, tocPart
)
case (pathPart, tocPart: Toc) =>
indexPages(
path.map(_ + "/" + pathPart).orElse(Some(pathPart)),
tocPart :: nav,
tocPart
)
}
}

indexPages(path, List(toc), toc).map(p => p.page -> p).toMap
}



private def findNext(name: String, nav: List[Toc]): Option[TocTree] = {
nav match {
case Nil => None
Expand All @@ -100,7 +100,6 @@ class PageIndex(val toc: Toc, path: Option[String] = None) {
* Get the page for the given page name
*/
def get(page: String): Option[Page] = byPage.get(page)

}

/**
Expand All @@ -120,52 +119,57 @@ case class Page(page: String, path: Option[String], title: String, nav: List[Toc
}

object PageIndex {

def parseFrom(repo: FileRepository, home: String, path: Option[String] = None): Option[PageIndex] = {
parseToc(repo, path, "", home) match {
case toc: Toc => Some(new PageIndex(toc, path))
case _ => None
case _ => None
}
}

private def parseToc(repo: FileRepository, path: Option[String], page: String, title: String,
descend: Boolean = true, next: Option[List[String]] = None): TocTree = {
repo.loadFile(path.fold("index.toc")(_ + "/index.toc"))(IOUtils.toString(_, "utf-8")).fold[TocTree](
TocPage(page, title, next)
) { content =>
// https://github.com/scala/bug/issues/11125#issuecomment-423375868
val lines = augmentString(content).lines.toList.map(_.trim).filter(_.nonEmpty)
// Remaining lines are the entries of the contents
val tocNodes = lines.map { entry =>
val linkAndTitle :: params = entry.split(";").toList
val (link, title) = {
linkAndTitle.split(":", 2) match {
case Array(p) => p -> p
case Array(p, t) => p -> t
private def parseToc(
repo: FileRepository,
path: Option[String],
page: String,
title: String,
descend: Boolean = true,
next: Option[List[String]] = None
): TocTree = {
repo
.loadFile(path.fold("index.toc")(_ + "/index.toc"))(IOUtils.toString(_, "utf-8"))
.fold[TocTree](
TocPage(page, title, next)
) { content =>
// https://github.com/scala/bug/issues/11125#issuecomment-423375868
val lines = augmentString(content).lines.toList.map(_.trim).filter(_.nonEmpty)
// Remaining lines are the entries of the contents
val tocNodes = lines.map { entry =>
val linkAndTitle :: params = entry.split(";").toList
val (link, title) = {
linkAndTitle.split(":", 2) match {
case Array(p) => p -> p
case Array(p, t) => p -> t
}
}
}
val parsedParams = params.map { param =>
param.split("=", 2) match {
case Array(k) => k -> k
case Array(k, v) => k -> v
val parsedParams = params.map { param =>
param.split("=", 2) match {
case Array(k) => k -> k
case Array(k, v) => k -> v
}
}.toMap

val next = parsedParams.get("next").map { n =>
n.split(",").toList
}
}.toMap

val next = parsedParams.get("next").map { n =>
n.split(",").toList
}
val (relPath, descend) = if (link.startsWith("!")) {
link.drop(1) -> false
} else {
link -> true
}

val (relPath, descend) = if (link.startsWith("!")) {
link.drop(1) -> false
} else {
link -> true
relPath -> parseToc(repo, path.map(_ + "/" + relPath).orElse(Some(relPath)), relPath, title, descend, next)
}

relPath -> parseToc(repo, path.map(_ + "/" + relPath).orElse(Some(relPath)), relPath, title, descend, next)

Toc(page, title, tocNodes, descend)
}
Toc(page, title, tocNodes, descend)
}
}

}
}
Loading

0 comments on commit 2bb9b52

Please sign in to comment.