-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #508 from eed3si9n/wip/snippet
implement Snippet mechanism like Paradox
- Loading branch information
Showing
12 changed files
with
158 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sbt._ | ||
import Keys._ | ||
|
||
object LowTechSnippetPamfletPlugin extends AutoPlugin { | ||
import com.typesafe.sbt.site.pamflet._ | ||
import PamfletPlugin.autoImport._ | ||
import pamflet._ | ||
|
||
override def requires = PamfletPlugin | ||
override def trigger = noTrigger | ||
override def projectSettings = pamfletSettings | ||
|
||
def pamfletSettings: Seq[Setting[_]] = | ||
Seq( | ||
mappings in Pamflet := generate( | ||
(sourceDirectory in Pamflet).value, | ||
(target.value / "lowtech_generated"), | ||
(target in Pamflet).value, | ||
(includeFilter in Pamflet).value, | ||
(pamfletFencePlugins in Pamflet).value | ||
) | ||
) | ||
|
||
def generate(input: File, | ||
generated: File, | ||
output: File, | ||
includeFilter: FileFilter, | ||
fencePlugins: Seq[FencePlugin]): Seq[(File, String)] = { | ||
// this is the added step | ||
Snippet.processDirectory(input, generated) | ||
val storage = FileStorage(generated, fencePlugins.toList) | ||
Produce(storage.globalized, output) | ||
output ** includeFilter --- output pair Path.relativeTo(output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import sbt._ | ||
import Keys._ | ||
import Path.rebase | ||
|
||
object Snippet { | ||
|
||
/** | ||
* Processes all files under base directory, and outputs to newBase. | ||
* Markdown files are snippet-processed, and the rest are just copied. | ||
*/ | ||
def processDirectory(base: File, newBase: File): File = { | ||
IO.createDirectory(newBase) | ||
val files: Seq[File] = (base ** (-DirectoryFilter)).get | ||
val isMarkdown = Set("markdown", "md") | ||
(files pair rebase(base :: Nil, newBase)) foreach { | ||
case (x, newFile) if isMarkdown(x.ext) => processFile(x, newFile) | ||
case (x, newFile) => IO.copyFile(x, newFile) | ||
} | ||
newBase | ||
} | ||
|
||
def processFile(baseFile: File, newFile: File): File = { | ||
val xs0 = IO.readLines(baseFile) | ||
val xs: List[String] = xs0 flatMap { | ||
case x if x.trim.startsWith("@@snip") => snippet(x, baseFile) | ||
case x => List(x) | ||
} | ||
IO.writeLines(newFile, xs) | ||
newFile | ||
} | ||
|
||
/** | ||
* Use the Lightbend Paradox syntax for snippet inclusion. | ||
* `@@snip [example.log](example.log) { #example-log type=text }` | ||
*/ | ||
lazy val Snippet = ("""\@\@snip\s*\[[^\]]+\]\(([^)]*)\)\s*""" | ||
+ """\{\s*(\#[\w-]+)?(\s+type\=[\w-]+)?.*\}\s*""").r | ||
def snippet(line: String, baseFile: File): List[String] = { | ||
def readFromRef(ref: File, tagOpt: Option[String], ty: String): List[String] = { | ||
tagOpt match { | ||
case Some(tag) => | ||
val xs0 = IO.readLines(ref) | ||
val xs = xs0 | ||
.dropWhile({ x => | ||
!x.contains(tag) | ||
}) | ||
.drop(1) | ||
.takeWhile({ x => | ||
!x.contains(tag) | ||
}) | ||
if (xs.isEmpty) { | ||
sys.error(s"@@snip was detected for $ref with tag $tag, but the code was empty!") | ||
} | ||
List(s"```$ty") ::: xs ::: List("```") | ||
case None => List(s"```$ty") ::: IO.readLines(ref) ::: List("```") | ||
} | ||
} | ||
line match { | ||
case Snippet(path0, tag0, ty0) => | ||
val tag = Option(tag0).map(_.trim) | ||
val ty = Option(ty0).map(_.trim).getOrElse("scala") | ||
val ref = resolvePath(path0, baseFile) | ||
if (!ref.exists) { | ||
sys.error(s"@@snip was detected, but $ref was not found!") | ||
} | ||
readFromRef(ref, tag, ty) | ||
case _ => sys.error(s"Invalid snippet notation: $line") | ||
} | ||
} | ||
|
||
/** | ||
* If the path starts from `$root$/` then use the path as is from root. | ||
* Otherwise, treat it as a relative path from the markdown file. | ||
*/ | ||
def resolvePath(p: String, baseFile: File): File = { | ||
if (p.startsWith("$root$/")) file(p.drop(7)) | ||
else new File(uri(baseFile.getParentFile.toURI.toString + p)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.0.0 | ||
sbt.version=1.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.3.0") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.2") | ||
|
||
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.2.0") | ||
libraryDependencies += { "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "Hello", | ||
scalaVersion := "$example_scala_version$" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "Hello", | ||
scalaVersion := "2.12.3" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
> name |