Skip to content

Commit

Permalink
Add support for snippet.base_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
sirthias committed Oct 24, 2016
1 parent 349a62f commit 9afc38f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ This will be rendered like this:

![multi_snip](docs/multi_snip.png)

In order to specify your snippet source paths off a certain base directory
you can define a `snippet.base_dir` property, e.g. like this:

```sbt
paradoxProperties in Compile ++= Map(
"snippet.base_dir" -> s"${(sourceDirectory in Test).value}/scala/org/example"
)
```

You can then refer to this snippet base directory by starting a snippet path with `.../`, e.g.

```
@@snip [Hello.scala](.../Hello.scala) { #hello_example }
```

### Parameterized links

Parameterized link directives help to manage links that references
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,15 @@ case class GitHubDirective(currentPath: String, variables: Map[String, String])
*
* Extracts snippets from source files into verbatim blocks.
*/
case class SnipDirective(page: Page) extends LeafBlockDirective("snip") {
case class SnipDirective(page: Page, variables: Map[String, String]) extends LeafBlockDirective("snip") {
private lazy val snippetBase =
new File(variables.getOrElse("snippet.base_dir", sys.error("Property `snippet.base_dir` is not defined")))
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
try {
val labels = node.attributes.values("identifier").asScala
val file = new File(page.file.getParentFile, node.source)
val file =
if (node.source startsWith ".../") new File(snippetBase, node.source drop 4)
else new File(page.file.getParentFile, node.source)
val text = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file))
new VerbatimNode(text, lang).accept(visitor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ object Writer {
ExtRefDirective(context.location.tree.label.path, context.properties),
ScaladocDirective(context.location.tree.label.path, context.properties),
GitHubDirective(context.location.tree.label.path, context.properties),
SnipDirective(context.location.tree.label),
SnipDirective(context.location.tree.label, context.properties),
FiddleDirective(context.location.tree.label),
TocDirective(context.location),
VarDirective(context.properties),
Expand Down

0 comments on commit 9afc38f

Please sign in to comment.