Skip to content

Commit

Permalink
Merge pull request #126 from jonas/fiddle-cleanups
Browse files Browse the repository at this point in the history
Fiddle cleanups
  • Loading branch information
pvlugter authored Jun 8, 2017
2 parents 6ea3ec1 + 3de37ee commit 7fc76a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
37 changes: 11 additions & 26 deletions core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,8 @@ case class SnipDirective(page: Page, variables: Map[String, String])
try {
val labels = node.attributes.values("identifier").asScala
val source = resolvedSource(node, page)
val file =
if (source startsWith "$") {
val baseKey = source.drop(1).takeWhile(_ != '$')
val base = new File(PropertyUrl(s"snip.$baseKey.base_dir", variables.get).base.trim)
val effectiveBase = if (base.isAbsolute) base else new File(page.file.getParentFile, base.toString)
new File(effectiveBase, source.drop(baseKey.length + 2))
} else new File(page.file.getParentFile, source)
val text = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file))
val (text, snippetLang) = Snippet("snip", source, labels, page, variables)
val lang = Option(node.attributes.value("type")).getOrElse(snippetLang)
val group = Option(node.attributes.value("group")).getOrElse("")
new VerbatimGroupNode(text, lang, group).accept(visitor)
} catch {
Expand Down Expand Up @@ -344,25 +337,17 @@ case class FiddleDirective(page: Page, variables: Map[String, String])
val extraParams = node.attributes.value("extraParams", "theme=light")
val cssStyle = node.attributes.value("cssStyle", "overflow: hidden;")
val source = resolvedSource(node, page)
val file = if (source startsWith "$") {
val baseKey = source.drop(1).takeWhile(_ != '$')
val base = new File(PropertyUrl(s"fiddle.$baseKey.base_dir", variables.get).base.trim)
val effectiveBase = if (base.isAbsolute) base else new File(page.file.getParentFile, base.toString)
new File(effectiveBase, source.drop(baseKey.length + 2))
} else new File(page.file.getParentFile, source)
val text = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file))
val (text, _) = Snippet("fiddle", source, labels, page, variables)

val fiddleSource = java.net.URLEncoder.encode(
"""
|import fiddle.Fiddle, Fiddle.println
| @scalajs.js.annotation.JSExport
| object ScalaFiddle {
| // $FiddleStart
|""".stripMargin + text + """
| // $FiddleEnd
| }
""".stripMargin, "UTF-8")
"""import fiddle.Fiddle, Fiddle.println
|@scalajs.js.annotation.JSExport
|object ScalaFiddle {
| // $FiddleStart
|""".stripMargin + text + """
| // $FiddleEnd
|}
|""".stripMargin, "UTF-8")

printer.println.print(s"""
<iframe class="$cssClass" $width $height src="$baseUrl?$extraParams&source=$fiddleSource" frameborder="0" style="$cssStyle"></iframe>
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ object Snippet {

class SnippetException(message: String) extends RuntimeException(message)

def apply(file: File, labels: Seq[String]): String = labels match {
def apply(propPrefix: String, source: String, labels: Seq[String], page: Page, variables: Map[String, String]): (String, String) = {
val file =
if (source startsWith "$") {
val baseKey = source.drop(1).takeWhile(_ != '$')
val base = new File(PropertyUrl(s"$propPrefix.$baseKey.base_dir", variables.get).base.trim)
val effectiveBase = if (base.isAbsolute) base else new File(page.file.getParentFile, base.toString)
new File(effectiveBase, source.drop(baseKey.length + 2))
} else new File(page.file.getParentFile, source)

(extract(file, labels), language(file))
}

def extract(file: File, labels: Seq[String]): String = labels match {
case Seq() => extract(file, _ => true, _ => false, addFilteredLine).snippetLines.mkString("\n")
case _ => labels.map(label => extract(file, label)).mkString("\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FiddleDirectiveSpec extends MarkdownBaseSpec {
|@@fiddle [FiddleDirectiveSpec.scala](./FiddleDirectiveSpec.scala) { #fiddle_code extraParams=theme=light&layout=v75 cssStyle=width:100%; }
""").values.head shouldEqual html("""
|<iframe class="fiddle" src=
"https://embed.scalafiddle.io/embed?theme=light&amp;layout=v75&amp;source=%0Aimport+fiddle.Fiddle%2C+Fiddle.println%0A+%40scalajs.js.annotation.JSExport%0A+object+ScalaFiddle+%7B%0A+++%2F%2F+%24FiddleStart%0Aval+sourcePath+%3D+new+java.io.File%28%22.%22%29.getAbsolutePath+%2B+%22%2Fcore%2Fsrc%2Ftest%2Fscala%2F%22%0A+++%2F%2F+%24FiddleEnd%0A+%7D%0A++++++++++"
"https://embed.scalafiddle.io/embed?theme=light&amp;layout=v75&amp;source=import+fiddle.Fiddle%2C+Fiddle.println%0A%40scalajs.js.annotation.JSExport%0Aobject+ScalaFiddle+%7B%0A++%2F%2F+%24FiddleStart%0Aval+sourcePath+%3D+new+java.io.File%28%22.%22%29.getAbsolutePath+%2B+%22%2Fcore%2Fsrc%2Ftest%2Fscala%2F%22%0A++%2F%2F+%24FiddleEnd%0A%7D%0A"
frameborder="0" style="width:100%;"></iframe>
""".stripMargin)
}
Expand All @@ -39,7 +39,7 @@ class FiddleDirectiveSpec extends MarkdownBaseSpec {
|@@fiddle [FiddleDirectiveSpec.scala](./FiddleDirectiveSpec.scala) { #fiddle_code width=100px height=100px extraParams=theme=light&layout=v75 cssStyle=width:100%; }
""").values.head shouldEqual html("""
|<iframe class="fiddle" width="100px" height="100px" src=
"https://embed.scalafiddle.io/embed?theme=light&amp;layout=v75&amp;source=%0Aimport+fiddle.Fiddle%2C+Fiddle.println%0A+%40scalajs.js.annotation.JSExport%0A+object+ScalaFiddle+%7B%0A+++%2F%2F+%24FiddleStart%0Aval+sourcePath+%3D+new+java.io.File%28%22.%22%29.getAbsolutePath+%2B+%22%2Fcore%2Fsrc%2Ftest%2Fscala%2F%22%0A+++%2F%2F+%24FiddleEnd%0A+%7D%0A++++++++++"
"https://embed.scalafiddle.io/embed?theme=light&amp;layout=v75&amp;source=import+fiddle.Fiddle%2C+Fiddle.println%0A%40scalajs.js.annotation.JSExport%0Aobject+ScalaFiddle+%7B%0A++%2F%2F+%24FiddleStart%0Aval+sourcePath+%3D+new+java.io.File%28%22.%22%29.getAbsolutePath+%2B+%22%2Fcore%2Fsrc%2Ftest%2Fscala%2F%22%0A++%2F%2F+%24FiddleEnd%0A%7D%0A"
frameborder="0" style="width:100%;"></iframe>
""".stripMargin)
}
Expand All @@ -50,7 +50,7 @@ class FiddleDirectiveSpec extends MarkdownBaseSpec {
|@@fiddle [FiddleDirectiveSpec.scala](./FiddleDirectiveSpec.scala) { #fiddle_code baseUrl=http://shadowscalafiddle.io width=100px height=100px extraParams=theme=light&layout=v75 cssStyle=width:100%; }
""").values.head shouldEqual html("""
|<iframe class="fiddle" width="100px" height="100px" src=
"http://shadowscalafiddle.io?theme=light&amp;layout=v75&amp;source=%0Aimport+fiddle.Fiddle%2C+Fiddle.println%0A+%40scalajs.js.annotation.JSExport%0A+object+ScalaFiddle+%7B%0A+++%2F%2F+%24FiddleStart%0Aval+sourcePath+%3D+new+java.io.File%28%22.%22%29.getAbsolutePath+%2B+%22%2Fcore%2Fsrc%2Ftest%2Fscala%2F%22%0A+++%2F%2F+%24FiddleEnd%0A+%7D%0A++++++++++"
"http://shadowscalafiddle.io?theme=light&amp;layout=v75&amp;source=import+fiddle.Fiddle%2C+Fiddle.println%0A%40scalajs.js.annotation.JSExport%0Aobject+ScalaFiddle+%7B%0A++%2F%2F+%24FiddleStart%0Aval+sourcePath+%3D+new+java.io.File%28%22.%22%29.getAbsolutePath+%2B+%22%2Fcore%2Fsrc%2Ftest%2Fscala%2F%22%0A++%2F%2F+%24FiddleEnd%0A%7D%0A"
frameborder="0" style="width:100%;"></iframe>
""".stripMargin)
}
Expand Down

0 comments on commit 7fc76a1

Please sign in to comment.