Skip to content

Commit

Permalink
Merge pull request #212 from 2m/wip-github-with-placeholders-2m
Browse files Browse the repository at this point in the history
Exception while trying to resolve link for github that contains placeholders
  • Loading branch information
2m authored Jun 24, 2018
2 parents 240f2a1 + 32093f8 commit c0ce819
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
25 changes: 19 additions & 6 deletions core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ sealed trait SourceDirective { this: Directive =>
}
}

protected def resolveFile(propPrefix: String, source: String, page: Page, variables: Map[String, String]): 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)
}

private lazy val referenceMap: Map[String, ReferenceNode] = {
val tempRoot = new RootNode
tempRoot.setReferences(page.markdown.getReferences)
Expand Down Expand Up @@ -271,9 +280,11 @@ trait GitHubResolver {
case None => throw Url.Error("[github.root.base_dir] is not defined")
case Some(dir) => new File(dir)
}
val file =
if (path.startsWith("/")) new File(root, path.drop(1))
else new File(page.file.getParentFile, path)
val file = path match {
case p if p.startsWith(Path.toUnixStyleRootPath(root.getAbsolutePath)) => new File(p)
case p if p.startsWith("/") => new File(root, path.drop(1))
case p => new File(page.file.getParentFile, path)
}
val labelFragment =
for {
label <- labelOpt
Expand Down Expand Up @@ -342,13 +353,14 @@ case class SnipDirective(page: Page, variables: Map[String, String])
try {
val labels = node.attributes.values("identifier").asScala
val source = resolvedSource(node, page)
val (text, snippetLang) = Snippet("snip", source, labels, page, variables)
val file = resolveFile("snip", source, page, variables)
val (text, snippetLang) = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(snippetLang)
val group = Option(node.attributes.value("group")).getOrElse("")
new VerbatimGroupNode(text, lang, group, node.attributes.classes).accept(visitor)
if (variables.contains(GitHubResolver.baseUrl) &&
variables.getOrElse(SnipDirective.showGithubLinks, "false") == "true") {
val p = resolvePath(page, source, labels.headOption).base.normalize.toString
val p = resolvePath(page, Path.toUnixStyleRootPath(file.getAbsolutePath), labels.headOption).base.normalize.toString
new ExpLinkNode("", p, new TextNode("Full source at GitHub")).accept(visitor)
}
} catch {
Expand Down Expand Up @@ -389,7 +401,8 @@ 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 (text, _) = Snippet("fiddle", source, labels, page, variables)
val file = resolveFile("fiddle", source, page, variables)
val (text, _) = Snippet(file, labels)

val fiddleSource = java.net.URLEncoder.encode(
"""import fiddle.Fiddle, Fiddle.println
Expand Down
12 changes: 1 addition & 11 deletions core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ object Snippet {

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

def apply(propPrefix: String, source: String, labels: Seq[String], page: Page, variables: Map[String, String]): (String, String) = {
val file = resolveFile(propPrefix, source, page, variables)
def apply(file: File, labels: Seq[String]): (String, String) = {
(extract(file, labels), language(file))
}

Expand All @@ -44,15 +43,6 @@ object Snippet {

type Line = (Int, String)

private def resolveFile(propPrefix: String, source: String, page: Page, variables: Map[String, String]): 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)
}

private def extractState(file: File, label: String): ExtractionState = {
if (!verifyLabel(label)) throw new SnippetException(s"Label [$label] for [$file] contains illegal characters. " +
"Only [a-zA-Z0-9_-] are allowed.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.lightbend.paradox.markdown

import java.io.File

import com.lightbend.paradox.tree.Tree.Location

class SnipDirectiveSpec extends MarkdownBaseSpec {
Expand Down Expand Up @@ -94,7 +96,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
it should "add link to source" in {
implicit val context = writerContextWithProperties(
"github.base_url" -> "https://github.com/lightbend/paradox/tree/v0.2.1",
"github.root.base_dir" -> ".",
"github.root.base_dir" -> new File(".").getAbsoluteFile.getParent,
"snip.github_link" -> "true")

markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example }""") shouldEqual html(
Expand All @@ -107,10 +109,27 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
|<a href="https://github.com/lightbend/paradox/tree/v0.2.1/tests/src/test/scala/com/lightbend/paradox/markdown/example.scala#L28-L30">Full source at GitHub</a>""")
}

it should "add link to source with placeholders" in {
implicit val context = writerContextWithProperties(
"github.base_url" -> "https://github.com/lightbend/paradox/tree/v0.2.1",
"github.root.base_dir" -> new File(".").getAbsoluteFile.getParent,
"snip.github_link" -> "true",
"snip.test.base_dir" -> "tests/src/test/scala/com/lightbend/paradox/markdown")

markdown("""@@snip[example.scala]($test$/example.scala) { #example }""") shouldEqual html(
"""<pre class="prettyprint">
|<code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
|</pre>
|<a href="https://github.com/lightbend/paradox/tree/v0.2.1/tests/src/test/scala/com/lightbend/paradox/markdown/example.scala#L28-L30">Full source at GitHub</a>""")
}

it should "not link to source if config says so" in {
implicit val context = writerContextWithProperties(
"github.base_url" -> "https://github.com/lightbend/paradox/tree/v0.2.1",
"github.root.base_dir" -> ".",
"github.root.base_dir" -> new File(".").getAbsoluteFile.getParent,
"snip.github_link" -> "false")

markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example }""") shouldEqual html(
Expand Down

0 comments on commit c0ce819

Please sign in to comment.