Last active
December 13, 2015 17:58
-
-
Save paradigmatic/4951387 to your computer and use it in GitHub Desktop.
Markdown string interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" ) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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