Skip to content

Commit

Permalink
Merge pull request #16 from andreaTP/fiddle
Browse files Browse the repository at this point in the history
Basic Scalafiddle integration
  • Loading branch information
eed3si9n authored Sep 5, 2016
2 parents effa0b3 + 2d157cb commit 090a1e9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
52 changes: 52 additions & 0 deletions core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,58 @@ object SnipDirective {

}

/**
* Fiddle directive.
*
* Extracts fiddles from source files into fiddle blocks.
*/
case class FiddleDirective(page: Page) extends LeafBlockDirective("fiddle") {
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
try {
val label = Option(node.attributes.identifier)

val baseUrl = node.attributes.value("baseUrl", "https://embed.scalafiddle.io/embed")
val cssClass = node.attributes.value("cssClass", "fiddle")
val width = Option(node.attributes.value("width")).map("width=" + _).getOrElse("")
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, node.source)
val text = Snippet(file, label)
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
| }
""".stripMargin, "UTF-8")

printer.println.print(s"""
<iframe class="$cssClass" $width $height src="$baseUrl?$extraParams&source=$fiddleSource" frameborder="0" style="$cssStyle"></iframe>
"""
)
} catch {
case e: FileNotFoundException =>
throw new FiddleDirective.LinkException(s"Unknown fiddle [${e.getMessage}] referenced from [${page.path}]")
}
}
}

object FiddleDirective {

/**
* Exception thrown for unknown snip links.
*/
class LinkException(message: String) extends RuntimeException(message)

}

/**
* Table of contents directive.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ object Writer {
def defaultDirectives(context: Context): Seq[Directive] = Seq(
RefDirective(context.location.tree.label.path, context.paths, Path.replaceExtension(context.sourceSuffix, context.targetSuffix)),
SnipDirective(context.location.tree.label),
FiddleDirective(context.location.tree.label),
TocDirective(context.location),
VarDirective(context.properties),
VarsDirective(context.properties)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © 2015 - 2016 Lightbend, Inc. <http://www.lightbend.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.lightbend.paradox.markdown

class FiddleDirectiveSpec extends MarkdownBaseSpec {

// #fiddle_code
val sourcePath = new java.io.File(".").getAbsolutePath + "/core/src/test/scala/"
// #fiddle_code

"Fiddle directive" should "generate fiddle iframe" in {
markdownPages(
sourcePath + "com/lightbend/paradox/markdown/FiddleDirectiveSpec.scala" -> """
|@@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++++++++++"
frameborder="0" style="width:100%;"></iframe>
""")
}

it should "properly add width and height" in {
{
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++++++++++"
frameborder="0" style="width:100%;"></iframe>
""")
}
}

it should "change base url" in {
{
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++++++++++"
frameborder="0" style="width:100%;"></iframe>
""")
}
}

}

0 comments on commit 090a1e9

Please sign in to comment.