Skip to content

Commit

Permalink
Share code to extract snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas committed Jun 2, 2017
1 parent b38f07a commit 3de37ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
19 changes: 3 additions & 16 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,13 +337,7 @@ 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 (text, _) = Snippet("fiddle", source, labels, page, variables)

val fiddleSource = java.net.URLEncoder.encode(
"""import fiddle.Fiddle, Fiddle.println
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

0 comments on commit 3de37ee

Please sign in to comment.