public
Last active

Markdown string interpolation

  • Download Gist
Markdown.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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 ) )
}
 
}
 
}
MarkdownInterpolation.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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)" )
 
}
output.html
Liquid
1
<p><strong><a href="http://en.wikipedia.org/wiki/Anchorage,_Alaska">Anchorage, AK</a>:</strong> <em>-3°C</em> (26°F)</p>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.