Write Pandoc filters in Scala. Very early release. Still in development.
Copyright 2016 Dave Gurnell. Licensed Apache 2.
Spandoc requires:
Use Spandoc in an Ammonite and use it with Pandoc's --filter parameter:
echo 'Lorem ipsum' | pandoc --to=html --filter=my-filter.sc
# <p>LOREM IPSUM</p>Here's an example script:
// Filename: my-filter.sc
#!/usr/bin/env amm
interp.load.ivy("com.davegurnell" %% "spandoc" % "<<VERSION>>")
@
import spandoc._, ast._, transform._
// An AST transform that uppercases inline text:
val uppercase = TopDown.inline {
case Str(str) => Str(str.toUpperCase)
}
// Run the transform on stdin, printing the result to stdout:
transformStdin(uppercase)A transform is simply a function of the following type:
import spandoc.ast.Pandoc
type TransformFunc = Pandoc => PandocSpandoc provides some helper classes to assist in the creation of transforms:
-
spandoc.transform.Transformis a class that breaks the transform down into smaller components:Blocknodes,Inlinenodes, and so on; -
spandoc.transform.BottomUpimplementsBlockandInlinetransforms in a bottom-up traversal order that processes every node in the AST; -
spandoc.transform.TopDownimplementsBlockandInlinetransforms in a top-down traversal order that allows you to shortcut transformation of subtrees.
You can create simple transforms using the methods on the companion objects of
BottomUp and TopDown (as in the example above),
or you can extend either type to create a more complex transform:
object transform extends BottomUp[cats.Id] {
override def blockTransform = {
// Change all ordered lists to bulleted lists:
case OrderedList(_, items) => BulletList(items)
}
override def inlineTransform - {
// Uppercase all body text:
case Str(str) => Str(str.toUpperCase)
}
}Transforms can be monadic, which is typically useful
to thread a State monad through the AST.
See the examples directory for use cases like this.