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

fiddle fixes: string format, path config from snip #117

Merged
merged 1 commit into from
May 18, 2017
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
28 changes: 17 additions & 11 deletions core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ object SnipDirective {
*
* Extracts fiddles from source files into fiddle blocks.
*/
case class FiddleDirective(page: Page) extends LeafBlockDirective("fiddle") with SourceDirective {
case class FiddleDirective(page: Page, variables: Map[String, String])
extends LeafBlockDirective("fiddle") with SourceDirective {

def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
try {
Expand All @@ -346,20 +347,25 @@ case class FiddleDirective(page: Page) extends LeafBlockDirective("fiddle") with
val height = Option(node.attributes.value("height")).map("height=" + _).getOrElse("")
val extraParams = node.attributes.value("extraParams", "theme=light")
val cssStyle = node.attributes.value("cssStyle", "overflow: hidden;")

val file = new File(page.file.getParentFile, resolvedSource(node, page))
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 fiddleSource = java.net.URLEncoder.encode(
"""|
| import fiddle.Fiddle, Fiddle.println
| @scalajs.js.annotation.JSExport
| object ScalaFiddle {
| // $FiddleStart
""" + text + """
| // $FiddleEnd
| }
"""
|import fiddle.Fiddle, Fiddle.println
| @scalajs.js.annotation.JSExport
| object ScalaFiddle {
| // $FiddleStart
|""".stripMargin + text + """
| // $FiddleEnd
| }
""".stripMargin, "UTF-8")

printer.println.print(s"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ object Writer {
context => JavadocDirective(context.location.tree.label, context.properties),
context => GitHubDirective(context.location.tree.label, context.properties),
context => SnipDirective(context.location.tree.label, context.properties),
context => FiddleDirective(context.location.tree.label),
context => FiddleDirective(context.location.tree.label, context.properties),
context => TocDirective(context.location),
context => VarDirective(context.properties),
context => VarsDirective(context.properties),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,31 @@ 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=%7C%0A+++++++++++%7C+import+fiddle.Fiddle%2C+Fiddle.println%0A+++++++++++%7C+%40scalajs.js.annotation.JSExport%0A+++++++++++%7C+object+ScalaFiddle+%7B%0A+++++++++++%7C+++%2F%2F+%24FiddleStart%0A++++++++++++++++++val+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=%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++++++++++"
frameborder="0" style="width:100%;"></iframe>
""")
""".stripMargin)
}

it should "properly add width and height" in {
{
markdownPages(
sourcePath + "com/lightbend/paradox/markdown/FiddleDirectiveSpec.scala" -> """
markdownPages(
sourcePath + "com/lightbend/paradox/markdown/FiddleDirectiveSpec.scala" -> """
|@@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=%7C%0A+++++++++++%7C+import+fiddle.Fiddle%2C+Fiddle.println%0A+++++++++++%7C+%40scalajs.js.annotation.JSExport%0A+++++++++++%7C+object+ScalaFiddle+%7B%0A+++++++++++%7C+++%2F%2F+%24FiddleStart%0A++++++++++++++++++val+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=%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++++++++++"
frameborder="0" style="width:100%;"></iframe>
""")
}
""".stripMargin)
}

it should "change base url" in {
{
markdownPages(
sourcePath + "com/lightbend/paradox/markdown/FiddleDirectiveSpec.scala" -> """
markdownPages(
sourcePath + "com/lightbend/paradox/markdown/FiddleDirectiveSpec.scala" -> """
|@@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=%7C%0A+++++++++++%7C+import+fiddle.Fiddle%2C+Fiddle.println%0A+++++++++++%7C+%40scalajs.js.annotation.JSExport%0A+++++++++++%7C+object+ScalaFiddle+%7B%0A+++++++++++%7C+++%2F%2F+%24FiddleStart%0A++++++++++++++++++val+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=%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++++++++++"
frameborder="0" style="width:100%;"></iframe>
""")
}
""".stripMargin)
}

}