From 3de37ee8c71db1dfa70a4521b165850dc8059cf2 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Fri, 2 Jun 2017 09:56:09 +0200 Subject: [PATCH] Share code to extract snippets --- .../paradox/markdown/Directive.scala | 19 +++---------------- .../lightbend/paradox/markdown/Snippet.scala | 14 +++++++++++++- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala b/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala index cc00a4c0..e7b35c69 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala @@ -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 { @@ -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 diff --git a/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala b/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala index 9a63d6b2..6bdb399c 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala @@ -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") }