Skip to content

Instantly share code, notes, and snippets.

@paradigmatic
Last active December 13, 2015 17:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paradigmatic/4951387 to your computer and use it in GitHub Desktop.
Save paradigmatic/4951387 to your computer and use it in GitHub Desktop.
Markdown string interpolation
package org.streum.interpol
/* Here is the interpolator itself, we just add a method `md`
to the `StringContext` using implicit conversions */
object Markdown {
implicit class MDContext( val context: StringContext ) extends AnyVal {
// I used knockoff markdown processor. I don't have any real experience with
// it but it looks very good
import com.tristanhunt.knockoff.DefaultDiscounter._
// args will contain the value that will be interpolated
def md( args: Any* ) = {
//Here we use the standard interpolation first
val interpolated = context.s(args:_*)
//We then call knockoff to process the resulting string
toXHTML( knockoff( interpolated ) )
}
}
}
package org.streum.interpol
/* Example usage. The ouput is presented in file output.html (below) */
object Main extends App {
import Markdown._
def farenheit( celsius: Int ) = ( celsius*9.0/5 + 32 ).toInt
val location = "Anchorage, AK"
val url = "http://en.wikipedia.org/wiki/Anchorage,_Alaska"
val temp = -3
println( md"**[$location]($url):** *$temp°C* (${farenheit(temp)}°F)" )
}
<p><strong><a href="http://en.wikipedia.org/wiki/Anchorage,_Alaska">Anchorage, AK</a>:</strong> <em>-3°C</em> (26°F)</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment